sob111 commited on
Commit
c746161
·
verified ·
1 Parent(s): 92a548d

Update finetune_xtts_hf.py

Browse files
Files changed (1) hide show
  1. finetune_xtts_hf.py +26 -42
finetune_xtts_hf.py CHANGED
@@ -1,58 +1,41 @@
1
  import os
2
- import subprocess
3
- import json
4
  from huggingface_hub import HfApi, HfFolder
5
- from datasets import load_dataset
 
6
 
7
  # === Configuración ===
8
- HF_TOKEN = os.environ.get("HF_TOKEN") # define en los Secrets del Space
 
 
 
9
  HF_REPO_ID = "sob111/xttsv2-es-finetuned" # tu repo de destino
 
10
  OUTPUT_PATH = "/tmp/output_model"
11
- CONFIG_PATH = "./config.json"
12
-
13
- # === Guardar token de Hugging Face ===
14
- print("=== Guardando token de Hugging Face ===")
15
- HfFolder.save_token(HF_TOKEN)
16
 
17
- # === Descargar dataset desde Hugging Face ===
18
- # print("=== Descargando dataset sob111/voxpopuli_es_500 ===")
19
- # ds = load_dataset("sob111/voxpopuli_es_500", split="train", token=HF_TOKEN)
 
20
 
21
- # Guardar metadata.json en el formato esperado por Coqui TTS
22
- # os.makedirs("/tmp/voxpopuli_es_500/wav_data", exist_ok=True)
23
- # meta_file = "/tmp/voxpopuli_es_500/metadata.json"
 
 
 
24
 
25
- # with open(meta_file, "w", encoding="utf-8") as f:
26
- # for i, sample in enumerate(ds):
27
- # Guardar cada audio en wav_data
28
- # audio_path = f"/tmp/voxpopuli_es_500/wav_data/sample_{i}.wav"
29
- # array = sample["audio"]["array"]
30
- # import soundfile as sf
31
- # sf.write(audio_path, array, sample["audio"]["sampling_rate"])
32
 
33
- # entry = {
34
- # "audio_file": audio_path,
35
- # "text": sample.get("text") or sample.get("sentence", ""),
36
- # "speaker_name": str(sample.get("speaker_id", "speaker"))
37
- # }
38
- # f.write(json.dumps(entry, ensure_ascii=False) + "\n")
39
-
40
- # print("✅ Metadata guardada en {meta_file}")
41
 
42
  # === Entrenamiento XTTSv2 ===
43
  print("=== Iniciando entrenamiento XTTSv2 ===")
44
- # Importante: usa el binario oficial de train del paquete TTS
45
- # y pasa sólo el config. No uses rutas a 'recipes/...'
46
  try:
47
- subprocess.run(
48
- [
49
- "python",
50
- "/home/user/TTS/bin/train.py",
51
- "--config_path", CONFIG_PATH
52
- ],
53
- check=True
54
- )
55
- except subprocess.CalledProcessError as e:
56
  raise RuntimeError("❌ El entrenamiento XTTSv2 falló. Revisa los logs anteriores.") from e
57
 
58
  print("=== Entrenamiento finalizado ===")
@@ -68,6 +51,7 @@ api.upload_folder(
68
  token=HF_TOKEN
69
  )
70
 
71
- print("✅ Fine-tuning completado y subido a {HF_REPO_ID}")
 
72
 
73
 
 
1
  import os
 
 
2
  from huggingface_hub import HfApi, HfFolder
3
+ from TTS.train import train # API oficial de entrenamiento en v0.22+
4
+ import json
5
 
6
  # === Configuración ===
7
+ HF_TOKEN = os.environ.get("HF_TOKEN") # definir en los Secrets del Space
8
+ if not HF_TOKEN:
9
+ raise RuntimeError("Falta HF_TOKEN en los Secrets del Space.")
10
+
11
  HF_REPO_ID = "sob111/xttsv2-es-finetuned" # tu repo de destino
12
+ CONFIG_PATH = os.path.join(os.getcwd(), "config.json")
13
  OUTPUT_PATH = "/tmp/output_model"
 
 
 
 
 
14
 
15
+ # Dataset local
16
+ DATA_DIR = "/tmp/voxpopuli_es_500"
17
+ META_TRAIN = os.path.join(DATA_DIR, "metadata.json")
18
+ AUDIO_DIR = os.path.join(DATA_DIR, "wav_data")
19
 
20
+ # === Validar dataset ===
21
+ print("=== Comprobando dataset local ===")
22
+ #if not os.path.isfile(META_TRAIN):
23
+ # raise FileNotFoundError(f"No se encontró {META_TRAIN}.")
24
+ #if not os.path.isdir(AUDIO_DIR):
25
+ # raise FileNotFoundError(f"No se encontró el directorio {AUDIO_DIR}.")
26
 
27
+ num_lines = sum(1 for _ in open(META_TRAIN, "r", encoding="utf-8"))
28
+ print(f"metadata.json: {num_lines} líneas")
 
 
 
 
 
29
 
30
+ # === Guardar token de Hugging Face ===
31
+ print("=== Guardando token de Hugging Face ===")
32
+ HfFolder.save_token(HF_TOKEN)
 
 
 
 
 
33
 
34
  # === Entrenamiento XTTSv2 ===
35
  print("=== Iniciando entrenamiento XTTSv2 ===")
 
 
36
  try:
37
+ train(config_path=CONFIG_PATH)
38
+ except Exception as e:
 
 
 
 
 
 
 
39
  raise RuntimeError("❌ El entrenamiento XTTSv2 falló. Revisa los logs anteriores.") from e
40
 
41
  print("=== Entrenamiento finalizado ===")
 
51
  token=HF_TOKEN
52
  )
53
 
54
+ print(f"✅ Fine-tuning completado y subido a {HF_REPO_ID}")
55
+
56
 
57