Sushantkas commited on
Commit
eeaecb8
·
verified ·
1 Parent(s): 340c7ea

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -13
app.py CHANGED
@@ -22,6 +22,9 @@ image_encoder = CLIPVisionModel.from_pretrained(
22
 
23
  print("###### Loading VAE encoder ######")
24
  vae = AutoencoderKLWan.from_pretrained(model_id, subfolder="vae", torch_dtype=torch.float32)
 
 
 
25
  pipe = WanImageToVideoPipeline.from_pretrained(
26
  model_id, vae=vae, image_encoder=image_encoder, torch_dtype=torch.bfloat16
27
  )
@@ -44,12 +47,21 @@ except:
44
  print("Model CPU Offload failed")
45
 
46
 
47
-
 
 
 
 
 
48
 
49
 
50
  # Loading function for Image
51
  from diffusers.utils import load_image
52
 
 
 
 
 
53
  def prepare_vertical_image(pipe, image_path, base_width=384, base_height=672):
54
  """
55
  Loads and resizes an image for Wan I2V vertical video generation.
@@ -84,20 +96,70 @@ def prepare_vertical_image(pipe, image_path, base_width=384, base_height=672):
84
 
85
 
86
 
87
- # how to use the Image loading
88
- image, width, height = prepare_vertical_image(
89
- pipe,
90
- "input.jpg",
91
- base_width=384,
92
- base_height=672
93
- )
94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95
 
 
96
 
97
 
98
- @spaces.GPU(duration=60)
99
- def greet(name):
100
- return "Hello " + name + "!!"
101
 
102
- demo = gr.Interface(fn=greet, inputs="text", outputs="text")
103
- demo.launch()
 
22
 
23
  print("###### Loading VAE encoder ######")
24
  vae = AutoencoderKLWan.from_pretrained(model_id, subfolder="vae", torch_dtype=torch.float32)
25
+
26
+ print("Loading Pipeline...")
27
+
28
  pipe = WanImageToVideoPipeline.from_pretrained(
29
  model_id, vae=vae, image_encoder=image_encoder, torch_dtype=torch.bfloat16
30
  )
 
47
  print("Model CPU Offload failed")
48
 
49
 
50
+ try:
51
+ print("Enabling Attention Slicing ")
52
+ pipe.enable_attention_slicing()
53
+ print("Attention Slicing Enabled")
54
+ except Exception as e:
55
+ print("Attention Slicing Failed")
56
 
57
 
58
  # Loading function for Image
59
  from diffusers.utils import load_image
60
 
61
+ # ================================
62
+ # Image Preparation Function
63
+ # ================================
64
+
65
  def prepare_vertical_image(pipe, image_path, base_width=384, base_height=672):
66
  """
67
  Loads and resizes an image for Wan I2V vertical video generation.
 
96
 
97
 
98
 
 
 
 
 
 
 
 
99
 
100
+ @spaces.GPU(size="xlarge", duration=get_duration)
101
+ def generate_video(input_image, prompt, negative_prompt):
102
+
103
+ if input_image is None:
104
+ return None
105
+
106
+ image = input_image
107
+
108
+ # Prepare 9:16 vertical reduced resolution
109
+ image, width, height = prepare_vertical_image(pipe, image)
110
+
111
+ print(f"Generating 10 sec vertical video at {width}x{height}")
112
+
113
+ # 10 seconds at 16 FPS = 160 frames
114
+ video_frames = pipe(
115
+ image=image,
116
+ prompt=prompt,
117
+ negative_prompt=negative_prompt,
118
+ height=height,
119
+ width=width,
120
+ num_frames=160,
121
+ guidance_scale=4.5,
122
+ num_inference_steps=25
123
+ ).frames[0]
124
+
125
+ output_path = "vertical_output.mp4"
126
+ export_to_video(video_frames, output_path, fps=16)
127
+
128
+ return output_path
129
+
130
+
131
+ # Gradio UI
132
+ # ================================
133
+
134
+ with gr.Blocks(title="Wan 14B Vertical I2V") as demo:
135
+
136
+ gr.Markdown("## 🎬 Wan 14B Image-to-Video Generator")
137
+ gr.Markdown("Generate 10-second Vertical (9:16) AI Videos")
138
+
139
+ with gr.Row():
140
+ input_image = gr.Image(type="pil", label="Upload Image")
141
+
142
+ prompt = gr.Textbox(
143
+ label="Prompt",
144
+ placeholder="Describe motion, camera movement, cinematic effect..."
145
+ )
146
+
147
+ negative_prompt = gr.Textbox(
148
+ label="Negative Prompt",
149
+ value="blurry, low quality, distorted, static",
150
+ )
151
+
152
+ generate_btn = gr.Button("Generate 10 Second Video")
153
+
154
+ output_video = gr.Video(label="Generated Video")
155
+
156
+ generate_btn.click(
157
+ generate_video,
158
+ inputs=[input_image, prompt, negative_prompt],
159
+ outputs=output_video
160
+ )
161
 
162
+ demo.launch(server_name="0.0.0.0", server_port=7860)
163
 
164
 
 
 
 
165