magnef commited on
Commit
e12cf73
·
verified ·
1 Parent(s): 9623f3a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +94 -297
app.py CHANGED
@@ -28,8 +28,8 @@ pipe.set_adapters(["causvid_lora"], adapter_weights=[0.95])
28
  pipe.fuse_lora()
29
 
30
  MOD_VALUE = 32
31
- DEFAULT_H_SLIDER_VALUE = 320
32
- DEFAULT_W_SLIDER_VALUE = 560
33
  NEW_FORMULA_MAX_AREA = 480.0 * 832.0
34
 
35
  SLIDER_MIN_H, SLIDER_MAX_H = 128, 896
@@ -37,170 +37,12 @@ SLIDER_MIN_W, SLIDER_MAX_W = 128, 896
37
  MAX_SEED = np.iinfo(np.int32).max
38
 
39
  FIXED_FPS = 24
40
- MIN_FRAMES_MODEL = 8
41
- MAX_FRAMES_MODEL = 120
42
 
43
  default_prompt_i2v = "make this image come alive, cinematic motion, smooth animation"
44
  default_negative_prompt = "Bright tones, overexposed, static, blurred details, subtitles, style, works, paintings, images, static, overall gray, worst quality, low quality, JPEG compression residue, ugly, incomplete, extra fingers, poorly drawn hands, poorly drawn faces, deformed, disfigured, misshapen limbs, fused fingers, still picture, messy background, three legs, many people in the background, walking backwards, watermark, text, signature"
45
 
46
- # CSS 스타일 정의
47
- custom_css = """
48
- /* 전체 배경 그라디언트 */
49
- .gradio-container {
50
- font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif !important;
51
- background: linear-gradient(135deg, #667eea 0%, #764ba2 25%, #f093fb 50%, #f5576c 75%, #fa709a 100%) !important;
52
- background-size: 400% 400% !important;
53
- animation: gradientShift 15s ease infinite !important;
54
- }
55
-
56
- @keyframes gradientShift {
57
- 0% { background-position: 0% 50%; }
58
- 50% { background-position: 100% 50%; }
59
- 100% { background-position: 0% 50%; }
60
- }
61
-
62
- /* 메인 컨테이너 스타일 */
63
- .main-container {
64
- backdrop-filter: blur(10px);
65
- background: rgba(255, 255, 255, 0.1) !important;
66
- border-radius: 20px !important;
67
- padding: 30px !important;
68
- box-shadow: 0 8px 32px 0 rgba(31, 38, 135, 0.37) !important;
69
- border: 1px solid rgba(255, 255, 255, 0.18) !important;
70
- }
71
-
72
- /* 헤더 스타일 */
73
- h1 {
74
- background: linear-gradient(45deg, #ffffff, #f0f0f0) !important;
75
- -webkit-background-clip: text !important;
76
- -webkit-text-fill-color: transparent !important;
77
- background-clip: text !important;
78
- font-weight: 800 !important;
79
- font-size: 2.5rem !important;
80
- text-align: center !important;
81
- margin-bottom: 2rem !important;
82
- text-shadow: 2px 2px 4px rgba(0,0,0,0.1) !important;
83
- }
84
-
85
- /* 컴포넌트 컨테이너 스타일 */
86
- .input-container, .output-container {
87
- background: rgba(255, 255, 255, 0.08) !important;
88
- border-radius: 15px !important;
89
- padding: 20px !important;
90
- margin: 10px 0 !important;
91
- backdrop-filter: blur(5px) !important;
92
- border: 1px solid rgba(255, 255, 255, 0.1) !important;
93
- }
94
-
95
- /* 입력 필드 스타일 */
96
- input, textarea, .gr-box {
97
- background: rgba(255, 255, 255, 0.9) !important;
98
- border: 1px solid rgba(255, 255, 255, 0.3) !important;
99
- border-radius: 10px !important;
100
- color: #333 !important;
101
- transition: all 0.3s ease !important;
102
- }
103
-
104
- input:focus, textarea:focus {
105
- background: rgba(255, 255, 255, 1) !important;
106
- border-color: #667eea !important;
107
- box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1) !important;
108
- }
109
-
110
- /* 버튼 스타일 */
111
- .generate-btn {
112
- background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
113
- color: white !important;
114
- font-weight: 600 !important;
115
- font-size: 1.1rem !important;
116
- padding: 12px 30px !important;
117
- border-radius: 50px !important;
118
- border: none !important;
119
- cursor: pointer !important;
120
- transition: all 0.3s ease !important;
121
- box-shadow: 0 4px 15px rgba(102, 126, 234, 0.4) !important;
122
- }
123
-
124
- .generate-btn:hover {
125
- transform: translateY(-2px) !important;
126
- box-shadow: 0 6px 20px rgba(102, 126, 234, 0.6) !important;
127
- }
128
-
129
- /* 슬라이더 스타일 */
130
- input[type="range"] {
131
- background: transparent !important;
132
- }
133
-
134
- input[type="range"]::-webkit-slider-track {
135
- background: rgba(255, 255, 255, 0.3) !important;
136
- border-radius: 5px !important;
137
- height: 6px !important;
138
- }
139
-
140
- input[type="range"]::-webkit-slider-thumb {
141
- background: linear-gradient(135deg, #667eea, #764ba2) !important;
142
- border: 2px solid white !important;
143
- border-radius: 50% !important;
144
- cursor: pointer !important;
145
- width: 18px !important;
146
- height: 18px !important;
147
- -webkit-appearance: none !important;
148
- }
149
-
150
- /* Accordion 스타일 */
151
- .gr-accordion {
152
- background: rgba(255, 255, 255, 0.05) !important;
153
- border-radius: 10px !important;
154
- border: 1px solid rgba(255, 255, 255, 0.1) !important;
155
- margin: 15px 0 !important;
156
- }
157
-
158
- /* 라벨 스타일 */
159
- label {
160
- color: #ffffff !important;
161
- font-weight: 500 !important;
162
- font-size: 0.95rem !important;
163
- margin-bottom: 5px !important;
164
- }
165
-
166
- /* 이미지 업로드 영역 */
167
- .image-upload {
168
- border: 2px dashed rgba(255, 255, 255, 0.3) !important;
169
- border-radius: 15px !important;
170
- background: rgba(255, 255, 255, 0.05) !important;
171
- transition: all 0.3s ease !important;
172
- }
173
-
174
- .image-upload:hover {
175
- border-color: rgba(255, 255, 255, 0.5) !important;
176
- background: rgba(255, 255, 255, 0.1) !important;
177
- }
178
-
179
- /* 비디오 출력 영역 */
180
- video {
181
- border-radius: 15px !important;
182
- box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3) !important;
183
- }
184
-
185
- /* Examples 섹션 스타일 */
186
- .gr-examples {
187
- background: rgba(255, 255, 255, 0.05) !important;
188
- border-radius: 15px !important;
189
- padding: 20px !important;
190
- margin-top: 20px !important;
191
- }
192
-
193
- /* Checkbox 스타일 */
194
- input[type="checkbox"] {
195
- accent-color: #667eea !important;
196
- }
197
-
198
- /* 반응형 애니메이션 */
199
- @media (max-width: 768px) {
200
- h1 { font-size: 2rem !important; }
201
- .main-container { padding: 20px !important; }
202
- }
203
- """
204
 
