Josedcape commited on
Commit
820ada0
verified
1 Parent(s): d9cd134

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -45
app.py CHANGED
@@ -12,7 +12,7 @@ import time
12
  from google.cloud import texttospeech
13
  from streamlit_webrtc import webrtc_streamer, WebRtcMode
14
  from Historial.historial_chat import cargar_historial, guardar_historial
15
- from agent_functions import leer_menu_csv, tomar_pedido_agente, procesar_orden_agente, generar_pdf_orden
16
 
17
  # Configuraci贸n de NLTK
18
  nltk.download('punkt')
@@ -50,47 +50,6 @@ def preprocesar_texto(texto):
50
  tokens = [stemmer.stem(word) for word in tokens]
51
  return " ".join(tokens)
52
 
53
- # Funci贸n para obtener respuesta de OpenAI usando el modelo GPT y convertir a audio
54
- def obtener_respuesta(pregunta, texto_preprocesado, modelo, temperatura=0.5):
55
- try:
56
- response = openai.ChatCompletion.create(
57
- model=modelo,
58
- messages=[
59
- {"role": "system", "content": "Actua como Ana una asesora de ventas del restaurante Sazon Burguer, tienes un tono muy amable y cordial"},
60
- {"role": "user", "content": f"{pregunta}\n\nContexto: {texto_preprocesado}"}
61
- ],
62
- temperature=temperatura
63
- )
64
- respuesta = response.choices[0].message['content'].strip()
65
-
66
- # Configura la solicitud de s铆ntesis de voz
67
- input_text = texttospeech.SynthesisInput(text=respuesta)
68
- voice = texttospeech.VoiceSelectionParams(
69
- language_code="es-ES", ssml_gender=texttospeech.SsmlVoiceGender.MALE,
70
- )
71
- audio_config = texttospeech.AudioConfig(
72
- audio_encoding=texttospeech.AudioEncoding.MP3
73
- )
74
-
75
- # Realiza la solicitud de s铆ntesis de voz
76
- response = client.synthesize_speech(
77
- input=input_text, voice=voice, audio_config=audio_config
78
- )
79
-
80
- # Guarda el audio en un archivo temporal
81
- audio_path = tempfile.mktemp(suffix=".mp3")
82
- with open(audio_path, "wb") as audio_file:
83
- audio_file.write(response.audio_content)
84
-
85
- # Reproduce el audio en Streamlit
86
- st.audio(audio_path, format="audio/mp3", start_time=0)
87
-
88
- return respuesta
89
-
90
- except openai.OpenAIError as e:
91
- st.error(f"Error al comunicarse con OpenAI: {e}")
92
- return "Lo siento, no puedo procesar tu solicitud en este momento."
93
-
94
  # Main App
95
  def main():
96
  # --- Dise帽o general ---
@@ -217,10 +176,11 @@ def main():
217
  st.markdown(pregunta_usuario)
218
 
219
  with st.spinner("Generando respuesta..."):
220
- respuesta = obtener_respuesta(pregunta_usuario, texto_preprocesado, modelo="gpt-4", temperatura=0.5)
221
  st.session_state.mensajes.append({"role": "assistant", "content": respuesta, "timestamp": time.time()})
222
  with st.chat_message("assistant"):
223
  st.markdown(respuesta)
 
224
 
225
  guardar_historial(st.session_state.mensajes)
226
  else:
@@ -252,10 +212,11 @@ def main():
252
  st.markdown(pregunta_usuario)
253
 
254
  with st.spinner("Generando respuesta..."):
255
- respuesta = obtener_respuesta(pregunta_usuario, texto_preprocesado, modelo="gpt-4", temperatura=0.5)
256
  st.session_state.mensajes.append({"role": "assistant", "content": respuesta, "timestamp": time.time()})
257
  with st.chat_message("assistant"):
258
  st.markdown(respuesta)
 
259
 
260
  guardar_historial(st.session_state.mensajes)
261
  else:
@@ -303,10 +264,11 @@ def main():
303
 
304
  # Procesar la respuesta del agente
305
  with st.spinner("El agente est谩 respondiendo..."):
306
- respuesta_agente = obtener_respuesta(agente_pregunta, '', modelo="gpt-4", temperatura=0.5)
307
  st.session_state.mensajes_agente.append({"role": "assistant", "content": respuesta_agente, "timestamp": time.time()})
308
  with st.chat_message("assistant"):
309
  st.markdown(respuesta_agente)
 
310
  else:
311
  st.warning("Por favor, ingresa una pregunta antes de enviar.")
312
 
 
12
  from google.cloud import texttospeech
13
  from streamlit_webrtc import webrtc_streamer, WebRtcMode
14
  from Historial.historial_chat import cargar_historial, guardar_historial
15
+ from agent_functions import leer_menu_csv, tomar_pedido_agente, procesar_orden_agente, generar_pdf_orden, obtener_respuesta
16
 
17
  # Configuraci贸n de NLTK
18
  nltk.download('punkt')
 
50
  tokens = [stemmer.stem(word) for word in tokens]
51
  return " ".join(tokens)
52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
  # Main App
54
  def main():
55
  # --- Dise帽o general ---
 
176
  st.markdown(pregunta_usuario)
177
 
178
  with st.spinner("Generando respuesta..."):
179
+ respuesta, audio_path = obtener_respuesta(pregunta_usuario, texto_preprocesado, modelo="gpt-4", temperatura=0.5)
180
  st.session_state.mensajes.append({"role": "assistant", "content": respuesta, "timestamp": time.time()})
181
  with st.chat_message("assistant"):
182
  st.markdown(respuesta)
183
+ st.audio(audio_path, format="audio/mp3", start_time=0)
184
 
185
  guardar_historial(st.session_state.mensajes)
186
  else:
 
212
  st.markdown(pregunta_usuario)
213
 
214
  with st.spinner("Generando respuesta..."):
215
+ respuesta, audio_path = obtener_respuesta(pregunta_usuario, texto_preprocesado, modelo="gpt-4", temperatura=0.5)
216
  st.session_state.mensajes.append({"role": "assistant", "content": respuesta, "timestamp": time.time()})
217
  with st.chat_message("assistant"):
218
  st.markdown(respuesta)
219
+ st.audio(audio_path, format="audio/mp3", start_time=0)
220
 
221
  guardar_historial(st.session_state.mensajes)
222
  else:
 
264
 
265
  # Procesar la respuesta del agente
266
  with st.spinner("El agente est谩 respondiendo..."):
267
+ respuesta_agente, audio_path = obtener_respuesta(agente_pregunta, '', modelo="gpt-4", temperatura=0.5)
268
  st.session_state.mensajes_agente.append({"role": "assistant", "content": respuesta_agente, "timestamp": time.time()})
269
  with st.chat_message("assistant"):
270
  st.markdown(respuesta_agente)
271
+ st.audio(audio_path, format="audio/mp3", start_time=0)
272
  else:
273
  st.warning("Por favor, ingresa una pregunta antes de enviar.")
274