Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -55,7 +55,7 @@ def generar_respuesta(texto, imagen_pil):
|
|
| 55 |
img_b64 = imagen_pil_a_base64(imagen_pil)
|
| 56 |
contenido.append({
|
| 57 |
"type": "image_url",
|
| 58 |
-
"image_url": {"url": f"
|
| 59 |
})
|
| 60 |
|
| 61 |
headers = {
|
|
@@ -94,13 +94,19 @@ def generar_audio(texto, voz_id=VOICE_ID_DEFAULT):
|
|
| 94 |
print("Error en Hume TTS:", e)
|
| 95 |
return None
|
| 96 |
|
| 97 |
-
def
|
| 98 |
if historial is None:
|
| 99 |
historial = []
|
| 100 |
|
| 101 |
-
|
| 102 |
-
if
|
| 103 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
historial.append({"role": "user", "content": texto_usuario})
|
| 106 |
respuesta = generar_respuesta(texto_usuario, imagen_pil)
|
|
@@ -113,16 +119,18 @@ def limpiar_chat():
|
|
| 113 |
return [], None, ""
|
| 114 |
|
| 115 |
# --- Interfaz Gradio ---
|
| 116 |
-
with gr.Blocks(title="Batuto AI Voz
|
| 117 |
-
gr.Markdown("#
|
| 118 |
-
gr.Markdown("
|
| 119 |
-
|
| 120 |
chat = gr.Chatbot(label="Conversación", height=400, type="messages")
|
| 121 |
-
|
| 122 |
with gr.Row():
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
|
|
|
|
|
|
| 126 |
with gr.Row():
|
| 127 |
voz_sel = gr.Dropdown(
|
| 128 |
label="Voz de respuesta",
|
|
@@ -135,13 +143,19 @@ with gr.Blocks(title="Batuto AI Voz-a-Voz", theme=gr.themes.Soft()) as demo:
|
|
| 135 |
)
|
| 136 |
enviar_btn = gr.Button("Enviar")
|
| 137 |
limpiar_btn = gr.Button("Limpiar")
|
| 138 |
-
|
| 139 |
salida_texto = gr.Textbox(label="Respuesta", interactive=False)
|
| 140 |
salida_audio = gr.Audio(label="Audio", autoplay=True, interactive=False)
|
| 141 |
|
|
|
|
| 142 |
enviar_btn.click(
|
| 143 |
-
|
| 144 |
-
inputs=[audio_in, imagen_in, chat, voz_sel],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
outputs=[chat, salida_audio, salida_texto]
|
| 146 |
)
|
| 147 |
limpiar_btn.click(limpiar_chat, outputs=[chat, salida_audio, salida_texto])
|
|
|
|
| 55 |
img_b64 = imagen_pil_a_base64(imagen_pil)
|
| 56 |
contenido.append({
|
| 57 |
"type": "image_url",
|
| 58 |
+
"image_url": {"url": f"image/jpeg;base64,{img_b64}"}
|
| 59 |
})
|
| 60 |
|
| 61 |
headers = {
|
|
|
|
| 94 |
print("Error en Hume TTS:", e)
|
| 95 |
return None
|
| 96 |
|
| 97 |
+
def manejar_entrada(texto_input, audio_input, imagen_pil, historial, voz_id):
|
| 98 |
if historial is None:
|
| 99 |
historial = []
|
| 100 |
|
| 101 |
+
# Determinar entrada: texto tiene prioridad sobre audio
|
| 102 |
+
if texto_input and texto_input.strip():
|
| 103 |
+
texto_usuario = texto_input.strip()
|
| 104 |
+
elif audio_input:
|
| 105 |
+
texto_usuario = audio_a_texto(audio_input)
|
| 106 |
+
if not texto_usuario:
|
| 107 |
+
return historial, None, "No entendí lo que dijiste. ¿Puedes repetirlo?"
|
| 108 |
+
else:
|
| 109 |
+
return historial, None, "Por favor, escribe algo o habla."
|
| 110 |
|
| 111 |
historial.append({"role": "user", "content": texto_usuario})
|
| 112 |
respuesta = generar_respuesta(texto_usuario, imagen_pil)
|
|
|
|
| 119 |
return [], None, ""
|
| 120 |
|
| 121 |
# --- Interfaz Gradio ---
|
| 122 |
+
with gr.Blocks(title="Batuto AI Voz + Texto → Voz", theme=gr.themes.Soft()) as demo:
|
| 123 |
+
gr.Markdown("# 🧠 Batuto AI: Texto o Voz → Respuesta en Voz")
|
| 124 |
+
gr.Markdown("Escribe **o** habla. Sube una imagen si lo deseas. Siempre respondo con **voz empática**.")
|
| 125 |
+
|
| 126 |
chat = gr.Chatbot(label="Conversación", height=400, type="messages")
|
| 127 |
+
|
| 128 |
with gr.Row():
|
| 129 |
+
texto_in = gr.Textbox(label="📝 Escribe tu mensaje", lines=1, placeholder="Ej: ¿Qué ves en esta imagen?")
|
| 130 |
+
audio_in = gr.Audio(sources=["microphone"], type="filepath", label="🎤 O habla aquí")
|
| 131 |
+
|
| 132 |
+
imagen_in = gr.Image(label="📸 Imagen opcional", type="pil")
|
| 133 |
+
|
| 134 |
with gr.Row():
|
| 135 |
voz_sel = gr.Dropdown(
|
| 136 |
label="Voz de respuesta",
|
|
|
|
| 143 |
)
|
| 144 |
enviar_btn = gr.Button("Enviar")
|
| 145 |
limpiar_btn = gr.Button("Limpiar")
|
| 146 |
+
|
| 147 |
salida_texto = gr.Textbox(label="Respuesta", interactive=False)
|
| 148 |
salida_audio = gr.Audio(label="Audio", autoplay=True, interactive=False)
|
| 149 |
|
| 150 |
+
# Eventos
|
| 151 |
enviar_btn.click(
|
| 152 |
+
manejar_entrada,
|
| 153 |
+
inputs=[texto_in, audio_in, imagen_in, chat, voz_sel],
|
| 154 |
+
outputs=[chat, salida_audio, salida_texto]
|
| 155 |
+
)
|
| 156 |
+
texto_in.submit(
|
| 157 |
+
manejar_entrada,
|
| 158 |
+
inputs=[texto_in, audio_in, imagen_in, chat, voz_sel],
|
| 159 |
outputs=[chat, salida_audio, salida_texto]
|
| 160 |
)
|
| 161 |
limpiar_btn.click(limpiar_chat, outputs=[chat, salida_audio, salida_texto])
|