dream2589632147 commited on
Commit
7abbdaa
ยท
verified ยท
1 Parent(s): b15dd60

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -4
app.py CHANGED
@@ -106,7 +106,7 @@ def prepare_image(image, preset_key):
106
  return image
107
  # ๐ŸŒŸ ูˆุธูŠูุฉ ู„ุชูˆู„ูŠุฏ ุงู„ููŠุฏูŠูˆ ู…ู† ุงู„ุตูˆุฑุฉ ูˆุงู„ู€ prompt
108
  @torch.no_grad()
109
- def generate_video(image, prompt, negative_prompt, num_frames, preset_key, guidance_scale=7.5, num_inference_steps=50):
110
  if image is None:
111
  raise ValueError("No image provided!")
112
 
@@ -116,6 +116,11 @@ def generate_video(image, prompt, negative_prompt, num_frames, preset_key, guida
116
  # Clamp num_frames
117
  num_frames = max(MIN_FRAMES_MODEL, min(num_frames, MAX_FRAMES_MODEL))
118
 
 
 
 
 
 
119
  video_frames = pipe(
120
  prompt=prompt,
121
  image=prepared_image,
@@ -140,20 +145,27 @@ def generate_video_only(image, prompt, negative_prompt, num_frames, preset_key):
140
  print("Generating video...")
141
  final_video = generate_video(image, prompt, negative_prompt, num_frames, preset_key)
142
 
 
 
 
 
 
143
  return final_video, "Success!"
 
 
144
  except Exception as e:
145
  return None, f"Error: {str(e)}"
146
  # ๐ŸŒŸ ุฅุนุฏุงุฏ ุงู„ูˆุงุฌู‡ุฉ ุจู€ Gradio
147
  with gr.Blocks(title="Wan2.2 Image-to-Video Generator") as demo:
148
  gr.Markdown("# ๐ŸŒŸ Wan2.2 I2V Generator")
149
- gr.Markdown("Upload an image, add a prompt, and generate a video!")
150
 
151
  with gr.Row():
152
  with gr.Column(scale=1):
153
  image_input = gr.Image(type="pil", label="Input Image")
154
  prompt_input = gr.Textbox(label="Prompt", placeholder="A dynamic scene from the image...", lines=2)
155
  negative_prompt_input = gr.Textbox(label="Negative Prompt", placeholder="blurry, low quality", lines=1)
156
- num_frames_slider = gr.Slider(MIN_FRAMES_MODEL, MAX_FRAMES_MODEL, value=64, step=8, label="Number of Frames (Max 45s at 16 FPS)")
157
  preset_dropdown = gr.Dropdown(choices=list(DIMENSION_PRESETS.keys()), value="Custom (Default)", label="Output Preset")
158
  generate_btn = gr.Button("Generate Video", variant="primary")
159
 
@@ -180,7 +192,7 @@ with gr.Blocks(title="Wan2.2 Image-to-Video Generator") as demo:
180
  None, # No example image; user to upload
181
  "The person in the image starts walking towards the camera with a smile.",
182
  "static, blurry",
183
- 32,
184
  "YouTube Full HD (16:9)"
185
  ]
186
  ],
 
106
  return image
107
  # ๐ŸŒŸ ูˆุธูŠูุฉ ู„ุชูˆู„ูŠุฏ ุงู„ููŠุฏูŠูˆ ู…ู† ุงู„ุตูˆุฑุฉ ูˆุงู„ู€ prompt
108
  @torch.no_grad()
109
+ def generate_video(image, prompt, negative_prompt, num_frames, preset_key, guidance_scale=7.5, num_inference_steps=20): # Reduced default steps to 20 for faster/less memory
110
  if image is None:
111
  raise ValueError("No image provided!")
112
 
 
116
  # Clamp num_frames
117
  num_frames = max(MIN_FRAMES_MODEL, min(num_frames, MAX_FRAMES_MODEL))
118
 
119
+ # Memory check and cleanup before generation
120
+ if torch.cuda.is_available():
121
+ print(f"GPU Memory before generation: {torch.cuda.memory_allocated() / 1024**3:.2f} GB")
122
+ torch.cuda.empty_cache()
123
+
124
  video_frames = pipe(
125
  prompt=prompt,
126
  image=prepared_image,
 
145
  print("Generating video...")
146
  final_video = generate_video(image, prompt, negative_prompt, num_frames, preset_key)
147
 
148
+ # Cleanup after generation
149
+ gc.collect()
150
+ if torch.cuda.is_available():
151
+ torch.cuda.empty_cache()
152
+
153
  return final_video, "Success!"
154
+ except torch.cuda.OutOfMemoryError:
155
+ return None, "Error: Out of GPU memory. Try reducing frames or resolution."
156
  except Exception as e:
157
  return None, f"Error: {str(e)}"
158
  # ๐ŸŒŸ ุฅุนุฏุงุฏ ุงู„ูˆุงุฌู‡ุฉ ุจู€ Gradio
159
  with gr.Blocks(title="Wan2.2 Image-to-Video Generator") as demo:
160
  gr.Markdown("# ๐ŸŒŸ Wan2.2 I2V Generator")
161
+ gr.Markdown("Upload an image, add a prompt, and generate a video! Note: For T4 GPU, use <32 frames for best results.")
162
 
163
  with gr.Row():
164
  with gr.Column(scale=1):
165
  image_input = gr.Image(type="pil", label="Input Image")
166
  prompt_input = gr.Textbox(label="Prompt", placeholder="A dynamic scene from the image...", lines=2)
167
  negative_prompt_input = gr.Textbox(label="Negative Prompt", placeholder="blurry, low quality", lines=1)
168
+ num_frames_slider = gr.Slider(MIN_FRAMES_MODEL, MAX_FRAMES_MODEL, value=16, step=8, label="Number of Frames (Max 45s at 16 FPS)") # Default to 16 to avoid OOM
169
  preset_dropdown = gr.Dropdown(choices=list(DIMENSION_PRESETS.keys()), value="Custom (Default)", label="Output Preset")
170
  generate_btn = gr.Button("Generate Video", variant="primary")
171
 
 
192
  None, # No example image; user to upload
193
  "The person in the image starts walking towards the camera with a smile.",
194
  "static, blurry",
195
+ 16, # Reduced for example
196
  "YouTube Full HD (16:9)"
197
  ]
198
  ],