Peeble commited on
Commit
fbc5276
Β·
verified Β·
1 Parent(s): d374f60

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +86 -43
app.py CHANGED
@@ -9,14 +9,42 @@ import cv2
9
 
10
  device = "cuda" if torch.cuda.is_available() else "cpu"
11
 
12
- pipe = StableVideoDiffusionPipeline.from_pretrained(
13
- "stabilityai/stable-video-diffusion-img2vid-xt",
14
- torch_dtype=torch.float16 if device == "cuda" else torch.float32
15
- )
16
- pipe = pipe.to(device)
17
 
 
 
 
18
 
19
- # πŸŽ₯ Extract first frame from video
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  def extract_frame(video_path):
21
  cap = cv2.VideoCapture(video_path)
22
  success, frame = cap.read()
@@ -28,60 +56,75 @@ def extract_frame(video_path):
28
  return None
29
 
30
 
31
- def generate_video(image, video, fps, motion_strength):
32
- # Decide input source
33
- if image is not None:
34
- input_image = image.convert("RGB")
35
- elif video is not None:
36
- input_image = extract_frame(video)
37
- if input_image is None:
38
  return None
39
- else:
40
- return None
41
 
42
- # Generate frames
43
- output = pipe(
44
- input_image,
45
- num_frames=32,
46
- decode_chunk_size=8,
47
- motion_bucket_id=int(motion_strength)
48
- )
 
 
49
 
50
- frames = output.frames[0]
 
51
 
52
- # Convert frames
53
- frames = [(frame * 255).astype(np.uint8) for frame in frames]
 
 
 
 
 
54
 
55
- filename = f"video_{uuid.uuid4().hex}.mp4"
 
56
 
57
- imageio.mimsave(
58
- filename,
59
- frames,
60
- fps=fps,
61
- codec="libx264",
62
- quality=8
63
- )
64
 
65
- return filename
 
 
 
 
 
66
 
 
67
 
 
 
 
 
 
 
68
  with gr.Blocks() as demo:
69
- gr.Markdown("# 🎬 StuffMotion AI (Image + Video β†’ Motion Video)")
70
- gr.Markdown("Upload an image OR a video to generate AI motion")
71
 
72
- image_input = gr.Image(type="pil", label="πŸ–ΌοΈ Image Input (optional)")
73
- video_input = gr.Video(label="πŸŽ₯ Video Input (optional)")
 
 
 
 
 
 
74
 
75
- fps = gr.Slider(8, 30, value=12, step=1, label="FPS")
76
- motion = gr.Slider(1, 255, value=127, label="Motion Strength")
77
 
78
- generate_btn = gr.Button("Generate")
79
 
80
- video_output = gr.Video(label="🎬 Output")
81
 
82
  generate_btn.click(
83
  fn=generate_video,
84
- inputs=[image_input, video_input, fps, motion],
85
  outputs=video_output
86
  )
87
 
 
9
 
10
  device = "cuda" if torch.cuda.is_available() else "cpu"
11
 
12
+ pipe = None
13
+ current_model = None
 
 
 
14
 
15
+ # πŸ”„ Load model only when needed (fixes slow startup)
16
+ def load_model(model_name):
17
+ global pipe, current_model
18
 
19
+ if current_model == model_name:
20
+ return pipe
21
+
22
+ try:
23
+ if model_name == "Fast (SVD)":
24
+ model_id = "stabilityai/stable-video-diffusion-img2vid"
25
+ else:
26
+ model_id = "stabilityai/stable-video-diffusion-img2vid-xt"
27
+
28
+ pipe = StableVideoDiffusionPipeline.from_pretrained(
29
+ model_id,
30
+ torch_dtype=torch.float16 if device == "cuda" else torch.float32
31
+ )
32
+
33
+ pipe = pipe.to(device)
34
+
35
+ if device == "cuda":
36
+ pipe.enable_attention_slicing()
37
+ pipe.enable_model_cpu_offload()
38
+
39
+ current_model = model_name
40
+ return pipe
41
+
42
+ except Exception as e:
43
+ print("Model load error:", e)
44
+ return None
45
+
46
+
47
+ # πŸŽ₯ Extract frame from video
48
  def extract_frame(video_path):
49
  cap = cv2.VideoCapture(video_path)
50
  success, frame = cap.read()
 
56
  return None
57
 
58
 
59
+ def generate_video(image, video, fps, motion_strength, model_choice):
60
+ try:
61
+ pipe = load_model(model_choice)
62
+ if pipe is None:
 
 
 
63
  return None
 
 
64
 
65
+ # Select input
66
+ if image is not None:
67
+ input_image = image.convert("RGB")
68
+ elif video is not None:
69
+ input_image = extract_frame(video)
70
+ if input_image is None:
71
+ return None
72
+ else:
73
+ return None
74
 
75
+ # Resize (⚑ HUGE speed boost)
76
+ input_image = input_image.resize((512, 512))
77
 
78
+ # Generate frames (reduced for speed)
79
+ output = pipe(
80
+ input_image,
81
+ num_frames=16, # ⚑ faster
82
+ decode_chunk_size=4,
83
+ motion_bucket_id=int(motion_strength)
84
+ )
85
 
86
+ frames = output.frames[0]
87
+ frames = [(frame * 255).astype(np.uint8) for frame in frames]
88
 
89
+ filename = f"video_{uuid.uuid4().hex}.mp4"
 
 
 
 
 
 
90
 
91
+ imageio.mimsave(
92
+ filename,
93
+ frames,
94
+ fps=fps,
95
+ codec="libx264"
96
+ )
97
 
98
+ return filename
99
 
100
+ except Exception as e:
101
+ print("Generation error:", e)
102
+ return None
103
+
104
+
105
+ # 🎨 UI
106
  with gr.Blocks() as demo:
107
+ gr.Markdown("# 🎬 StuffMotion AI (FAST + MODEL SELECT)")
 
108
 
109
+ image_input = gr.Image(type="pil", label="πŸ–ΌοΈ Image Input")
110
+ video_input = gr.Video(label="πŸŽ₯ Video Input")
111
+
112
+ model_choice = gr.Dropdown(
113
+ ["Fast (SVD)", "High Quality (XT)"],
114
+ value="Fast (SVD)",
115
+ label="🧠 Model"
116
+ )
117
 
118
+ fps = gr.Slider(8, 24, value=12, step=1, label="FPS")
119
+ motion = gr.Slider(1, 255, value=100, label="Motion")
120
 
121
+ generate_btn = gr.Button("⚑ Generate")
122
 
123
+ video_output = gr.Video()
124
 
125
  generate_btn.click(
126
  fn=generate_video,
127
+ inputs=[image_input, video_input, fps, motion, model_choice],
128
  outputs=video_output
129
  )
130