Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1167,34 +1167,38 @@ async def generate_beat_ai(request: GenerateAIRequest):
|
|
| 1167 |
output_id = generate_file_id()
|
| 1168 |
output_path = OUTPUT_DIR / f"{output_id}.wav"
|
| 1169 |
|
| 1170 |
-
headers = {
|
|
|
|
|
|
|
|
|
|
| 1171 |
if HF_TOKEN:
|
| 1172 |
headers["Authorization"] = f"Bearer {HF_TOKEN}"
|
| 1173 |
|
| 1174 |
payload = {
|
| 1175 |
"inputs": request.prompt,
|
| 1176 |
-
"parameters": {
|
| 1177 |
-
"max_new_tokens": request.duration * 50, # ~50 tokens per second
|
| 1178 |
-
},
|
| 1179 |
}
|
| 1180 |
|
| 1181 |
try:
|
| 1182 |
print(f"MusicGen AI generating: '{request.prompt}' ({request.duration}s)")
|
| 1183 |
-
async with httpx.AsyncClient(timeout=
|
| 1184 |
response = await client.post(
|
| 1185 |
MUSICGEN_API_URL,
|
| 1186 |
headers=headers,
|
| 1187 |
json=payload,
|
| 1188 |
)
|
| 1189 |
|
|
|
|
|
|
|
| 1190 |
if response.status_code == 503:
|
| 1191 |
-
|
|
|
|
| 1192 |
raise HTTPException(
|
| 1193 |
status_code=503,
|
| 1194 |
-
detail="MusicGen model is loading, please try again in ~
|
| 1195 |
)
|
| 1196 |
if response.status_code != 200:
|
| 1197 |
-
error_msg = response.text[:
|
|
|
|
| 1198 |
raise HTTPException(
|
| 1199 |
status_code=502,
|
| 1200 |
detail=f"HF Inference API error ({response.status_code}): {error_msg}"
|
|
|
|
| 1167 |
output_id = generate_file_id()
|
| 1168 |
output_path = OUTPUT_DIR / f"{output_id}.wav"
|
| 1169 |
|
| 1170 |
+
headers = {
|
| 1171 |
+
"Content-Type": "application/json",
|
| 1172 |
+
"x-wait-for-model": "true", # Wait for model to load instead of 503
|
| 1173 |
+
}
|
| 1174 |
if HF_TOKEN:
|
| 1175 |
headers["Authorization"] = f"Bearer {HF_TOKEN}"
|
| 1176 |
|
| 1177 |
payload = {
|
| 1178 |
"inputs": request.prompt,
|
|
|
|
|
|
|
|
|
|
| 1179 |
}
|
| 1180 |
|
| 1181 |
try:
|
| 1182 |
print(f"MusicGen AI generating: '{request.prompt}' ({request.duration}s)")
|
| 1183 |
+
async with httpx.AsyncClient(timeout=300) as client:
|
| 1184 |
response = await client.post(
|
| 1185 |
MUSICGEN_API_URL,
|
| 1186 |
headers=headers,
|
| 1187 |
json=payload,
|
| 1188 |
)
|
| 1189 |
|
| 1190 |
+
print(f"HF API response: status={response.status_code}, content-type={response.headers.get('content-type', 'unknown')}, size={len(response.content)} bytes")
|
| 1191 |
+
|
| 1192 |
if response.status_code == 503:
|
| 1193 |
+
error_data = response.json() if response.headers.get("content-type", "").startswith("application/json") else {}
|
| 1194 |
+
wait_time = error_data.get("estimated_time", 30)
|
| 1195 |
raise HTTPException(
|
| 1196 |
status_code=503,
|
| 1197 |
+
detail=f"MusicGen model is loading, please try again in ~{int(wait_time)} seconds."
|
| 1198 |
)
|
| 1199 |
if response.status_code != 200:
|
| 1200 |
+
error_msg = response.text[:500]
|
| 1201 |
+
print(f"HF API error: {error_msg}")
|
| 1202 |
raise HTTPException(
|
| 1203 |
status_code=502,
|
| 1204 |
detail=f"HF Inference API error ({response.status_code}): {error_msg}"
|