Spaces:
Runtime error
Runtime error
Add application file
Browse files- .gitignore +1 -0
- app.py +126 -0
- requirements.txt +108 -0
.gitignore
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
venv
|
app.py
ADDED
@@ -0,0 +1,126 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline, SpeechT5Processor, SpeechT5ForTextToSpeech, SpeechT5HifiGan
|
3 |
+
from datasets import load_dataset
|
4 |
+
import torch
|
5 |
+
|
6 |
+
|
7 |
+
|
8 |
+
|
9 |
+
replacements = [
|
10 |
+
("á", "a"),
|
11 |
+
("í", "i"),
|
12 |
+
("ñ", "n"),
|
13 |
+
("ó", "o"),
|
14 |
+
("ú", "u"),
|
15 |
+
("ü", "u"),
|
16 |
+
]
|
17 |
+
|
18 |
+
|
19 |
+
def cleanup_text(text):
|
20 |
+
for src, dst in replacements:
|
21 |
+
text = text.replace(src, dst)
|
22 |
+
return text
|
23 |
+
|
24 |
+
|
25 |
+
def modelo1(image):
|
26 |
+
imageToText = pipeline(
|
27 |
+
"image-to-text", model="Salesforce/blip-image-captioning-large")
|
28 |
+
|
29 |
+
resultado = imageToText(image)
|
30 |
+
resultado = resultado[0]["generated_text"].replace("araffe ", "")
|
31 |
+
return resultado
|
32 |
+
|
33 |
+
|
34 |
+
def modelo2(text):
|
35 |
+
enToEs = pipeline("translation", model="Helsinki-NLP/opus-mt-en-es")
|
36 |
+
|
37 |
+
resultado = enToEs(text)
|
38 |
+
return resultado[0]["translation_text"]
|
39 |
+
|
40 |
+
|
41 |
+
def modelo3En(text):
|
42 |
+
processor = SpeechT5Processor.from_pretrained("microsoft/speecht5_tts")
|
43 |
+
model = SpeechT5ForTextToSpeech.from_pretrained("microsoft/speecht5_tts")
|
44 |
+
vocoder = SpeechT5HifiGan.from_pretrained("microsoft/speecht5_hifigan")
|
45 |
+
embeddings_dataset = load_dataset("Matthijs/cmu-arctic-xvectors", split="validation")
|
46 |
+
speaker_embeddings = torch.tensor(embeddings_dataset[7306]["xvector"]).unsqueeze(0)
|
47 |
+
|
48 |
+
inputs = processor(text=text, return_tensors="pt")
|
49 |
+
|
50 |
+
speech = model.generate_speech(inputs["input_ids"], speaker_embeddings, vocoder=vocoder)
|
51 |
+
|
52 |
+
return gr.Audio.update(value=(16000, speech.cpu().numpy()))
|
53 |
+
|
54 |
+
|
55 |
+
def modelo3Es(text):
|
56 |
+
model = SpeechT5ForTextToSpeech.from_pretrained("Sandiago21/speecht5_finetuned_facebook_voxpopuli_spanish")
|
57 |
+
vocoder = SpeechT5HifiGan.from_pretrained("microsoft/speecht5_hifigan")
|
58 |
+
embeddings_dataset = load_dataset("Matthijs/cmu-arctic-xvectors", split="validation")
|
59 |
+
processor = SpeechT5Processor.from_pretrained("microsoft/speecht5_tts")
|
60 |
+
speaker_embeddings = torch.tensor(embeddings_dataset[7440]["xvector"]).unsqueeze(0)
|
61 |
+
|
62 |
+
text = cleanup_text(text)
|
63 |
+
inputs = processor(text=text, return_tensors="pt")
|
64 |
+
|
65 |
+
speech = model.generate_speech(inputs["input_ids"], speaker_embeddings, vocoder=vocoder)
|
66 |
+
|
67 |
+
return gr.Audio.update(value=(16000, speech.cpu().numpy()))
|
68 |
+
|
69 |
+
|
70 |
+
def executionIMG(image, lan):
|
71 |
+
print(lan)
|
72 |
+
if lan == 'english':
|
73 |
+
model1res = modelo1(image)
|
74 |
+
model3res = modelo3En(model1res)
|
75 |
+
return model3res
|
76 |
+
elif lan == 'spanish':
|
77 |
+
model1res = modelo1(image)
|
78 |
+
model2res = modelo2(model1res)
|
79 |
+
model3res = modelo3Es(model2res)
|
80 |
+
return model3res
|
81 |
+
|
82 |
+
|
83 |
+
def executionTEXT(text, lan):
|
84 |
+
if lan == 'english':
|
85 |
+
model3res = modelo3En(text)
|
86 |
+
return model3res
|
87 |
+
elif lan == 'spanish':
|
88 |
+
model3res = modelo3Es(text)
|
89 |
+
return model3res
|
90 |
+
|
91 |
+
|
92 |
+
with gr.Blocks() as demo:
|
93 |
+
gr.Markdown(
|
94 |
+
"""
|
95 |
+
Se quiere hacer un programa que saque un audio de una imagen o de un texto, el cual tiene que ser introducido por el usuario. Para resolver este problema se realiza el siguiente programa. Se van a usar
|
96 |
+
tres modelos (Salesforce/blip-image-captioning-large, Helsinki-NLP/opus-mt-en-es, microsoft/speecht5_tts), los cuales se describen a continuación cuál es la función de cada uno: \n
|
97 |
+
- Primero necesitaremos dos Tabs, uno con un input tipo Image (IMAGE) en el que pasaremos una imagen y otro con un input tipo Textbox (TEXT) en el que pasaremos un texto. \n
|
98 |
+
- En el caso de la imagen, la pasaremos a texto usando un modelo con esta función (Salesforce/blip-image-captioning-large). Este modelo está entrenado para sacar texto describiendo qué hay en la
|
99 |
+
foto. El modelo nos sacará un texto en inglés. \n
|
100 |
+
- En caso del texto, no hace falta usar el modelo anterior ya que directamente tenemos el texto que queremos pasar a audio. \n
|
101 |
+
- Luego, tenemos un evento de tipo Radio, con el cual podemos elegir el idioma en el que vamos a sacar el audio. En el caso de la imagen, dado que el modelo saca el texto de esta imagen en inglés,
|
102 |
+
si hemos seleccionado que queremos sacar el audio en español tendremos que traducir este texto de inglés a español. En el caso del texto se da por hecho que el texto va a ser introducido en el
|
103 |
+
mismo idioma que se quiere sacar el audio. \n
|
104 |
+
- Para traducir el texto usaremos un modelo que está entrenado para pasar texto de inglés a español (Helsinki-NLP/opus-mt-en-es), por lo que nos devolverá un texto casi perfectamente traducido al
|
105 |
+
español. \n
|
106 |
+
- Una vez tenemos el texto que queremos pasar a audio en el idioma deseado, con el último modelo pasaremos este texto a audio (microsoft/speecht5_tts). Este modelo está entrenado para sacar audio a
|
107 |
+
raíz de un texto, en el que
|
108 |
+
se escucha justo lo que pone en el texto que le mandamos. \n
|
109 |
+
- Por último tendremos un output de tipo Audio que nos mostrará el audio que hemos conseguido con el último modelo.
|
110 |
+
""")
|
111 |
+
|
112 |
+
with gr.Tab("IMAGE"):
|
113 |
+
inp = gr.inputs.Image(type="pil")
|
114 |
+
language = gr.Radio(["english", "spanish"], label="Language", info="Choose the language in which you want the audio to appear", value='english', interactive=True)
|
115 |
+
out = gr.Audio()
|
116 |
+
btn = gr.Button("RUN")
|
117 |
+
btn.click(fn=executionIMG, inputs=[inp, language], outputs=out)
|
118 |
+
|
119 |
+
with gr.Tab("TEXT"):
|
120 |
+
inp = gr.inputs.Textbox()
|
121 |
+
language = gr.Radio(["english", "spanish"], label="Language", info="Choose the language in which you want the audio to appear", value='english', interactive=True)
|
122 |
+
out = gr.Audio()
|
123 |
+
btn = gr.Button("RUN")
|
124 |
+
btn.click(fn=executionTEXT, inputs=[inp, language], outputs=out)
|
125 |
+
|
126 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,108 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
aiofiles==23.2.1
|
2 |
+
aiohttp==3.8.6
|
3 |
+
aiosignal==1.3.1
|
4 |
+
altair==5.1.2
|
5 |
+
annotated-types==0.6.0
|
6 |
+
anyio==3.7.1
|
7 |
+
async-timeout==4.0.3
|
8 |
+
attrs==23.1.0
|
9 |
+
audioread==3.0.1
|
10 |
+
certifi==2023.7.22
|
11 |
+
cffi==1.16.0
|
12 |
+
charset-normalizer==3.3.1
|
13 |
+
click==8.1.7
|
14 |
+
contourpy==1.1.1
|
15 |
+
cycler==0.12.1
|
16 |
+
datasets==2.14.6
|
17 |
+
decorator==5.1.1
|
18 |
+
dill==0.3.7
|
19 |
+
exceptiongroup==1.1.3
|
20 |
+
fastapi==0.104.0
|
21 |
+
ffmpy==0.3.1
|
22 |
+
filelock==3.13.0
|
23 |
+
fonttools==4.43.1
|
24 |
+
frozenlist==1.4.0
|
25 |
+
fsspec==2023.10.0
|
26 |
+
gradio==3.50.2
|
27 |
+
gradio_client==0.6.1
|
28 |
+
h11==0.14.0
|
29 |
+
httpcore==0.18.0
|
30 |
+
httpx==0.25.0
|
31 |
+
huggingface-hub==0.17.3
|
32 |
+
idna==3.4
|
33 |
+
importlib-resources==6.1.0
|
34 |
+
Jinja2==3.1.2
|
35 |
+
joblib==1.3.2
|
36 |
+
jsonschema==4.19.1
|
37 |
+
jsonschema-specifications==2023.7.1
|
38 |
+
kiwisolver==1.4.5
|
39 |
+
lazy_loader==0.3
|
40 |
+
librosa==0.10.1
|
41 |
+
llvmlite==0.41.1
|
42 |
+
MarkupSafe==2.1.3
|
43 |
+
matplotlib==3.8.0
|
44 |
+
mpmath==1.3.0
|
45 |
+
msgpack==1.0.7
|
46 |
+
multidict==6.0.4
|
47 |
+
multiprocess==0.70.15
|
48 |
+
networkx==3.2.1
|
49 |
+
numba==0.58.1
|
50 |
+
numpy==1.26.1
|
51 |
+
nvidia-cublas-cu12==12.1.3.1
|
52 |
+
nvidia-cuda-cupti-cu12==12.1.105
|
53 |
+
nvidia-cuda-nvrtc-cu12==12.1.105
|
54 |
+
nvidia-cuda-runtime-cu12==12.1.105
|
55 |
+
nvidia-cudnn-cu12==8.9.2.26
|
56 |
+
nvidia-cufft-cu12==11.0.2.54
|
57 |
+
nvidia-curand-cu12==10.3.2.106
|
58 |
+
nvidia-cusolver-cu12==11.4.5.107
|
59 |
+
nvidia-cusparse-cu12==12.1.0.106
|
60 |
+
nvidia-nccl-cu12==2.18.1
|
61 |
+
nvidia-nvjitlink-cu12==12.3.52
|
62 |
+
nvidia-nvtx-cu12==12.1.105
|
63 |
+
orjson==3.9.10
|
64 |
+
packaging==23.2
|
65 |
+
pandas==2.1.2
|
66 |
+
Pillow==10.1.0
|
67 |
+
platformdirs==3.11.0
|
68 |
+
pooch==1.8.0
|
69 |
+
psutil==5.9.6
|
70 |
+
pyarrow==13.0.0
|
71 |
+
pycparser==2.21
|
72 |
+
pydantic==2.4.2
|
73 |
+
pydantic_core==2.10.1
|
74 |
+
pydub==0.25.1
|
75 |
+
pyparsing==3.1.1
|
76 |
+
python-dateutil==2.8.2
|
77 |
+
python-multipart==0.0.6
|
78 |
+
pytz==2023.3.post1
|
79 |
+
PyYAML==6.0.1
|
80 |
+
referencing==0.30.2
|
81 |
+
regex==2023.10.3
|
82 |
+
requests==2.31.0
|
83 |
+
rpds-py==0.10.6
|
84 |
+
safetensors==0.4.0
|
85 |
+
scikit-learn==1.3.2
|
86 |
+
scipy==1.11.3
|
87 |
+
semantic-version==2.10.0
|
88 |
+
sentencepiece==0.1.99
|
89 |
+
six==1.16.0
|
90 |
+
sniffio==1.3.0
|
91 |
+
soundfile==0.12.1
|
92 |
+
soxr==0.3.7
|
93 |
+
starlette==0.27.0
|
94 |
+
sympy==1.12
|
95 |
+
threadpoolctl==3.2.0
|
96 |
+
tokenizers==0.14.1
|
97 |
+
toolz==0.12.0
|
98 |
+
torch==2.1.0
|
99 |
+
tqdm==4.66.1
|
100 |
+
transformers==4.34.1
|
101 |
+
triton==2.1.0
|
102 |
+
typing_extensions==4.8.0
|
103 |
+
tzdata==2023.3
|
104 |
+
urllib3==2.0.7
|
105 |
+
uvicorn==0.23.2
|
106 |
+
websockets==11.0.3
|
107 |
+
xxhash==3.4.1
|
108 |
+
yarl==1.9.2
|