aducsdr commited on
Commit
be6b030
·
verified ·
1 Parent(s): ed2f9cb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -62
app.py CHANGED
@@ -11,10 +11,64 @@
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
- import subprocess
16
  import os
17
  import sys
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
  # --- ETAPA 1: Clonar o Repositório do GitHub ---
20
  repo_name = "SeedVR"
@@ -25,50 +79,10 @@ if not os.path.exists(repo_name):
25
  # --- ETAPA 2: Mudar para o Diretório e Configurar o Ambiente ---
26
  os.chdir(repo_name)
27
  print(f"Diretório de trabalho alterado para: {os.getcwd()}")
28
-
29
  sys.path.insert(0, os.path.abspath('.'))
30
- print(f"Diretório atual adicionado ao sys.path.")
31
-
32
- # --- ETAPA 3: Instalar Dependências Corretamente ---
33
- python_executable = sys.executable
34
-
35
- # Instalar flash-attn
36
- print("Instalando flash-attn...")
37
- subprocess.run(
38
- [python_executable, "-m", "pip", "install", "flash-attn", "--no-build-isolation"],
39
- check=True
40
- )
41
- print("✅ Configuração do flash-attn concluída.")
42
-
43
- # CORREÇÃO DEFINITIVA: Usar um método de instalação explícito para o Apex que força a compilação
44
- # das extensões CUDA através de variáveis de ambiente, que é a abordagem mais robusta.
45
- print("Clonando o repositório Apex para compilação manual...")
46
- if not os.path.exists("apex"):
47
- subprocess.run("git clone 'https://huggingface.co/ByteDance-Seed/SeedVR2-3B/resolve/main/apex-0.1-cp310-cp310-linux_x86_64.whl'", shell=True, check=True)
48
-
49
- print("Instalando Apex a partir do código-fonte clonado com variáveis de ambiente de compilação...")
50
 
51
- # Cria uma cópia do ambiente atual e adiciona as flags de compilação do Apex
52
- env = os.environ.copy()
53
- env["APEX_CPP_EXT"] = "1"
54
- env["APEX_CUDA_EXT"] = "1"
55
-
56
- # Comando de instalação
57
- apex_install_command = [
58
- python_executable, "-m", "pip", "install",
59
- "--no-build-isolation",
60
- "--no-cache-dir",
61
- "./apex"
62
- ]
63
-
64
- # Executa o comando com as variáveis de ambiente modificadas, o que força a compilação
65
- subprocess.run(apex_install_command, check=True, env=env)
66
- print("✅ Configuração do Apex concluída.")
67
-
68
-
69
- from pathlib import Path
70
- from urllib.parse import urlparse
71
- from torch.hub import download_url_to_file, get_dir
72
 
73
  def load_file_from_url(url, model_dir='.', progress=True, file_name=None):
74
  os.makedirs(model_dir, exist_ok=True)
@@ -81,10 +95,6 @@ def load_file_from_url(url, model_dir='.', progress=True, file_name=None):
81
  download_url_to_file(url, cached_file, hash_prefix=None, progress=progress)
82
  return cached_file
83
 
