Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -16,9 +16,7 @@ def load_questions(file_path):
|
|
| 16 |
for block in question_blocks:
|
| 17 |
parts = block.split('Possible Answers:')
|
| 18 |
question_text = parts[0].strip()
|
| 19 |
-
|
| 20 |
-
possible_answers = [ans.strip() for ans in re.split(r'\d+\.\s+', answers_text) if ans.strip()]
|
| 21 |
-
questions.append({'question': question_text, 'answers': possible_answers})
|
| 22 |
return questions
|
| 23 |
|
| 24 |
all_questions = load_questions('knowledge.txt')
|
|
@@ -33,7 +31,7 @@ questions_by_type = {
|
|
| 33 |
'Competency-Based Interview': [q for q in all_questions if any(keyword in q['question'].lower() for keyword in [
|
| 34 |
"debugging", "learning", "deadlines", "teamwork", "leadership", "mistake", "conflict", "decision"])],
|
| 35 |
'Case': [q for q in all_questions if any(keyword in q['question'].lower() for keyword in [
|
| 36 |
-
"testing", "financial", "automation", "analysis", "regression", "business", "stakeholder"])]
|
| 37 |
}
|
| 38 |
|
| 39 |
# models
|
|
@@ -52,7 +50,7 @@ def transcribe_audio(file_path):
|
|
| 52 |
except Exception as e:
|
| 53 |
return f"β ERROR: {str(e)}"
|
| 54 |
|
| 55 |
-
#
|
| 56 |
def set_type(choice, user_profile):
|
| 57 |
user_profile["interview_type"] = choice
|
| 58 |
return "Great! Whatβs your background and what field/role are you aiming for?", user_profile
|
|
@@ -62,7 +60,7 @@ def save_background(info, user_profile):
|
|
| 62 |
user_profile["field"] = info
|
| 63 |
return "Awesome! Type 'start' below to begin your interview.", user_profile
|
| 64 |
|
| 65 |
-
# step 3
|
| 66 |
def respond(message, chat_history, user_profile):
|
| 67 |
message_lower = message.strip().lower()
|
| 68 |
|
|
@@ -71,7 +69,6 @@ def respond(message, chat_history, user_profile):
|
|
| 71 |
chat_history.append((message, bot_msg))
|
| 72 |
return chat_history
|
| 73 |
|
| 74 |
-
# interview process
|
| 75 |
if message_lower == 'start':
|
| 76 |
interview_type = user_profile['interview_type']
|
| 77 |
selected_questions = questions_by_type.get(interview_type, [])
|
|
@@ -115,7 +112,7 @@ def respond(message, chat_history, user_profile):
|
|
| 115 |
chat_history.append((message, feedback))
|
| 116 |
return chat_history
|
| 117 |
|
| 118 |
-
#
|
| 119 |
messages = [{"role": "system", "content": f"You are a professional interviewer conducting a {user_profile['interview_type']} interview for a candidate in {user_profile['field']}."}]
|
| 120 |
for q, a in chat_history:
|
| 121 |
messages.append({"role": "user", "content": q})
|
|
@@ -127,21 +124,27 @@ def respond(message, chat_history, user_profile):
|
|
| 127 |
chat_history.append((message, bot_msg))
|
| 128 |
return chat_history
|
| 129 |
|
|
|
|
| 130 |
def generate_feedback(user_profile):
|
| 131 |
-
feedback = []
|
| 132 |
questions = user_profile.get('questions', [])
|
| 133 |
answers = user_profile.get('user_answers', [])
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
return "\n".join(feedback)
|
| 143 |
|
| 144 |
-
# handle audio
|
| 145 |
def handle_audio(audio_file, chat_history, user_profile):
|
| 146 |
transcribed = transcribe_audio(audio_file)
|
| 147 |
if transcribed.startswith("β"):
|
|
@@ -149,7 +152,7 @@ def handle_audio(audio_file, chat_history, user_profile):
|
|
| 149 |
return chat_history
|
| 150 |
return respond(transcribed, chat_history, user_profile)
|
| 151 |
|
| 152 |
-
#
|
| 153 |
with gr.Blocks() as demo:
|
| 154 |
user_profile = gr.State({"interview_type": "", "field": "", "interview_in_progress": False})
|
| 155 |
chat_history = gr.State([])
|
|
@@ -187,10 +190,8 @@ with gr.Blocks() as demo:
|
|
| 187 |
send_btn = gr.Button("Send Text")
|
| 188 |
audio_btn = gr.Button("Send Audio")
|
| 189 |
|
| 190 |
-
|
| 191 |
send_btn.click(respond, inputs=[msg, chat_history, user_profile], outputs=[chatbot], queue=False)
|
| 192 |
send_btn.click(lambda: "", None, msg, queue=False)
|
| 193 |
audio_btn.click(handle_audio, inputs=[audio_input, chat_history, user_profile], outputs=[chatbot], queue=False)
|
| 194 |
|
| 195 |
-
|
| 196 |
-
demo.launch()
|
|
|
|
| 16 |
for block in question_blocks:
|
| 17 |
parts = block.split('Possible Answers:')
|
| 18 |
question_text = parts[0].strip()
|
| 19 |
+
questions.append({'question': question_text})
|
|
|
|
|
|
|
| 20 |
return questions
|
| 21 |
|
| 22 |
all_questions = load_questions('knowledge.txt')
|
|
|
|
| 31 |
'Competency-Based Interview': [q for q in all_questions if any(keyword in q['question'].lower() for keyword in [
|
| 32 |
"debugging", "learning", "deadlines", "teamwork", "leadership", "mistake", "conflict", "decision"])],
|
| 33 |
'Case': [q for q in all_questions if any(keyword in q['question'].lower() for keyword in [
|
| 34 |
+
"testing", "financial", "automation", "analysis", "regression", "business", "stakeholder"])]
|
| 35 |
}
|
| 36 |
|
| 37 |
# models
|
|
|
|
| 50 |
except Exception as e:
|
| 51 |
return f"β ERROR: {str(e)}"
|
| 52 |
|
| 53 |
+
# step 1
|
| 54 |
def set_type(choice, user_profile):
|
| 55 |
user_profile["interview_type"] = choice
|
| 56 |
return "Great! Whatβs your background and what field/role are you aiming for?", user_profile
|
|
|
|
| 60 |
user_profile["field"] = info
|
| 61 |
return "Awesome! Type 'start' below to begin your interview.", user_profile
|
| 62 |
|
| 63 |
+
# updated step 3
|
| 64 |
def respond(message, chat_history, user_profile):
|
| 65 |
message_lower = message.strip().lower()
|
| 66 |
|
|
|
|
| 69 |
chat_history.append((message, bot_msg))
|
| 70 |
return chat_history
|
| 71 |
|
|
|
|
| 72 |
if message_lower == 'start':
|
| 73 |
interview_type = user_profile['interview_type']
|
| 74 |
selected_questions = questions_by_type.get(interview_type, [])
|
|
|
|
| 112 |
chat_history.append((message, feedback))
|
| 113 |
return chat_history
|
| 114 |
|
| 115 |
+
# chatbot fallback
|
| 116 |
messages = [{"role": "system", "content": f"You are a professional interviewer conducting a {user_profile['interview_type']} interview for a candidate in {user_profile['field']}."}]
|
| 117 |
for q, a in chat_history:
|
| 118 |
messages.append({"role": "user", "content": q})
|
|
|
|
| 124 |
chat_history.append((message, bot_msg))
|
| 125 |
return chat_history
|
| 126 |
|
| 127 |
+
# updated feedback using model
|
| 128 |
def generate_feedback(user_profile):
|
|
|
|
| 129 |
questions = user_profile.get('questions', [])
|
| 130 |
answers = user_profile.get('user_answers', [])
|
| 131 |
+
feedback = []
|
| 132 |
+
|
| 133 |
+
for i, (question, user_answer) in enumerate(zip(questions, answers)):
|
| 134 |
+
prompt = (
|
| 135 |
+
f"You are an expert interviewer. Please evaluate the following response:\n\n"
|
| 136 |
+
f"Question: {question['question']}\n"
|
| 137 |
+
f"Candidate's Answer: {user_answer}\n\n"
|
| 138 |
+
f"Give a brief, constructive evaluation. Say whether it's a strong, okay, or weak answer and what could be improved."
|
| 139 |
+
)
|
| 140 |
+
|
| 141 |
+
response = client.text_generation(prompt, max_new_tokens=200)
|
| 142 |
+
evaluation = response.strip()
|
| 143 |
+
feedback.append(f"Question {i+1}:\n{evaluation}\n")
|
| 144 |
+
|
| 145 |
return "\n".join(feedback)
|
| 146 |
|
| 147 |
+
# handle audio
|
| 148 |
def handle_audio(audio_file, chat_history, user_profile):
|
| 149 |
transcribed = transcribe_audio(audio_file)
|
| 150 |
if transcribed.startswith("β"):
|
|
|
|
| 152 |
return chat_history
|
| 153 |
return respond(transcribed, chat_history, user_profile)
|
| 154 |
|
| 155 |
+
# UI
|
| 156 |
with gr.Blocks() as demo:
|
| 157 |
user_profile = gr.State({"interview_type": "", "field": "", "interview_in_progress": False})
|
| 158 |
chat_history = gr.State([])
|
|
|
|
| 190 |
send_btn = gr.Button("Send Text")
|
| 191 |
audio_btn = gr.Button("Send Audio")
|
| 192 |
|
|
|
|
| 193 |
send_btn.click(respond, inputs=[msg, chat_history, user_profile], outputs=[chatbot], queue=False)
|
| 194 |
send_btn.click(lambda: "", None, msg, queue=False)
|
| 195 |
audio_btn.click(handle_audio, inputs=[audio_input, chat_history, user_profile], outputs=[chatbot], queue=False)
|
| 196 |
|
| 197 |
+
demo.launch()
|
|
|