Update app.py
Browse files
app.py
CHANGED
|
@@ -5,8 +5,8 @@ from google.cloud import texttospeech_v1 as texttospeech
|
|
| 5 |
import streamlit as st
|
| 6 |
from dotenv import load_dotenv
|
| 7 |
import speech_recognition as sr
|
| 8 |
-
from threading import Timer
|
| 9 |
from datetime import datetime, timedelta
|
|
|
|
| 10 |
|
| 11 |
# Configurar la p谩gina de Streamlit
|
| 12 |
st.set_page_config(page_title="Galatea Asistente - OMARDENT", layout="wide")
|
|
@@ -54,26 +54,33 @@ def obtener_respuesta_groq(pregunta, contexto="", modelo="llama3-8b-8192", tempe
|
|
| 54 |
)
|
| 55 |
|
| 56 |
# Realizar la solicitud de s铆ntesis de voz
|
| 57 |
-
|
| 58 |
input=input_text, voice=voice, audio_config=audio_config
|
| 59 |
)
|
| 60 |
|
| 61 |
-
#
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
except Exception as e:
|
| 66 |
st.error(f"Error al generar la respuesta y el audio: {e}")
|
| 67 |
-
return "Lo siento, ocurri贸 un error al procesar tu solicitud."
|
| 68 |
|
| 69 |
# Funci贸n para manejar el chat del asistente
|
| 70 |
def mostrar_galatea_asistente():
|
| 71 |
st.title("馃Ψ Galatea - Asistente Virtual OMARDENT")
|
| 72 |
|
| 73 |
-
# Inicializa el historial del chat
|
| 74 |
if 'mensajes_chat' not in st.session_state:
|
| 75 |
st.session_state['mensajes_chat'] = []
|
| 76 |
|
|
|
|
|
|
|
|
|
|
| 77 |
# Mostrar los mensajes del chat
|
| 78 |
for mensaje in st.session_state['mensajes_chat']:
|
| 79 |
clase = "user" if mensaje["role"] == "user" else "assistant"
|
|
@@ -107,29 +114,42 @@ def mostrar_galatea_asistente():
|
|
| 107 |
# Guardar la pregunta en el historial
|
| 108 |
st.session_state['mensajes_chat'].append({"role": "user", "content": pregunta_usuario})
|
| 109 |
|
| 110 |
-
# Funci贸n para ejecutar la respuesta despu茅s del tiempo seleccionado
|
| 111 |
-
def ejecutar_respuesta():
|
| 112 |
-
respuesta = obtener_respuesta_groq(pregunta_usuario)
|
| 113 |
-
|
| 114 |
-
# Guardar la respuesta en el historial
|
| 115 |
-
st.session_state['mensajes_chat'].append({"role": "assistant", "content": respuesta})
|
| 116 |
-
|
| 117 |
-
# Mostrar la respuesta
|
| 118 |
-
st.write(f"**Respuesta:** {respuesta}")
|
| 119 |
-
|
| 120 |
# Calcular el tiempo en segundos
|
| 121 |
tiempo_en_segundos = intervalo * 60
|
| 122 |
-
|
| 123 |
-
# Mostrar un mensaje con la hora programada
|
| 124 |
tiempo_ejecucion = datetime.now() + timedelta(minutes=intervalo)
|
| 125 |
st.success(f"La respuesta se generar谩 a las {tiempo_ejecucion.strftime('%H:%M:%S')}")
|
| 126 |
|
| 127 |
-
#
|
| 128 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
|
| 130 |
# Funci贸n principal
|
| 131 |
def main():
|
| 132 |
mostrar_galatea_asistente()
|
| 133 |
|
| 134 |
if __name__ == "__main__":
|
| 135 |
-
main()
|
|
|
|
| 5 |
import streamlit as st
|
| 6 |
from dotenv import load_dotenv
|
| 7 |
import speech_recognition as sr
|
|
|
|
| 8 |
from datetime import datetime, timedelta
|
| 9 |
+
from streamlit_autorefresh import st_autorefresh
|
| 10 |
|
| 11 |
# Configurar la p谩gina de Streamlit
|
| 12 |
st.set_page_config(page_title="Galatea Asistente - OMARDENT", layout="wide")
|
|
|
|
| 54 |
)
|
| 55 |
|
| 56 |
# Realizar la solicitud de s铆ntesis de voz
|
| 57 |
+
response_audio = client.synthesize_speech(
|
| 58 |
input=input_text, voice=voice, audio_config=audio_config
|
| 59 |
)
|
| 60 |
|
| 61 |
+
# Guardar el archivo de audio temporalmente
|
| 62 |
+
audio_file_path = f'tmp_respuesta_{int(time.time())}.mp3'
|
| 63 |
+
with open(audio_file_path, 'wb') as audio_file:
|
| 64 |
+
audio_file.write(response_audio.audio_content)
|
| 65 |
+
|
| 66 |
+
# Devolver la respuesta y la ruta del archivo de audio
|
| 67 |
+
return respuesta, audio_file_path
|
| 68 |
|
| 69 |
except Exception as e:
|
| 70 |
st.error(f"Error al generar la respuesta y el audio: {e}")
|
| 71 |
+
return "Lo siento, ocurri贸 un error al procesar tu solicitud.", None
|
| 72 |
|
| 73 |
# Funci贸n para manejar el chat del asistente
|
| 74 |
def mostrar_galatea_asistente():
|
| 75 |
st.title("馃Ψ Galatea - Asistente Virtual OMARDENT")
|
| 76 |
|
| 77 |
+
# Inicializa el historial del chat y el temporizador
|
| 78 |
if 'mensajes_chat' not in st.session_state:
|
| 79 |
st.session_state['mensajes_chat'] = []
|
| 80 |
|
| 81 |
+
if 'respuesta_programada' not in st.session_state:
|
| 82 |
+
st.session_state['respuesta_programada'] = None
|
| 83 |
+
|
| 84 |
# Mostrar los mensajes del chat
|
| 85 |
for mensaje in st.session_state['mensajes_chat']:
|
| 86 |
clase = "user" if mensaje["role"] == "user" else "assistant"
|
|
|
|
| 114 |
# Guardar la pregunta en el historial
|
| 115 |
st.session_state['mensajes_chat'].append({"role": "user", "content": pregunta_usuario})
|
| 116 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
# Calcular el tiempo en segundos
|
| 118 |
tiempo_en_segundos = intervalo * 60
|
|
|
|
|
|
|
| 119 |
tiempo_ejecucion = datetime.now() + timedelta(minutes=intervalo)
|
| 120 |
st.success(f"La respuesta se generar谩 a las {tiempo_ejecucion.strftime('%H:%M:%S')}")
|
| 121 |
|
| 122 |
+
# Guardar la informaci贸n de la respuesta programada
|
| 123 |
+
st.session_state['respuesta_programada'] = {
|
| 124 |
+
'pregunta': pregunta_usuario,
|
| 125 |
+
'tiempo_ejecucion': tiempo_ejecucion,
|
| 126 |
+
'tiempo_en_segundos': tiempo_en_segundos,
|
| 127 |
+
'audio_file_path': None
|
| 128 |
+
}
|
| 129 |
+
|
| 130 |
+
# Actualizar la interfaz cada segundo para verificar si el tiempo ha pasado
|
| 131 |
+
st_autorefresh(interval=1000, key="data_refresh")
|
| 132 |
+
|
| 133 |
+
# Verificar si hay una respuesta programada que deba ejecutarse
|
| 134 |
+
if 'respuesta_programada' in st.session_state and st.session_state['respuesta_programada']:
|
| 135 |
+
respuesta_programada = st.session_state['respuesta_programada']
|
| 136 |
+
if datetime.now() >= respuesta_programada['tiempo_ejecucion'] and respuesta_programada['audio_file_path'] is None:
|
| 137 |
+
# Generar la respuesta
|
| 138 |
+
respuesta, audio_file_path = obtener_respuesta_groq(respuesta_programada['pregunta'])
|
| 139 |
+
|
| 140 |
+
# Guardar la respuesta y el audio en el estado
|
| 141 |
+
st.session_state['mensajes_chat'].append({"role": "assistant", "content": respuesta})
|
| 142 |
+
st.session_state['respuesta_programada']['audio_file_path'] = audio_file_path
|
| 143 |
+
|
| 144 |
+
# Reproducir el audio si ya fue generado
|
| 145 |
+
if st.session_state['respuesta_programada']['audio_file_path']:
|
| 146 |
+
audio_file_path = st.session_state['respuesta_programada']['audio_file_path']
|
| 147 |
+
with open(audio_file_path, "rb") as audio_file:
|
| 148 |
+
st.audio(audio_file.read(), format="audio/mp3")
|
| 149 |
|
| 150 |
# Funci贸n principal
|
| 151 |
def main():
|
| 152 |
mostrar_galatea_asistente()
|
| 153 |
|
| 154 |
if __name__ == "__main__":
|
| 155 |
+
main()
|