File size: 2,387 Bytes
1d0f027
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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()