Update app.py
Browse files
app.py
CHANGED
|
@@ -1,43 +1,44 @@
|
|
| 1 |
import os
|
| 2 |
-
import
|
| 3 |
from google.cloud import texttospeech_v1 as texttospeech
|
| 4 |
import streamlit as st
|
| 5 |
-
import speech_recognition as sr
|
| 6 |
from dotenv import load_dotenv
|
|
|
|
| 7 |
|
| 8 |
# Configurar la página de Streamlit
|
| 9 |
st.set_page_config(page_title="Galatea Asistente - OMARDENT", layout="wide")
|
| 10 |
|
| 11 |
# Cargar las claves API desde el archivo .env
|
| 12 |
load_dotenv()
|
| 13 |
-
|
| 14 |
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "botidinamix-g.json"
|
| 15 |
|
| 16 |
-
# Verificar que
|
| 17 |
-
if not
|
| 18 |
-
st.error("No API key provided for
|
| 19 |
else:
|
| 20 |
-
|
|
|
|
| 21 |
|
| 22 |
-
# Función para obtener respuesta de
|
| 23 |
-
def
|
| 24 |
if not pregunta:
|
| 25 |
st.error("La pregunta no puede estar vacía.")
|
| 26 |
return "Lo siento, la pregunta no puede estar vacía."
|
| 27 |
|
| 28 |
try:
|
| 29 |
with st.spinner('Evaluando su respuesta...'):
|
| 30 |
-
|
| 31 |
-
|
| 32 |
messages=[
|
| 33 |
{"role": "system", "content": contexto},
|
| 34 |
{"role": "user", "content": pregunta}
|
| 35 |
],
|
| 36 |
-
|
| 37 |
)
|
| 38 |
respuesta = response.choices[0].message['content'].strip()
|
| 39 |
|
| 40 |
-
#
|
| 41 |
client = texttospeech.TextToSpeechClient()
|
| 42 |
input_text = texttospeech.SynthesisInput(text=respuesta)
|
| 43 |
voice = texttospeech.VoiceSelectionParams(
|
|
@@ -47,19 +48,15 @@ def obtener_respuesta(pregunta, contexto="", modelo="gpt-3.5-turbo", temperatura
|
|
| 47 |
audio_encoding=texttospeech.AudioEncoding.MP3
|
| 48 |
)
|
| 49 |
|
| 50 |
-
#
|
| 51 |
response = client.synthesize_speech(
|
| 52 |
input=input_text, voice=voice, audio_config=audio_config
|
| 53 |
)
|
| 54 |
|
| 55 |
-
#
|
| 56 |
st.audio(response.audio_content, format="audio/mp3")
|
| 57 |
return respuesta
|
| 58 |
|
| 59 |
-
except openai.OpenAIError as e:
|
| 60 |
-
st.error(f"Error al comunicarse con OpenAI: {e}")
|
| 61 |
-
return "Lo siento, no puedo procesar tu solicitud en este momento."
|
| 62 |
-
|
| 63 |
except Exception as e:
|
| 64 |
st.error(f"Error al generar la respuesta y el audio: {e}")
|
| 65 |
return "Lo siento, ocurrió un error al procesar tu solicitud."
|
|
@@ -96,14 +93,14 @@ def mostrar_galatea_asistente():
|
|
| 96 |
except sr.RequestError:
|
| 97 |
st.error("Error al solicitar el servicio de reconocimiento de voz")
|
| 98 |
|
| 99 |
-
# Enviar la pregunta y obtener la respuesta
|
| 100 |
if st.button("Enviar Pregunta"):
|
| 101 |
if pregunta_usuario:
|
| 102 |
# Guardar la pregunta en el historial
|
| 103 |
st.session_state['mensajes_chat'].append({"role": "user", "content": pregunta_usuario})
|
| 104 |
|
| 105 |
-
# Obtener respuesta de
|
| 106 |
-
respuesta =
|
| 107 |
|
| 108 |
# Guardar la respuesta en el historial
|
| 109 |
st.session_state['mensajes_chat'].append({"role": "assistant", "content": respuesta})
|
|
|
|
| 1 |
import os
|
| 2 |
+
import groq
|
| 3 |
from google.cloud import texttospeech_v1 as texttospeech
|
| 4 |
import streamlit as st
|
|
|
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
+
import speech_recognition as sr
|
| 7 |
|
| 8 |
# Configurar la página de Streamlit
|
| 9 |
st.set_page_config(page_title="Galatea Asistente - OMARDENT", layout="wide")
|
| 10 |
|
| 11 |
# Cargar las claves API desde el archivo .env
|
| 12 |
load_dotenv()
|
| 13 |
+
groq_api_key = os.getenv("GROQ_API_KEY")
|
| 14 |
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "botidinamix-g.json"
|
| 15 |
|
| 16 |
+
# Verificar que la clave API de Groq está configurada
|
| 17 |
+
if not groq_api_key:
|
| 18 |
+
st.error("No API key provided for Groq. Please set your API key in the .env file.")
|
| 19 |
else:
|
| 20 |
+
# Inicializar el cliente de Groq
|
| 21 |
+
client_groq = groq.Groq(api_key=groq_api_key)
|
| 22 |
|
| 23 |
+
# Función para obtener respuesta de Groq y generar audio con Google TTS
|
| 24 |
+
def obtener_respuesta_groq(pregunta, contexto="", modelo="llama3-8b-8192", temperatura=0.5):
|
| 25 |
if not pregunta:
|
| 26 |
st.error("La pregunta no puede estar vacía.")
|
| 27 |
return "Lo siento, la pregunta no puede estar vacía."
|
| 28 |
|
| 29 |
try:
|
| 30 |
with st.spinner('Evaluando su respuesta...'):
|
| 31 |
+
# Hacer la llamada a la API de Groq
|
| 32 |
+
response = client_groq.chat.completions.create(
|
| 33 |
messages=[
|
| 34 |
{"role": "system", "content": contexto},
|
| 35 |
{"role": "user", "content": pregunta}
|
| 36 |
],
|
| 37 |
+
model=modelo
|
| 38 |
)
|
| 39 |
respuesta = response.choices[0].message['content'].strip()
|
| 40 |
|
| 41 |
+
# Configurar la solicitud de síntesis de voz (Google TTS)
|
| 42 |
client = texttospeech.TextToSpeechClient()
|
| 43 |
input_text = texttospeech.SynthesisInput(text=respuesta)
|
| 44 |
voice = texttospeech.VoiceSelectionParams(
|
|
|
|
| 48 |
audio_encoding=texttospeech.AudioEncoding.MP3
|
| 49 |
)
|
| 50 |
|
| 51 |
+
# Realizar la solicitud de síntesis de voz
|
| 52 |
response = client.synthesize_speech(
|
| 53 |
input=input_text, voice=voice, audio_config=audio_config
|
| 54 |
)
|
| 55 |
|
| 56 |
+
# Reproducir el audio en Streamlit
|
| 57 |
st.audio(response.audio_content, format="audio/mp3")
|
| 58 |
return respuesta
|
| 59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
except Exception as e:
|
| 61 |
st.error(f"Error al generar la respuesta y el audio: {e}")
|
| 62 |
return "Lo siento, ocurrió un error al procesar tu solicitud."
|
|
|
|
| 93 |
except sr.RequestError:
|
| 94 |
st.error("Error al solicitar el servicio de reconocimiento de voz")
|
| 95 |
|
| 96 |
+
# Enviar la pregunta y obtener la respuesta usando Groq
|
| 97 |
if st.button("Enviar Pregunta"):
|
| 98 |
if pregunta_usuario:
|
| 99 |
# Guardar la pregunta en el historial
|
| 100 |
st.session_state['mensajes_chat'].append({"role": "user", "content": pregunta_usuario})
|
| 101 |
|
| 102 |
+
# Obtener respuesta de Groq
|
| 103 |
+
respuesta = obtener_respuesta_groq(pregunta_usuario)
|
| 104 |
|
| 105 |
# Guardar la respuesta en el historial
|
| 106 |
st.session_state['mensajes_chat'].append({"role": "assistant", "content": respuesta})
|