pcdivakar commited on
Commit
0f0d9b7
·
1 Parent(s): 03c61c8

Upload 2 files

Browse files
Files changed (2) hide show
  1. requirements.txt +4 -0
  2. streamlit.py +217 -0
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ gTTS==2.3.2
2
+ openai==0.28.0
3
+ pygame==2.5.1
4
+ SpeechRecognition==3.10.0
streamlit.py ADDED
@@ -0,0 +1,217 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import openai
2
+ from gtts import gTTS
3
+ import speech_recognition as sr
4
+ import streamlit as st
5
+ import random
6
+ import time
7
+ import pygame
8
+ import io
9
+
10
+ # Set up your OpenAI API key
11
+ openai.api_key = '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'
12
+
13
+ # Function to get technical keywords using OpenAI
14
+ def get_technical_keywords(job_description, skills):
15
+ conversation = [
16
+ {"role": "system", "content": "You are a helpful assistant that extracts technical keywords."},
17
+ {"role": "user", "content": f"Extract technical keywords from job description: {job_description}\nSkills: {skills}"}
18
+ ]
19
+
20
+ response = openai.ChatCompletion.create(
21
+ model="gpt-3.5-turbo-0613",
22
+ messages=conversation
23
+ )
24
+
25
+ extracted_keywords = response.choices[0].message.content.strip()
26
+ return extracted_keywords
27
+
28
+ # Function to format and display keywords
29
+ def display_keywords(keywords):
30
+ st.subheader("Extracted Keywords:")
31
+ st.write(keywords)
32
+
33
+ # Function to generate a single technical question based on a keyword and job role
34
+ def generate_question(keyword, job_role):
35
+ conversation = [
36
+ {"role": "system", "content": "You are a helpful assistant that generates technical questions."},
37
+ {"role": "user", "content": f"Generate a technical question for the job role of {job_role} based on the keyword: {keyword}"}
38
+ ]
39
+
40
+ response = openai.ChatCompletion.create(
41
+ model="gpt-3.5-turbo-16k-0613",
42
+ messages=conversation,
43
+ max_tokens=1000 # Adjust the token limit as needed
44
+ )
45
+
46
+ generated_question = response.choices[0].message.content.strip()
47
+ return generated_question
48
+
49
+ # Function to format and display feedback
50
+ def display_feedback(feedback):
51
+ st.subheader("Feedback:")
52
+ st.write(feedback)
53
+
54
+ # Function to generate a follow-up question based on the user's answer using ChatGPT
55
+ def get_follow_up_question(user_answer):
56
+ conversation = [
57
+ {"role": "system", "content": "You are a helpful assistant that generates follow-up questions."},
58
+ {"role": "user", "content": f"Generate a follow-up question based on the user's answer: {user_answer}"}
59
+ ]
60
+
61
+ response = openai.ChatCompletion.create(
62
+ model="gpt-3.5-turbo-0613",
63
+ messages=conversation,
64
+ max_tokens=1000 # Adjust the token limit as needed
65
+ )
66
+
67
+ follow_up_question = response.choices[0].message.content.strip()
68
+ return follow_up_question
69
+
70
+ # Modify the ask_question function to save the audio in memory
71
+ def ask_question(question):
72
+ mp3_data = io.BytesIO() # Create an in-memory stream
73
+ tts = gTTS(text=question, lang='en')
74
+ tts.write_to_fp(mp3_data)
75
+ mp3_data.seek(0) # Rewind the stream to the beginning
76
+
77
+ pygame.mixer.init()
78
+ pygame.mixer.music.load(mp3_data)
79
+ pygame.mixer.music.play()
80
+
81
+ # Wait for the audio to finish playing
82
+ while pygame.mixer.music.get_busy():
83
+ pygame.time.Clock().tick(10) # Adjust the tick rate as needed
84
+
85
+ # Function to get user's answer via speech-to-text
86
+ def get_user_answer():
87
+ recognizer = sr.Recognizer()
88
+ with sr.Microphone() as source:
89
+ st.text("Please answer the question:")
90
+ audio = recognizer.listen(source, timeout=None) # No timeout, wait until user finishes speaking
91
+ try:
92
+ answer = recognizer.recognize_google(audio, show_all=True)
93
+ if 'alternative' in answer:
94
+ answer = answer['alternative'][0]['transcript']
95
+ return answer
96
+ except Exception as e:
97
+ st.error(f"Error: {str(e)}")
98
+ return None
99
+
100
+ # Function to get feedback on the user's answer
101
+ def get_feedback(user_answer):
102
+ conversation = [
103
+ {"role": "system", "content": "You are a helpful assistant that provides feedback on user answers."},
104
+ {"role": "user", "content": f"Provide feedback on the user's answer based on clarity, confidence, content, effectiveness, quantification, and usage of STAR framework: {user_answer}"}
105
+ ]
106
+
107
+ response = openai.ChatCompletion.create(
108
+ model="gpt-3.5-turbo-0613",
109
+ messages=conversation,
110
+ max_tokens=1000 # Adjust the token limit as needed
111
+ )
112
+
113
+ feedback = response.choices[0].message.content.strip()
114
+ return feedback
115
+
116
+ # Function to evaluate whether the answer is technically correct using ChatGPT
117
+ def evaluate_answer(answer):
118
+ conversation = [
119
+ {"role": "system", "content": "You are a helpful assistant that evaluates the technical correctness of the answer."},
120
+ {"role": "user", "content": f"Evaluate whether the following answer is technically correct: {answer}"}
121
+ ]
122
+
123
+ response = openai.ChatCompletion.create(
124
+ model="gpt-3.5-turbo-0613",
125
+ messages=conversation,
126
+ max_tokens=1000 # Adjust the token limit as needed
127
+ )
128
+
129
+ evaluation_result = response.choices[0].message.content.strip()
130
+ return evaluation_result
131
+
132
+ # Function to conduct the interview in an infinite loop
133
+ def conduct_interview_loop(keywords_list, job_role):
134
+ while True:
135
+ random.shuffle(keywords_list) # Shuffle the keywords
136
+
137
+ for keyword in keywords_list:
138
+ # Generate a question for the keyword and job role
139
+ question = generate_question(keyword, job_role)
140
+
141
+ # Display the question in the GUI
142
+ st.subheader("Question:")
143
+ st.write(question)
144
+
145
+ # Ask the question via audio with medium speed
146
+ ask_question(question)
147
+
148
+ # Get the user's answer
149
+ user_answer = get_user_answer()
150
+ if user_answer is not None:
151
+ # Display the user's answer in the GUI
152
+ st.subheader("User's Answer:")
153
+ st.write(user_answer)
154
+
155
+ # Provide feedback on the user's answer
156
+ feedback = get_feedback(user_answer)
157
+
158
+ # Display the feedback in the GUI
159
+ display_feedback(feedback)
160
+
161
+ # Evaluate whether the answer is technically correct
162
+ evaluation_result = evaluate_answer(user_answer)
163
+
164
+ # Display the evaluation result in the GUI
165
+ st.subheader("Evaluation Result:")
166
+ st.write(evaluation_result)
167
+
168
+ # Wait for the user to read feedback
169
+ time.sleep(5) # Adjust the delay time (in seconds) as needed
170
+
171
+ # Process user's answer using OpenAI
172
+ follow_up_question = get_follow_up_question(user_answer)
173
+
174
+ if follow_up_question:
175
+ # Display and ask the follow-up question
176
+ st.subheader("Follow-up Question:")
177
+ st.write(follow_up_question)
178
+ ask_question(follow_up_question)
179
+
180
+ # Get the user's answer to the follow-up question
181
+ user_follow_up_answer = get_user_answer()
182
+ if user_follow_up_answer is not None:
183
+ # Display the user's answer in the GUI
184
+ st.subheader("User's Follow-up Answer:")
185
+ st.write(user_follow_up_answer)
186
+
187
+ # Allow some time for the user to finish their answer (optional)
188
+ time.sleep(5) # Adjust the delay time (in seconds) as needed
189
+
190
+ # Define Streamlit app
191
+ def main():
192
+ st.title("Interview Chatbot")
193
+
194
+ # Create job description input
195
+ job_description = st.text_area("Job Description:")
196
+
197
+ # Create technical skills input
198
+ skills = st.text_area("Technical Skills:")
199
+
200
+ # Create job role input
201
+ job_role = st.text_input("Job Role:")
202
+
203
+ # Create submit button
204
+ if st.button("Submit"):
205
+ # Step 2: Extract technical keywords
206
+ keywords = get_technical_keywords(job_description, skills)
207
+
208
+ # Display extracted technical keywords
209
+ display_keywords(keywords)
210
+
211
+ # Split keywords and conduct the interview
212
+ keywords_list = keywords.split(", ")
213
+ conduct_interview_loop(keywords_list, job_role)
214
+
215
+ # Define Streamlit app
216
+ if __name__ == '__main__':
217
+ main()