84
- # --- ETAPA 4: Baixar os Modelos Pré-treinados ---
85
- print("Baixando modelos pré-treinados...")
86
- import torch
87
-
88
  pretrain_model_url = {
89
  'vae': 'https://huggingface.co/ByteDance-Seed/SeedVR2-3B/resolve/main/ema_vae.pth',
90
  'dit': 'https://huggingface.co/ByteDance-Seed/SeedVR2-3B/resolve/main/seedvr2_ema_3b.pth',
@@ -98,12 +108,11 @@ for key, url in pretrain_model_url.items():
98
  load_file_from_url(url=url, model_dir=model_dir)
99
 
100
 
101
- # --- ETAPA 5: Executar a Aplicação Principal ---
102
  import mediapy
103
  from einops import rearrange
104
  from omegaconf import OmegaConf
105
  import datetime
106
- from tqdm import tqdm
107
  import gc
108
  from PIL import Image
109
  import gradio as gr
@@ -127,10 +136,6 @@ os.environ["MASTER_PORT"] = "12355"
127
  os.environ["RANK"] = str(0)
128
  os.environ["WORLD_SIZE"] = str(1)
129
 
130
- use_colorfix = os.path.exists("projects/video_diffusion_sr/color_fix.py")
131
- if use_colorfix:
132
- from projects.video_diffusion_sr.color_fix import wavelet_reconstruction
133
-
134
  def configure_runner():
135
  config = load_config('configs_3b/main.yaml')
136
  runner = VideoDiffusionInfer(config)
@@ -161,7 +166,6 @@ def generation_step(runner, text_embeds_dict, cond_latents):
161
  def generation_loop(video_path, seed=666, fps_out=24):
162
  if video_path is None: return None, None, None
163
  runner = configure_runner()
164
- # Adicionado `weights_only=True` para segurança e para suprimir o aviso
165
  text_embeds = {
166
  "texts_pos": [torch.load('pos_emb.pt', weights_only=True).to("cuda")],
167
  "texts_neg": [torch.load('neg_emb.pt', weights_only=True).to("cuda")]
@@ -169,7 +173,6 @@ def generation_loop(video_path, seed=666, fps_out=24):
169
  runner.configure_diffusion()
170
  set_seed(int(seed))
171
  os.makedirs("output", exist_ok=True)
172
-
173
  res_h, res_w = 1280, 720
174
  transform = Compose([
175
  NaResize(resolution=(res_h * res_w)**0.5, mode="area", downsample_only=False),
@@ -178,10 +181,8 @@ def generation_loop(video_path, seed=666, fps_out=24):
178
  Normalize(0.5, 0.5),
179
  Rearrange("t c h w -> c t h w")
180
  ])
181
-
182
  media_type, _ = mimetypes.guess_type(video_path)
183
  is_video = media_type and media_type.startswith("video")
184
-
185
  if is_video:
186
  video, _, _ = read_video(video_path, output_format="TCHW")
187
  video = video[:121] / 255.0
@@ -189,14 +190,12 @@ def generation_loop(video_path, seed=666, fps_out=24):
189
  else:
190
  video = T.ToTensor()(Image.open(video_path).convert("RGB")).unsqueeze(0)
191
  output_path = os.path.join("output", f"{uuid.uuid4()}.png")
192
-
193
  cond_latents = [transform(video.to("cuda"))]
194
  ori_length = cond_latents[0].size(2)
195
  cond_latents = runner.vae_encode(cond_latents)
196
  samples = generation_step(runner, text_embeds, cond_latents)
197
  sample = samples[0][:ori_length].cpu()
198
  sample = rearrange(sample, "t c h w -> t h w c").clip(-1, 1).add(1).mul(127.5).byte().numpy()
199
-
200
  if is_video:
201
  mediapy.write_video(output_path, sample, fps=fps_out)
202
  return None, output_path, output_path
@@ -209,7 +208,7 @@ with gr.Blocks(title="SeedVR") as demo:
209
  <p><b>Demonstração oficial do Gradio</b> para
210
  <a href='https://github.com/ByteDance-Seed/SeedVR' target='_blank'>
211
  <b>SeedVR2: One-Step Video Restoration via Diffusion Adversarial Post-Training</b></a>.<br>
212
- 🔥 <b>SeedVR2</b> é um algoritmo de imagem e vídeo em um passo para conteúdo do mundo real e AIGC.
213
  </p>
214
  """)
215
  with gr.Row():
@@ -222,6 +221,5 @@ with gr.Blocks(title="SeedVR") as demo:
222
  output_video = gr.Video(label="Vídeo de Saída")
223
  download_link = gr.File(label="Baixar Resultado")
224
  run_button.click(fn=generation_loop, inputs=[input_file, seed, fps], outputs=[output_image, output_video, download_link])
225
-
226
 
227
  demo.queue().launch(share=True)
 
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
  import sys
16
+ import subprocess
17
+ import importlib
18
+
19
+ # --- ETAPA 0: CONFIGURAÇÃO ROBUSTA DO AMBIENTE COM CONDA ---
20
+
21
+ CONDA_PYTHON_EXECUTABLE = "/home/user/miniconda3/bin/python"
22
+
23
+ # Verifica se o ambiente conda já foi configurado
24
+ if not os.path.exists(CONDA_PYTHON_EXECUTABLE):
25
+ print("Iniciando a configuração do ambiente Conda pela primeira vez. Isso pode levar alguns minutos...")
26
+
27
+ # Função para executar comandos no shell
28
+ def run_command(command):
29
+ subprocess.run(command, shell=True, check=True, executable='/bin/bash')
30
+
31
+ # Instala o Miniconda
32
+ print("Baixando e instalando o Miniconda...")
33
+ run_command("wget https://repo.anaconda.com/miniconda/Miniconda3-py310_23.11.0-2-Linux-x86_64.sh -O miniconda.sh")
34
+ run_command("bash miniconda.sh -b -p /home/user/miniconda3")
35
+ run_command("rm miniconda.sh")
36
+ print("✅ Instalação do Miniconda concluída.")
37
+
38
+ # Define o caminho para o executável do Conda
39
+ conda_executable = "/home/user/miniconda3/bin/conda"
40
+ os.environ['PATH'] = f"/home/user/miniconda3/bin:{os.environ['PATH']}"
41
+
42
+ # Instala as dependências críticas com Conda (PyTorch, Apex pré-compilado)
43
+ print("Instalando PyTorch, Apex e dependências CUDA com Conda...")
44
+ run_command(f"{conda_executable} install -y -c pytorch -c nvidia pytorch torchvision pytorch-cuda=12.1 apex")
45
+ print("✅ Instalação de PyTorch e Apex via Conda concluída.")
46
+
47
+ # Instala as dependências restantes com o pip do ambiente conda
48
+ print("Instalando pacotes restantes com Pip (incluindo flash-attn)...")
49
+ pip_packages = [
50
+ "einops", "omegaconf", "lpips", "bson", "tensorflow", "opencv-python",
51
+ "sentencepiece", "diffusers", "rotary-embedding-torch", "transformers",
52
+ "tiktoken", "transformers_stream_generator", "torchmetrics", "pycocotools",
53
+ "torch-fidelity", "moviepy", "imageio", "tabulate", "deepdiff",
54
+ "parameterized", "mediapy", "av", "gradio",
55
+ "flash-attn" # Tenta instalar por último no ambiente já configurado
56
+ ]
57
+ run_command(f"{CONDA_PYTHON_EXECUTABLE} -m pip install {' '.join(pip_packages)} --no-build-isolation")
58
+ print("✅ Instalação das dependências do Pip concluída.")
59
+
60
+ print("\n*** Configuração do ambiente concluída. Reiniciando o script agora. ***\n")
61
+ # Executa novamente o script, mas desta vez com o interpretador Python do Conda
62
+ os.execle(CONDA_PYTHON_EXECUTABLE, CONDA_PYTHON_EXECUTABLE, *sys.argv)
63
+
64
+ # A partir daqui, o script é executado pelo Python do ambiente Conda
65
+ #---------------------------------------------------------------------
66
+
67
+ import spaces
68
+ from pathlib import Path
69
+ from urllib.parse import urlparse
70
+ from torch.hub import download_url_to_file
71
+ import torch
72
 
73
  # --- ETAPA 1: Clonar o Repositório do GitHub ---
74
  repo_name = "SeedVR"
 
79
  # --- ETAPA 2: Mudar para o Diretório e Configurar o Ambiente ---
80
  os.chdir(repo_name)
81
  print(f"Diretório de trabalho alterado para: {os.getcwd()}")
 
82
  sys.path.insert(0, os.path.abspath('.'))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83
 
84
+ # --- ETAPA 3: Baixar os Modelos Pré-treinados ---
85
+ print("Baixando modelos pré-treinados...")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86
 
87
  def load_file_from_url(url, model_dir='.', progress=True, file_name=None):
88
  os.makedirs(model_dir, exist_ok=True)
 
95
  download_url_to_file(url, cached_file, hash_prefix=None, progress=progress)
96
  return cached_file
97
 
 
 
 
 
98
  pretrain_model_url = {
99
  'vae': 'https://huggingface.co/ByteDance-Seed/SeedVR2-3B/resolve/main/ema_vae.pth',
100
  'dit': 'https://huggingface.co/ByteDance-Seed/SeedVR2-3B/resolve/main/seedvr2_ema_3b.pth',
 
108
  load_file_from_url(url=url, model_dir=model_dir)
109
 
110
 
111
+ # --- ETAPA 4: Executar a Aplicação Principal ---
112
  import mediapy
113
  from einops import rearrange
114
  from omegaconf import OmegaConf
115
  import datetime
 
116
  import gc
117
  from PIL import Image
118
  import gradio as gr
 
136
  os.environ["RANK"] = str(0)
137
  os.environ["WORLD_SIZE"] = str(1)
138
 
 
 
 
 
139
  def configure_runner():
140
  config = load_config('configs_3b/main.yaml')
141
  runner = VideoDiffusionInfer(config)
 
166
  def generation_loop(video_path, seed=666, fps_out=24):
167
  if video_path is None: return None, None, None
168
  runner = configure_runner()
 
169
  text_embeds = {
170
  "texts_pos": [torch.load('pos_emb.pt', weights_only=True).to("cuda")],
171
  "texts_neg": [torch.load('neg_emb.pt', weights_only=True).to("cuda")]
 
173
  runner.configure_diffusion()
174
  set_seed(int(seed))
175
  os.makedirs("output", exist_ok=True)
 
176
  res_h, res_w = 1280, 720
177
  transform = Compose([
178
  NaResize(resolution=(res_h * res_w)**0.5, mode="area", downsample_only=False),
 
181
  Normalize(0.5, 0.5),
182
  Rearrange("t c h w -> c t h w")
183
  ])
 
184
  media_type, _ = mimetypes.guess_type(video_path)
185
  is_video = media_type and media_type.startswith("video")
 
186
  if is_video:
187
  video, _, _ = read_video(video_path, output_format="TCHW")
188
  video = video[:121] / 255.0
 
190
  else:
191
  video = T.ToTensor()(Image.open(video_path).convert("RGB")).unsqueeze(0)
192
  output_path = os.path.join("output", f"{uuid.uuid4()}.png")
 
193
  cond_latents = [transform(video.to("cuda"))]
194
  ori_length = cond_latents[0].size(2)
195
  cond_latents = runner.vae_encode(cond_latents)
196
  samples = generation_step(runner, text_embeds, cond_latents)
197
  sample = samples[0][:ori_length].cpu()
198
  sample = rearrange(sample, "t c h w -> t h w c").clip(-1, 1).add(1).mul(127.5).byte().numpy()
 
199
  if is_video:
200
  mediapy.write_video(output_path, sample, fps=fps_out)
201
  return None, output_path, output_path
 
208
  <p><b>Demonstração oficial do Gradio</b> para
209
  <a href='https://github.com/ByteDance-Seed/SeedVR' target='_blank'>
210
  <b>SeedVR2: One-Step Video Restoration via Diffusion Adversarial Post-Training</b></a>.<br>
211
+ 🔥 <b>SeedVR2</b> é um algoritmo de restauração de imagem e vídeo em um passo para conteúdo do mundo real e AIGC.
212
  </p>
213
  """)
214
  with gr.Row():
 
221
  output_video = gr.Video(label="Vídeo de Saída")
222
  download_link = gr.File(label="Baixar Resultado")
223
  run_button.click(fn=generation_loop, inputs=[input_file, seed, fps], outputs=[output_image, output_video, download_link])
 
224
 
225
  demo.queue().launch(share=True)