Spaces:
Runtime error
Runtime error
import os | |
import requests | |
import subprocess | |
import sys | |
import gdown | |
import shutil | |
from tqdm import tqdm | |
def download_file(url, destination, use_gdown=False): | |
if not os.path.exists(os.path.dirname(destination)): | |
os.makedirs(os.path.dirname(destination)) | |
if use_gdown: | |
gdown.download(url, destination, quiet=False) | |
else: | |
response = requests.get(url, stream=True) | |
total_size = int(response.headers.get('content-length', 0)) | |
with open(destination, 'wb') as f, tqdm( | |
desc=destination, | |
total=total_size, | |
unit='iB', | |
unit_scale=True, | |
unit_divisor=1024, | |
) as pbar: | |
for data in response.iter_content(chunk_size=1024): | |
size = f.write(data) | |
pbar.update(size) | |
def main(): | |
# Instalar gdown si no est谩 instalado | |
subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'gdown']) | |
# Crear directorios necesarios | |
directories = [ | |
'checkpoints', | |
'temp_audio', | |
'temp_previews', | |
'temp_wav2lip', | |
'uploads', | |
'models', | |
'face_detection/detection/sfd', | |
'audio_processing' | |
] | |
for dir_name in directories: | |
if not os.path.exists(dir_name): | |
os.makedirs(dir_name, exist_ok=True) | |
# URLs alternativas para el modelo wav2lip | |
model_urls = [ | |
('https://drive.google.com/uc?id=1PwHvZJu8Z8AzztRkm8zPh8_6VG2LTJkR', True), | |
('https://huggingface.co/spaces/vumichien/wav2lip/resolve/main/wav2lip_gan.pth', False), | |
] | |
# Descargar modelo wav2lip probando diferentes fuentes | |
checkpoint_path = 'checkpoints/wav2lip_gan.pth' | |
if not os.path.exists(checkpoint_path): | |
success = False | |
for url, use_gdown in model_urls: | |
if download_file(url, checkpoint_path, use_gdown): | |
success = True | |
break | |
if not success: | |
print("Error: No se pudo descargar el modelo wav2lip_gan.pth") | |
print("Por favor, descarga manualmente el modelo desde:") | |
print("https://drive.google.com/uc?id=1PwHvZJu8Z8AzztRkm8zPh8_6VG2LTJkR") | |
print("Y col贸calo en la carpeta 'checkpoints' con el nombre 'wav2lip_gan.pth'") | |
return | |
# Archivos necesarios de wav2lip con URLs alternativas | |
files_to_download = { | |
'inference.py': [ | |
'https://raw.githubusercontent.com/Rudrabha/Wav2Lip/master/inference.py', | |
'https://huggingface.co/spaces/vumichien/wav2lip/raw/main/inference.py' | |
], | |
'models/wav2lip.py': [ | |
'https://raw.githubusercontent.com/Rudrabha/Wav2Lip/master/models/wav2lip.py', | |
'https://huggingface.co/spaces/vumichien/wav2lip/raw/main/models/wav2lip.py' | |
], | |
'face_detection/detection/sfd/sfd_detector.py': [ | |
'https://raw.githubusercontent.com/Rudrabha/Wav2Lip/master/face_detection/detection/sfd/sfd_detector.py', | |
'https://huggingface.co/spaces/vumichien/wav2lip/raw/main/face_detection/detection/sfd/sfd_detector.py' | |
], | |
'face_detection/detection/sfd/net_s3fd.py': [ | |
'https://raw.githubusercontent.com/Rudrabha/Wav2Lip/master/face_detection/detection/sfd/net_s3fd.py', | |
'https://huggingface.co/spaces/vumichien/wav2lip/raw/main/face_detection/detection/sfd/net_s3fd.py' | |
], | |
'face_detection/api.py': [ | |
'https://raw.githubusercontent.com/Rudrabha/Wav2Lip/master/face_detection/api.py', | |
'https://huggingface.co/spaces/vumichien/wav2lip/raw/main/face_detection/api.py' | |
], | |
'audio_processing/audio.py': [ | |
'https://raw.githubusercontent.com/Rudrabha/Wav2Lip/master/audio.py', | |
'https://huggingface.co/spaces/vumichien/wav2lip/raw/main/audio.py' | |
] | |
} | |
# Crear archivos __init__.py necesarios | |
init_files = [ | |
'models/__init__.py', | |
'face_detection/__init__.py', | |
'face_detection/detection/__init__.py', | |
'face_detection/detection/sfd/__init__.py', | |
'audio_processing/__init__.py' | |
] | |
for init_file in init_files: | |
init_path = os.path.join(os.path.dirname(__file__), init_file) | |
if not os.path.exists(init_path): | |
os.makedirs(os.path.dirname(init_path), exist_ok=True) | |
with open(init_path, 'w') as f: | |
pass | |
for file_path, urls in files_to_download.items(): | |
if not os.path.exists(file_path): | |
success = False | |
for url in urls: | |
if download_file(url, file_path): | |
success = True | |
break | |
if not success: | |
print(f"Error: No se pudo descargar {file_path}") | |
return | |
# Modificar inference.py para usar el m贸dulo audio_processing | |
with open('inference.py', 'r') as f: | |
content = f.read() | |
content = content.replace('import audio', 'from audio_processing import audio') | |
with open('inference.py', 'w') as f: | |
f.write(content) | |
# Instalar dependencias espec铆ficas de wav2lip | |
dependencies = [ | |
'torch==1.8.0', | |
'torchvision==0.9.0', | |
'torchaudio==0.8.0', | |
'opencv-python', | |
'numpy', | |
'scipy', | |
'librosa==0.8.0', | |
'face-alignment==1.3.5', | |
'audioread==3.0.1', | |
'soundfile==0.12.1', | |
'resampy==0.4.2', | |
'numba==0.57.1', | |
'scikit-learn' | |
] | |
print("\nInstalando dependencias...") | |
for dep in dependencies: | |
try: | |
subprocess.check_call([sys.executable, '-m', 'pip', 'install', dep]) | |
except subprocess.CalledProcessError as e: | |
print(f"Error instalando {dep}: {str(e)}") | |
if "torch" in dep: | |
print("Intentando instalar torch con una versi贸n compatible...") | |
try: | |
subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'torch', 'torchvision', 'torchaudio']) | |
except: | |
print("Error instalando torch. Por favor, inst谩lalo manualmente seg煤n tu sistema:") | |
print("https://pytorch.org/get-started/locally/") | |
print("\nInstalaci贸n completada.") | |
print("Ahora puedes ejecutar la aplicaci贸n con: python app.py") | |
if __name__ == '__main__': | |
main() |