# // Copyright (c) 2025 Bytedance Ltd. and/or its affiliates # // # // Licensed under the Apache License, Version 2.0 (the "License"); # // you may not use this file except in compliance with the License. # // You may not obtain a copy of the License at # // # // http://www.apache.org/licenses/LICENSE-2.0 # // # // Unless required by applicable law or agreed to in writing, software # // distributed under the License is distributed on an "AS IS" BASIS, # // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # // See the License for the specific language governing permissions and # // limitations under the License. import torch.distributed as dist import os import gc import logging import sys import subprocess from pathlib import Path from urllib.parse import urlparse from torch.hub import download_url_to_file import gradio as gr import mediapy from einops import rearrange import shutil from omegaconf import OmegaConf # --- ETAPA 1: Clonar o Repositório Oficial do GitHub --- repo_name = "SeedVR" if not os.path.exists(repo_name): print(f"Clonando o repositório {repo_name} do GitHub...") subprocess.run(f"git clone https://github.com/ByteDance-Seed/{repo_name}.git", shell=True, check=True) # --- ETAPA 2: Mudar para o Diretório e Configurar o Ambiente --- os.chdir(repo_name) print(f"Diretório de trabalho alterado para: {os.getcwd()}") # Adicionar o diretório ao path do Python para que as importações funcionem sys.path.insert(0, os.path.abspath('.')) print(f"Diretório atual adicionado ao sys.path.") # --- ETAPA 3: Instalar Dependências Conforme as Instruções --- python_executable = sys.executable print("Instalando dependências do requirements.txt...") subprocess.run([python_executable, "-m", "pip", "install", "-r", "requirements.txt"], check=True) print("Instalando flash-attn...") subprocess.run([python_executable, "-m", "pip", "install", "flash-attn==2.5.9.post1", "--no-build-isolation"], check=True) from pathlib import Path from urllib.parse import urlparse from torch.hub import download_url_to_file, get_dir # Função auxiliar para downloads def load_file_from_url(url, model_dir='.', progress=True, file_name=None): os.makedirs(model_dir, exist_ok=True) if not file_name: parts = urlparse(url) file_name = os.path.basename(parts.path) cached_file = os.path.join(model_dir, file_name) if not os.path.exists(cached_file): print(f'Baixando: "{url}" para {cached_file}\n') download_url_to_file(url, cached_file, hash_prefix=None, progress=progress) return cached_file # Baixar e instalar Apex pré-compilado (crucial para o ambiente do Spaces) apex_url = 'https://huggingface.co/ByteDance-Seed/SeedVR2-3B/resolve/main/apex-0.1-cp310-cp310-linux_x86_64.whl' apex_wheel_path = load_file_from_url(url=apex_url) print("Instalando Apex a partir do wheel baixado...") subprocess.run([python_executable, "-m", "pip", "install", "--force-reinstall", "--no-cache-dir", apex_wheel_path], check=True) print("✅ Configuração do Apex concluída.") # --- ETAPA 4: Baixar os Modelos Pré-treinados --- print("Baixando modelos pré-treinados...") pretrain_model_url = { 'vae': 'https://huggingface.co/ByteDance-Seed/SeedVR2-3B/resolve/main/ema_vae.pth', 'dit': 'https://huggingface.co/ByteDance-Seed/SeedVR2-3B/resolve/main/seedvr2_ema_3b.pth', 'pos_emb': 'https://huggingface.co/ByteDance-Seed/SeedVR2-3B/resolve/main/pos_emb.pt', 'neg_emb': 'https://huggingface.co/ByteDance-Seed/SeedVR2-3B/resolve/main/neg_emb.pt', } Path('./ckpts').mkdir(exist_ok=True) for key, url in pretrain_model_url.items(): model_dir = './ckpts' if key in ['vae', 'dit'] else '.' load_file_from_url(url=url, model_dir=model_dir) # Baixar vídeos de exemplo #torch.hub.download_url_to_file('https://huggingface.co/datasets/Iceclear/SeedVR_VideoDemos/resolve/main/seedvr_videos_crf23/aigc1k/23_1_lq.mp4', '01.mp4') #torch.hub.download_url_to_file('https://huggingface.co/datasets/Iceclear/SeedVR_VideoDemos/resolve/main/seedvr_videos_crf23/aigc1k/28_1_lq.mp4', '02.mp4') #torch.hub.download_url_to_file('https://huggingface.co/datasets/Iceclear/SeedVR_VideoDemos/resolve/main/seedvr_videos_crf23/aigc1k/2_1_lq.mp4', '03.mp4') print("✅ Setup completo. Iniciando a aplicação...") # --- ETAPA 5: Executar a Aplicação Principal --- import torch import mediapy from einops import rearrange from omegaconf import OmegaConf import datetime from tqdm import tqdm import gc from PIL import Image import gradio as gr import uuid import mimetypes import torchvision.transforms as T from torchvision.transforms import Compose, Lambda, Normalize from torchvision.io.video import read_video from data.image.transforms.divisible_crop import DivisibleCrop from data.image.transforms.na_resize import NaResize from data.video.transforms.rearrange import Rearrange from common.config import load_config from common.distributed import init_torch from common.distributed.advanced import init_sequence_parallel from common.seed import set_seed from common.partition import partition_by_size from projects.video_diffusion_sr.infer import VideoDiffusionInfer from common.distributed.ops import sync_data os.environ["MASTER_ADDR"] = "127.0.0.1" os.environ["MASTER_PORT"] = "12355" os.environ["RANK"] = str(0) os.environ["WORLD_SIZE"] = str(1) if os.path.exists("projects/video_diffusion_sr/color_fix.py"): from projects.video_diffusion_sr.color_fix import wavelet_reconstruction use_colorfix = True else: use_colorfix = False def configure_runner(): config = load_config('configs_3b/main.yaml') runner = VideoDiffusionInfer(config) OmegaConf.set_readonly(runner.config, False) init_torch(cudnn_benchmark=False, timeout=datetime.timedelta(seconds=3600)) runner.configure_dit_model(device="cuda", checkpoint='ckpts/seedvr2_ema_3b.pth') runner.configure_vae_model() if hasattr(runner.vae, "set_memory_limit"): runner.vae.set_memory_limit(**runner.config.vae.memory_limit) return runner def generation_step(runner, text_embeds_dict, cond_latents): def _move_to_cuda(x): return [i.to("cuda") for i in x] noises = [torch.randn_like(latent) for latent in cond_latents] aug_noises = [torch.randn_like(latent) for latent in cond_latents] noises, aug_noises, cond_latents = sync_data((noises, aug_noises, cond_latents), 0) noises, aug_noises, cond_latents = list(map(_move_to_cuda, (noises, aug_noises, cond_latents))) def _add_noise(x, aug_noise): t = torch.tensor([100.0], device="cuda") shape = torch.tensor(x.shape[1:], device="cuda")[None] t = runner.timestep_transform(t, shape) return runner.schedule.forward(x, aug_noise, t) conditions = [runner.get_condition(n, task="sr", latent_blur=_add_noise(l, an)) for n, an, l in zip(noises, aug_noises, cond_latents)] with torch.no_grad(), torch.autocast("cuda", torch.bfloat16, enabled=True): video_tensors = runner.inference(noises=noises, conditions=conditions, **text_embeds_dict) return [rearrange(v, "c t h w -> t c h w") for v in video_tensors] @spaces.GPU def generation_loop(video_path, seed=666, fps_out=24): if video_path is None: return None, None, None runner = configure_runner() text_embeds = {"texts_pos": [torch.load('pos_emb.pt').to("cuda")], "texts_neg": [torch.load('neg_emb.pt').to("cuda")]} runner.configure_diffusion() set_seed(int(seed)) os.makedirs("output", exist_ok=True) video_transform = Compose([NaResize(1024), DivisibleCrop(16), Normalize(0.5, 0.5), Rearrange("t c h w -> c t h w")]) media_type, _ = mimetypes.guess_type(video_path) is_video = media_type and media_type.startswith("video") if is_video: video, _, _ = read_video(video_path, output_format="TCHW") video = video[:121] / 255.0 output_path = os.path.join("output", f"{uuid.uuid4()}.mp4") else: video = T.ToTensor()(Image.open(video_path).convert("RGB")).unsqueeze(0) output_path = os.path.join("output", f"{uuid.uuid4()}.png") cond_latents = [video_transform(video.to("cuda"))] ori_length = cond_latents[0].size(2) cond_latents = runner.vae_encode(cond_latents) samples = generation_step(runner, text_embeds, cond_latents) sample = samples[0][:ori_length].cpu() sample = rearrange(sample, "t c h w -> t h w c").clip(-1, 1).add(1).mul(127.5).byte().numpy() if is_video: mediapy.write_video(output_path, sample, fps=fps_out) return None, output_path, output_path else: mediapy.write_image(output_path, sample[0]) return output_path, None, output_path with gr.Blocks(title="SeedVR") as demo: gr.HTML(f"""
...""") with gr.Row(): input_file = gr.File(label="Carregar Imagem ou Vídeo") with gr.Column(): seed = gr.Number(label="Seed", value=42) fps = gr.Number(label="FPS de Saída", value=24) run_button = gr.Button("Executar") output_image = gr.Image(label="Imagem de Saída") output_video = gr.Video(label="Vídeo de Saída") download_link = gr.File(label="Baixar Resultado") run_button.click(fn=generation_loop, inputs=[input_file, seed, fps], outputs=[output_image, output_video, download_link]) gr.Examples(examples=[["01.mp4", 42, 24], ["02.mp4", 42, 24], ["03.mp4", 42, 24]], inputs=[input_file, seed, fps]) gr.HTML("""
...""") demo.queue().launch(share=True)