Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -124,6 +124,20 @@ print("Pipeline ready!")
|
|
| 124 |
print("=" * 80)
|
| 125 |
|
| 126 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
@spaces.GPU(duration=120, size="xlarge")
|
| 128 |
def generate_video(
|
| 129 |
input_image,
|
|
@@ -138,6 +152,9 @@ def generate_video(
|
|
| 138 |
):
|
| 139 |
"""Generate a video based on the given parameters."""
|
| 140 |
try:
|
|
|
|
|
|
|
|
|
|
| 141 |
current_seed = random.randint(0, MAX_SEED) if randomize_seed else int(seed)
|
| 142 |
|
| 143 |
frame_rate = DEFAULT_FRAME_RATE
|
|
@@ -145,6 +162,8 @@ def generate_video(
|
|
| 145 |
# 8k+1 format
|
| 146 |
num_frames = ((num_frames - 1 + 7) // 8) * 8 + 1
|
| 147 |
|
|
|
|
|
|
|
| 148 |
# Handle image input
|
| 149 |
images = []
|
| 150 |
temp_image_path = None
|
|
@@ -189,6 +208,7 @@ def generate_video(
|
|
| 189 |
if audio_context is not None:
|
| 190 |
audio_context = audio_context.to("cuda")
|
| 191 |
print("Embeddings loaded successfully")
|
|
|
|
| 192 |
except Exception as e:
|
| 193 |
raise RuntimeError(
|
| 194 |
f"Failed to get embeddings from text encoder space: {e}\n"
|
|
@@ -209,6 +229,8 @@ def generate_video(
|
|
| 209 |
tiling_config = TilingConfig.default()
|
| 210 |
video_chunks_number = get_video_chunks_number(num_frames, tiling_config)
|
| 211 |
|
|
|
|
|
|
|
| 212 |
video, audio = pipeline(
|
| 213 |
prompt=prompt,
|
| 214 |
seed=current_seed,
|
|
@@ -221,6 +243,8 @@ def generate_video(
|
|
| 221 |
enhance_prompt=False, # Already enhanced by text encoder space
|
| 222 |
)
|
| 223 |
|
|
|
|
|
|
|
| 224 |
output_path = tempfile.mktemp(suffix=".mp4")
|
| 225 |
encode_video(
|
| 226 |
video=video,
|
|
@@ -230,6 +254,8 @@ def generate_video(
|
|
| 230 |
video_chunks_number=video_chunks_number,
|
| 231 |
)
|
| 232 |
|
|
|
|
|
|
|
| 233 |
return str(output_path), current_seed
|
| 234 |
finally:
|
| 235 |
# Restore original encode_prompts
|
|
@@ -237,6 +263,7 @@ def generate_video(
|
|
| 237 |
|
| 238 |
except Exception as e:
|
| 239 |
import traceback
|
|
|
|
| 240 |
error_msg = f"Error: {str(e)}\n{traceback.format_exc()}"
|
| 241 |
print(error_msg)
|
| 242 |
return None, current_seed
|
|
|
|
| 124 |
print("=" * 80)
|
| 125 |
|
| 126 |
|
| 127 |
+
def log_memory(tag: str):
|
| 128 |
+
"""Log GPU memory usage at a given point."""
|
| 129 |
+
if torch.cuda.is_available():
|
| 130 |
+
allocated = torch.cuda.memory_allocated() / 1024**3
|
| 131 |
+
reserved = torch.cuda.memory_reserved() / 1024**3
|
| 132 |
+
peak = torch.cuda.max_memory_allocated() / 1024**3
|
| 133 |
+
free, total = torch.cuda.mem_get_info()
|
| 134 |
+
free_gb = free / 1024**3
|
| 135 |
+
total_gb = total / 1024**3
|
| 136 |
+
print(f"[VRAM {tag}] allocated={allocated:.2f}GB reserved={reserved:.2f}GB peak={peak:.2f}GB free={free_gb:.2f}GB total={total_gb:.2f}GB")
|
| 137 |
+
else:
|
| 138 |
+
print(f"[VRAM {tag}] CUDA not available")
|
| 139 |
+
|
| 140 |
+
|
| 141 |
@spaces.GPU(duration=120, size="xlarge")
|
| 142 |
def generate_video(
|
| 143 |
input_image,
|
|
|
|
| 152 |
):
|
| 153 |
"""Generate a video based on the given parameters."""
|
| 154 |
try:
|
| 155 |
+
torch.cuda.reset_peak_memory_stats()
|
| 156 |
+
log_memory("start")
|
| 157 |
+
|
| 158 |
current_seed = random.randint(0, MAX_SEED) if randomize_seed else int(seed)
|
| 159 |
|
| 160 |
frame_rate = DEFAULT_FRAME_RATE
|
|
|
|
| 162 |
# 8k+1 format
|
| 163 |
num_frames = ((num_frames - 1 + 7) // 8) * 8 + 1
|
| 164 |
|
| 165 |
+
print(f"Generating: {height}x{width}, {num_frames} frames ({duration}s), seed={current_seed}")
|
| 166 |
+
|
| 167 |
# Handle image input
|
| 168 |
images = []
|
| 169 |
temp_image_path = None
|
|
|
|
| 208 |
if audio_context is not None:
|
| 209 |
audio_context = audio_context.to("cuda")
|
| 210 |
print("Embeddings loaded successfully")
|
| 211 |
+
log_memory("after embeddings loaded")
|
| 212 |
except Exception as e:
|
| 213 |
raise RuntimeError(
|
| 214 |
f"Failed to get embeddings from text encoder space: {e}\n"
|
|
|
|
| 229 |
tiling_config = TilingConfig.default()
|
| 230 |
video_chunks_number = get_video_chunks_number(num_frames, tiling_config)
|
| 231 |
|
| 232 |
+
log_memory("before pipeline call")
|
| 233 |
+
|
| 234 |
video, audio = pipeline(
|
| 235 |
prompt=prompt,
|
| 236 |
seed=current_seed,
|
|
|
|
| 243 |
enhance_prompt=False, # Already enhanced by text encoder space
|
| 244 |
)
|
| 245 |
|
| 246 |
+
log_memory("after pipeline call")
|
| 247 |
+
|
| 248 |
output_path = tempfile.mktemp(suffix=".mp4")
|
| 249 |
encode_video(
|
| 250 |
video=video,
|
|
|
|
| 254 |
video_chunks_number=video_chunks_number,
|
| 255 |
)
|
| 256 |
|
| 257 |
+
log_memory("after encode_video")
|
| 258 |
+
|
| 259 |
return str(output_path), current_seed
|
| 260 |
finally:
|
| 261 |
# Restore original encode_prompts
|
|
|
|
| 263 |
|
| 264 |
except Exception as e:
|
| 265 |
import traceback
|
| 266 |
+
log_memory("on error")
|
| 267 |
error_msg = f"Error: {str(e)}\n{traceback.format_exc()}"
|
| 268 |
print(error_msg)
|
| 269 |
return None, current_seed
|