Sabbirr12 commited on
Commit
2e2fc3f
·
verified ·
1 Parent(s): 398c981

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -26
app.py CHANGED
@@ -1,41 +1,37 @@
1
- import gradio as gr
 
2
  import torch
3
  from diffusers import StableDiffusionPipeline, StableVideoDiffusionPipeline
4
  from diffusers.utils import export_to_video
5
  from PIL import Image
6
 
7
- # Load models
 
 
 
 
 
 
 
8
  text2img_pipe = StableDiffusionPipeline.from_pretrained(
9
- "runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16
10
- ).to("cuda")
11
- text2img_pipe.enable_model_cpu_offload()
12
 
 
13
  video_pipe = StableVideoDiffusionPipeline.from_pretrained(
14
- "stabilityai/stable-video-diffusion-img2vid-xt", torch_dtype=torch.float16, variant="fp16"
15
- ).to("cuda")
16
- video_pipe.enable_model_cpu_offload()
 
17
 
18
- def generate_video(prompt, aspect_ratio):
19
- # Generate image
20
  image = text2img_pipe(prompt).images[0]
21
- if aspect_ratio == "16:9":
22
- image = image.resize((1024, 576))
23
- else:
24
- image = image.resize((576, 1024))
25
 
26
- # Generate video
27
  generator = torch.manual_seed(42)
28
  frames = video_pipe(image, decode_chunk_size=8, generator=generator).frames[0]
29
  export_to_video(frames, "output.mp4", fps=7)
30
- return "output.mp4"
31
 
32
- gr.Interface(
33
- fn=generate_video,
34
- inputs=[
35
- gr.Textbox(label="Scene Prompt", placeholder="e.g. A 3D Islamic city at sunset, 4K"),
36
- gr.Radio(["16:9", "9:16"], label="Aspect Ratio", value="16:9")
37
- ],
38
- outputs=gr.Video(label="Generated Video"),
39
- title="Text → Image → Video Generator",
40
- description="Enter a scene prompt and get a cinematic video. Powered by Stable Diffusion + Stable Video Diffusion."
41
- ).launch()
 
1
+ from fastapi import FastAPI, Form
2
+ from fastapi.responses import FileResponse
3
  import torch
4
  from diffusers import StableDiffusionPipeline, StableVideoDiffusionPipeline
5
  from diffusers.utils import export_to_video
6
  from PIL import Image
7
 
8
+ app = FastAPI()
9
+
10
+ # Check device
11
+ device = "cuda" if torch.cuda.is_available() else "cpu"
12
+ dtype = torch.float16 if device == "cuda" else torch.float32
13
+ variant = "fp16" if device == "cuda" else None
14
+
15
+ # Load text-to-image model
16
  text2img_pipe = StableDiffusionPipeline.from_pretrained(
17
+ "runwayml/stable-diffusion-v1-5",
18
+ torch_dtype=dtype
19
+ ).to(device)
20
 
21
+ # Load image-to-video model
22
  video_pipe = StableVideoDiffusionPipeline.from_pretrained(
23
+ "stabilityai/stable-video-diffusion-img2vid-xt",
24
+ torch_dtype=dtype,
25
+ variant=variant
26
+ ).to(device)
27
 
28
+ @app.post("/generate")
29
+ def generate_video(prompt: str = Form(...), aspect: str = Form("16:9")):
30
  image = text2img_pipe(prompt).images[0]
31
+ image = image.resize((1024, 576) if aspect == "16:9" else (576, 1024))
 
 
 
32
 
 
33
  generator = torch.manual_seed(42)
34
  frames = video_pipe(image, decode_chunk_size=8, generator=generator).frames[0]
35
  export_to_video(frames, "output.mp4", fps=7)
 
36
 
37
+ return FileResponse("output.mp4", media_type="video/mp4", filename="output.mp4")