osman93 commited on
Commit
7219ca8
·
verified ·
1 Parent(s): fb4ad29

Update app.py

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