Spaces:
Running on Zero
Running on Zero
Upload 4 files
Browse files
app.py
CHANGED
|
@@ -11,9 +11,14 @@
|
|
| 11 |
# // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
# // See the License for the specific language governing permissions and
|
| 13 |
# // limitations under the License.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
import spaces
|
| 15 |
|
| 16 |
-
import os
|
| 17 |
import torch
|
| 18 |
import mediapy
|
| 19 |
from einops import rearrange
|
|
@@ -222,7 +227,20 @@ def generation_step(runner, text_embeds_dict, cond_latents):
|
|
| 222 |
return samples
|
| 223 |
|
| 224 |
@spaces.GPU(duration=100)
|
| 225 |
-
def generation_loop(video_path='./test_videos', seed=666, fps_out=12,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 226 |
runner = configure_runner(1)
|
| 227 |
|
| 228 |
def _extract_text_embeds():
|
|
@@ -240,8 +258,8 @@ def generation_loop(video_path='./test_videos', seed=666, fps_out=12, batch_size
|
|
| 240 |
return positive_prompts_embeds
|
| 241 |
|
| 242 |
def cut_videos(videos, sp_size):
|
| 243 |
-
if videos.size(1) >
|
| 244 |
-
videos = videos[:, :
|
| 245 |
t = videos.size(1)
|
| 246 |
if t <= 4 * sp_size:
|
| 247 |
print(f"Cut input video size: {videos.size()}")
|
|
@@ -317,8 +335,8 @@ def generation_loop(video_path='./test_videos', seed=666, fps_out=12, batch_size
|
|
| 317 |
)[0]
|
| 318 |
/ 255.0
|
| 319 |
)
|
| 320 |
-
if video.size(0) >
|
| 321 |
-
video = video[:
|
| 322 |
print(f"Read video size: {video.size()}")
|
| 323 |
output_dir = 'output/' + str(uuid.uuid4()) + '.mp4'
|
| 324 |
else:
|
|
@@ -356,6 +374,11 @@ def generation_loop(video_path='./test_videos', seed=666, fps_out=12, batch_size
|
|
| 356 |
print(f"Encoding videos: {list(map(lambda x: x.size(), cond_latents))}")
|
| 357 |
# runner.vae.to(torch.device("cuda"))
|
| 358 |
cond_latents = runner.vae_encode(cond_latents)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 359 |
# runner.vae.to("cpu")
|
| 360 |
# runner.dit.to(torch.device("cuda"))
|
| 361 |
|
|
@@ -429,13 +452,28 @@ with gr.Blocks(title="SeedVR2: One-Step Video Restoration via Diffusion Adversar
|
|
| 429 |
seed = gr.Number(label="Seeds", value=666)
|
| 430 |
fps = gr.Number(label="fps", value=24)
|
| 431 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 432 |
with gr.Row():
|
| 433 |
output_video = gr.Video(label="Output_Video")
|
| 434 |
output_image = gr.Image(label="Output_Image")
|
| 435 |
download_link = gr.File(label="Download the output")
|
| 436 |
|
| 437 |
run_button = gr.Button("Run")
|
| 438 |
-
run_button.click(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 439 |
|
| 440 |
# Examples
|
| 441 |
gr.Examples(
|
|
|
|
| 11 |
# // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
# // See the License for the specific language governing permissions and
|
| 13 |
# // limitations under the License.
|
| 14 |
+
import os
|
| 15 |
+
|
| 16 |
+
# ZeroGPU: evitar fragmentacion del asignador CUDA durante los picos de memoria
|
| 17 |
+
# de SeedVR2. Debe establecerse antes de importar spaces o torch.
|
| 18 |
+
os.environ.setdefault("PYTORCH_CUDA_ALLOC_CONF", "expandable_segments:True")
|
| 19 |
+
|
| 20 |
import spaces
|
| 21 |
|
|
|
|
| 22 |
import torch
|
| 23 |
import mediapy
|
| 24 |
from einops import rearrange
|
|
|
|
| 227 |
return samples
|
| 228 |
|
| 229 |
@spaces.GPU(duration=100)
|
| 230 |
+
def generation_loop(video_path='./test_videos', seed=666, fps_out=12,
|
| 231 |
+
max_frames=17, work_height=540, batch_size=1,
|
| 232 |
+
cfg_scale=1.0, cfg_rescale=0.0, sample_steps=1,
|
| 233 |
+
sp_size=1):
|
| 234 |
+
# Diagnostico controlado: primero validar 17 fotogramas a 540p; luego
|
| 235 |
+
# ampliar gradualmente. La salida se recorta a max_frames deliberadamente.
|
| 236 |
+
max_frames = int(max_frames)
|
| 237 |
+
work_height = int(work_height)
|
| 238 |
+
if max_frames not in (17, 33, 65, 121):
|
| 239 |
+
raise ValueError("Elige 17, 33, 65 o 121 fotogramas.")
|
| 240 |
+
if work_height not in (360, 540, 720):
|
| 241 |
+
raise ValueError("Elige una altura de 360, 540 o 720 pixeles.")
|
| 242 |
+
res_h, res_w = int(work_height * 16 / 9), work_height
|
| 243 |
+
print(f"DIAGNOSTICO: hasta {max_frames} fotogramas, objetivo {work_height}p.")
|
| 244 |
runner = configure_runner(1)
|
| 245 |
|
| 246 |
def _extract_text_embeds():
|
|
|
|
| 258 |
return positive_prompts_embeds
|
| 259 |
|
| 260 |
def cut_videos(videos, sp_size):
|
| 261 |
+
if videos.size(1) > max_frames:
|
| 262 |
+
videos = videos[:, :max_frames]
|
| 263 |
t = videos.size(1)
|
| 264 |
if t <= 4 * sp_size:
|
| 265 |
print(f"Cut input video size: {videos.size()}")
|
|
|
|
| 335 |
)[0]
|
| 336 |
/ 255.0
|
| 337 |
)
|
| 338 |
+
if video.size(0) > max_frames:
|
| 339 |
+
video = video[:max_frames]
|
| 340 |
print(f"Read video size: {video.size()}")
|
| 341 |
output_dir = 'output/' + str(uuid.uuid4()) + '.mp4'
|
| 342 |
else:
|
|
|
|
| 374 |
print(f"Encoding videos: {list(map(lambda x: x.size(), cond_latents))}")
|
| 375 |
# runner.vae.to(torch.device("cuda"))
|
| 376 |
cond_latents = runner.vae_encode(cond_latents)
|
| 377 |
+
gc.collect()
|
| 378 |
+
torch.cuda.empty_cache()
|
| 379 |
+
print("Memoria CUDA antes del DiT: "
|
| 380 |
+
f"asignada={torch.cuda.memory_allocated() / 2**30:.2f} GiB, "
|
| 381 |
+
f"reservada={torch.cuda.memory_reserved() / 2**30:.2f} GiB")
|
| 382 |
# runner.vae.to("cpu")
|
| 383 |
# runner.dit.to(torch.device("cuda"))
|
| 384 |
|
|
|
|
| 452 |
seed = gr.Number(label="Seeds", value=666)
|
| 453 |
fps = gr.Number(label="fps", value=24)
|
| 454 |
|
| 455 |
+
gr.Markdown("**Prueba de memoria:** se exportan solo los primeros fotogramas "
|
| 456 |
+
"seleccionados. Empieza por 17 fotogramas / 540p; si funciona, "
|
| 457 |
+
"sube a 33 y después a 720p.")
|
| 458 |
+
with gr.Row():
|
| 459 |
+
max_frames_control = gr.Dropdown(
|
| 460 |
+
label="Fotogramas maximos (recorta la salida)",
|
| 461 |
+
choices=[17, 33, 65, 121], value=17)
|
| 462 |
+
work_height_control = gr.Dropdown(
|
| 463 |
+
label="Altura de procesamiento (aproximada)",
|
| 464 |
+
choices=[360, 540, 720], value=540)
|
| 465 |
+
|
| 466 |
with gr.Row():
|
| 467 |
output_video = gr.Video(label="Output_Video")
|
| 468 |
output_image = gr.Image(label="Output_Image")
|
| 469 |
download_link = gr.File(label="Download the output")
|
| 470 |
|
| 471 |
run_button = gr.Button("Run")
|
| 472 |
+
run_button.click(
|
| 473 |
+
fn=generation_loop,
|
| 474 |
+
inputs=[input_video, seed, fps, max_frames_control, work_height_control],
|
| 475 |
+
outputs=[output_image, output_video, download_link],
|
| 476 |
+
)
|
| 477 |
|
| 478 |
# Examples
|
| 479 |
gr.Examples(
|