Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -524,6 +524,7 @@ async def generate_text(request: Request):
|
|
| 524 |
|
| 525 |
raise HTTPException(500, "Unknown provider routing error")
|
| 526 |
|
|
|
|
| 527 |
@app.get("/gen/sfx")
|
| 528 |
@app.post("/gen/sfx")
|
| 529 |
async def gensfx(request: Request, prompt: str = None):
|
|
@@ -537,4 +538,52 @@ async def gensfx(request: Request, prompt: str = None):
|
|
| 537 |
return Response(
|
| 538 |
response.content,
|
| 539 |
media_type="audio/mpeg"
|
| 540 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 524 |
|
| 525 |
raise HTTPException(500, "Unknown provider routing error")
|
| 526 |
|
| 527 |
+
@app.head("/gen/sfx")
|
| 528 |
@app.get("/gen/sfx")
|
| 529 |
@app.post("/gen/sfx")
|
| 530 |
async def gensfx(request: Request, prompt: str = None):
|
|
|
|
| 538 |
return Response(
|
| 539 |
response.content,
|
| 540 |
media_type="audio/mpeg"
|
| 541 |
+
)
|
| 542 |
+
|
| 543 |
+
@app.get("/gen/video")
|
| 544 |
+
@app.post("/gen/video")
|
| 545 |
+
async def genvideo(request: Request, prompt: str = None):
|
| 546 |
+
client_ip = request.client.host
|
| 547 |
+
check_audio_rate_limit(client_ip)
|
| 548 |
+
|
| 549 |
+
if prompt is None:
|
| 550 |
+
prompt = (await request.json()).prompt
|
| 551 |
+
|
| 552 |
+
url = f"https://gen.pollinations.ai/image/{prompt}?model=grok-video&key={PKEY}"
|
| 553 |
+
|
| 554 |
+
async def fetch_with_retry(url, max_retries=6):
|
| 555 |
+
async with httpx.AsyncClient(timeout=None) as client:
|
| 556 |
+
for attempt in range(max_retries):
|
| 557 |
+
response = await client.get(url)
|
| 558 |
+
|
| 559 |
+
if response.status_code == 200:
|
| 560 |
+
return response
|
| 561 |
+
|
| 562 |
+
body_text = ""
|
| 563 |
+
try:
|
| 564 |
+
body_text = response.text
|
| 565 |
+
except:
|
| 566 |
+
pass
|
| 567 |
+
|
| 568 |
+
if response.status_code == 429 and "api.airforce" in body_text:
|
| 569 |
+
wait = 0.5 * (2 ** attempt)
|
| 570 |
+
print(f"[VIDEO RETRY] 429 from api.airforce, retrying in {wait:.2f}s...")
|
| 571 |
+
await asyncio.sleep(wait)
|
| 572 |
+
continue
|
| 573 |
+
|
| 574 |
+
raise HTTPException(
|
| 575 |
+
status_code=500,
|
| 576 |
+
detail=f"Pollinations error: {response.status_code}"
|
| 577 |
+
)
|
| 578 |
+
|
| 579 |
+
raise HTTPException(
|
| 580 |
+
status_code=503,
|
| 581 |
+
detail="Pollinations video generation overloaded (api.airforce). Try again later."
|
| 582 |
+
)
|
| 583 |
+
|
| 584 |
+
response = await fetch_with_retry(url)
|
| 585 |
+
|
| 586 |
+
return Response(
|
| 587 |
+
content=response.content,
|
| 588 |
+
media_type="video/mp4"
|
| 589 |
+
)
|