nemece commited on
Commit
f89daca
·
verified ·
1 Parent(s): 306a22a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -15
app.py CHANGED
@@ -1,11 +1,12 @@
1
  import torch
2
  from diffusers import StableVideoDiffusionPipeline
3
  from PIL import Image
 
4
  import os
5
 
6
- HF_TOKEN = os.getenv("HF_TOKEN") # SAFER (use HF Space secrets)
7
 
8
- # 1. Load pipeline
9
  pipe = StableVideoDiffusionPipeline.from_pretrained(
10
  "stabilityai/stable-video-diffusion-img2vid",
11
  torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
@@ -16,20 +17,45 @@ pipe = StableVideoDiffusionPipeline.from_pretrained(
16
  device = "cuda" if torch.cuda.is_available() else "cpu"
17
  pipe = pipe.to(device)
18
 
19
- # === 2. Load local input image ===
20
- input_path = "input.jpg" # ← Put your image in the same folder as script
21
- img = Image.open(input_path).convert("RGB")
22
- img = img.resize((576, 320))
23
 
24
- # 3. Generate video frames
25
- frames = pipe(img, num_frames=5).frames[0]
 
26
 
27
- # 4. Save frames
28
- os.makedirs("frames", exist_ok=True)
29
- for i, f in enumerate(frames):
30
- f.save(f"frames/frame_{i:03d}.png")
31
 
32
- # 5. Build MP4 video
33
- os.system("ffmpeg -y -framerate 10 -i frames/frame_%03d.png output.mp4")
34
 
35
- print("Video saved as output.mp4")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import torch
2
  from diffusers import StableVideoDiffusionPipeline
3
  from PIL import Image
4
+ import gradio as gr
5
  import os
6
 
7
+ HF_TOKEN = None # Uses your Space token automatically
8
 
9
+ # Load pipeline once at startup
10
  pipe = StableVideoDiffusionPipeline.from_pretrained(
11
  "stabilityai/stable-video-diffusion-img2vid",
12
  torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
 
17
  device = "cuda" if torch.cuda.is_available() else "cpu"
18
  pipe = pipe.to(device)
19
 
 
 
 
 
20
 
21
+ def generate_video(inp_img, num_frames):
22
+ if inp_img is None:
23
+ return "No image uploaded!", None
24
 
25
+ # Resize image to SVD expected size
26
+ img = inp_img.convert("RGB").resize((576, 320))
 
 
27
 
28
+ # Generate frames
29
+ frames = pipe(img, num_frames=num_frames).frames[0]
30
 
31
+ # Save frames to video
32
+ os.makedirs("frames", exist_ok=True)
33
+ for i, f in enumerate(frames):
34
+ f.save(f"frames/frame_{i:03d}.png")
35
+
36
+ # Output video filename
37
+ out_path = "output.mp4"
38
+
39
+ # Build MP4 video
40
+ os.system(f"ffmpeg -y -framerate 10 -i frames/frame_%03d.png {out_path}")
41
+
42
+ return out_path
43
+
44
+
45
+ # Gradio UI
46
+ with gr.Blocks() as demo:
47
+ gr.Markdown("# 🐱 AI Image → Video Generator (SVD)")
48
+ gr.Markdown("Upload an image and generate a short AI video using **Stable Video Diffusion img2vid**.")
49
+
50
+ with gr.Row():
51
+ inp_img = gr.Image(type="pil", label="Upload an input image")
52
+ num_frames = gr.Slider(4, 24, value=8, step=1, label="Number of Frames")
53
+
54
+ btn = gr.Button("Generate Video")
55
+
56
+ out_vid = gr.Video(label="Generated Video")
57
+
58
+ btn.click(generate_video, inputs=[inp_img, num_frames], outputs=out_vid)
59
+
60
+
61
+ demo.launch()