-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
executable file
·206 lines (161 loc) · 8.07 KB
/
app.py
File metadata and controls
executable file
·206 lines (161 loc) · 8.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import streamlit as st
from openai import OpenAI
from streamlit_js_eval import streamlit_js_eval
st.set_page_config(
page_title="Interview Bot",
page_icon="icon.png"
)
# Setting up the Streamlit page configuration
col1, col2 = st.columns([1, 5])
with col1:
st.image("icon.png", width=80)
with col2:
st.markdown("## AI Interviewer — Prototype🚧")
# 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
def show_feedback():
st.session_state.feedback_shown = True
# Setup stage for collecting user details
if not st.session_state.setup_complete:
st.subheader('Personal Information')
# Initialize session state for personal information
if "name" not in st.session_state:
st.session_state["name"] = ""
if "experience" not in st.session_state:
st.session_state["experience"] = ""
if "skills" not in st.session_state:
st.session_state["skills"] = ""
# Get personal information input
st.session_state["name"] = st.text_input(label="Name", value=st.session_state["name"], placeholder="Enter your name", max_chars=40)
st.session_state["experience"] = st.text_area(label="Experience", value=st.session_state["experience"], placeholder="Describe your experience", max_chars=200)
st.session_state["skills"] = st.text_area(label="Skills", value=st.session_state["skills"], placeholder="List your skills", max_chars=200)
# Company and Position Section
st.subheader('Company and Position')
# Initialize session state for company and position information and setting default values
if "level" not in st.session_state:
st.session_state["level"] = "Junior"
if "position" not in st.session_state:
st.session_state["position"] = "Data Scientist"
if "company" not in st.session_state:
st.session_state["company"] = "Asyst"
col1, col2 = st.columns(2)
with col1:
st.session_state["level"] = st.radio(
"Choose level",
key="visibility",
options=["Junior", "Mid-level", "Senior"],
index=["Junior", "Mid-level", "Senior"].index(st.session_state["level"])
)
with col2:
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(st.session_state["position"])
)
st.session_state["company"] = st.selectbox(
"Select a Company",
("Asyst", "Amazon", "Meta", "MediaBox", "365 Company", "Nestle", "LinkedIn", "Spotify"),
index=("Asyst", "Amazon", "Meta", "MediaBox", "365 Company", "Nestle", "LinkedIn", "Spotify").index(st.session_state["company"])
)
# 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.feedback_shown and not st.session_state.chat_complete:
st.info(
"""
Start by introducing yourself
""",
icon="👋🏾",
)
# Check if API key is empty or missing
if "OPENAI_API_KEY" not in st.secrets or not st.secrets["OPENAI_API_KEY"].strip():
st.warning(
f"⚠️ Hey {st.session_state['name']} ! This prototype isn’t fully wired yet.\n\n"
"I need an OpenAI API key to actually run the interview. "
"For now, feel free to explore the UI — the magic happens once the key is added🤖🔥."
)
st.stop()
# 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 executive that interviews an interviewee called {st.session_state['name']} "
f"with experience {st.session_state['experience']} and skills {st.session_state['skills']}. "
f"You should interview him for the position {st.session_state['level']} {st.session_state['position']} "
f"at 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
# Show "Get Feedback"
if st.session_state.chat_complete and not st.session_state.feedback_shown:
if st.button("Get Feedback", on_click=show_feedback):
st.write("Fetching feedback...")
# Show feedback screen
if st.session_state.feedback_shown:
st.subheader("Feedback")
conversation_history = "\n".join([f"{msg['role']}: {msg['content']}" for msg in st.session_state.messages])
# Initialize new OpenAI client instance for feedback
feedback_client = OpenAI(api_key=st.secrets["OPENAI_API_KEY"])
# Generate feedback using the stored messages and write a system prompt for the feedback
feedback_completion = feedback_client.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "system", "content": """You are a helpful tool that provides feedback on an interviewee performance.
Before the Feedback give a score of 1 to 10.
Follow this format:
Overal Score: //Your score
Feedback: //Here you put your feedback
Give only the feedback do not ask any additional questins.
"""},
{"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}"}
]
)
st.write(feedback_completion.choices[0].message.content)
# Button to restart the interview
if st.button("Restart Interview", type="primary"):
streamlit_js_eval(js_expressions="parent.window.location.reload()")