Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,17 +1,18 @@
|
|
| 1 |
# app.py
|
| 2 |
-
from fastapi import FastAPI
|
| 3 |
from fastapi.staticfiles import StaticFiles
|
| 4 |
from pydantic import BaseModel
|
| 5 |
-
from huggingface_hub import hf_hub_download
|
| 6 |
import uvicorn
|
| 7 |
import os
|
| 8 |
import uuid
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
| 11 |
|
| 12 |
app = FastAPI(title="WAN2 GGUF API", version="1.0")
|
| 13 |
|
| 14 |
-
#
|
| 15 |
MODEL_REPO = "calcuis/wan2-gguf"
|
| 16 |
MODEL_FILE = "wan2.2-animate-14b-q4_0.gguf"
|
| 17 |
MODEL_DIR = "models"
|
|
@@ -20,7 +21,9 @@ OUTPUT_DIR = "outputs"
|
|
| 20 |
os.makedirs(MODEL_DIR, exist_ok=True)
|
| 21 |
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
| 22 |
|
| 23 |
-
#
|
|
|
|
|
|
|
| 24 |
model_path = hf_hub_download(
|
| 25 |
repo_id=MODEL_REPO,
|
| 26 |
filename=MODEL_FILE,
|
|
@@ -28,70 +31,59 @@ model_path = hf_hub_download(
|
|
| 28 |
)
|
| 29 |
print("✅ Model downloaded to:", model_path)
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
# Request schema
|
| 33 |
class PromptRequest(BaseModel):
|
| 34 |
prompt: str
|
| 35 |
steps: int = 20
|
| 36 |
|
| 37 |
-
|
| 38 |
@app.get("/")
|
| 39 |
def root():
|
| 40 |
return {"message": "WAN2 GGUF API is running!"}
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
|
|
|
| 45 |
# Unique filename
|
| 46 |
file_id = str(uuid.uuid4())
|
| 47 |
file_path = os.path.join(OUTPUT_DIR, f"{file_id}.mp4")
|
| 48 |
|
| 49 |
-
#
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
| 53 |
)
|
| 54 |
|
| 55 |
-
#
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
r = requests.get(placeholder_url, stream=True)
|
| 59 |
-
with open(placeholder_file, "wb") as f:
|
| 60 |
-
shutil.copyfileobj(r.raw, f)
|
| 61 |
-
|
| 62 |
-
# Copy placeholder to simulate output
|
| 63 |
-
shutil.copy(placeholder_file, file_path)
|
| 64 |
|
| 65 |
-
#
|
| 66 |
base_url = "https://abrahamdw882-wan2-api.hf.space"
|
| 67 |
video_url = f"{base_url}/file/{file_id}.mp4"
|
| 68 |
|
| 69 |
return {
|
| 70 |
"status": "success",
|
| 71 |
"model_file": MODEL_FILE,
|
| 72 |
-
"prompt": prompt,
|
| 73 |
-
"steps": steps,
|
| 74 |
-
"video_url": video_url
|
| 75 |
-
"note": "Replace this with actual inference code."
|
| 76 |
}
|
| 77 |
|
| 78 |
-
|
| 79 |
-
# ✅ POST endpoint
|
| 80 |
-
@app.post("/generate")
|
| 81 |
-
def generate_post(request: PromptRequest):
|
| 82 |
-
return generate_output(request.prompt, request.steps)
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
# ✅ NEW: GET endpoint with query ?q=
|
| 86 |
-
@app.get("/generate")
|
| 87 |
-
def generate_get(q: str = Query(..., description="Prompt for generation"), steps: int = 20):
|
| 88 |
-
return generate_output(q, steps)
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
# ✅ Serve output videos
|
| 92 |
app.mount("/file", StaticFiles(directory=OUTPUT_DIR), name="file")
|
| 93 |
|
| 94 |
-
|
| 95 |
-
# ✅ Run server in HF Spaces
|
| 96 |
if __name__ == "__main__":
|
| 97 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 1 |
# app.py
|
| 2 |
+
from fastapi import FastAPI
|
| 3 |
from fastapi.staticfiles import StaticFiles
|
| 4 |
from pydantic import BaseModel
|
|
|
|
| 5 |
import uvicorn
|
| 6 |
import os
|
| 7 |
import uuid
|
| 8 |
+
|
| 9 |
+
# GGUF & video libraries
|
| 10 |
+
from gguf_runtime import GGUFModel # Make sure gguf-runtime is installed
|
| 11 |
+
from moviepy.editor import ImageSequenceClip
|
| 12 |
|
| 13 |
app = FastAPI(title="WAN2 GGUF API", version="1.0")
|
| 14 |
|
| 15 |
+
# -------------------- Directories --------------------
|
| 16 |
MODEL_REPO = "calcuis/wan2-gguf"
|
| 17 |
MODEL_FILE = "wan2.2-animate-14b-q4_0.gguf"
|
| 18 |
MODEL_DIR = "models"
|
|
|
|
| 21 |
os.makedirs(MODEL_DIR, exist_ok=True)
|
| 22 |
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
| 23 |
|
| 24 |
+
# -------------------- Download model --------------------
|
| 25 |
+
from huggingface_hub import hf_hub_download
|
| 26 |
+
|
| 27 |
model_path = hf_hub_download(
|
| 28 |
repo_id=MODEL_REPO,
|
| 29 |
filename=MODEL_FILE,
|
|
|
|
| 31 |
)
|
| 32 |
print("✅ Model downloaded to:", model_path)
|
| 33 |
|
| 34 |
+
# -------------------- Load GGUF model --------------------
|
| 35 |
+
print("Loading WAN2 GGUF model...")
|
| 36 |
+
wan2_model = GGUFModel(model_path)
|
| 37 |
+
print("✅ WAN2 model loaded")
|
| 38 |
|
| 39 |
+
# -------------------- Request schema --------------------
|
| 40 |
class PromptRequest(BaseModel):
|
| 41 |
prompt: str
|
| 42 |
steps: int = 20
|
| 43 |
|
| 44 |
+
# -------------------- Routes --------------------
|
| 45 |
@app.get("/")
|
| 46 |
def root():
|
| 47 |
return {"message": "WAN2 GGUF API is running!"}
|
| 48 |
|
| 49 |
+
@app.get("/generate")
|
| 50 |
+
def generate_video_get(q: str, steps: int = 20):
|
| 51 |
+
"""Allows GET requests with ?q=... for browser testing"""
|
| 52 |
+
return generate_video(PromptRequest(prompt=q, steps=steps))
|
| 53 |
|
| 54 |
+
@app.post("/generate")
|
| 55 |
+
def generate_video(request: PromptRequest):
|
| 56 |
+
"""Generates video from prompt using WAN2 GGUF"""
|
| 57 |
# Unique filename
|
| 58 |
file_id = str(uuid.uuid4())
|
| 59 |
file_path = os.path.join(OUTPUT_DIR, f"{file_id}.mp4")
|
| 60 |
|
| 61 |
+
# ---- WAN2 inference ----
|
| 62 |
+
# This generates frames from prompt
|
| 63 |
+
frames = wan2_model.generate_video(
|
| 64 |
+
prompt=request.prompt,
|
| 65 |
+
steps=request.steps
|
| 66 |
)
|
| 67 |
|
| 68 |
+
# ---- Save frames as MP4 ----
|
| 69 |
+
clip = ImageSequenceClip(frames, fps=12)
|
| 70 |
+
clip.write_videofile(file_path, codec="libx264", audio=False, verbose=False, logger=None)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
+
# Build full URL for Hugging Face Space
|
| 73 |
base_url = "https://abrahamdw882-wan2-api.hf.space"
|
| 74 |
video_url = f"{base_url}/file/{file_id}.mp4"
|
| 75 |
|
| 76 |
return {
|
| 77 |
"status": "success",
|
| 78 |
"model_file": MODEL_FILE,
|
| 79 |
+
"prompt": request.prompt,
|
| 80 |
+
"steps": request.steps,
|
| 81 |
+
"video_url": video_url
|
|
|
|
| 82 |
}
|
| 83 |
|
| 84 |
+
# -------------------- Serve output videos --------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
app.mount("/file", StaticFiles(directory=OUTPUT_DIR), name="file")
|
| 86 |
|
| 87 |
+
# -------------------- Run server --------------------
|
|
|
|
| 88 |
if __name__ == "__main__":
|
| 89 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|