TravelCompanion / app.py
AshokReddy's picture
Update app.py
0f55a04
raw
history blame
No virus
2.18 kB
## Importing Libraries
import streamlit as st
from streamlit_chat import message
from utils import get_initial_message, get_chatgpt_response, update_chat
import openai
## Retrieving the openAI Key from secrets.toml
openai.api_key = st.secrets["api_secret"]
##Page Config
st.set_page_config(page_title= 'Travel Advisor',
page_icon= 'page_icon.jpg')
st.title('AI ChatBot - Travel')
st.subheader('powered by CloudHub (https://cloudhubs.nl/)')
st.markdown("Ask about Travel")
##Choosing the GPT Model
model = "gpt-3.5-turbo"
#model = "gpt-4"
##Initializing Session
if 'generated' not in st.session_state:
st.session_state['generated'] = []
if 'past' not in st.session_state:
st.session_state['past'] = []
if 'messages' not in st.session_state:
st.session_state['messages'] = get_initial_message()
##Chat Display and expand functionality
if st.session_state['generated']:
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))
# Initialize reset_input flag
if 'reset_input' not in st.session_state:
st.session_state['reset_input'] = False
# Place the user input box and the button at the bottom
# Use reset_input flag to determine the default value of the input box
default_value = "" if st.session_state['reset_input'] else st.session_state.get('input', '')
query = st.text_input('Query: ', value=default_value, key='input')
clicked = st.button('Chat!')
##Process the user input with a spinner graph
if clicked:
with st.spinner("generating..."):
messages = st.session_state['messages']
messages = update_chat(messages, "user", query)
response = get_chatgpt_response(messages, model)
messages = update_chat(messages, "assistant", response)
st.session_state.past.append(query)
st.session_state.generated.append(response)
# Indicate the need to reset the input box on the next render
st.session_state['reset_input'] = True
st.experimental_rerun()
else:
# If not clicked, reset the flag
st.session_state['reset_input'] = False