linoyts HF Staff commited on
Commit
fe70d6a
·
verified ·
1 Parent(s): a6939eb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -44
app.py CHANGED
@@ -5,7 +5,7 @@ os.system('pip install --upgrade --pre --extra-index-url https://download.pytorc
5
  # Actual demo code
6
  import spaces
7
  import torch
8
- from diffusers.pipelines.wan.pipeline_wan import WanPipeline
9
  from diffusers.models.transformers.transformer_wan import WanTransformer3DModel
10
  from diffusers.utils.export_utils import export_to_video
11
  import gradio as gr
@@ -30,7 +30,7 @@ MAX_FRAMES_MODEL = 81
30
  MIN_DURATION = round(MIN_FRAMES_MODEL/FIXED_FPS,1)
31
  MAX_DURATION = round(MAX_FRAMES_MODEL/FIXED_FPS,1)
32
 
33
-
34
  pipe = WanPipeline.from_pretrained(MODEL_ID,
35
  transformer=WanTransformer3DModel.from_pretrained('cbensimon/Wan2.2-I2V-A14B-bf16-Diffusers',
36
  subfolder='transformer',
@@ -42,6 +42,7 @@ pipe = WanPipeline.from_pretrained(MODEL_ID,
42
  torch_dtype=torch.bfloat16,
43
  device_map='cuda',
44
  ),
 
45
  torch_dtype=torch.bfloat16,
46
  ).to('cuda')
47
 
@@ -80,7 +81,6 @@ for i in range(3):
80
  torch.cuda.empty_cache()
81
 
82
  optimize_pipeline_(pipe,
83
- # image=Image.new('RGB', (LANDSCAPE_WIDTH, LANDSCAPE_HEIGHT)),
84
  prompt='prompt',
85
  height=LANDSCAPE_HEIGHT,
86
  width=LANDSCAPE_WIDTH,
@@ -88,34 +88,11 @@ optimize_pipeline_(pipe,
88
  )
89
 
90
 
91
- default_prompt_i2v = "make this image come alive, cinematic motion, smooth animation"
92
  default_negative_prompt = "色调艳丽, 过曝, 静态, 细节模糊不清, 字幕, 风格, 作品, 画作, 画面, 静止, 整体发灰, 最差质量, 低质量, JPEG压缩残留, 丑陋的, 残缺的, 多余的手指, 画得不好的手部, 画得不好的脸部, 畸形的, 毁容的, 形态畸形的肢体, 手指融合, 静止不动的画面, 杂乱的背景, 三条腿, 背景人很多, 倒着走"
93
 
94
 
95
- def resize_image(image: Image.Image) -> Image.Image:
96
- if image.height > image.width:
97
- transposed = image.transpose(Image.Transpose.ROTATE_90)
98
- resized = resize_image_landscape(transposed)
99
- return resized.transpose(Image.Transpose.ROTATE_270)
100
- return resize_image_landscape(image)
101
-
102
-
103
- def resize_image_landscape(image: Image.Image) -> Image.Image:
104
- target_aspect = LANDSCAPE_WIDTH / LANDSCAPE_HEIGHT
105
- width, height = image.size
106
- in_aspect = width / height
107
- if in_aspect > target_aspect:
108
- new_width = round(height * target_aspect)
109
- left = (width - new_width) // 2
110
- image = image.crop((left, 0, left + new_width, height))
111
- else:
112
- new_height = round(width / target_aspect)
113
- top = (height - new_height) // 2
114
- image = image.crop((0, top, width, top + new_height))
115
- return image.resize((LANDSCAPE_WIDTH, LANDSCAPE_HEIGHT), Image.LANCZOS)
116
-
117
  def get_duration(
118
- input_image,
119
  prompt,
120
  negative_prompt,
121
  duration_seconds,
@@ -130,13 +107,12 @@ def get_duration(
130
 
131
  @spaces.GPU(duration=get_duration)
132
  def generate_video(
133
- input_image,
134
  prompt,
135
  negative_prompt=default_negative_prompt,
136
  duration_seconds = MAX_DURATION,
137
  guidance_scale = 1,
138
  guidance_scale_2 = 3,
139
- steps = 6,
140
  seed = 42,
141
  randomize_seed = False,
142
  progress=gr.Progress(track_tqdm=True),
@@ -149,7 +125,6 @@ def generate_video(
149
  for fast generation in 6-8 steps.
150
 
151
  Args:
152
- input_image (PIL.Image): The input image to animate. Will be resized to target dimensions.
153
  prompt (str): Text prompt describing the desired animation or motion.
154
  negative_prompt (str, optional): Negative prompt to avoid unwanted elements.
155
  Defaults to default_negative_prompt (contains unwanted visual artifacts).
@@ -182,15 +157,11 @@ def generate_video(
182
  - The function uses GPU acceleration via the @spaces.GPU decorator
183
  - Generation time varies based on steps and duration (see get_duration function)
184
  """
185
- # if input_image is None:
186
- # raise gr.Error("Please upload an input image.")
187
 
188
  num_frames = np.clip(int(round(duration_seconds * FIXED_FPS)), MIN_FRAMES_MODEL, MAX_FRAMES_MODEL)
189
  current_seed = random.randint(0, MAX_SEED) if randomize_seed else int(seed)
190
- # resized_image = resize_image(input_image)
191
 
192
  output_frames_list = pipe(
193
- #image=resized_image,
194
  prompt=prompt,
195
  negative_prompt=negative_prompt,
196
  height=480,
@@ -214,7 +185,6 @@ with gr.Blocks() as demo:
214
  gr.Markdown("run Wan 2.2 in just 6-8 steps, with [FusionX Phantom LoRA by DeeJayT](https://huggingface.co/vrgamedevgirl84/Wan14BT2VFusioniX/tree/main/FusionX_LoRa), compatible with 🧨 diffusers")
215
  with gr.Row():
216
  with gr.Column():
217
- input_image_component = gr.Image(type="pil", label="Input Image (auto-resized to target H/W)", visible=False)
218
  prompt_input = gr.Textbox(label="Prompt", value=default_prompt_i2v)
219
  duration_seconds_input = gr.Slider(minimum=MIN_DURATION, maximum=MAX_DURATION, step=0.1, value=MAX_DURATION, label="Duration (seconds)", info=f"Clamped to model's {MIN_FRAMES_MODEL}-{MAX_FRAMES_MODEL} frames at {FIXED_FPS}fps.")
220
 
@@ -237,15 +207,14 @@ with gr.Blocks() as demo:
237
  ]
238
  generate_button.click(fn=generate_video, inputs=ui_inputs, outputs=[video_output, seed_input])
239
 
240
- # gr.Examples(
241
- # examples=[
242
- # [
243
- # "wan_i2v_input.JPG",
244
- # "Summer beach vacation style, a white cat wearing sunglasses sits on a surfboard. The fluffy-furred feline gazes directly at the camera with a relaxed expression. Blurred beach scenery forms the background featuring crystal-clear waters, distant green hills, and a blue sky dotted with white clouds. The cat assumes a naturally relaxed posture, as if savoring the sea breeze and warm sunlight. A close-up shot highlights the feline's intricate details and the refreshing atmosphere of the seaside.",
245
- # ],
246
- # ],
247
- # inputs=[input_image_component, prompt_input], outputs=[video_output, seed_input], fn=generate_video, cache_examples="lazy"
248
- # )
249
 
250
  if __name__ == "__main__":
251
  demo.queue().launch(mcp_server=True)
 
5
  # Actual demo code
6
  import spaces
7
  import torch
8
+ from diffusers import WanPipeline, AutoencoderKLWan
9
  from diffusers.models.transformers.transformer_wan import WanTransformer3DModel
10
  from diffusers.utils.export_utils import export_to_video
11
  import gradio as gr
 
30
  MIN_DURATION = round(MIN_FRAMES_MODEL/FIXED_FPS,1)
31
  MAX_DURATION = round(MAX_FRAMES_MODEL/FIXED_FPS,1)
32
 
33
+ vae = AutoencoderKLWan.from_pretrained("Wan-AI/Wan2.2-T2V-A14B-Diffusers", subfolder="vae", torch_dtype=torch.float32)
34
  pipe = WanPipeline.from_pretrained(MODEL_ID,
35
  transformer=WanTransformer3DModel.from_pretrained('cbensimon/Wan2.2-I2V-A14B-bf16-Diffusers',
36
  subfolder='transformer',
 
42
  torch_dtype=torch.bfloat16,
43
  device_map='cuda',
44
  ),
45
+ vae=vae,
46
  torch_dtype=torch.bfloat16,
47
  ).to('cuda')
48
 
 
81
  torch.cuda.empty_cache()
82
 
83
  optimize_pipeline_(pipe,
 
84
  prompt='prompt',
85
  height=LANDSCAPE_HEIGHT,
86
  width=LANDSCAPE_WIDTH,
 
88
  )
89
 
90
 
91
+ default_prompt_i2v = "Two anthropomorphic cats in comfy boxing gear and bright gloves fight intensely on a spotlighted stage."
92
  default_negative_prompt = "色调艳丽, 过曝, 静态, 细节模糊不清, 字幕, 风格, 作品, 画作, 画面, 静止, 整体发灰, 最差质量, 低质量, JPEG压缩残留, 丑陋的, 残缺的, 多余的手指, 画得不好的手部, 画得不好的脸部, 畸形的, 毁容的, 形态畸形的肢体, 手指融合, 静止不动的画面, 杂乱的背景, 三条腿, 背景人很多, 倒着走"
93
 
94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95
  def get_duration(
 
96
  prompt,
97
  negative_prompt,
98
  duration_seconds,
 
107
 
108
  @spaces.GPU(duration=get_duration)
109
  def generate_video(
 
110
  prompt,
111
  negative_prompt=default_negative_prompt,
112
  duration_seconds = MAX_DURATION,
113
  guidance_scale = 1,
114
  guidance_scale_2 = 3,
115
+ steps = 4,
116
  seed = 42,
117
  randomize_seed = False,
118
  progress=gr.Progress(track_tqdm=True),
 
125
  for fast generation in 6-8 steps.
126
 
127
  Args:
 
128
  prompt (str): Text prompt describing the desired animation or motion.
129
  negative_prompt (str, optional): Negative prompt to avoid unwanted elements.
130
  Defaults to default_negative_prompt (contains unwanted visual artifacts).
 
157
  - The function uses GPU acceleration via the @spaces.GPU decorator
158
  - Generation time varies based on steps and duration (see get_duration function)
159
  """
 
 
160
 
161
  num_frames = np.clip(int(round(duration_seconds * FIXED_FPS)), MIN_FRAMES_MODEL, MAX_FRAMES_MODEL)
162
  current_seed = random.randint(0, MAX_SEED) if randomize_seed else int(seed)
 
163
 
164
  output_frames_list = pipe(
 
165
  prompt=prompt,
166
  negative_prompt=negative_prompt,
167
  height=480,
 
185
  gr.Markdown("run Wan 2.2 in just 6-8 steps, with [FusionX Phantom LoRA by DeeJayT](https://huggingface.co/vrgamedevgirl84/Wan14BT2VFusioniX/tree/main/FusionX_LoRa), compatible with 🧨 diffusers")
186
  with gr.Row():
187
  with gr.Column():
 
188
  prompt_input = gr.Textbox(label="Prompt", value=default_prompt_i2v)
189
  duration_seconds_input = gr.Slider(minimum=MIN_DURATION, maximum=MAX_DURATION, step=0.1, value=MAX_DURATION, label="Duration (seconds)", info=f"Clamped to model's {MIN_FRAMES_MODEL}-{MAX_FRAMES_MODEL} frames at {FIXED_FPS}fps.")
190
 
 
207
  ]
208
  generate_button.click(fn=generate_video, inputs=ui_inputs, outputs=[video_output, seed_input])
209
 
210
+ gr.Examples(
211
+ examples=[
212
+ [
213
+ "Summer beach vacation style, a white cat wearing sunglasses sits on a surfboard. The fluffy-furred feline gazes directly at the camera with a relaxed expression. Blurred beach scenery forms the background featuring crystal-clear waters, distant green hills, and a blue sky dotted with white clouds. The cat assumes a naturally relaxed posture, as if savoring the sea breeze and warm sunlight. A close-up shot highlights the feline's intricate details and the refreshing atmosphere of the seaside.",
214
+ ],
215
+ ],
216
+ inputs=[prompt_input], outputs=[video_output, seed_input], fn=generate_video, cache_examples="lazy"
217
+ )
 
218
 
219
  if __name__ == "__main__":
220
  demo.queue().launch(mcp_server=True)