Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -1,101 +1,3 @@
|
|
1 |
-
import os
|
2 |
-
import re
|
3 |
-
import time
|
4 |
-
import sys
|
5 |
-
import subprocess
|
6 |
-
import scipy.io.wavfile as wavfile
|
7 |
-
import torch
|
8 |
-
import torchaudio
|
9 |
-
import gradio as gr
|
10 |
-
from TTS.api import TTS
|
11 |
-
from TTS.tts.configs.xtts_config import XttsConfig
|
12 |
-
from TTS.tts.models.xtts import Xtts
|
13 |
-
from TTS.utils.generic_utils import get_user_data_dir
|
14 |
-
from huggingface_hub import hf_hub_download
|
15 |
-
|
16 |
-
# Configuración inicial
|
17 |
-
os.environ["COQUI_TOS_AGREED"] = "1"
|
18 |
-
|
19 |
-
def check_and_install(package):
|
20 |
-
try:
|
21 |
-
__import__(package)
|
22 |
-
except ImportError:
|
23 |
-
print(f"{package} no está instalado. Instalando...")
|
24 |
-
subprocess.check_call([sys.executable, "-m", "pip", "install", package])
|
25 |
-
|
26 |
-
print("Descargando y configurando el modelo...")
|
27 |
-
repo_id = "Blakus/Pedro_Lab_XTTS"
|
28 |
-
local_dir = os.path.join(get_user_data_dir("tts"), "tts_models--multilingual--multi-dataset--xtts_v2")
|
29 |
-
os.makedirs(local_dir, exist_ok=True)
|
30 |
-
files_to_download = ["config.json", "model.pth", "vocab.json"]
|
31 |
-
|
32 |
-
for file_name in files_to_download:
|
33 |
-
print(f"Descargando {file_name} de {repo_id}")
|
34 |
-
hf_hub_download(repo_id=repo_id, filename=file_name, local_dir=local_dir)
|
35 |
-
|
36 |
-
config_path = os.path.join(local_dir, "config.json")
|
37 |
-
checkpoint_path = os.path.join(local_dir, "model.pth")
|
38 |
-
vocab_path = os.path.join(local_dir, "vocab.json")
|
39 |
-
|
40 |
-
config = XttsConfig()
|
41 |
-
config.load_json(config_path)
|
42 |
-
|
43 |
-
model = Xtts.init_from_config(config)
|
44 |
-
model.load_checkpoint(config, checkpoint_path=checkpoint_path, vocab_path=vocab_path, eval=True, use_deepspeed=True)
|
45 |
-
|
46 |
-
model.cuda()
|
47 |
-
|
48 |
-
print("Modelo cargado en GPU")
|
49 |
-
|
50 |
-
def predict(prompt, language, reference_audio):
|
51 |
-
try:
|
52 |
-
if len(prompt) < 2 or len(prompt) > 600:
|
53 |
-
return None, "El texto debe tener entre 2 y 600 caracteres."
|
54 |
-
|
55 |
-
# Obtener los parámetros de la configuración JSON
|
56 |
-
temperature = config.model_args.get("temperature", 0.85)
|
57 |
-
length_penalty = config.model_args.get("length_penalty", 1.0)
|
58 |
-
repetition_penalty = config.model_args.get("repetition_penalty", 2.0)
|
59 |
-
top_k = config.model_args.get("top_k", 50)
|
60 |
-
top_p = config.model_args.get("top_p", 0.85)
|
61 |
-
|
62 |
-
gpt_cond_latent, speaker_embedding = model.get_conditioning_latents(
|
63 |
-
audio_path=reference_audio
|
64 |
-
)
|
65 |
-
|
66 |
-
start_time = time.time()
|
67 |
-
|
68 |
-
out = model.inference(
|
69 |
-
prompt,
|
70 |
-
language,
|
71 |
-
gpt_cond_latent,
|
72 |
-
speaker_embedding,
|
73 |
-
temperature=temperature,
|
74 |
-
length_penalty=length_penalty,
|
75 |
-
repetition_penalty=repetition_penalty,
|
76 |
-
top_k=top_k,
|
77 |
-
top_p=top_p
|
78 |
-
)
|
79 |
-
|
80 |
-
inference_time = time.time() - start_time
|
81 |
-
|
82 |
-
output_path = "pedro_labattaglia_TTS.wav"
|
83 |
-
# Guardar el audio directamente desde el output del modelo
|
84 |
-
import scipy.io.wavfile as wavfile
|
85 |
-
wavfile.write(output_path, config.audio["output_sample_rate"], out["wav"])
|
86 |
-
|
87 |
-
audio_length = len(out["wav"]) / config.audio["output_sample_rate"] # duración del audio en segundos
|
88 |
-
real_time_factor = inference_time / audio_length
|
89 |
-
|
90 |
-
metrics_text = f"Tiempo de generación: {inference_time:.2f} segundos\n"
|
91 |
-
metrics_text += f"Factor de tiempo real: {real_time_factor:.2f}"
|
92 |
-
|
93 |
-
return output_path, metrics_text
|
94 |
-
|
95 |
-
except Exception as e:
|
96 |
-
print(f"Error detallado: {str(e)}")
|
97 |
-
return None, f"Error: {str(e)}"
|
98 |
-
|
99 |
# Configuración de la interfaz de Gradio
|
100 |
supported_languages = ["es", "en"]
|
101 |
reference_audios = [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
# Configuración de la interfaz de Gradio
|
2 |
supported_languages = ["es", "en"]
|
3 |
reference_audios = [
|