Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -29,7 +29,7 @@ pipe.to("cuda")
|
|
| 29 |
pipe_upsample.to("cuda")
|
| 30 |
pipe.vae.enable_tiling()
|
| 31 |
|
| 32 |
-
def prepare_image_condition(image, size=(
|
| 33 |
image = ImageOps.contain(image, size)
|
| 34 |
canvas = Image.new("RGB", size, background)
|
| 35 |
offset = ((size[0] - image.width) // 2, (size[1] - image.height) // 2)
|
|
@@ -46,22 +46,22 @@ def generate_video(prompt, image_url):
|
|
| 46 |
raw_image = Image.open(BytesIO(requests.get(image_url).content)).convert("RGB")
|
| 47 |
image = prepare_image_condition(raw_image)
|
| 48 |
|
| 49 |
-
# Set target resolutions
|
| 50 |
-
base_width, base_height =
|
| 51 |
-
down_width, down_height =
|
| 52 |
|
| 53 |
-
# Step 1: Generate latents at lower resolution
|
| 54 |
latents = pipe(
|
| 55 |
prompt=prompt,
|
| 56 |
image=image,
|
| 57 |
width=down_width,
|
| 58 |
height=down_height,
|
| 59 |
num_frames=60,
|
| 60 |
-
num_inference_steps=7
|
| 61 |
output_type="latent",
|
| 62 |
-
guidance_scale=1.0
|
| 63 |
-
decode_timestep=0.
|
| 64 |
-
decode_noise_scale=0.
|
| 65 |
generator=generator
|
| 66 |
).frames
|
| 67 |
|
|
@@ -74,19 +74,19 @@ def generate_video(prompt, image_url):
|
|
| 74 |
torch.cuda.empty_cache()
|
| 75 |
gc.collect()
|
| 76 |
|
| 77 |
-
# Step 3: Decode upscaled latents to frames using the pipeline
|
| 78 |
-
# Create a new pipeline call specifically for decoding
|
| 79 |
frames = pipe(
|
| 80 |
-
prompt=
|
| 81 |
latents=upscaled_latents,
|
| 82 |
width=base_width,
|
| 83 |
height=base_height,
|
| 84 |
num_frames=60,
|
| 85 |
-
num_inference_steps=
|
| 86 |
output_type="pil",
|
| 87 |
-
guidance_scale=
|
| 88 |
-
decode_timestep=0.
|
| 89 |
-
decode_noise_scale=0.
|
|
|
|
| 90 |
generator=generator
|
| 91 |
).frames[0]
|
| 92 |
|
|
@@ -131,41 +131,61 @@ def generate_video(prompt, image_url):
|
|
| 131 |
with open("subtitles.srt", "w", encoding="utf-8") as f:
|
| 132 |
f.write(srt_content)
|
| 133 |
|
| 134 |
-
# Step 7: Merge video + audio + subtitles
|
| 135 |
final_output = "final_with_audio.mp4"
|
| 136 |
try:
|
|
|
|
|
|
|
| 137 |
(
|
| 138 |
ffmpeg
|
| 139 |
.input(video_path)
|
| 140 |
-
.
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
loglevel="error"
|
| 145 |
-
)
|
| 146 |
-
.run(overwrite_output=True)
|
| 147 |
)
|
| 148 |
|
| 149 |
-
#
|
| 150 |
(
|
| 151 |
ffmpeg
|
| 152 |
-
.input(
|
| 153 |
-
.input(
|
| 154 |
.output(
|
| 155 |
-
|
| 156 |
-
|
|
|
|
| 157 |
shortest=None,
|
| 158 |
-
loglevel=
|
| 159 |
)
|
| 160 |
-
.
|
|
|
|
| 161 |
)
|
| 162 |
|
| 163 |
-
return
|
| 164 |
|
| 165 |
except Exception as e:
|
| 166 |
print(f"FFmpeg error: {e}")
|
| 167 |
-
# Fallback:
|
| 168 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
|
| 170 |
def format_time(seconds):
|
| 171 |
"""Convert seconds to SRT time format"""
|
|
@@ -184,7 +204,7 @@ demo = gr.Interface(
|
|
| 184 |
],
|
| 185 |
outputs=gr.Video(label="Generated Video"),
|
| 186 |
title="🎬 LTX AI Video Generator",
|
| 187 |
-
description="AI-powered video with voiceover and subtitles. Now outputs at
|
| 188 |
)
|
| 189 |
|
| 190 |
demo.launch()
|
|
|
|
| 29 |
pipe_upsample.to("cuda")
|
| 30 |
pipe.vae.enable_tiling()
|
| 31 |
|
| 32 |
+
def prepare_image_condition(image, size=(512, 512), background=(0, 0, 0)):
|
| 33 |
image = ImageOps.contain(image, size)
|
| 34 |
canvas = Image.new("RGB", size, background)
|
| 35 |
offset = ((size[0] - image.width) // 2, (size[1] - image.height) // 2)
|
|
|
|
| 46 |
raw_image = Image.open(BytesIO(requests.get(image_url).content)).convert("RGB")
|
| 47 |
image = prepare_image_condition(raw_image)
|
| 48 |
|
| 49 |
+
# Set target resolutions - using higher quality settings
|
| 50 |
+
base_width, base_height = 512, 512 # Increased from 480x480
|
| 51 |
+
down_width, down_height = 384, 384 # Increased from 320x320 for better quality
|
| 52 |
|
| 53 |
+
# Step 1: Generate latents at lower resolution with better quality settings
|
| 54 |
latents = pipe(
|
| 55 |
prompt=prompt,
|
| 56 |
image=image,
|
| 57 |
width=down_width,
|
| 58 |
height=down_height,
|
| 59 |
num_frames=60,
|
| 60 |
+
num_inference_steps=12, # Increased from 7 for better quality
|
| 61 |
output_type="latent",
|
| 62 |
+
guidance_scale=2.0, # Increased from 1.0 for better prompt adherence
|
| 63 |
+
decode_timestep=0.1, # Adjusted for better quality
|
| 64 |
+
decode_noise_scale=0.1, # Adjusted for better quality
|
| 65 |
generator=generator
|
| 66 |
).frames
|
| 67 |
|
|
|
|
| 74 |
torch.cuda.empty_cache()
|
| 75 |
gc.collect()
|
| 76 |
|
| 77 |
+
# Step 3: Decode upscaled latents to frames using the pipeline with better settings
|
|
|
|
| 78 |
frames = pipe(
|
| 79 |
+
prompt=prompt, # Use original prompt for better consistency
|
| 80 |
latents=upscaled_latents,
|
| 81 |
width=base_width,
|
| 82 |
height=base_height,
|
| 83 |
num_frames=60,
|
| 84 |
+
num_inference_steps=15, # Increased for better decoding quality
|
| 85 |
output_type="pil",
|
| 86 |
+
guidance_scale=2.0, # Consistent with generation
|
| 87 |
+
decode_timestep=0.1,
|
| 88 |
+
decode_noise_scale=0.1,
|
| 89 |
+
denoise_strength=0.2, # Reduced for less noise
|
| 90 |
generator=generator
|
| 91 |
).frames[0]
|
| 92 |
|
|
|
|
| 131 |
with open("subtitles.srt", "w", encoding="utf-8") as f:
|
| 132 |
f.write(srt_content)
|
| 133 |
|
| 134 |
+
# Step 7: Merge video + audio + subtitles with proper FFmpeg handling
|
| 135 |
final_output = "final_with_audio.mp4"
|
| 136 |
try:
|
| 137 |
+
# First, create video with subtitles
|
| 138 |
+
video_with_subs = "video_with_subs.mp4"
|
| 139 |
(
|
| 140 |
ffmpeg
|
| 141 |
.input(video_path)
|
| 142 |
+
.filter('subtitles', 'subtitles.srt')
|
| 143 |
+
.output(video_with_subs, vcodec='libx264', acodec='aac', loglevel='error')
|
| 144 |
+
.overwrite_output()
|
| 145 |
+
.run()
|
|
|
|
|
|
|
|
|
|
| 146 |
)
|
| 147 |
|
| 148 |
+
# Then add audio track
|
| 149 |
(
|
| 150 |
ffmpeg
|
| 151 |
+
.input(video_with_subs)
|
| 152 |
+
.input('voice.wav')
|
| 153 |
.output(
|
| 154 |
+
final_output,
|
| 155 |
+
vcodec='copy',
|
| 156 |
+
acodec='aac',
|
| 157 |
shortest=None,
|
| 158 |
+
loglevel='error'
|
| 159 |
)
|
| 160 |
+
.overwrite_output()
|
| 161 |
+
.run()
|
| 162 |
)
|
| 163 |
|
| 164 |
+
return final_output
|
| 165 |
|
| 166 |
except Exception as e:
|
| 167 |
print(f"FFmpeg error: {e}")
|
| 168 |
+
# Fallback: try simpler approach without subtitles
|
| 169 |
+
try:
|
| 170 |
+
(
|
| 171 |
+
ffmpeg
|
| 172 |
+
.input(video_path)
|
| 173 |
+
.input('voice.wav')
|
| 174 |
+
.output(
|
| 175 |
+
final_output,
|
| 176 |
+
vcodec='libx264',
|
| 177 |
+
acodec='aac',
|
| 178 |
+
shortest=None,
|
| 179 |
+
loglevel='error'
|
| 180 |
+
)
|
| 181 |
+
.overwrite_output()
|
| 182 |
+
.run()
|
| 183 |
+
)
|
| 184 |
+
return final_output
|
| 185 |
+
except Exception as e2:
|
| 186 |
+
print(f"FFmpeg fallback error: {e2}")
|
| 187 |
+
# Final fallback: return original video
|
| 188 |
+
return video_path
|
| 189 |
|
| 190 |
def format_time(seconds):
|
| 191 |
"""Convert seconds to SRT time format"""
|
|
|
|
| 204 |
],
|
| 205 |
outputs=gr.Video(label="Generated Video"),
|
| 206 |
title="🎬 LTX AI Video Generator",
|
| 207 |
+
description="AI-powered video with voiceover and subtitles. Now outputs at 512x512 resolution with improved quality."
|
| 208 |
)
|
| 209 |
|
| 210 |
demo.launch()
|