Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -432,6 +432,13 @@ async def generate_image(request: Request, prompt: str = None):
|
|
| 432 |
else:
|
| 433 |
chosen_model = "zimage"
|
| 434 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 435 |
print(f"[IMAGE GEN] Routing to model: {chosen_model}")
|
| 436 |
|
| 437 |
url = f"https://gen.pollinations.ai/image/{prompt}?model={chosen_model}&key={PKEY2}"
|
|
@@ -760,91 +767,6 @@ async def genvideo(request: Request, prompt: str = None):
|
|
| 760 |
}
|
| 761 |
)
|
| 762 |
|
| 763 |
-
@app.get("/gen/video/exp/{prompt}")
|
| 764 |
-
@app.post("/gen/video/exp")
|
| 765 |
-
async def genvideo_wan(request: Request, prompt: str = None):
|
| 766 |
-
client_ip = request.client.host
|
| 767 |
-
check_rate_limit(client_ip)
|
| 768 |
-
|
| 769 |
-
if prompt is None:
|
| 770 |
-
body = await request.json()
|
| 771 |
-
prompt = body.get("prompt")
|
| 772 |
-
|
| 773 |
-
if not prompt:
|
| 774 |
-
raise HTTPException(400, "Prompt is required")
|
| 775 |
-
|
| 776 |
-
session_hash = uuid.uuid4().hex
|
| 777 |
-
|
| 778 |
-
join_payload = {
|
| 779 |
-
"fn_index": 0,
|
| 780 |
-
"session_hash": session_hash,
|
| 781 |
-
"data": [prompt],
|
| 782 |
-
}
|
| 783 |
-
|
| 784 |
-
async with httpx.AsyncClient(timeout=60.0) as client:
|
| 785 |
-
# 1️⃣ Join queue
|
| 786 |
-
join = await client.post(
|
| 787 |
-
f"{WAN_API_BASE}/queue/join",
|
| 788 |
-
json=join_payload,
|
| 789 |
-
)
|
| 790 |
-
|
| 791 |
-
if join.status_code != 200:
|
| 792 |
-
raise HTTPException(
|
| 793 |
-
join.status_code,
|
| 794 |
-
f"Failed to join WAN queue: {join.text}"
|
| 795 |
-
)
|
| 796 |
-
|
| 797 |
-
for _ in range(120):
|
| 798 |
-
await asyncio.sleep(1)
|
| 799 |
-
|
| 800 |
-
poll = await client.get(
|
| 801 |
-
f"{WAN_API_BASE}/queue/data",
|
| 802 |
-
params={"session_hash": session_hash},
|
| 803 |
-
)
|
| 804 |
-
|
| 805 |
-
if poll.status_code != 200:
|
| 806 |
-
continue
|
| 807 |
-
|
| 808 |
-
data = poll.json()
|
| 809 |
-
|
| 810 |
-
if data.get("status") == "complete":
|
| 811 |
-
try:
|
| 812 |
-
video_url = data["data"][0]
|
| 813 |
-
except Exception:
|
| 814 |
-
raise HTTPException(
|
| 815 |
-
500,
|
| 816 |
-
"WAN completed but returned unexpected format"
|
| 817 |
-
)
|
| 818 |
-
|
| 819 |
-
# 3️⃣ Fetch video
|
| 820 |
-
video = await client.get(video_url)
|
| 821 |
-
|
| 822 |
-
if video.status_code != 200:
|
| 823 |
-
raise HTTPException(
|
| 824 |
-
502,
|
| 825 |
-
"Failed to fetch generated video"
|
| 826 |
-
)
|
| 827 |
-
|
| 828 |
-
return Response(
|
| 829 |
-
content=video.content,
|
| 830 |
-
media_type="video/mp4",
|
| 831 |
-
headers={
|
| 832 |
-
"Content-Length": str(len(video.content)),
|
| 833 |
-
"Accept-Ranges": "bytes",
|
| 834 |
-
}
|
| 835 |
-
)
|
| 836 |
-
|
| 837 |
-
if data.get("status") == "error":
|
| 838 |
-
raise HTTPException(
|
| 839 |
-
500,
|
| 840 |
-
f"WAN generation error: {data}"
|
| 841 |
-
)
|
| 842 |
-
|
| 843 |
-
raise HTTPException(
|
| 844 |
-
504,
|
| 845 |
-
"WAN video generation timed out"
|
| 846 |
-
)
|
| 847 |
-
|
| 848 |
AIRFORCE_KEY = os.getenv("AIRFORCE")
|
| 849 |
AIRFORCE_VIDEO_MODEL = "grok-imagine-video"
|
| 850 |
AIRFORCE_API_URL = "https://api.airforce/v1/images/generations"
|
|
|
|
| 432 |
else:
|
| 433 |
chosen_model = "zimage"
|
| 434 |
|
| 435 |
+
if prompt is None:
|
| 436 |
+
if (await request.json()).get("mode"):
|
| 437 |
+
if mode == "fantasy":
|
| 438 |
+
chosen_model = "flux"
|
| 439 |
+
elif mode == "realistic":
|
| 440 |
+
chosen_model = "zimage"
|
| 441 |
+
|
| 442 |
print(f"[IMAGE GEN] Routing to model: {chosen_model}")
|
| 443 |
|
| 444 |
url = f"https://gen.pollinations.ai/image/{prompt}?model={chosen_model}&key={PKEY2}"
|
|
|
|
| 767 |
}
|
| 768 |
)
|
| 769 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 770 |
AIRFORCE_KEY = os.getenv("AIRFORCE")
|
| 771 |
AIRFORCE_VIDEO_MODEL = "grok-imagine-video"
|
| 772 |
AIRFORCE_API_URL = "https://api.airforce/v1/images/generations"
|