jbilcke-hf HF Staff commited on
Commit
08832e7
·
verified ·
1 Parent(s): 02d0c19

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +84 -16
app.py CHANGED
@@ -7,6 +7,7 @@ import spaces
7
  from huggingface_hub import hf_hub_download
8
  import numpy as np
9
  import random
 
10
 
11
  MODEL_ID = "Wan-AI/Wan2.1-T2V-1.3B-Diffusers"
12
  LORA_REPO_ID = "Kijai/WanVideo_comfy"
@@ -28,8 +29,31 @@ MOD_VALUE = 32
28
  DEFAULT_H_SLIDER_VALUE = 384 # 512
29
  DEFAULT_W_SLIDER_VALUE = 640 # 896
30
 
31
- SLIDER_MIN_H, SLIDER_MAX_H = 128, 1280
32
- SLIDER_MIN_W, SLIDER_MAX_W = 128, 1280
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  MAX_SEED = np.iinfo(np.int32).max
34
 
35
  FIXED_FPS = 24
@@ -62,7 +86,7 @@ def generate_video(prompt, height, width,
62
 
63
  This function takes a text prompt and generates a video based on the provided
64
  prompt and parameters. It uses the Wan 2.1 1.3B Text-to-Video model with CausVid LoRA
65
- for fast generation in 4-8 steps.
66
 
67
  Args:
68
  prompt (str): Text prompt describing the desired video content.
@@ -97,7 +121,14 @@ def generate_video(prompt, height, width,
97
  - Generation time varies based on steps and duration (see get_duration function)
98
  """
99
  if not prompt or prompt.strip() == "":
100
- raise gr.Error("Please enter a text prompt.")
 
 
 
 
 
 
 
101
 
102
  target_h = max(MOD_VALUE, (int(height) // MOD_VALUE) * MOD_VALUE)
103
  target_w = max(MOD_VALUE, (int(width) // MOD_VALUE) * MOD_VALUE)
@@ -120,21 +151,45 @@ def generate_video(prompt, height, width,
120
  return video_path, current_seed
121
 
122
  with gr.Blocks() as demo:
123
- gr.Markdown("# InstaVideo")
124
- gr.Markdown("This 🧨 diffusers demo uses the [Wan2.1 1.3B CausVid LoRA by Kijai](https://huggingface.co/Kijai/WanVideo_comfy/blob/main/Wan21_CausVid_bidirect2_T2V_1_3B_lora_rank32.safetensors).")
 
 
 
 
 
125
  with gr.Row():
126
  with gr.Column():
127
  prompt_input = gr.Textbox(label="Prompt", value=default_prompt_t2v, placeholder="Describe the video you want to generate...")
128
- 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.")
 
 
 
 
 
 
 
129
 
130
  with gr.Accordion("Advanced Settings", open=False):
131
  negative_prompt_input = gr.Textbox(label="Negative Prompt", value=default_negative_prompt, lines=3)
132
  seed_input = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=42, interactive=True)
133
  randomize_seed_checkbox = gr.Checkbox(label="Randomize seed", value=True, interactive=True)
134
  with gr.Row():
135
- 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})")
136
- 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})")
137
- steps_slider = gr.Slider(minimum=1, maximum=8, step=1, value=3, label="Inference Steps")
 
 
 
 
 
 
 
 
 
 
 
 
138
  guidance_scale_input = gr.Slider(minimum=0.0, maximum=20.0, step=0.5, value=1.0, label="Guidance Scale", visible=False)
139
 
140
  generate_button = gr.Button("Generate Video", variant="primary")
@@ -148,13 +203,26 @@ with gr.Blocks() as demo:
148
  ]
149
  generate_button.click(fn=generate_video, inputs=ui_inputs, outputs=[video_output, seed_input])
150
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
151
  gr.Examples(
152
- examples=[
153
- ["a majestic eagle soaring through mountain peaks, cinematic aerial view", 896, 512],
154
- ["a serene ocean wave crashing on a sandy beach at sunset", 448, 832],
155
- ["a field of flowers swaying in the wind, spring morning light", 512, 896],
156
- ],
157
- inputs=[prompt_input, height_input, width_input], outputs=[video_output, seed_input], fn=generate_video, cache_examples="lazy"
158
  )
159
 
160
  if __name__ == "__main__":
 
7
  from huggingface_hub import hf_hub_download
8
  import numpy as np
9
  import random
10
+ import os
11
 
12
  MODEL_ID = "Wan-AI/Wan2.1-T2V-1.3B-Diffusers"
13
  LORA_REPO_ID = "Kijai/WanVideo_comfy"
 
29
  DEFAULT_H_SLIDER_VALUE = 384 # 512
30
  DEFAULT_W_SLIDER_VALUE = 640 # 896
31
 
32
+ # Environment variable check
33
+ IS_ORIGINAL_SPACE = os.environ.get("IS_ORIGINAL_SPACE", "False") == "True"
34
+
35
+ # Original limits
36
+ ORIGINAL_SLIDER_MIN_H, ORIGINAL_SLIDER_MAX_H = 128, 1280
37
+ ORIGINAL_SLIDER_MIN_W, ORIGINAL_SLIDER_MAX_W = 128, 1280
38
+ ORIGINAL_MAX_DURATION = round(81/24, 1) # MAX_FRAMES_MODEL/FIXED_FPS
39
+
40
+ # Limited space constants
41
+ LIMITED_MAX_RESOLUTION = 640
42
+ LIMITED_MAX_DURATION = 2.0
43
+ LIMITED_MAX_STEPS = 3
44
+
45
+ # Set limits based on environment variable
46
+ if IS_ORIGINAL_SPACE:
47
+ SLIDER_MIN_H, SLIDER_MAX_H = ORIGINAL_SLIDER_MIN_H, ORIGINAL_SLIDER_MAX_H
48
+ SLIDER_MIN_W, SLIDER_MAX_W = ORIGINAL_SLIDER_MIN_W, ORIGINAL_SLIDER_MAX_W
49
+ MAX_DURATION = ORIGINAL_MAX_DURATION
50
+ MAX_STEPS = 8
51
+ else:
52
+ SLIDER_MIN_H, SLIDER_MAX_H = 128, LIMITED_MAX_RESOLUTION
53
+ SLIDER_MIN_W, SLIDER_MAX_W = 128, LIMITED_MAX_RESOLUTION
54
+ MAX_DURATION = LIMITED_MAX_DURATION
55
+ MAX_STEPS = LIMITED_MAX_STEPS
56
+
57
  MAX_SEED = np.iinfo(np.int32).max
58
 
59
  FIXED_FPS = 24
 
86
 
87
  This function takes a text prompt and generates a video based on the provided
88
  prompt and parameters. It uses the Wan 2.1 1.3B Text-to-Video model with CausVid LoRA
89
+ for fast generation in 3-8 steps.
90
 
91
  Args:
92
  prompt (str): Text prompt describing the desired video content.
 
121
  - Generation time varies based on steps and duration (see get_duration function)
122
  """
123
  if not prompt or prompt.strip() == "":
124
+ raise gr.Error("Please enter a text prompt. Try to use long and precise descriptions.")
125
+
126
+ # Apply limits based on environment variable
127
+ if not IS_ORIGINAL_SPACE:
128
+ height = min(height, LIMITED_MAX_RESOLUTION)
129
+ width = min(width, LIMITED_MAX_RESOLUTION)
130
+ duration_seconds = min(duration_seconds, LIMITED_MAX_DURATION)
131
+ steps = min(steps, LIMITED_MAX_STEPS)
132
 
133
  target_h = max(MOD_VALUE, (int(height) // MOD_VALUE) * MOD_VALUE)
134
  target_w = max(MOD_VALUE, (int(width) // MOD_VALUE) * MOD_VALUE)
 
151
  return video_path, current_seed
152
 
153
  with gr.Blocks() as demo:
154
+ gr.Markdown("# InstaVideo")
155
+ gr.Markdown("This Gradio space is forked by [wan2-1-fast from multimodalart](https://huggingface.co/spaces/multimodalart/wan2-1-fast), and is powered by the Wan CausVid LoRA [from Kijai](https://huggingface.co/Kijai/WanVideo_comfy/blob/main/Wan21_CausVid_bidirect2_T2V_1_3B_lora_rank32.safetensors).")
156
+
157
+ # Add notice for limited spaces
158
+ if not IS_ORIGINAL_SPACE:
159
+ gr.Markdown("This free public demo limits the resolution to 640x640, duration to 2s, and inference steps to 3. For full capabilities please duplicate this space.")
160
+
161
  with gr.Row():
162
  with gr.Column():
163
  prompt_input = gr.Textbox(label="Prompt", value=default_prompt_t2v, placeholder="Describe the video you want to generate...")
164
+ duration_seconds_input = gr.Slider(
165
+ minimum=round(MIN_FRAMES_MODEL/FIXED_FPS,1),
166
+ maximum=MAX_DURATION,
167
+ step=0.1,
168
+ value=2,
169
+ label="Duration (seconds)",
170
+ info=f"Clamped to model's {MIN_FRAMES_MODEL}-{MAX_FRAMES_MODEL} frames at {FIXED_FPS}fps."
171
+ )
172
 
173
  with gr.Accordion("Advanced Settings", open=False):
174
  negative_prompt_input = gr.Textbox(label="Negative Prompt", value=default_negative_prompt, lines=3)
175
  seed_input = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=42, interactive=True)
176
  randomize_seed_checkbox = gr.Checkbox(label="Randomize seed", value=True, interactive=True)
177
  with gr.Row():
178
+ height_input = gr.Slider(
179
+ minimum=SLIDER_MIN_H,
180
+ maximum=SLIDER_MAX_H,
181
+ step=MOD_VALUE,
182
+ value=min(DEFAULT_H_SLIDER_VALUE, SLIDER_MAX_H),
183
+ label=f"Output Height (multiple of {MOD_VALUE})"
184
+ )
185
+ width_input = gr.Slider(
186
+ minimum=SLIDER_MIN_W,
187
+ maximum=SLIDER_MAX_W,
188
+ step=MOD_VALUE,
189
+ value=min(DEFAULT_W_SLIDER_VALUE, SLIDER_MAX_W),
190
+ label=f"Output Width (multiple of {MOD_VALUE})"
191
+ )
192
+ steps_slider = gr.Slider(minimum=1, maximum=MAX_STEPS, step=1, value=3, label="Inference Steps")
193
  guidance_scale_input = gr.Slider(minimum=0.0, maximum=20.0, step=0.5, value=1.0, label="Guidance Scale", visible=False)
194
 
195
  generate_button = gr.Button("Generate Video", variant="primary")
 
203
  ]
204
  generate_button.click(fn=generate_video, inputs=ui_inputs, outputs=[video_output, seed_input])
205
 
206
+ # Adjust examples based on space limits
207
+ example_configs = [
208
+ ["a majestic eagle soaring through mountain peaks, cinematic aerial view", 896, 512],
209
+ ["a serene ocean wave crashing on a sandy beach at sunset", 448, 832],
210
+ ["a field of flowers swaying in the wind, spring morning light", 512, 896],
211
+ ]
212
+
213
+ if not IS_ORIGINAL_SPACE:
214
+ # Limit example resolutions for limited spaces
215
+ example_configs = [
216
+ [example[0], min(example[1], LIMITED_MAX_RESOLUTION), min(example[2], LIMITED_MAX_RESOLUTION)]
217
+ for example in example_configs
218
+ ]
219
+
220
  gr.Examples(
221
+ examples=example_configs,
222
+ inputs=[prompt_input, height_input, width_input],
223
+ outputs=[video_output, seed_input],
224
+ fn=generate_video,
225
+ cache_examples="lazy"
 
226
  )
227
 
228
  if __name__ == "__main__":