File size: 2,578 Bytes
e7ef0c0
 
 
 
 
 
 
 
 
 
f79eaae
3eb7ca4
7460231
 
 
 
 
 
e7ef0c0
 
 
 
 
9eba268
 
e7ef0c0
 
 
f79eaae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e7ef0c0
9eba268
 
 
 
 
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 streamlit as st
from streamlit_chat import message
from utils import get_initial_message, get_chatgpt_response, update_chat
import os
from dotenv import load_dotenv
load_dotenv()
import openai

openai.api_key = os.getenv('openai_key')

st.title("ChileanGPT 2.0: An Experimental Chilean AI Language Model")
st.subheader("Leveraging the Power of GPT-Enhanced for Natural Chilean Spanish Dialogues")
hide_footer_style = """
    <style>
    .main footer {visibility: hidden;}
    </style>
    """
st.markdown(hide_footer_style, unsafe_allow_html=True)

if 'generated' not in st.session_state:
    st.session_state['generated'] = []
if 'past' not in st.session_state:
    st.session_state['past'] = []
if 'interaction_count' not in st.session_state:
    st.session_state['interaction_count'] = 0
if 'messages' not in st.session_state:
    st.session_state['messages'] = get_initial_message()
 

def generate_response(_input_user):
    if _input_user:
        with st.spinner("pensando..."):
            st.session_state['interaction_count'] += 1
            messages = st.session_state['messages']
            messages = update_chat(messages, "user", _input_user, st.session_state['interaction_count'])
            # st.write("Before  making the API call")
            # st.write(messages)
            response = get_chatgpt_response(messages,"gpt-4")
            messages = update_chat(messages, "assistant", response, st.session_state['interaction_count'])
    return response

# container for chat history
response_container = st.container()
# container for text box
container = st.container()

with container:
    with st.form("chat_input", clear_on_submit=True):
        a, b = st.columns([4, 1])
        user_input = a.text_input(
            label="Mensaje:",
            placeholder="Escribe algo...",
            label_visibility="collapsed",
        )
        submit_button = b.form_submit_button("Send", use_container_width=True)
        if user_input or submit_button:
            output = generate_response(user_input)
            st.session_state.generated.append(output)
            st.session_state.past.append(user_input)
    
    if st.session_state['generated']:
        with response_container:
            for i in range(len(st.session_state['generated'])):
                message(st.session_state['past'][i], is_user=True, key=str(i) + '_user')
                message(st.session_state["generated"][i], key=str(i))

# with st.expander("session_state"):
#          st.write(st.session_state)

# with st.expander("Mensajes"):
#          st.write(messages)