Update app.py
Browse files
app.py
CHANGED
|
@@ -102,8 +102,31 @@ def read_json(r, step):
|
|
| 102 |
return body
|
| 103 |
|
| 104 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
def startup_check():
|
| 106 |
-
"""Log whether the API is reachable and the key works (key is never printed)
|
|
|
|
| 107 |
if not API_KEY:
|
| 108 |
print("[startup] DRAMA_API_KEY is not set; demo is switched off.")
|
| 109 |
return
|
|
@@ -114,6 +137,14 @@ def startup_check():
|
|
| 114 |
print(f"[startup] GET {path} -> HTTP {r.status_code}: {r.text[:300]}")
|
| 115 |
except Exception as e:
|
| 116 |
print(f"[startup] GET {path} failed: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
|
| 118 |
|
| 119 |
def build_drama(mood, excited, angry):
|
|
@@ -134,7 +165,7 @@ def build_drama(mood, excited, angry):
|
|
| 134 |
def submit_job(video, audio, drama):
|
| 135 |
with open(video, "rb") as v, open(audio, "rb") as a:
|
| 136 |
r = requests.post(
|
| 137 |
-
f"{BASE}
|
| 138 |
headers=HEADERS,
|
| 139 |
data={
|
| 140 |
"skip_find_face": "1",
|
|
@@ -157,14 +188,14 @@ def submit_job(video, audio, drama):
|
|
| 157 |
|
| 158 |
|
| 159 |
def job_status(request_id):
|
| 160 |
-
r = requests.get(f"{BASE}
|
| 161 |
params={"request_id": request_id}, timeout=30)
|
| 162 |
body = read_json(r, "get-status")
|
| 163 |
return str((body.get("data") or {}).get("status", "")).lower()
|
| 164 |
|
| 165 |
|
| 166 |
def download(request_id, workdir):
|
| 167 |
-
r = requests.get(f"{BASE}
|
| 168 |
params={"request_id": request_id}, timeout=300, stream=True)
|
| 169 |
if "video" not in r.headers.get("Content-Type", ""):
|
| 170 |
raise gr.Error("The result isn't ready to download yet. Please try again.")
|
|
|
|
| 102 |
return body
|
| 103 |
|
| 104 |
|
| 105 |
+
PATHS = {"generate": "/generate", "get-status": "/get-status", "get-file": "/get-file"}
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
def route_exists(path):
|
| 109 |
+
"""GET a route without parameters. Only a router-level 404 means it's missing.
|
| 110 |
+
A GET never creates a job or spends credits."""
|
| 111 |
+
try:
|
| 112 |
+
r = requests.get(f"{BASE}{path}", headers=HEADERS, timeout=15)
|
| 113 |
+
except Exception as e:
|
| 114 |
+
print(f"[startup] probe GET {path} failed: {e}")
|
| 115 |
+
return False
|
| 116 |
+
missing = False
|
| 117 |
+
if r.status_code == 404:
|
| 118 |
+
try:
|
| 119 |
+
missing = r.json().get("detail") == "Not Found"
|
| 120 |
+
except ValueError:
|
| 121 |
+
missing = True
|
| 122 |
+
print(f"[startup] probe GET {path} -> HTTP {r.status_code}"
|
| 123 |
+
f"{' (route missing)' if missing else ' (route exists)'}")
|
| 124 |
+
return not missing
|
| 125 |
+
|
| 126 |
+
|
| 127 |
def startup_check():
|
| 128 |
+
"""Log whether the API is reachable and the key works (key is never printed),
|
| 129 |
+
and pick the right path for each endpoint (with or without /v2)."""
|
| 130 |
if not API_KEY:
|
| 131 |
print("[startup] DRAMA_API_KEY is not set; demo is switched off.")
|
| 132 |
return
|
|
|
|
| 137 |
print(f"[startup] GET {path} -> HTTP {r.status_code}: {r.text[:300]}")
|
| 138 |
except Exception as e:
|
| 139 |
print(f"[startup] GET {path} failed: {e}")
|
| 140 |
+
for name in PATHS:
|
| 141 |
+
for candidate in (f"/{name}", f"/v2/{name}"):
|
| 142 |
+
if route_exists(candidate):
|
| 143 |
+
PATHS[name] = candidate
|
| 144 |
+
break
|
| 145 |
+
else:
|
| 146 |
+
print(f"[startup] WARNING: no route found for {name}; keeping {PATHS[name]}")
|
| 147 |
+
print(f"[startup] using paths: {PATHS}")
|
| 148 |
|
| 149 |
|
| 150 |
def build_drama(mood, excited, angry):
|
|
|
|
| 165 |
def submit_job(video, audio, drama):
|
| 166 |
with open(video, "rb") as v, open(audio, "rb") as a:
|
| 167 |
r = requests.post(
|
| 168 |
+
f"{BASE}{PATHS['generate']}",
|
| 169 |
headers=HEADERS,
|
| 170 |
data={
|
| 171 |
"skip_find_face": "1",
|
|
|
|
| 188 |
|
| 189 |
|
| 190 |
def job_status(request_id):
|
| 191 |
+
r = requests.get(f"{BASE}{PATHS['get-status']}", headers=HEADERS,
|
| 192 |
params={"request_id": request_id}, timeout=30)
|
| 193 |
body = read_json(r, "get-status")
|
| 194 |
return str((body.get("data") or {}).get("status", "")).lower()
|
| 195 |
|
| 196 |
|
| 197 |
def download(request_id, workdir):
|
| 198 |
+
r = requests.get(f"{BASE}{PATHS['get-file']}", headers=HEADERS,
|
| 199 |
params={"request_id": request_id}, timeout=300, stream=True)
|
| 200 |
if "video" not in r.headers.get("Content-Type", ""):
|
| 201 |
raise gr.Error("The result isn't ready to download yet. Please try again.")
|