ChileanGPT / app.py
Matias Stager
Tempalte update 3
0c087fd
raw
history blame
2.58 kB
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("Enviar", 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)