iespsurmreqmer commited on
Commit
5774bbe
·
1 Parent(s): 44aa1b2
Files changed (1) hide show
  1. app4.py +31 -31
app4.py CHANGED
@@ -1,39 +1,39 @@
1
- import streamlit as st
2
- import requests
3
  import os
 
 
4
  from dotenv import load_dotenv
5
 
 
6
  load_dotenv()
7
 
8
- HF_TOKEN = os.getenv("TOKEN")
9
- if HF_TOKEN is None:
10
- st.error("❌ La variable TOKEN no está definida en .env")
11
- st.stop()
12
 
13
- API_URL = "https://api-inference.huggingface.co/models/facebook/mms-tts-eng"
14
- headers = {"Authorization": f"Bearer {HF_TOKEN}"}
15
 
16
- def text_to_speech(text):
17
- payload = {"inputs": text}
18
- response = requests.post(API_URL, headers=headers, json=payload)
19
- return response.content # devuelve WAV
20
-
21
- st.title("Texto a Voz Accesible (Inference Provider)")
22
- st.write("Convierte texto en audio para ayudar a personas con discapacidad visual.")
23
-
24
- texto = st.text_area("Escribe el texto para convertir")
25
-
26
- if st.button("Generar audio"):
27
  if not texto.strip():
28
- st.warning("Por favor, escribe algo.")
29
- else:
30
- audio_bytes = text_to_speech(texto)
31
-
32
- st.audio(audio_bytes, format="audio/wav")
33
-
34
- st.download_button(
35
- "Descargar WAV",
36
- audio_bytes,
37
- file_name="audio.wav",
38
- mime="audio/wav"
39
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import os
2
+ import gradio as gr
3
+ from huggingface_hub import InferenceClient
4
  from dotenv import load_dotenv
5
 
6
+ # Cargar variables de entorno
7
  load_dotenv()
8
 
9
+ TOKEN = os.getenv("TOKEN")
10
+ if TOKEN is None:
11
+ raise ValueError("❌ No se encontró la variable HF_TOKEN en el .env")
 
12
 
13
+ # Inicializar cliente de inferencia con Fal-AI
14
+ client = InferenceClient(provider="fal-ai", api_key=TOKEN)
15
 
16
+ def generar_audio(texto):
 
 
 
 
 
 
 
 
 
 
17
  if not texto.strip():
18
+ return None
19
+ # Llamada a la API
20
+ audio_bytes = client.text_to_speech(texto, model="hexgrad/Kokoro-82M")
21
+
22
+ # Guardar temporalmente para que Gradio lo reproduzca
23
+ filename = "voz_generada.wav"
24
+ with open(filename, "wb") as f:
25
+ f.write(audio_bytes)
26
+ return filename
27
+
28
+ # Interfaz Gradio
29
+ with gr.Blocks() as demo:
30
+ gr.Markdown("# 🗣️ Texto a Voz con Kokoro-82M")
31
+ gr.Markdown("Escribe un texto y escucha el audio generado usando Inference Provider Fal-AI.")
32
+
33
+ texto = gr.Textbox(label="Escribe tu texto aquí", lines=4)
34
+ boton = gr.Button("Generar Audio")
35
+ audio_output = gr.Audio(label="Resultado", type="filepath")
36
+
37
+ boton.click(fn=generar_audio, inputs=texto, outputs=audio_output)
38
+
39
+ demo.launch()