|
import streamlit as st |
|
from openai import OpenAI |
|
|
|
|
|
st.set_page_config(page_title="StreamlitChatMessageHistory", page_icon="💬") |
|
st.title("Chatbot") |
|
|
|
|
|
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 = [] |
|
|
|
|
|
|
|
def complete_setup(): |
|
st.session_state.setup_complete = True |
|
|
|
|
|
if not st.session_state.setup_complete: |
|
|
|
st.subheader('Personal Information afaefaef') |
|
|
|
|
|
st.session_state["name"] = st.text_input(label="Name", value="", placeholder="Enter your name", max_chars=40) |
|
|
|
|
|
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") |
|
) |
|
|
|
|
|
if st.button("Start Interview", on_click=complete_setup): |
|
st.write("Setup complete. Starting interview...") |
|
|
|
|
|
if st.session_state.setup_complete and not st.session_state.chat_complete: |
|
|
|
|
|
client = OpenAI(api_key=st.secrets["OPENAI_API_KEY"]) |
|
|
|
|
|
if "openai_model" not in st.session_state: |
|
st.session_state["openai_model"] = "gpt-4o" |
|
|
|
|
|
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']}") |
|
}] |
|
|
|
|
|
for message in st.session_state.messages: |
|
if message["role"] != "system": |
|
with st.chat_message(message["role"]): |
|
st.markdown(message["content"]) |
|
|
|
|
|
|
|
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}) |
|
|
|
|
|
st.session_state.user_message_count += 1 |
|
|
|
|
|
if st.session_state.user_message_count >= 5: |
|
st.session_state.chat_complete = True |