Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,37 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
import torch
|
| 4 |
+
from diffusers import StableVideoDiffusionPipeline
|
| 5 |
|
| 6 |
+
# API Init
|
| 7 |
+
app = FastAPI()
|
| 8 |
|
| 9 |
+
# Load Model
|
| 10 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 11 |
+
pipe = StableVideoDiffusionPipeline.from_pretrained(
|
| 12 |
+
"stabilityai/stable-video-diffusion-img2vid"
|
| 13 |
+
)
|
| 14 |
+
pipe.to(device)
|
| 15 |
|
| 16 |
+
# Request Model
|
| 17 |
+
class VideoRequest(BaseModel):
|
| 18 |
+
prompt: str
|
| 19 |
|
| 20 |
+
# Root Endpoint
|
| 21 |
+
@app.get("/")
|
| 22 |
+
def home():
|
| 23 |
+
return {"message": "AI Video Generator API is running!"}
|
| 24 |
+
|
| 25 |
+
# Generate Video Endpoint
|
| 26 |
+
@app.post("/generate-video")
|
| 27 |
+
def generate_video(request: VideoRequest):
|
| 28 |
+
video_frames = pipe(request.prompt, num_inference_steps=50).frames
|
| 29 |
+
video_path = "output.mp4"
|
| 30 |
+
video_frames[0].save(video_path)
|
| 31 |
+
return {"message": "Video generated successfully!", "video_url": video_path}
|
| 32 |
+
|
| 33 |
+
# Run API
|
| 34 |
+
if __name__ == "__main__":
|
| 35 |
+
import uvicorn
|
| 36 |
+
|
| 37 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|