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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from openai import OpenAI
|
| 3 |
+
from streamlit_js_eval import streamlit_js_eval
|
| 4 |
+
|
| 5 |
+
# Setting up the Streamlit page configuration
|
| 6 |
+
st.set_page_config(page_title="StreamlitChatMessageHistory", page_icon="💬")
|
| 7 |
+
st.title("Chatbot")
|
| 8 |
+
|
| 9 |
+
# Initialize session state variables
|
| 10 |
+
if "setup_complete" not in st.session_state:
|
| 11 |
+
st.session_state.setup_complete = False
|
| 12 |
+
if "user_message_count" not in st.session_state:
|
| 13 |
+
st.session_state.user_message_count = 0
|
| 14 |
+
if "feedback_shown" not in st.session_state:
|
| 15 |
+
st.session_state.feedback_shown = False
|
| 16 |
+
if "chat_complete" not in st.session_state:
|
| 17 |
+
st.session_state.chat_complete = False
|
| 18 |
+
if "messages" not in st.session_state:
|
| 19 |
+
st.session_state.messages = []
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
# Helper functions to update session state
|
| 23 |
+
def complete_setup():
|
| 24 |
+
st.session_state.setup_complete = True
|
| 25 |
+
|
| 26 |
+
def show_feedback():
|
| 27 |
+
st.session_state.feedback_shown = True
|
| 28 |
+
|
| 29 |
+
# Setup stage for collecting user details
|
| 30 |
+
if not st.session_state.setup_complete:
|
| 31 |
+
st.subheader('Personal Information')
|
| 32 |
+
|
| 33 |
+
# Initialize session state for personal information
|
| 34 |
+
if "name" not in st.session_state:
|
| 35 |
+
st.session_state["name"] = ""
|
| 36 |
+
if "experience" not in st.session_state:
|
| 37 |
+
st.session_state["experience"] = ""
|
| 38 |
+
if "skills" not in st.session_state:
|
| 39 |
+
st.session_state["skills"] = ""
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
# Get personal information input
|
| 43 |
+
st.session_state["name"] = st.text_input(label="Name", value=st.session_state["name"], placeholder="Enter your name", max_chars=40)
|
| 44 |
+
st.session_state["experience"] = st.text_area(label="Experience", value=st.session_state["experience"], placeholder="Describe your experience", max_chars=200)
|
| 45 |
+
st.session_state["skills"] = st.text_area(label="Skills", value=st.session_state["skills"], placeholder="List your skills", max_chars=200)
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
# Company and Position Section
|
| 49 |
+
st.subheader('Company and Position')
|
| 50 |
+
|
| 51 |
+
# Initialize session state for company and position information and setting default values
|
| 52 |
+
if "level" not in st.session_state:
|
| 53 |
+
st.session_state["level"] = "Junior"
|
| 54 |
+
if "position" not in st.session_state:
|
| 55 |
+
st.session_state["position"] = "Data Scientist"
|
| 56 |
+
if "company" not in st.session_state:
|
| 57 |
+
st.session_state["company"] = "Amazon"
|
| 58 |
+
|
| 59 |
+
col1, col2 = st.columns(2)
|
| 60 |
+
with col1:
|
| 61 |
+
st.session_state["level"] = st.radio(
|
| 62 |
+
"Choose level",
|
| 63 |
+
key="visibility",
|
| 64 |
+
options=["Junior", "Mid-level", "Senior"],
|
| 65 |
+
index=["Junior", "Mid-level", "Senior"].index(st.session_state["level"])
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
with col2:
|
| 69 |
+
st.session_state["position"] = st.selectbox(
|
| 70 |
+
"Choose a position",
|
| 71 |
+
("Data Scientist", "Data Engineer", "ML Engineer", "BI Analyst", "Financial Analyst"),
|
| 72 |
+
index=("Data Scientist", "Data Engineer", "ML Engineer", "BI Analyst", "Financial Analyst").index(st.session_state["position"])
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
st.session_state["company"] = st.selectbox(
|
| 76 |
+
"Select a Company",
|
| 77 |
+
("Amazon", "Meta", "Udemy", "365 Company", "Nestle", "LinkedIn", "Spotify"),
|
| 78 |
+
index=("Amazon", "Meta", "Udemy", "365 Company", "Nestle", "LinkedIn", "Spotify").index(st.session_state["company"])
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
# Button to complete setup
|
| 84 |
+
if st.button("Start Interview", on_click=complete_setup):
|
| 85 |
+
st.write("Setup complete. Starting interview...")
|
| 86 |
+
|
| 87 |
+
# Interview phase
|
| 88 |
+
if st.session_state.setup_complete and not st.session_state.feedback_shown and not st.session_state.chat_complete:
|
| 89 |
+
|
| 90 |
+
st.info(
|
| 91 |
+
"""
|
| 92 |
+
Start by introducing yourself
|
| 93 |
+
""",
|
| 94 |
+
icon="👋",
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
+
# Initialize OpenAI client
|
| 98 |
+
client = OpenAI(api_key=st.secrets["OPENAI_API_KEY"])
|
| 99 |
+
|
| 100 |
+
# Setting OpenAI model if not already initialized
|
| 101 |
+
if "openai_model" not in st.session_state:
|
| 102 |
+
st.session_state["openai_model"] = "gpt-4o"
|
| 103 |
+
|
| 104 |
+
# Initializing the system prompt for the chatbot
|
| 105 |
+
if not st.session_state.messages:
|
| 106 |
+
st.session_state.messages = [{
|
| 107 |
+
"role": "system",
|
| 108 |
+
"content": (f"You are an HR executive that interviews an interviewee called {st.session_state['name']} "
|
| 109 |
+
f"with experience {st.session_state['experience']} and skills {st.session_state['skills']}. "
|
| 110 |
+
f"You should interview him for the position {st.session_state['level']} {st.session_state['position']} "
|
| 111 |
+
f"at the company {st.session_state['company']}")
|
| 112 |
+
}]
|
| 113 |
+
|
| 114 |
+
# Display chat messages
|
| 115 |
+
for message in st.session_state.messages:
|
| 116 |
+
if message["role"] != "system":
|
| 117 |
+
with st.chat_message(message["role"]):
|
| 118 |
+
st.markdown(message["content"])
|
| 119 |
+
|
| 120 |
+
# Handle user input and OpenAI response
|
| 121 |
+
# Put a max_chars limit
|
| 122 |
+
if st.session_state.user_message_count < 5:
|
| 123 |
+
if prompt := st.chat_input("Your response", max_chars=1000):
|
| 124 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 125 |
+
with st.chat_message("user"):
|
| 126 |
+
st.markdown(prompt)
|
| 127 |
+
|
| 128 |
+
if st.session_state.user_message_count < 4:
|
| 129 |
+
with st.chat_message("assistant"):
|
| 130 |
+
stream = client.chat.completions.create(
|
| 131 |
+
model=st.session_state["openai_model"],
|
| 132 |
+
messages=[
|
| 133 |
+
{"role": m["role"], "content": m["content"]}
|
| 134 |
+
for m in st.session_state.messages
|
| 135 |
+
],
|
| 136 |
+
stream=True,
|
| 137 |
+
)
|
| 138 |
+
response = st.write_stream(stream)
|
| 139 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
| 140 |
+
|
| 141 |
+
# Increment the user message count
|
| 142 |
+
st.session_state.user_message_count += 1
|
| 143 |
+
|
| 144 |
+
# Check if the user message count reaches 5
|
| 145 |
+
if st.session_state.user_message_count >= 5:
|
| 146 |
+
st.session_state.chat_complete = True
|
| 147 |
+
|
| 148 |
+
# Show "Get Feedback"
|
| 149 |
+
if st.session_state.chat_complete and not st.session_state.feedback_shown:
|
| 150 |
+
if st.button("Get Feedback", on_click=show_feedback):
|
| 151 |
+
st.write("Fetching feedback...")
|
| 152 |
+
|
| 153 |
+
# Show feedback screen
|
| 154 |
+
if st.session_state.feedback_shown:
|
| 155 |
+
st.subheader("Feedback")
|
| 156 |
+
|
| 157 |
+
conversation_history = "\n".join([f"{msg['role']}: {msg['content']}" for msg in st.session_state.messages])
|
| 158 |
+
|
| 159 |
+
# Initialize new OpenAI client instance for feedback
|
| 160 |
+
feedback_client = OpenAI(api_key=st.secrets["OPENAI_API_KEY"])
|
| 161 |
+
|
| 162 |
+
# Generate feedback using the stored messages and write a system prompt for the feedback
|
| 163 |
+
feedback_completion = feedback_client.chat.completions.create(
|
| 164 |
+
model="gpt-4o",
|
| 165 |
+
messages=[
|
| 166 |
+
{"role": "system", "content": """You are a helpful tool that provides feedback on an interviewee performance.
|
| 167 |
+
Before the Feedback give a score of 1 to 10.
|
| 168 |
+
Follow this format:
|
| 169 |
+
Overal Score: //Your score
|
| 170 |
+
Feedback: //Here you put your feedback
|
| 171 |
+
Give only the feedback do not ask any additional questins.
|
| 172 |
+
"""},
|
| 173 |
+
{"role": "user", "content": f"This is the interview you need to evaluate. Keep in mind that you are only a tool. And you shouldn't engage in any converstation: {conversation_history}"}
|
| 174 |
+
]
|
| 175 |
+
)
|
| 176 |
+
|
| 177 |
+
st.write(feedback_completion.choices[0].message.content)
|
| 178 |
+
|
| 179 |
+
# Button to restart the interview
|
| 180 |
+
if st.button("Restart Interview", type="primary"):
|
| 181 |
+
streamlit_js_eval(js_expressions="parent.window.location.reload()")
|