import tempfile import openai import streamlit as st from streamlit_chat import message from audio_recorder_streamlit import audio_recorder import os from dotenv import load_dotenv from speech_to_text import transcribe_speech_recognition, transcribe_whisper from utils import ( get_initial_message, get_chatgpt_response, update_chat, ) # Carga las claves load_dotenv() openai.api_key = os.getenv("OPENAI_API_KEY") # Streamlit Application def main(): st.title("Smart Form") st.markdown( """ Hackaton 2023 - Team Spaidermen """ ) if 'messages' not in st.session_state: with st.spinner("Inicializando nuevo entorno..."): st.session_state['messages'] = get_initial_message() if st.session_state['messages']: for i, msg in enumerate(st.session_state['messages']): if msg['role'] == 'user': message(msg['content'], is_user=True, key=str(i)) else: message(msg['content'], key=str(i)) # Inicializar el texto transcrito como vacĂ­o if 'transcribed_text' not in st.session_state: st.session_state['transcribed_text'] = '' # Grabar audio st.session_state['text'] = '' audio_bytes = audio_recorder(pause_threshold=5, sample_rate=16_000) if audio_bytes: with st.spinner('Transcribiendo...'): # Guardar audio grabado en un archivo temporal with tempfile.NamedTemporaryFile(suffix='.wav', delete=False) as temp_audio: temp_path = temp_audio.name temp_audio.write(audio_bytes) text = transcribe_speech_recognition(temp_path) print(f"Texto transcrito: {text}") # Actualizar el valor de transcribed_text con el texto transcrito st.session_state['transcribed_text'] = text # Mostrar el texto transcrito en el textarea query = st.text_input("Ingresa tu texto", value=st.session_state['transcribed_text']) if st.button("Enviar") and query: with st.spinner("Enviando mensaje..."): st.session_state['messages'] = update_chat(st.session_state['messages'], "user", query) response = get_chatgpt_response(st.session_state['messages']) st.session_state['messages'] = update_chat(st.session_state['messages'], "assistant", response) if __name__ == "__main__": main()