Add application file and dependencies
Browse files- app.py +86 -0
- requirements.txt +8 -0
app.py
ADDED
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import SpeechT5Processor, SpeechT5ForTextToSpeech, SpeechT5HifiGan
|
2 |
+
import torch
|
3 |
+
import soundfile as sf
|
4 |
+
import gradio as gr
|
5 |
+
from datasets import load_dataset
|
6 |
+
from runware import Runware, IImageInference
|
7 |
+
import asyncio
|
8 |
+
from dotenv import load_dotenv
|
9 |
+
import os
|
10 |
+
|
11 |
+
# Cargar las variables de entorno desde el archivo .env
|
12 |
+
load_dotenv()
|
13 |
+
|
14 |
+
RUNWARE_API_KEY = os.getenv("RUNWARE_API_KEY")
|
15 |
+
if not RUNWARE_API_KEY:
|
16 |
+
raise ValueError("API key no encontrada. Aseg煤rate de configurarla en la variable de entorno 'RUNWARE_API_KEY'.")
|
17 |
+
|
18 |
+
# Cargar modelos de texto a voz
|
19 |
+
processor = SpeechT5Processor.from_pretrained("microsoft/speecht5_tts")
|
20 |
+
model = SpeechT5ForTextToSpeech.from_pretrained("microsoft/speecht5_tts")
|
21 |
+
vocoder = SpeechT5HifiGan.from_pretrained("microsoft/speecht5_hifigan")
|
22 |
+
|
23 |
+
# Funci贸n para generar imagen desde texto usando la API de Runware
|
24 |
+
async def generar_imagen_desde_texto(texto):
|
25 |
+
if not (3 <= len(texto) <= 2000):
|
26 |
+
return "Error: El texto debe tener entre 3 y 2000 caracteres."
|
27 |
+
|
28 |
+
runware = Runware(api_key=RUNWARE_API_KEY)
|
29 |
+
await runware.connect()
|
30 |
+
|
31 |
+
request_image = IImageInference(
|
32 |
+
positivePrompt=texto,
|
33 |
+
model="civitai:36520@76907",
|
34 |
+
numberResults=1,
|
35 |
+
negativePrompt="cloudy, rainy",
|
36 |
+
height=512,
|
37 |
+
width=512,
|
38 |
+
)
|
39 |
+
|
40 |
+
images = await runware.imageInference(requestImage=request_image)
|
41 |
+
if images:
|
42 |
+
return images[0].imageURL
|
43 |
+
else:
|
44 |
+
return "No se gener贸 ninguna imagen."
|
45 |
+
|
46 |
+
# Funci贸n de texto a voz
|
47 |
+
def text_to_speech(text):
|
48 |
+
if not (3 <= len(text) <= 2000):
|
49 |
+
return "Error: El texto debe tener entre 3 y 2000 caracteres.", None
|
50 |
+
|
51 |
+
# Procesar el texto
|
52 |
+
inputs = processor(text=text, return_tensors="pt")
|
53 |
+
|
54 |
+
# Obtener el embedding de voz
|
55 |
+
embeddings_dataset = load_dataset("Matthijs/cmu-arctic-xvectors", split="validation")
|
56 |
+
speaker_embeddings = torch.tensor(embeddings_dataset[7306]["xvector"]).unsqueeze(0)
|
57 |
+
|
58 |
+
# Generar el discurso
|
59 |
+
with torch.no_grad():
|
60 |
+
speech = model.generate_speech(inputs["input_ids"], speaker_embeddings, vocoder=vocoder)
|
61 |
+
|
62 |
+
# Guardar el archivo de audio
|
63 |
+
audio_path = "speech.wav"
|
64 |
+
sf.write(audio_path, speech.numpy(), samplerate=16000)
|
65 |
+
|
66 |
+
# Generar la imagen usando la API de Runware
|
67 |
+
imagen_url = asyncio.run(generar_imagen_desde_texto(text))
|
68 |
+
|
69 |
+
# Imprimir la URL de la imagen generada
|
70 |
+
print(f"URL de la imagen generada: {imagen_url}")
|
71 |
+
|
72 |
+
return audio_path, imagen_url
|
73 |
+
|
74 |
+
# Interfaz de Gradio
|
75 |
+
iface = gr.Interface(
|
76 |
+
fn=text_to_speech,
|
77 |
+
inputs=gr.Textbox(label="Escribe tu texto aqu铆"),
|
78 |
+
outputs=[
|
79 |
+
gr.Audio(label="Escucha el audio generado"),
|
80 |
+
gr.Image(label="Imagen generada")
|
81 |
+
],
|
82 |
+
title="Generaci贸n de texto a voz e imagen seg煤n texto",
|
83 |
+
live=True
|
84 |
+
)
|
85 |
+
|
86 |
+
iface.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
transformers
|
2 |
+
torch
|
3 |
+
soundfile
|
4 |
+
gradio
|
5 |
+
requests
|
6 |
+
datasets
|
7 |
+
runware
|
8 |
+
python-dotenv
|