Josedcape commited on
Commit
fd3da00
路
verified 路
1 Parent(s): 855f95f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -13
app.py CHANGED
@@ -8,6 +8,7 @@ from datetime import datetime, timedelta
8
  from streamlit_autorefresh import st_autorefresh
9
  import requests
10
  import base64
 
11
 
12
  # Configurar la p谩gina de Streamlit
13
  st.set_page_config(page_title="Galatea Asistente - OMARDENT", layout="wide")
@@ -15,7 +16,7 @@ st.set_page_config(page_title="Galatea Asistente - OMARDENT", layout="wide")
15
  # Crear un placeholder para el audio
16
  audio_placeholder = st.empty()
17
 
18
- # Estilo CSS para el fondo
19
  st.markdown(
20
  """
21
  <style>
@@ -73,6 +74,33 @@ else:
73
  # Inicializar el cliente de Groq
74
  client_groq = groq.Groq(api_key=groq_api_key)
75
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76
  # Funci贸n para obtener respuesta de Groq y generar audio con Google TTS
77
  def obtener_respuesta_groq(pregunta, contexto="", modelo="llama3-8b-8192", temperatura=0.5):
78
  if not pregunta:
@@ -114,16 +142,8 @@ def obtener_respuesta_groq(pregunta, contexto="", modelo="llama3-8b-8192", tempe
114
  with open(audio_file_path, 'wb') as audio_file:
115
  audio_file.write(response_audio.audio_content)
116
 
117
- # Convertir el archivo de audio a base64 para reproducir con autoplay
118
- audio_base64 = base64.b64encode(response_audio.audio_content).decode()
119
-
120
- # Reproducir el audio con autoplay usando el placeholder
121
- audio_html = f"""
122
- <audio autoplay>
123
- <source src="data:audio/mp3;base64,{audio_base64}" type="audio/mp3">
124
- </audio>
125
- """
126
- audio_placeholder.markdown(audio_html, unsafe_allow_html=True)
127
 
128
  # Devolver la respuesta y la ruta del archivo de audio
129
  return respuesta, audio_file_path
@@ -190,8 +210,7 @@ if 'respuesta_programada' in st.session_state and st.session_state['respuesta_pr
190
  # Reproducir la notificaci贸n si el tiempo ha pasado y la notificaci贸n a煤n no se ha reproducido
191
  if datetime.now() >= respuesta_programada['tiempo_ejecucion'] and not respuesta_programada['notificacion_reproducida']:
192
  # Reproducir la notificaci贸n descargada
193
- with open("notificacion.mp3", "rb") as audio_file:
194
- st.audio(audio_file.read(), format="audio/mp3")
195
  st.session_state['respuesta_programada']['notificacion_reproducida'] = True # Marcar que se ha reproducido
196
 
197
  # Generar la respuesta si ya se ha reproducido la notificaci贸n y no se ha generado el audio
 
8
  from streamlit_autorefresh import st_autorefresh
9
  import requests
10
  import base64
11
+ import streamlit.components.v1 as components # Importar el m贸dulo de componentes
12
 
13
  # Configurar la p谩gina de Streamlit
14
  st.set_page_config(page_title="Galatea Asistente - OMARDENT", layout="wide")
 
16
  # Crear un placeholder para el audio
17
  audio_placeholder = st.empty()
18
 
19
+ # Estilo CSS para el fondo y mensajes de chat
20
  st.markdown(
21
  """
22
  <style>
 
74
  # Inicializar el cliente de Groq
75
  client_groq = groq.Groq(api_key=groq_api_key)
76
 
77
+ # Funci贸n para reproducir audio usando componentes de Streamlit
78
+ def reproducir_audio(audio_content, autoplay=True):
79
+ audio_base64 = base64.b64encode(audio_content).decode()
80
+ autoplay_attribute = 'autoplay' if autoplay else ''
81
+ # Inyectar HTML y JavaScript para reproducir el audio autom谩ticamente
82
+ audio_html = f"""
83
+ <audio id="audio_player" {autoplay_attribute}>
84
+ <source src="data:audio/mp3;base64,{audio_base64}" type="audio/mp3">
85
+ Tu navegador no soporta la reproducci贸n de audio.
86
+ </audio>
87
+ <script>
88
+ const audio = document.getElementById("audio_player");
89
+ if ({str(autoplay).lower()} && audio) {{
90
+ audio.play().catch(function(error) {{
91
+ console.log("Autoplay failed:", error);
92
+ }});
93
+ }}
94
+ </script>
95
+ """
96
+ components.html(audio_html, height=0, width=0) # height y width en 0 para no mostrar el reproductor
97
+
98
+ # Funci贸n para reproducir notificaci贸n desde archivo
99
+ def reproducir_notificacion(nombre_archivo, autoplay=True):
100
+ with open(nombre_archivo, "rb") as audio_file:
101
+ audio_content = audio_file.read()
102
+ reproducir_audio(audio_content, autoplay)
103
+
104
  # Funci贸n para obtener respuesta de Groq y generar audio con Google TTS
105
  def obtener_respuesta_groq(pregunta, contexto="", modelo="llama3-8b-8192", temperatura=0.5):
106
  if not pregunta:
 
142
  with open(audio_file_path, 'wb') as audio_file:
143
  audio_file.write(response_audio.audio_content)
144
 
145
+ # Reproducir el audio autom谩ticamente usando componentes de Streamlit
146
+ reproducir_audio(response_audio.audio_content, autoplay=True)
 
 
 
 
 
 
 
 
147
 
148
  # Devolver la respuesta y la ruta del archivo de audio
149
  return respuesta, audio_file_path
 
210
  # Reproducir la notificaci贸n si el tiempo ha pasado y la notificaci贸n a煤n no se ha reproducido
211
  if datetime.now() >= respuesta_programada['tiempo_ejecucion'] and not respuesta_programada['notificacion_reproducida']:
212
  # Reproducir la notificaci贸n descargada
213
+ reproducir_notificacion("notificacion.mp3", autoplay=True)
 
214
  st.session_state['respuesta_programada']['notificacion_reproducida'] = True # Marcar que se ha reproducido
215
 
216
  # Generar la respuesta si ya se ha reproducido la notificaci贸n y no se ha generado el audio