Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -120,180 +120,219 @@ def main():
|
|
| 120 |
|
| 121 |
# --- Página principal ---
|
| 122 |
if pagina == "Chat":
|
| 123 |
-
|
| 124 |
-
if st.button("Buscar Historial"):
|
| 125 |
-
st.session_state.mostrar_historial = True
|
| 126 |
-
if st.button("Borrar Historial"):
|
| 127 |
-
st.session_state.mensajes = []
|
| 128 |
-
st.session_state.mostrar_historial = False
|
| 129 |
-
st.success("Historial borrado correctamente")
|
| 130 |
-
|
| 131 |
-
# --- Chatbot ---
|
| 132 |
-
if 'mensajes' not in st.session_state:
|
| 133 |
-
st.session_state.mensajes = cargar_historial()
|
| 134 |
-
|
| 135 |
-
for mensaje in st.session_state.mensajes:
|
| 136 |
-
with st.chat_message(mensaje["role"]):
|
| 137 |
-
st.markdown(mensaje["content"])
|
| 138 |
-
|
| 139 |
-
# Función para manejar la entrada de audio
|
| 140 |
-
def on_audio(audio_bytes):
|
| 141 |
-
with st.spinner("Transcribiendo..."):
|
| 142 |
-
transcript = openai.Audio.transcribe("whisper-1", audio_bytes)
|
| 143 |
-
pregunta_usuario = transcript["text"]
|
| 144 |
-
st.session_state.mensajes.append({"role": "user", "content": pregunta_usuario, "timestamp": time.time()})
|
| 145 |
-
with st.chat_message("user"):
|
| 146 |
-
st.markdown(pregunta_usuario)
|
| 147 |
-
|
| 148 |
-
st.subheader("🎤 Captura de voz")
|
| 149 |
-
st.info("Haz clic en el micrófono y comienza a hablar. Tu pregunta se transcribirá automáticamente.")
|
| 150 |
-
with st.container():
|
| 151 |
-
if st.button("Grabar 🎙️"):
|
| 152 |
-
st.session_state.run_webrtc = True
|
| 153 |
-
if st.session_state.get("run_webrtc", False):
|
| 154 |
-
webrtc_ctx = webrtc_streamer(
|
| 155 |
-
key="example",
|
| 156 |
-
mode=WebRtcMode.SENDONLY,
|
| 157 |
-
audio_receiver_size=256,
|
| 158 |
-
rtc_configuration={
|
| 159 |
-
"iceServers": [{"urls": ["stun:stun.l.google.com:19302"]}]
|
| 160 |
-
},
|
| 161 |
-
media_stream_constraints={"audio": True},
|
| 162 |
-
audio_processor_factory=AudioProcessor,
|
| 163 |
-
)
|
| 164 |
-
|
| 165 |
-
if webrtc_ctx.audio_receiver:
|
| 166 |
-
audio_frames = webrtc_ctx.audio_receiver.get_frames(timeout=1)
|
| 167 |
-
for audio_frame in audio_frames:
|
| 168 |
-
audio_bytes = audio_frame.to_ndarray().tobytes()
|
| 169 |
-
on_audio(audio_bytes)
|
| 170 |
-
|
| 171 |
-
st.markdown("---")
|
| 172 |
-
st.subheader("📄 Subir PDF")
|
| 173 |
-
st.info("Sube un archivo PDF y el asistente responderá en función de su contenido.")
|
| 174 |
-
archivo_pdf = st.file_uploader("Selecciona un archivo PDF", type=["pdf"])
|
| 175 |
-
|
| 176 |
-
texto_extraido = ""
|
| 177 |
-
if archivo_pdf:
|
| 178 |
-
texto_extraido = extraer_texto_pdf(archivo_pdf)
|
| 179 |
-
st.success("Texto extraído del PDF exitosamente.")
|
| 180 |
-
st.text_area("Texto extraído", value=texto_extraido, height=200)
|
| 181 |
-
|
| 182 |
-
if not texto_extraido:
|
| 183 |
-
texto_extraido = st.text_area("Texto extraído", height=200)
|
| 184 |
-
|
| 185 |
-
texto_preprocesado = preprocesar_texto(texto_extraido)
|
| 186 |
-
|
| 187 |
-
# --- Opciones de entrada de usuario ---
|
| 188 |
-
st.markdown("---")
|
| 189 |
-
pregunta_usuario = st.text_input("Escribe tu pregunta:")
|
| 190 |
-
if st.button("Enviar"):
|
| 191 |
-
if pregunta_usuario:
|
| 192 |
-
st.session_state.mensajes.append({"role": "user", "content": pregunta_usuario, "timestamp": time.time()})
|
| 193 |
-
with st.chat_message("user"):
|
| 194 |
-
st.markdown(pregunta_usuario)
|
| 195 |
-
|
| 196 |
-
with st.spinner("Generando respuesta..."):
|
| 197 |
-
respuesta, audio_path = obtener_respuesta(pregunta_usuario, texto_preprocesado, modelo="gpt-4", temperatura=0.5)
|
| 198 |
-
st.session_state.mensajes.append({"role": "assistant", "content": respuesta, "timestamp": time.time()})
|
| 199 |
-
with st.chat_message("assistant"):
|
| 200 |
-
st.markdown(respuesta)
|
| 201 |
-
st.audio(audio_path, format="audio/mp3", start_time=0)
|
| 202 |
-
|
| 203 |
-
guardar_historial(st.session_state.mensajes)
|
| 204 |
-
else:
|
| 205 |
-
st.warning("Por favor, ingresa una pregunta antes de enviar.")
|
| 206 |
-
|
| 207 |
elif pagina == "Subir PDF":
|
| 208 |
-
|
| 209 |
-
st.info("Sube un archivo PDF y el asistente responderá en función de su contenido.")
|
| 210 |
-
archivo_pdf = st.file_uploader("Selecciona un archivo PDF", type=["pdf"])
|
| 211 |
-
|
| 212 |
-
texto_extraido = ""
|
| 213 |
-
if archivo_pdf:
|
| 214 |
-
texto_extraido = extraer_texto_pdf(archivo_pdf)
|
| 215 |
-
st.success("Texto extraído del PDF exitosamente.")
|
| 216 |
-
st.text_area("Texto extraído", value=texto_extraido, height=200)
|
| 217 |
-
|
| 218 |
-
if not texto_extraido:
|
| 219 |
-
texto_extraido = st.text_area("Texto extraído", height=200)
|
| 220 |
-
|
| 221 |
-
texto_preprocesado = preprocesar_texto(texto_extraido)
|
| 222 |
-
|
| 223 |
-
# --- Opciones de entrada de usuario ---
|
| 224 |
-
st.markdown("---")
|
| 225 |
-
pregunta_usuario = st.text_input("Escribe tu pregunta:")
|
| 226 |
-
if st.button("Enviar"):
|
| 227 |
-
if pregunta_usuario:
|
| 228 |
-
st.session_state.mensajes.append({"role": "user", "content": pregunta_usuario, "timestamp": time.time()})
|
| 229 |
-
with st.chat_message("user"):
|
| 230 |
-
st.markdown(pregunta_usuario)
|
| 231 |
-
|
| 232 |
-
with st.spinner("Generando respuesta..."):
|
| 233 |
-
respuesta, audio_path = obtener_respuesta(pregunta_usuario, texto_preprocesado, modelo="gpt-4", temperatura=0.5)
|
| 234 |
-
st.session_state.mensajes.append({"role": "assistant", "content": respuesta, "timestamp": time.time()})
|
| 235 |
-
with st.chat_message("assistant"):
|
| 236 |
-
st.markdown(respuesta)
|
| 237 |
-
st.audio(audio_path, format="audio/mp3", start_time=0)
|
| 238 |
-
|
| 239 |
-
guardar_historial(st.session_state.mensajes)
|
| 240 |
-
else:
|
| 241 |
-
st.warning("Por favor, ingresa una pregunta antes de enviar.")
|
| 242 |
-
|
| 243 |
elif pagina == "Agentes":
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 269 |
with st.chat_message("assistant"):
|
| 270 |
-
st.markdown(
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
if 'mensajes_agente' not in st.session_state:
|
| 275 |
-
st.session_state.mensajes_agente = []
|
| 276 |
-
|
| 277 |
-
for mensaje in st.session_state.mensajes_agente:
|
| 278 |
-
with st.chat_message(mensaje["role"]):
|
| 279 |
-
st.markdown(mensaje["content"])
|
| 280 |
-
|
| 281 |
-
agente_pregunta = st.text_input("Escribe tu pregunta para el agente:")
|
| 282 |
-
if st.button("Enviar al Agente"):
|
| 283 |
-
if agente_pregunta:
|
| 284 |
-
st.session_state.mensajes_agente.append({"role": "user", "content": agente_pregunta, "timestamp": time.time()})
|
| 285 |
-
with st.chat_message("user"):
|
| 286 |
-
st.markdown(agente_pregunta)
|
| 287 |
-
|
| 288 |
-
# Procesar la respuesta del agente
|
| 289 |
-
with st.spinner("El agente está respondiendo..."):
|
| 290 |
-
respuesta_agente, audio_path = obtener_respuesta(agente_pregunta, '', modelo="gpt-4", temperatura=0.5)
|
| 291 |
-
st.session_state.mensajes_agente.append({"role": "assistant", "content": respuesta_agente, "timestamp": time.time()})
|
| 292 |
-
with st.chat_message("assistant"):
|
| 293 |
-
st.markdown(respuesta_agente)
|
| 294 |
-
st.audio(audio_path, format="audio/mp3", start_time=0)
|
| 295 |
-
else:
|
| 296 |
-
st.warning("Por favor, ingresa una pregunta antes de enviar.")
|
| 297 |
|
| 298 |
if __name__ == "__main__":
|
| 299 |
main()
|
|
|
|
| 120 |
|
| 121 |
# --- Página principal ---
|
| 122 |
if pagina == "Chat":
|
| 123 |
+
mostrar_chat()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
elif pagina == "Subir PDF":
|
| 125 |
+
mostrar_subir_pdf()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
elif pagina == "Agentes":
|
| 127 |
+
mostrar_agentes()
|
| 128 |
+
else:
|
| 129 |
+
mostrar_principal()
|
| 130 |
+
|
| 131 |
+
def mostrar_chat():
|
| 132 |
+
# --- Botones de historial ---
|
| 133 |
+
if st.button("Buscar Historial"):
|
| 134 |
+
st.session_state.mostrar_historial = True
|
| 135 |
+
if st.button("Borrar Historial"):
|
| 136 |
+
st.session_state.mensajes = []
|
| 137 |
+
st.session_state.mostrar_historial = False
|
| 138 |
+
st.success("Historial borrado correctamente")
|
| 139 |
+
|
| 140 |
+
# --- Chatbot ---
|
| 141 |
+
if 'mensajes' not in st.session_state:
|
| 142 |
+
st.session_state.mensajes = cargar_historial()
|
| 143 |
+
|
| 144 |
+
for mensaje in st.session_state.mensajes:
|
| 145 |
+
with st.chat_message(mensaje["role"]):
|
| 146 |
+
st.markdown(mensaje["content"])
|
| 147 |
+
|
| 148 |
+
# Función para manejar la entrada de audio
|
| 149 |
+
def on_audio(audio_bytes):
|
| 150 |
+
with st.spinner("Transcribiendo..."):
|
| 151 |
+
transcript = openai.Audio.transcribe("whisper-1", audio_bytes)
|
| 152 |
+
pregunta_usuario = transcript["text"]
|
| 153 |
+
st.session_state.mensajes.append({"role": "user", "content": pregunta_usuario, "timestamp": time.time()})
|
| 154 |
+
with st.chat_message("user"):
|
| 155 |
+
st.markdown(pregunta_usuario)
|
| 156 |
+
|
| 157 |
+
st.subheader("🎤 Captura de voz")
|
| 158 |
+
st.info("Haz clic en el micrófono y comienza a hablar. Tu pregunta se transcribirá automáticamente.")
|
| 159 |
+
with st.container():
|
| 160 |
+
if st.button("Grabar 🎙️"):
|
| 161 |
+
st.session_state.run_webrtc = True
|
| 162 |
+
if st.session_state.get("run_webrtc", False):
|
| 163 |
+
webrtc_ctx = webrtc_streamer(
|
| 164 |
+
key="example",
|
| 165 |
+
mode=WebRtcMode.SENDONLY,
|
| 166 |
+
audio_receiver_size=256,
|
| 167 |
+
rtc_configuration={
|
| 168 |
+
"iceServers": [{"urls": ["stun:stun.l.google.com:19302"]}]
|
| 169 |
+
},
|
| 170 |
+
media_stream_constraints={"audio": True},
|
| 171 |
+
audio_processor_factory=AudioProcessor,
|
| 172 |
+
)
|
| 173 |
+
|
| 174 |
+
if webrtc_ctx.audio_receiver:
|
| 175 |
+
audio_frames = webrtc_ctx.audio_receiver.get_frames(timeout=1)
|
| 176 |
+
for audio_frame in audio_frames:
|
| 177 |
+
audio_bytes = audio_frame.to_ndarray().tobytes()
|
| 178 |
+
on_audio(audio_bytes)
|
| 179 |
+
|
| 180 |
+
st.markdown("---")
|
| 181 |
+
st.subheader("📄 Subir PDF")
|
| 182 |
+
st.info("Sube un archivo PDF y el asistente responderá en función de su contenido.")
|
| 183 |
+
archivo_pdf = st.file_uploader("Selecciona un archivo PDF", type=["pdf"])
|
| 184 |
+
|
| 185 |
+
texto_extraido = ""
|
| 186 |
+
if archivo_pdf:
|
| 187 |
+
texto_extraido = extraer_texto_pdf(archivo_pdf)
|
| 188 |
+
st.success("Texto extraído del PDF exitosamente.")
|
| 189 |
+
st.text_area("Texto extraído", value=texto_extraido, height=200)
|
| 190 |
+
|
| 191 |
+
if not texto_extraido:
|
| 192 |
+
texto_extraido = st.text_area("Texto extraído", height=200)
|
| 193 |
+
|
| 194 |
+
texto_preprocesado = preprocesar_texto(texto_extraido)
|
| 195 |
+
|
| 196 |
+
# --- Opciones de entrada de usuario ---
|
| 197 |
+
st.markdown("---")
|
| 198 |
+
pregunta_usuario = st.text_input("Escribe tu pregunta:")
|
| 199 |
+
if st.button("Enviar"):
|
| 200 |
+
if pregunta_usuario:
|
| 201 |
+
st.session_state.mensajes.append({"role": "user", "content": pregunta_usuario, "timestamp": time.time()})
|
| 202 |
+
with st.chat_message("user"):
|
| 203 |
+
st.markdown(pregunta_usuario)
|
| 204 |
+
|
| 205 |
+
with st.spinner("Generando respuesta..."):
|
| 206 |
+
respuesta, audio_path = obtener_respuesta(pregunta_usuario, texto_preprocesado, modelo="gpt-4", temperatura=0.5)
|
| 207 |
+
st.session_state.mensajes.append({"role": "assistant", "content": respuesta, "timestamp": time.time()})
|
| 208 |
+
with st.chat_message("assistant"):
|
| 209 |
+
st.markdown(respuesta)
|
| 210 |
+
st.audio(audio_path, format="audio/mp3", start_time=0, autoplay=True)
|
| 211 |
+
|
| 212 |
+
# Reproducir video automáticamente
|
| 213 |
+
st.video("https://www.canva.com/design/DAGJTK28LQ8/VJaWOIwiJcHVuEuTnVx_vA/edit?utm_content=DAGJTK28LQ8&utm_campaign=designshare&utm_medium=link2&utm_source=sharebutton", start_time=0)
|
| 214 |
+
|
| 215 |
+
guardar_historial(st.session_state.mensajes)
|
| 216 |
+
else:
|
| 217 |
+
st.warning("Por favor, ingresa una pregunta antes de enviar.")
|
| 218 |
+
|
| 219 |
+
def mostrar_subir_pdf():
|
| 220 |
+
st.subheader("📄 Subir PDF")
|
| 221 |
+
st.info("Sube un archivo PDF y el asistente responderá en función de su contenido.")
|
| 222 |
+
archivo_pdf = st.file_uploader("Selecciona un archivo PDF", type=["pdf"])
|
| 223 |
+
|
| 224 |
+
texto_extraido = ""
|
| 225 |
+
if archivo_pdf:
|
| 226 |
+
texto_extraido = extraer_texto_pdf(archivo_pdf)
|
| 227 |
+
st.success("Texto extraído del PDF exitosamente.")
|
| 228 |
+
st.text_area("Texto extraído", value=texto_extraido, height=200)
|
| 229 |
+
|
| 230 |
+
if not texto_extraido:
|
| 231 |
+
texto_extraido = st.text_area("Texto extraído", height=200)
|
| 232 |
+
|
| 233 |
+
texto_preprocesado = preprocesar_texto(texto_extraido)
|
| 234 |
+
|
| 235 |
+
# --- Opciones de entrada de usuario ---
|
| 236 |
+
st.markdown("---")
|
| 237 |
+
pregunta_usuario = st.text_input("Escribe tu pregunta:")
|
| 238 |
+
if st.button("Enviar"):
|
| 239 |
+
if pregunta_usuario:
|
| 240 |
+
st.session_state.mensajes.append({"role": "user", "content": pregunta_usuario, "timestamp": time.time()})
|
| 241 |
+
with st.chat_message("user"):
|
| 242 |
+
st.markdown(pregunta_usuario)
|
| 243 |
+
|
| 244 |
+
with st.spinner("Generando respuesta..."):
|
| 245 |
+
respuesta, audio_path = obtener_respuesta(pregunta_usuario, texto_preprocesado, modelo="gpt-4", temperatura=0.5)
|
| 246 |
+
st.session_state.mensajes.append({"role": "assistant", "content": respuesta, "timestamp": time.time()})
|
| 247 |
+
with st.chat_message("assistant"):
|
| 248 |
+
st.markdown(respuesta)
|
| 249 |
+
st.audio(audio_path, format="audio/mp3", start_time=0, autoplay=True)
|
| 250 |
+
|
| 251 |
+
guardar_historial(st.session_state.mensajes)
|
| 252 |
+
else:
|
| 253 |
+
st.warning("Por favor, ingresa una pregunta antes de enviar.")
|
| 254 |
+
|
| 255 |
+
def mostrar_agentes():
|
| 256 |
+
st.subheader("📋 Menú y Pedidos")
|
| 257 |
+
st.success("Menú cargado exitosamente. Listo para tomar pedidos.")
|
| 258 |
+
st.write(menu_df)
|
| 259 |
+
|
| 260 |
+
# Captura de pedido
|
| 261 |
+
st.markdown("Selecciona los items del menú:")
|
| 262 |
+
items_seleccionados = st.multiselect("Items", menu_df['item'].tolist())
|
| 263 |
+
|
| 264 |
+
if st.button("Tomar Pedido"):
|
| 265 |
+
if items_seleccionados:
|
| 266 |
+
pedido_usuario = ','.join(items_seleccionados)
|
| 267 |
+
confirmados = tomar_pedido_agente(pedido_usuario)
|
| 268 |
+
st.info(f"Pedido confirmado: {confirmados}")
|
| 269 |
+
|
| 270 |
+
total = procesar_orden_agente(','.join(confirmados))
|
| 271 |
+
st.success(f"Total del pedido: ${total}")
|
| 272 |
+
|
| 273 |
+
# Generar PDF de la orden
|
| 274 |
+
order_details = {item: {'price': menu_df[menu_df['item'] == item]['price'].values[0]} for item in confirmados}
|
| 275 |
+
pdf_path = generar_pdf_orden(order_details)
|
| 276 |
+
st.markdown(f"[Descargar PDF de la Orden]({pdf_path})", unsafe_allow_html=True)
|
| 277 |
+
|
| 278 |
+
# Generar mensaje automático
|
| 279 |
+
mensaje_automatico = generar_mensaje_automatico(confirmados)
|
| 280 |
+
st.session_state.mensajes_agente.append({"role": "assistant", "content": mensaje_automatico, "timestamp": time.time()})
|
| 281 |
+
with st.chat_message("assistant"):
|
| 282 |
+
st.markdown(mensaje_automatico)
|
| 283 |
+
|
| 284 |
+
# --- Chat para Agentes ---
|
| 285 |
+
st.subheader("Chat con Agentes")
|
| 286 |
+
if 'mensajes_agente' not in st.session_state:
|
| 287 |
+
st.session_state.mensajes_agente = []
|
| 288 |
+
|
| 289 |
+
for mensaje in st.session_state.mensajes_agente:
|
| 290 |
+
with st.chat_message(mensaje["role"]):
|
| 291 |
+
st.markdown(mensaje["content"])
|
| 292 |
+
|
| 293 |
+
agente_pregunta = st.text_input("Escribe tu pregunta para el agente:")
|
| 294 |
+
if st.button("Enviar al Agente"):
|
| 295 |
+
if agente_pregunta:
|
| 296 |
+
st.session_state.mensajes_agente.append({"role": "user", "content": agente_pregunta, "timestamp": time.time()})
|
| 297 |
+
with st.chat_message("user"):
|
| 298 |
+
st.markdown(agente_pregunta)
|
| 299 |
+
|
| 300 |
+
# Procesar la respuesta del agente
|
| 301 |
+
with st.spinner("El agente está respondiendo..."):
|
| 302 |
+
respuesta_agente, audio_path = obtener_respuesta(agente_pregunta, '', modelo="gpt-4", temperatura=0.5)
|
| 303 |
+
st.session_state.mensajes_agente.append({"role": "assistant", "content": respuesta_agente, "timestamp": time.time()})
|
| 304 |
+
with st.chat_message("assistant"):
|
| 305 |
+
st.markdown(respuesta_agente)
|
| 306 |
+
st.audio(audio_path, format="audio/mp3", start_time=0, autoplay=True)
|
| 307 |
+
else:
|
| 308 |
+
st.warning("Por favor, ingresa una pregunta antes de enviar.")
|
| 309 |
+
|
| 310 |
+
def mostrar_principal():
|
| 311 |
+
st.subheader("🗣️ Asistente Virtual con Chat y Respuestas en Voz")
|
| 312 |
+
st.video("", start_time=0)
|
| 313 |
+
|
| 314 |
+
if 'mensajes' not in st.session_state:
|
| 315 |
+
st.session_state.mensajes = []
|
| 316 |
+
|
| 317 |
+
for mensaje in st.session_state.mensajes:
|
| 318 |
+
with st.chat_message(mensaje["role"]):
|
| 319 |
+
st.markdown(mensaje["content"])
|
| 320 |
+
|
| 321 |
+
pregunta_usuario = st.text_input("Escribe tu pregunta:")
|
| 322 |
+
if st.button("Enviar"):
|
| 323 |
+
if pregunta_usuario:
|
| 324 |
+
st.session_state.mensajes.append({"role": "user", "content": pregunta_usuario, "timestamp": time.time()})
|
| 325 |
+
with st.chat_message("user"):
|
| 326 |
+
st.markdown(pregunta_usuario)
|
| 327 |
+
|
| 328 |
+
with st.spinner("Generando respuesta..."):
|
| 329 |
+
respuesta, audio_path = obtener_respuesta(pregunta_usuario, '', modelo="gpt-4", temperatura=0.5)
|
| 330 |
+
st.session_state.mensajes.append({"role": "assistant", "content": respuesta, "timestamp": time.time()})
|
| 331 |
with st.chat_message("assistant"):
|
| 332 |
+
st.markdown(respuesta)
|
| 333 |
+
st.audio(audio_path, format="audio/mp3", start_time=0, autoplay=True)
|
| 334 |
+
else:
|
| 335 |
+
st.warning("Por favor, ingresa una pregunta antes de enviar.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 336 |
|
| 337 |
if __name__ == "__main__":
|
| 338 |
main()
|