deneme / app.py
osman93's picture
Update app.py
8cf160b verified
import streamlit as st
from openai import OpenAI
# Setting up the Streamlit page configuration
st.set_page_config(page_title="StreamlitChatMessageHistory", page_icon="💬")
st.title("Chatbot")
# Initialize session state variables
if "setup_complete" not in st.session_state:
st.session_state.setup_complete = False
if "user_message_count" not in st.session_state:
st.session_state.user_message_count = 0
if "feedback_shown" not in st.session_state:
st.session_state.feedback_shown = False
if "chat_complete" not in st.session_state:
st.session_state.chat_complete = False
if "messages" not in st.session_state:
st.session_state.messages = []
# Helper functions to update session state
def complete_setup():
st.session_state.setup_complete = True
# Setup stage for collecting user details
if not st.session_state.setup_complete:
# st.subheader('Personal Information')
st.subheader('Personal Information afaefaef')
# Get personal information input
st.session_state["name"] = st.text_input(label="Name", value="", placeholder="Enter your name", max_chars=40)
# Company and Position Section
st.subheader('Company and Position')
st.session_state["position"] = st.selectbox(
"Choose a position",
("Data Scientist", "Data Engineer", "ML Engineer", "BI Analyst", "Financial Analyst"),
index=("Data Scientist", "Data Engineer", "ML Engineer", "BI Analyst", "Financial Analyst").index("Data Scientist")
)
st.session_state["company"] = st.selectbox(
"Select a Company",
("Amazon", "Meta", "Udemy", "365 Company", "Nestle", "LinkedIn", "Spotify"),
index=("Amazon", "Meta", "Udemy", "365 Company", "Nestle", "LinkedIn", "Spotify").index("Amazon")
)
# Button to complete setup
if st.button("Start Interview", on_click=complete_setup):
st.write("Setup complete. Starting interview...")
# Interview phase
if st.session_state.setup_complete and not st.session_state.chat_complete:
# Initialize OpenAI client
client = OpenAI(api_key=st.secrets["OPENAI_API_KEY"])
# Setting OpenAI model if not already initialized
if "openai_model" not in st.session_state:
st.session_state["openai_model"] = "gpt-4o"
# Initializing the system prompt for the chatbot
if not st.session_state.messages:
st.session_state.messages = [{
"role": "system",
"content": (f"You are an HR that interviews {st.session_state['name']}. You should interview him for the "
f"{st.session_state['position']} position in the company {st.session_state['company']}")
}]
# Display chat messages
for message in st.session_state.messages:
if message["role"] != "system":
with st.chat_message(message["role"]):
st.markdown(message["content"])
# Handle user input and OpenAI response
# Put a max_chars limit
if st.session_state.user_message_count < 5:
if prompt := st.chat_input("Your response", max_chars=1000):
st.session_state.messages.append({"role": "user", "content": prompt})
with st.chat_message("user"):
st.markdown(prompt)
if st.session_state.user_message_count < 4:
with st.chat_message("assistant"):
stream = client.chat.completions.create(
model=st.session_state["openai_model"],
messages=[{"role": m["role"], "content": m["content"]} for m in st.session_state.messages],
stream=True,
)
response = st.write_stream(stream)
st.session_state.messages.append({"role": "assistant", "content": response})
# Increment the user message count
st.session_state.user_message_count += 1
# Check if the user message count reaches 5
if st.session_state.user_message_count >= 5:
st.session_state.chat_complete = True