Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os, subprocess, sys
|
| 2 |
+
|
| 3 |
+
os.environ["HF_HUB_DISABLE_SYMLINKS_WARNING"] = "1"
|
| 4 |
+
os.environ["HF_HUB_DISABLE_HF_TRANSFER"] = "1"
|
| 5 |
+
os.environ["HF_HUB_ENABLE_XET"] = "0"
|
| 6 |
+
|
| 7 |
+
os.environ["NUMBA_CACHE_DIR"] = "/tmp/numba_cache"
|
| 8 |
+
os.makedirs("/tmp/numba_cache", exist_ok=True)
|
| 9 |
+
os.environ["NUMBA_DISABLE_JIT"] = "1"
|
| 10 |
+
|
| 11 |
+
from huggingface_hub import HfApi, HfFolder, upload_folder, snapshot_download
|
| 12 |
+
|
| 13 |
+
# 🔒 Eliminar hf_transfer si está presente
|
| 14 |
+
subprocess.run([sys.executable, "-m", "pip", "uninstall", "-y", "hf_transfer"])
|
| 15 |
+
|
| 16 |
+
# === Configuración ===
|
| 17 |
+
HF_MODEL_ID = "tu_usuario/xtts-v2-finetuned" # <--- cambia con tu repo en HF
|
| 18 |
+
HF_TOKEN = os.environ.get("HF_TOKEN") # Debe estar definido en tu Space/entorno
|
| 19 |
+
DATASET_PATH = "/home/user/app/dataset" # Ruta a tu dataset
|
| 20 |
+
OUTPUT_PATH = "/tmp/output_model"
|
| 21 |
+
BASE_MODEL = "coqui/XTTS-v2"
|
| 22 |
+
|
| 23 |
+
os.makedirs("/tmp/xtts_cache", exist_ok=True)
|
| 24 |
+
os.chmod("/tmp/xtts_cache", 0o777)
|
| 25 |
+
|
| 26 |
+
os.makedirs("/tmp/xtts_model", exist_ok=True)
|
| 27 |
+
os.chmod("/tmp/xtts_model", 0o777)
|
| 28 |
+
|
| 29 |
+
os.makedirs("/tmp/xtts_model/.huggingface", exist_ok=True)
|
| 30 |
+
os.chmod("/tmp/xtts_model/.huggingface", 0o777)
|
| 31 |
+
|
| 32 |
+
# Continúa con tu lógica, usando las nuevas rutas de manera consistent
|
| 33 |
+
|
| 34 |
+
# 🔧 Forzar descarga sin symlinks ni hf_transfer
|
| 35 |
+
model_dir = snapshot_download(
|
| 36 |
+
repo_id="coqui/XTTS-v2",
|
| 37 |
+
local_dir="/tmp/xtts_model", # descarga directa aquí
|
| 38 |
+
cache_dir="/tmp/hf_cache", # cache seguro en /tmp
|
| 39 |
+
#local_dir_use_symlinks=False, # 🔑 evita enlaces simbólicos
|
| 40 |
+
resume_download=True,
|
| 41 |
+
token=HF_TOKEN
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
print(f"✅ Modelo descargado en: {model_dir}")
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
CONFIG_PATH = "/tmp/xtts_model/config.json"
|
| 48 |
+
RESTORE_PATH = "/tmp/xtts_model/model.pth"
|
| 49 |
+
|
| 50 |
+
# === 2. Editar configuración para tu dataset VoxPopuli ===
|
| 51 |
+
print("=== Editando configuración para fine-tuning con VoxPopuli ===")
|
| 52 |
+
import json
|
| 53 |
+
with open(CONFIG_PATH, "r") as f:
|
| 54 |
+
config = json.load(f)
|
| 55 |
+
|
| 56 |
+
config["output_path"] = OUTPUT_PATH
|
| 57 |
+
config["datasets"] = [
|
| 58 |
+
{
|
| 59 |
+
"formatter": "voxpopuli",
|
| 60 |
+
"path": DATASET_PATH,
|
| 61 |
+
"meta_file_train": "metadata.json"
|
| 62 |
+
}
|
| 63 |
+
]
|
| 64 |
+
config["run_name"] = "xtts-finetune-voxpopuli"
|
| 65 |
+
config["lr"] = 1e-5 # más bajo para fine-tuning
|
| 66 |
+
|
| 67 |
+
with open(CONFIG_PATH, "w") as f:
|
| 68 |
+
json.dump(config, f, indent=2)
|
| 69 |
+
|
| 70 |
+
# === 3. Lanzar entrenamiento ===
|
| 71 |
+
print("=== Iniciando fine-tuning de XTTS-v2 ===")
|
| 72 |
+
|
| 73 |
+
import librosa
|
| 74 |
+
from librosa.core.spectrum import magphase
|
| 75 |
+
|
| 76 |
+
# Parchear dinámicamente
|
| 77 |
+
librosa.magphase = magphase
|
| 78 |
+
|
| 79 |
+
# subprocess.run([
|
| 80 |
+
# "python", "/home/user/TTS/TTS/bin/train_tts.py",
|
| 81 |
+
# "--config_path", CONFIG_PATH,
|
| 82 |
+
# "--restore_path", RESTORE_PATH
|
| 83 |
+
# ], check=True)
|
| 84 |
+
|
| 85 |
+
subprocess.run([
|
| 86 |
+
"python", "-m", "TTS.bin.train",
|
| 87 |
+
"--config_path", CONFIG_PATH,
|
| 88 |
+
"--restore_path", RESTORE_PATH
|
| 89 |
+
], check=True)
|
| 90 |
+
|
| 91 |
+
# === 4. Subir modelo resultante a HF ===
|
| 92 |
+
print("=== Subiendo modelo fine-tuneado a Hugging Face Hub ===")
|
| 93 |
+
api = HfApi()
|
| 94 |
+
HfFolder.save_token(HF_TOKEN)
|
| 95 |
+
|
| 96 |
+
upload_folder(
|
| 97 |
+
repo_id=HF_MODEL_ID,
|
| 98 |
+
repo_type="model",
|
| 99 |
+
folder_path=OUTPUT_PATH,
|
| 100 |
+
token=HF_TOKEN
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
print("✅ Fine-tuning completado y modelo subido a Hugging Face.")
|