205
  def _calculate_new_dimensions_wan(pil_image, mod_val, calculation_max_area,
206
  min_slider_h, max_slider_h,
@@ -251,11 +93,51 @@ def get_duration(input_image, prompt, height, width,
251
 
252
  @spaces.GPU(duration=get_duration)
253
  def generate_video(input_image, prompt, height, width,
254
- negative_prompt=default_negative_prompt, duration_seconds = 2,
255
  guidance_scale = 1, steps = 4,
256
  seed = 42, randomize_seed = False,
257
  progress=gr.Progress(track_tqdm=True)):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
258
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
259
  if input_image is None:
260
  raise gr.Error("Please upload an input image.")
261
 
@@ -281,140 +163,55 @@ def generate_video(input_image, prompt, height, width,
281
  export_to_video(output_frames_list, video_path, fps=FIXED_FPS)
282
  return video_path, current_seed
283
 
284
- with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
285
- with gr.Column(elem_classes=["main-container"]):
286
- gr.Markdown("# Fast 4 steps Wan 2.1 I2V (14B) with CausVid LoRA")
287
-
288
- # Add badges side by side
289
- gr.HTML("""
290
- <div class="badge-container">
291
- <a href="https://huggingface.co/spaces/Heartsync/WAN2-1-fast-T2V-FusioniX" target="_blank">
292
- <img src="https://img.shields.io/static/v1?label=BASE&message=WAN%202.1%20T2V-FusioniX&color=%23008080&labelColor=%23533a7d&logo=huggingface&logoColor=%23ffffff&style=for-the-badge" alt="Base Model">
293
- </a>
294
- <a href="https://huggingface.co/spaces/Heartsync/WAN2-1-fast-T2V-FusioniX2" target="_blank">
295
- <img src="https://img.shields.io/static/v1?label=BASE&message=WAN%202.1%20T2V-Fusioni2X&color=%23008080&labelColor=%23533a7d&logo=huggingface&logoColor=%23ffffff&style=for-the-badge" alt="Base Model">
296
- </a>
297
- <a href="https://huggingface.co/spaces/Heartsync/wan2-1-fast-security" target="_blank">
298
- <img src="https://img.shields.io/static/v1?label=WAN%202.1&message=FAST%20%26%20Furios&color=%23008080&labelColor=%230000ff&logo=huggingface&logoColor=%23ffa500&style=for-the-badge" alt="badge">
299
- </a>
300
- </div>
301
- """)
302
-
303
- with gr.Row():
304
- with gr.Column(elem_classes=["input-container"]):
305
- input_image_component = gr.Image(
306
- type="pil",
307
- label="🖼️ Input Image (auto-resized to target H/W)",
308
- elem_classes=["image-upload"]
309
- )
310
- prompt_input = gr.Textbox(
311
- label="✏️ Prompt",
312
- value=default_prompt_i2v,
313
- lines=2
314
- )
315
- duration_seconds_input = gr.Slider(
316
- minimum=round(MIN_FRAMES_MODEL/FIXED_FPS,1),
317
- maximum=round(MAX_FRAMES_MODEL/FIXED_FPS,1),
318
- step=0.1,
319
- value=2,
320
- label="⏱️ Duration (seconds)",
321
- info=f"Clamped to model's {MIN_FRAMES_MODEL}-{MAX_FRAMES_MODEL} frames at {FIXED_FPS}fps."
322
- )
323
-
324
- with gr.Accordion("⚙️ Advanced Settings", open=False):
325
- negative_prompt_input = gr.Textbox(
326
- label="❌ Negative Prompt",
327
- value=default_negative_prompt,
328
- lines=3
329
- )
330
- seed_input = gr.Slider(
331
- label="🎲 Seed",
332
- minimum=0,
333
- maximum=MAX_SEED,
334
- step=1,
335
- value=42,
336
- interactive=True
337
- )
338
- randomize_seed_checkbox = gr.Checkbox(
339
- label="🔀 Randomize seed",
340
- value=True,
341
- interactive=True
342
- )
343
- with gr.Row():
344
- height_input = gr.Slider(
345
- minimum=SLIDER_MIN_H,
346
- maximum=SLIDER_MAX_H,
347
- step=MOD_VALUE,
348
- value=DEFAULT_H_SLIDER_VALUE,
349
- label=f"📏 Output Height (multiple of {MOD_VALUE})"
350
- )
351
- width_input = gr.Slider(
352
- minimum=SLIDER_MIN_W,
353
- maximum=SLIDER_MAX_W,
354
- step=MOD_VALUE,
355
- value=DEFAULT_W_SLIDER_VALUE,
356
- label=f"📐 Output Width (multiple of {MOD_VALUE})"
357
- )
358
- steps_slider = gr.Slider(
359
- minimum=1,
360
- maximum=30,
361
- step=1,
362
- value=4,
363
- label="🚀 Inference Steps"
364
- )
365
- guidance_scale_input = gr.Slider(
366
- minimum=0.0,
367
- maximum=20.0,
368
- step=0.5,
369
- value=1.0,
370
- label="🎯 Guidance Scale",
371
- visible=False
372
- )
373
-
374
- generate_button = gr.Button(
375
- "🎬 Generate Video",
376
- variant="primary",
377
- elem_classes=["generate-btn"]
378
- )
379
-
380
- with gr.Column(elem_classes=["output-container"]):
381
- video_output = gr.Video(
382
- label="🎥 Generated Video",
383
- autoplay=True,
384
- interactive=False
385
- )
386
-
387
- input_image_component.upload(
388
- fn=handle_image_upload_for_dims_wan,
389
- inputs=[input_image_component, height_input, width_input],
390
- outputs=[height_input, width_input]
391
- )
392
-
393
- input_image_component.clear(
394
- fn=handle_image_upload_for_dims_wan,
395
- inputs=[input_image_component, height_input, width_input],
396
- outputs=[height_input, width_input]
397
- )
398
-
399
- ui_inputs = [
400
- input_image_component, prompt_input, height_input, width_input,
401
- negative_prompt_input, duration_seconds_input,
402
- guidance_scale_input, steps_slider, seed_input, randomize_seed_checkbox
403
- ]
404
- generate_button.click(fn=generate_video, inputs=ui_inputs, outputs=[video_output, seed_input])
405
-
406
  with gr.Column():
407
- gr.Examples(
408
- examples=[
409
- ["peng.png", "a penguin playfully dancing in the snow, Antarctica", 896, 896],
410
- ["forg.jpg", "the frog jumps around", 832, 832],
411
- ],
412
- inputs=[input_image_component, prompt_input, height_input, width_input],
413
- outputs=[video_output, seed_input],
414
- fn=generate_video,
415
- cache_examples="lazy",
416
- label="🌟 Example Gallery"
417
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
418
 
419
  if __name__ == "__main__":
420
- demo.queue().launch()
 
28
  pipe.fuse_lora()
29
 
30
  MOD_VALUE = 32
31
+ DEFAULT_H_SLIDER_VALUE = 512
32
+ DEFAULT_W_SLIDER_VALUE = 896
33
  NEW_FORMULA_MAX_AREA = 480.0 * 832.0
34
 
35
  SLIDER_MIN_H, SLIDER_MAX_H = 128, 896
 
37
  MAX_SEED = np.iinfo(np.int32).max
38
 
39
  FIXED_FPS = 24
40
+ MIN_FRAMES_MODEL = 60
41
+ MAX_FRAMES_MODEL = 600
42
 
43
  default_prompt_i2v = "make this image come alive, cinematic motion, smooth animation"
44
  default_negative_prompt = "Bright tones, overexposed, static, blurred details, subtitles, style, works, paintings, images, static, overall gray, worst quality, low quality, JPEG compression residue, ugly, incomplete, extra fingers, poorly drawn hands, poorly drawn faces, deformed, disfigured, misshapen limbs, fused fingers, still picture, messy background, three legs, many people in the background, walking backwards, watermark, text, signature"
45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
 
47
  def _calculate_new_dimensions_wan(pil_image, mod_val, calculation_max_area,
48
  min_slider_h, max_slider_h,
 
93
 
94
  @spaces.GPU(duration=get_duration)
95
  def generate_video(input_image, prompt, height, width,
96
+ negative_prompt=default_negative_prompt, duration_seconds = 5,
97
  guidance_scale = 1, steps = 4,
98
  seed = 42, randomize_seed = False,
99
  progress=gr.Progress(track_tqdm=True)):
100
+ """
101
+ Generate a video from an input image using the Wan 2.1 I2V model with CausVid LoRA.
102
+
103
+ This function takes an input image and generates a video animation based on the provided
104
+ prompt and parameters. It uses the Wan 2.1 14B Image-to-Video model with CausVid LoRA
105
+ for fast generation in 4-8 steps.
106
+
107
+ Args:
108
+ input_image (PIL.Image): The input image to animate. Will be resized to target dimensions.
109
+ prompt (str): Text prompt describing the desired animation or motion.
110
+ height (int): Target height for the output video. Will be adjusted to multiple of MOD_VALUE (32).
111
+ width (int): Target width for the output video. Will be adjusted to multiple of MOD_VALUE (32).
112
+ negative_prompt (str, optional): Negative prompt to avoid unwanted elements.
113
+ Defaults to default_negative_prompt (contains unwanted visual artifacts).
114
+ duration_seconds (float, optional): Duration of the generated video in seconds.
115
+ Defaults to 2. Clamped between MIN_FRAMES_MODEL/FIXED_FPS and MAX_FRAMES_MODEL/FIXED_FPS.
116
+ guidance_scale (float, optional): Controls adherence to the prompt. Higher values = more adherence.
117
+ Defaults to 1.0. Range: 0.0-20.0.
118
+ steps (int, optional): Number of inference steps. More steps = higher quality but slower.
119
+ Defaults to 4. Range: 1-30.
120
+ seed (int, optional): Random seed for reproducible results. Defaults to 42.
121
+ Range: 0 to MAX_SEED (2147483647).
122
+ randomize_seed (bool, optional): Whether to use a random seed instead of the provided seed.
123
+ Defaults to False.
124
+ progress (gr.Progress, optional): Gradio progress tracker. Defaults to gr.Progress(track_tqdm=True).
125
 
126
+ Returns:
127
+ tuple: A tuple containing:
128
+ - video_path (str): Path to the generated video file (.mp4)
129
+ - current_seed (int): The seed used for generation (useful when randomize_seed=True)
130
+
131
+ Raises:
132
+ gr.Error: If input_image is None (no image uploaded).
133
+
134
+ Note:
135
+ - The function automatically resizes the input image to the target dimensions
136
+ - Frame count is calculated as duration_seconds * FIXED_FPS (24)
137
+ - Output dimensions are adjusted to be multiples of MOD_VALUE (32)
138
+ - The function uses GPU acceleration via the @spaces.GPU decorator
139
+ - Generation time varies based on steps and duration (see get_duration function)
140
+ """
141
  if input_image is None:
142
  raise gr.Error("Please upload an input image.")
143
 
 
163
  export_to_video(output_frames_list, video_path, fps=FIXED_FPS)
164
  return video_path, current_seed
165
 
166
+ with gr.Blocks() as demo:
167
+ gr.Markdown("# Fast 4 steps Wan 2.1 I2V (14B) with CausVid LoRA")
168
+ gr.Markdown("[CausVid](https://github.com/tianweiy/CausVid) is a distilled version of Wan 2.1 to run faster in just 4-8 steps, [extracted as LoRA by Kijai](https://huggingface.co/Kijai/WanVideo_comfy/blob/main/Wan21_CausVid_14B_T2V_lora_rank32.safetensors) and is compatible with 🧨 diffusers")
169
+ with gr.Row():
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
170
  with gr.Column():
171
+ input_image_component = gr.Image(type="pil", label="Input Image (auto-resized to target H/W)")
172
+ prompt_input = gr.Textbox(label="Prompt", value=default_prompt_i2v)
173
+ duration_seconds_input = gr.Slider(minimum=round(MIN_FRAMES_MODEL/FIXED_FPS,1), maximum=round(MAX_FRAMES_MODEL/FIXED_FPS,1), step=0.1, value=2, label="Duration (seconds)", info=f"Clamped to model's {MIN_FRAMES_MODEL}-{MAX_FRAMES_MODEL} frames at {FIXED_FPS}fps.")
174
+
175
+ with gr.Accordion("Advanced Settings", open=False):
176
+ negative_prompt_input = gr.Textbox(label="Negative Prompt", value=default_negative_prompt, lines=3)
177
+ seed_input = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=42, interactive=True)
178
+ randomize_seed_checkbox = gr.Checkbox(label="Randomize seed", value=True, interactive=True)
179
+ with gr.Row():
180
+ height_input = gr.Slider(minimum=SLIDER_MIN_H, maximum=SLIDER_MAX_H, step=MOD_VALUE, value=DEFAULT_H_SLIDER_VALUE, label=f"Output Height (multiple of {MOD_VALUE})")
181
+ width_input = gr.Slider(minimum=SLIDER_MIN_W, maximum=SLIDER_MAX_W, step=MOD_VALUE, value=DEFAULT_W_SLIDER_VALUE, label=f"Output Width (multiple of {MOD_VALUE})")
182
+ steps_slider = gr.Slider(minimum=1, maximum=30, step=1, value=4, label="Inference Steps")
183
+ guidance_scale_input = gr.Slider(minimum=0.0, maximum=20.0, step=0.5, value=1.0, label="Guidance Scale", visible=False)
184
+
185
+ generate_button = gr.Button("Generate Video", variant="primary")
186
+ with gr.Column():
187
+ video_output = gr.Video(label="Generated Video", autoplay=True, interactive=False)
188
+
189
+ input_image_component.upload(
190
+ fn=handle_image_upload_for_dims_wan,
191
+ inputs=[input_image_component, height_input, width_input],
192
+ outputs=[height_input, width_input]
193
+ )
194
+
195
+ input_image_component.clear(
196
+ fn=handle_image_upload_for_dims_wan,
197
+ inputs=[input_image_component, height_input, width_input],
198
+ outputs=[height_input, width_input]
199
+ )
200
+
201
+ ui_inputs = [
202
+ input_image_component, prompt_input, height_input, width_input,
203
+ negative_prompt_input, duration_seconds_input,
204
+ guidance_scale_input, steps_slider, seed_input, randomize_seed_checkbox
205
+ ]
206
+ generate_button.click(fn=generate_video, inputs=ui_inputs, outputs=[video_output, seed_input])
207
+
208
+ gr.Examples(
209
+ examples=[
210
+ ["peng.png", "a penguin playfully dancing in the snow, Antarctica", 896, 512],
211
+ ["forg.jpg", "the frog jumps around", 448, 832],
212
+ ],
213
+ inputs=[input_image_component, prompt_input, height_input, width_input], outputs=[video_output, seed_input], fn=generate_video, cache_examples="lazy"
214
+ )
215
 
216
  if __name__ == "__main__":
217
+ demo.queue().launch(mcp_server=True)