Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -841,50 +841,91 @@ AIRFORCE_KEY = os.getenv("AIRFORCE")
|
|
| 841 |
AIRFORCE_VIDEO_MODEL = "grok-imagine-video"
|
| 842 |
AIRFORCE_API_URL = "https://api.airforce/v1/images/generations"
|
| 843 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 844 |
MAX_VIDEO_RETRIES = 6
|
| 845 |
|
|
|
|
| 846 |
@app.get("/gen/video/airforce/{prompt}")
|
| 847 |
@app.post("/gen/video/airforce")
|
| 848 |
async def genvideo_airforce(request: Request, prompt: str = None):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 849 |
if prompt is None:
|
| 850 |
-
|
| 851 |
-
prompt =
|
| 852 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 853 |
|
| 854 |
-
|
| 855 |
-
|
|
|
|
|
|
|
|
|
|
| 856 |
"prompt": prompt,
|
| 857 |
"n": 1,
|
| 858 |
"size": "1024x1024",
|
| 859 |
"response_format": "b64_json",
|
| 860 |
"sse": False,
|
| 861 |
-
"mode":
|
| 862 |
-
"aspectRatio":
|
| 863 |
}
|
| 864 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 865 |
async with httpx.AsyncClient(timeout=600) as client:
|
| 866 |
resp = await client.post(
|
| 867 |
-
|
| 868 |
-
headers={
|
| 869 |
-
|
|
|
|
|
|
|
|
|
|
| 870 |
)
|
| 871 |
|
| 872 |
if resp.status_code != 200:
|
| 873 |
return JSONResponse(status_code=resp.status_code, content=resp.json())
|
|
|
|
| 874 |
if not resp.content:
|
| 875 |
-
raise HTTPException(
|
| 876 |
-
status_code=502,
|
| 877 |
-
detail="api.airforce returned empty response"
|
| 878 |
-
)
|
| 879 |
|
| 880 |
try:
|
| 881 |
result = resp.json()
|
| 882 |
b64_video = result["data"][0]["b64_json"]
|
| 883 |
except Exception:
|
| 884 |
-
raise HTTPException(
|
| 885 |
-
502,
|
| 886 |
-
f"Invalid api.airforce response: {resp.text[:500]}"
|
| 887 |
-
)
|
| 888 |
|
| 889 |
if not b64_video:
|
| 890 |
raise HTTPException(502, "Airforce returned empty b64_json")
|
|
|
|
| 841 |
AIRFORCE_VIDEO_MODEL = "grok-imagine-video"
|
| 842 |
AIRFORCE_API_URL = "https://api.airforce/v1/images/generations"
|
| 843 |
|
| 844 |
+
valid_ratios = {"3:2", "2:3", "1:1", "", None}
|
| 845 |
+
ratios = {"3:2", "2:3", "1:1"}
|
| 846 |
+
|
| 847 |
+
valid_modes = {"normal", "fun", "", None}
|
| 848 |
+
modes = {"normal", "fun"}
|
| 849 |
+
|
| 850 |
MAX_VIDEO_RETRIES = 6
|
| 851 |
|
| 852 |
+
|
| 853 |
@app.get("/gen/video/airforce/{prompt}")
|
| 854 |
@app.post("/gen/video/airforce")
|
| 855 |
async def genvideo_airforce(request: Request, prompt: str = None):
|
| 856 |
+
aspectRatio = "3:2"
|
| 857 |
+
inputMode = "normal"
|
| 858 |
+
|
| 859 |
+
user_body = {}
|
| 860 |
if prompt is None:
|
| 861 |
+
user_body = await request.json()
|
| 862 |
+
prompt = user_body.get("prompt")
|
| 863 |
+
ratio = user_body.get("ratio")
|
| 864 |
+
mode = user_body.get("mode")
|
| 865 |
+
image_urls = user_body.get("image_urls")
|
| 866 |
+
|
| 867 |
+
if not prompt:
|
| 868 |
+
raise HTTPException(400, "Prompt is required")
|
| 869 |
+
|
| 870 |
+
if ratio not in valid_ratios:
|
| 871 |
+
raise HTTPException(
|
| 872 |
+
status_code=400,
|
| 873 |
+
detail=f"Invalid aspect ratio {ratio}. Must be one of 3:2, 2:3, or 1:1. Default is 3:2"
|
| 874 |
+
)
|
| 875 |
+
if ratio in ratios:
|
| 876 |
+
aspectRatio = ratio
|
| 877 |
+
|
| 878 |
+
if mode not in valid_modes:
|
| 879 |
+
raise HTTPException(
|
| 880 |
+
status_code=400,
|
| 881 |
+
detail=f"Invalid mode {mode}. Must be 'normal' or 'fun'. Default is normal"
|
| 882 |
+
)
|
| 883 |
+
if mode in modes:
|
| 884 |
+
inputMode = mode
|
| 885 |
+
|
| 886 |
+
if image_urls:
|
| 887 |
+
if not isinstance(image_urls, list):
|
| 888 |
+
raise HTTPException(400, "image_urls must be a list")
|
| 889 |
|
| 890 |
+
if len(image_urls) > 2:
|
| 891 |
+
raise HTTPException(400, "You may provide at most two image URLs")
|
| 892 |
+
|
| 893 |
+
payload = {
|
| 894 |
+
"model": AIRFORCE_VIDEO_MODEL,
|
| 895 |
"prompt": prompt,
|
| 896 |
"n": 1,
|
| 897 |
"size": "1024x1024",
|
| 898 |
"response_format": "b64_json",
|
| 899 |
"sse": False,
|
| 900 |
+
"mode": inputMode,
|
| 901 |
+
"aspectRatio": aspectRatio
|
| 902 |
}
|
| 903 |
|
| 904 |
+
if prompt is None:
|
| 905 |
+
if image_urls:
|
| 906 |
+
payload["image_urls"] = image_urls
|
| 907 |
+
|
| 908 |
async with httpx.AsyncClient(timeout=600) as client:
|
| 909 |
resp = await client.post(
|
| 910 |
+
AIRFORCE_API_URL,
|
| 911 |
+
headers={
|
| 912 |
+
"Authorization": f"Bearer {AIRFORCE_KEY}",
|
| 913 |
+
"Content-Type": "application/json"
|
| 914 |
+
},
|
| 915 |
+
json=payload
|
| 916 |
)
|
| 917 |
|
| 918 |
if resp.status_code != 200:
|
| 919 |
return JSONResponse(status_code=resp.status_code, content=resp.json())
|
| 920 |
+
|
| 921 |
if not resp.content:
|
| 922 |
+
raise HTTPException(502, "api.airforce returned empty response")
|
|
|
|
|
|
|
|
|
|
| 923 |
|
| 924 |
try:
|
| 925 |
result = resp.json()
|
| 926 |
b64_video = result["data"][0]["b64_json"]
|
| 927 |
except Exception:
|
| 928 |
+
raise HTTPException(502, f"Invalid api.airforce response: {resp.text[:500]}")
|
|
|
|
|
|
|
|
|
|
| 929 |
|
| 930 |
if not b64_video:
|
| 931 |
raise HTTPException(502, "Airforce returned empty b64_json")
|