Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| def mostrar_galatea_asistente(): | |
| # Inicializar el estado de la pregunta si no existe | |
| if 'pregunta' not in st.session_state: | |
| st.session_state.pregunta = "" | |
| # Configuración en columnas | |
| col1, col2 = st.columns([2, 1]) | |
| with col1: | |
| pregunta = st.text_input( | |
| "💬 Escribe tu pregunta para Galatea:", | |
| value=st.session_state.pregunta, | |
| placeholder="Ejemplo: ¿Cuáles son los horarios de atención?", | |
| key="pregunta_input" | |
| ) | |
| st.session_state.pregunta = pregunta # Actualizar el estado con la pregunta actual | |
| with col2: | |
| st.markdown("### 🎛️ Configuración") | |
| voz_seleccionada = st.selectbox( | |
| "🎤 Voz OpenAI:", | |
| ["nova", "alloy", "echo", "fable", "onyx", "shimmer"], | |
| index=0, | |
| help="Nova: Femenina joven | Alloy: Neutral | Echo: Masculina" | |
| ) | |
| modelo_seleccionado = st.selectbox( | |
| "🤖 Modelo:", | |
| ["gpt-4o-mini", "gpt-3.5-turbo", "gpt-4"], | |
| index=0 | |
| ) | |
| # Botones de acción | |
| col_btn1, col_btn2, col_btn3 = st.columns(3) | |
| with col_btn1: | |
| if st.button("✨ Mejorar Prompt", use_container_width=True): | |
| if st.session_state.pregunta: | |
| with st.spinner("🔄 Mejorando pregunta..."): | |
| pregunta_mejorada = enhance_prompt_with_ai(st.session_state.pregunta) | |
| st.session_state.pregunta = pregunta_mejorada | |
| st.rerun() | |
| else: | |
| st.warning("⚠️ Por favor, escribe una pregunta primero.") | |
| with col_btn2: | |
| if st.button("🚀 Respuesta Inmediata", use_container_width=True): | |
| if st.session_state.pregunta: | |
| procesar_respuesta() | |
| else: | |
| st.warning("⚠️ Por favor, escribe una pregunta primero.") | |
| with col_btn3: | |
| st.markdown("### ⏱️ Temporizador") | |
| intervalo = st.slider("Segundos:", 5, 60, 10) | |
| if st.button("⏱️ Iniciar Temporizador", use_container_width=True): | |
| if st.session_state.pregunta: | |
| reloj_placeholder = st.empty() | |
| for i in range(intervalo, 0, -1): | |
| reloj_placeholder.markdown( | |
| f""" | |
| <div style="text-align: center; padding: 2rem; background: linear-gradient(135deg, #ff6b6b, #ee5a24); border-radius: 15px; margin: 1rem 0;"> | |
| <h2 style="color: white; margin: 0;">⏳ Tiempo restante: {i} segundos</h2> | |
| <p style="color: #ffe8e8; margin: 0;">Preparando respuesta de Galatea...</p> | |
| </div> | |
| """, | |
| unsafe_allow_html=True | |
| ) | |
| time.sleep(1) | |
| reloj_placeholder.markdown( | |
| """ | |
| <div style="text-align: center; padding: 2rem; background: linear-gradient(135deg, #00b894, #00a085); border-radius: 15px; margin: 1rem 0;"> | |
| <h2 style="color: white; margin: 0;">✅ ¡Tiempo finalizado!</h2> | |
| <p style="color: #e8f8f5; margin: 0;">Galatea está respondiendo...</p> | |
| </div> | |
| """, | |
| unsafe_allow_html=True | |
| ) | |
| procesar_respuesta() | |
| else: | |
| st.warning("⚠️ Por favor, escribe una pregunta primero.") | |
| def procesar_respuesta(): | |
| if st.session_state.pregunta: | |
| with st.spinner("🤖 Generando respuesta y audio con OpenAI TTS..."): | |
| respuesta, audio_base64 = obtener_respuesta_openai( | |
| st.session_state.pregunta, | |
| modelo=modelo_seleccionado, | |
| voz=voz_seleccionada | |
| ) | |
| # Mostrar respuesta | |
| st.markdown("---") | |
| st.markdown( | |
| f""" | |
| <div class="success-box"> | |
| <h3>🤖 Galatea responde:</h3> | |
| <p style="font-size: 1.1em; line-height: 1.6;">{respuesta}</p> | |
| </div> | |
| """, | |
| unsafe_allow_html=True | |
| ) | |
| if audio_base64: | |
| st.markdown("### 🎵 Audio generado con OpenAI TTS:") | |
| st.markdown( | |
| f""" | |
| <div style="background: #f0f8ff; padding: 1rem; border-radius: 10px; margin: 1rem 0;"> | |
| <audio autoplay controls style="width: 100%;"> | |
| <source src="data:audio/mp3;base64,{audio_base64}" type="audio/mp3"> | |
| Tu navegador no soporta el elemento de audio. | |
| </audio> | |
| <p style="margin-top: 0.5rem; font-size: 0.9em; color: #666;"> | |
| 🎤 Voz: <strong>{voz_seleccionada}</strong> | | |
| 🤖 Modelo: <strong>{modelo_seleccionado}</strong> | |
| </p> | |
| </div> | |
| """, unsafe_allow_html=True) | |
| else: | |
| st.error("❌ No se pudo generar el audio") | |