Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -20,8 +20,7 @@ def load_questions(file_path):
|
|
| 20 |
|
| 21 |
all_questions = load_questions('knowledge.txt')
|
| 22 |
|
| 23 |
-
# ----
|
| 24 |
-
# You can replace this later with better tagging
|
| 25 |
questions_by_type = {
|
| 26 |
'Technical': [q for q in all_questions if any(keyword in q['question'].lower() for keyword in [
|
| 27 |
'function', 'linked list', 'url', 'rest', 'graphql', 'garbage', 'cap theorem', 'sql', 'hash table',
|
|
@@ -30,30 +29,16 @@ questions_by_type = {
|
|
| 30 |
'sql injection', 'https', 'xss', 'hash', 'vulnerabilities'])],
|
| 31 |
|
| 32 |
'Competency-Based Interview': [q for q in all_questions if any(keyword in q['question'].lower() for keyword in [
|
| 33 |
-
"Debugging",
|
| 34 |
-
"Learning Fast",
|
| 35 |
-
"Deadlines",
|
| 36 |
-
"Teamwork",
|
| 37 |
-
"Leadership",
|
| 38 |
-
"Mistake Recovery",
|
| 39 |
-
"Conflict Management",
|
| 40 |
-
"Decision Making"])],
|
| 41 |
|
| 42 |
'Case': [q for q in all_questions if any(keyword in q['question'].lower() for keyword in [
|
| 43 |
-
"A/B Testing",
|
| 44 |
-
"Financial Modeling",
|
| 45 |
-
"Automation",
|
| 46 |
-
"Data Analysis",
|
| 47 |
-
"Regression",
|
| 48 |
-
"Business Opportunity",
|
| 49 |
-
"Stakeholder Alignment"])]
|
| 50 |
}
|
| 51 |
|
| 52 |
-
|
| 53 |
# ---- Hugging Face Client ----
|
| 54 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 55 |
|
| 56 |
-
# ----
|
| 57 |
def set_type(choice, user_profile):
|
| 58 |
user_profile["interview_type"] = choice
|
| 59 |
return "Great! Whatβs your background and what field/role are you aiming for?", user_profile
|
|
@@ -65,13 +50,15 @@ def save_background(info, user_profile):
|
|
| 65 |
|
| 66 |
# ---- Main respond logic ----
|
| 67 |
def respond(message, chat_history, user_profile):
|
|
|
|
|
|
|
| 68 |
if not user_profile.get("interview_type") or not user_profile.get("field"):
|
| 69 |
bot_msg = "Please finish steps 1 and 2 before starting the interview."
|
| 70 |
chat_history.append((message, bot_msg))
|
| 71 |
return chat_history
|
| 72 |
|
| 73 |
-
# Start interview
|
| 74 |
-
if message
|
| 75 |
interview_type = user_profile['interview_type']
|
| 76 |
selected_questions = questions_by_type.get(interview_type, [])
|
| 77 |
user_profile['questions'] = selected_questions
|
|
@@ -84,75 +71,59 @@ def respond(message, chat_history, user_profile):
|
|
| 84 |
chat_history.append((message, bot_msg))
|
| 85 |
return chat_history
|
| 86 |
|
| 87 |
-
#
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
# q_index += 1
|
| 93 |
-
# user_profile['current_q'] = q_index
|
| 94 |
-
|
| 95 |
-
# if q_index < len(user_profile['questions']):
|
| 96 |
-
# bot_msg = f"Next question: {user_profile['questions'][q_index]['question']}"
|
| 97 |
-
# else:
|
| 98 |
-
# bot_msg = "Interview complete! Type 'feedback' if you'd like me to analyze your answers."
|
| 99 |
-
# chat_history.append((message, bot_msg))
|
| 100 |
-
# return chat_history
|
| 101 |
-
if user_profile.get("questions"):
|
| 102 |
-
|
| 103 |
-
# --- NEW STOP LOGIC ---
|
| 104 |
-
if message.strip().lower() == 'stop':
|
| 105 |
-
bot_msg = "Thank you for chatting with Intervu! The interview has been stopped. Type 'feedback' if you'd like me to analyze your answers."
|
| 106 |
-
chat_history.append((message, bot_msg))
|
| 107 |
-
user_profile['questions'] = [] # clear questions list to stop
|
| 108 |
-
return chat_history
|
| 109 |
-
|
| 110 |
-
# Existing interview logic continues here:
|
| 111 |
-
q_index = user_profile['current_q']
|
| 112 |
-
user_profile['user_answers'].append(message)
|
| 113 |
-
|
| 114 |
-
q_index += 1
|
| 115 |
-
user_profile['current_q'] = q_index
|
| 116 |
-
|
| 117 |
-
if q_index < len(user_profile['questions']):
|
| 118 |
-
bot_msg = f"Next question: {user_profile['questions'][q_index]['question']}"
|
| 119 |
-
else:
|
| 120 |
-
bot_msg = "Interview complete! Type 'feedback' if you'd like me to analyze your answers."
|
| 121 |
-
|
| 122 |
chat_history.append((message, bot_msg))
|
| 123 |
return chat_history
|
| 124 |
-
|
| 125 |
|
| 126 |
-
# Handle feedback
|
| 127 |
-
if message
|
|
|
|
|
|
|
|
|
|
| 128 |
feedback = generate_feedback(user_profile)
|
| 129 |
chat_history.append((message, feedback))
|
| 130 |
return chat_history
|
| 131 |
|
| 132 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
messages = [
|
| 134 |
{"role": "system", "content": f"You are a professional interviewer conducting a {user_profile['interview_type']} interview for a candidate in the {user_profile['field']} field."}
|
| 135 |
]
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
|
| 141 |
response = client.chat_completion(messages, max_tokens=150, stream=False)
|
| 142 |
bot_msg = response.choices[0].message.content
|
| 143 |
chat_history.append((message, bot_msg))
|
| 144 |
return chat_history
|
| 145 |
|
| 146 |
-
# ----
|
| 147 |
def generate_feedback(user_profile):
|
| 148 |
feedback = []
|
| 149 |
questions = user_profile.get('questions', [])
|
| 150 |
answers = user_profile.get('user_answers', [])
|
| 151 |
|
| 152 |
-
num_questions = min(len(questions), len(answers))
|
| 153 |
-
|
| 154 |
if num_questions == 0:
|
| 155 |
-
return "No completed interview found.
|
| 156 |
|
| 157 |
for i in range(num_questions):
|
| 158 |
user_ans = answers[i]
|
|
@@ -163,10 +134,9 @@ def generate_feedback(user_profile):
|
|
| 163 |
else:
|
| 164 |
fb = f"Question {i+1}: β You missed some key points: {correct_answers[0]}"
|
| 165 |
feedback.append(fb)
|
| 166 |
-
|
| 167 |
return "\n".join(feedback)
|
| 168 |
|
| 169 |
-
# ---- Gradio
|
| 170 |
with gr.Blocks() as demo:
|
| 171 |
user_profile = gr.State({"interview_type": "", "field": ""})
|
| 172 |
chat_history = gr.State([])
|
|
@@ -175,10 +145,9 @@ with gr.Blocks() as demo:
|
|
| 175 |
|
| 176 |
gr.Markdown("### Step 1: Choose Interview Type")
|
| 177 |
with gr.Row():
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
btn3 = gr.Button("Case")
|
| 182 |
type_output = gr.Textbox(label="Bot response", interactive=False)
|
| 183 |
|
| 184 |
btn1.click(set_type, inputs=[gr.Textbox(value="Technical", visible=False), user_profile], outputs=[type_output, user_profile])
|
|
@@ -201,4 +170,3 @@ with gr.Blocks() as demo:
|
|
| 201 |
send_btn.click(lambda: "", None, msg, queue=False)
|
| 202 |
|
| 203 |
demo.launch()
|
| 204 |
-
|
|
|
|
| 20 |
|
| 21 |
all_questions = load_questions('knowledge.txt')
|
| 22 |
|
| 23 |
+
# ---- Tagging interview questions ----
|
|
|
|
| 24 |
questions_by_type = {
|
| 25 |
'Technical': [q for q in all_questions if any(keyword in q['question'].lower() for keyword in [
|
| 26 |
'function', 'linked list', 'url', 'rest', 'graphql', 'garbage', 'cap theorem', 'sql', 'hash table',
|
|
|
|
| 29 |
'sql injection', 'https', 'xss', 'hash', 'vulnerabilities'])],
|
| 30 |
|
| 31 |
'Competency-Based Interview': [q for q in all_questions if any(keyword in q['question'].lower() for keyword in [
|
| 32 |
+
"Debugging","Learning Fast","Deadlines","Teamwork","Leadership","Mistake Recovery","Conflict Management","Decision Making"])],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
'Case': [q for q in all_questions if any(keyword in q['question'].lower() for keyword in [
|
| 35 |
+
"A/B Testing","Financial Modeling","Automation","Data Analysis","Regression","Business Opportunity","Stakeholder Alignment"])]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
}
|
| 37 |
|
|
|
|
| 38 |
# ---- Hugging Face Client ----
|
| 39 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 40 |
|
| 41 |
+
# ---- Set interview type ----
|
| 42 |
def set_type(choice, user_profile):
|
| 43 |
user_profile["interview_type"] = choice
|
| 44 |
return "Great! Whatβs your background and what field/role are you aiming for?", user_profile
|
|
|
|
| 50 |
|
| 51 |
# ---- Main respond logic ----
|
| 52 |
def respond(message, chat_history, user_profile):
|
| 53 |
+
message = message.strip().lower()
|
| 54 |
+
|
| 55 |
if not user_profile.get("interview_type") or not user_profile.get("field"):
|
| 56 |
bot_msg = "Please finish steps 1 and 2 before starting the interview."
|
| 57 |
chat_history.append((message, bot_msg))
|
| 58 |
return chat_history
|
| 59 |
|
| 60 |
+
# Start interview
|
| 61 |
+
if message == 'start':
|
| 62 |
interview_type = user_profile['interview_type']
|
| 63 |
selected_questions = questions_by_type.get(interview_type, [])
|
| 64 |
user_profile['questions'] = selected_questions
|
|
|
|
| 71 |
chat_history.append((message, bot_msg))
|
| 72 |
return chat_history
|
| 73 |
|
| 74 |
+
# Stop interview early
|
| 75 |
+
if message == 'stop':
|
| 76 |
+
user_profile['questions'] = []
|
| 77 |
+
bot_msg = "Interview stopped. Type 'start' to begin again or 'feedback' to get feedback."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
chat_history.append((message, bot_msg))
|
| 79 |
return chat_history
|
|
|
|
| 80 |
|
| 81 |
+
# Handle feedback
|
| 82 |
+
if message == 'feedback':
|
| 83 |
+
if not user_profile.get("questions"):
|
| 84 |
+
chat_history.append((message, "No completed interview found. Please complete the interview first."))
|
| 85 |
+
return chat_history
|
| 86 |
feedback = generate_feedback(user_profile)
|
| 87 |
chat_history.append((message, feedback))
|
| 88 |
return chat_history
|
| 89 |
|
| 90 |
+
# Handle interview questions
|
| 91 |
+
if user_profile.get("questions"):
|
| 92 |
+
q_index = user_profile['current_q']
|
| 93 |
+
if q_index < len(user_profile['questions']):
|
| 94 |
+
user_profile['user_answers'].append(message)
|
| 95 |
+
user_profile['current_q'] += 1
|
| 96 |
+
|
| 97 |
+
if user_profile['current_q'] < len(user_profile['questions']):
|
| 98 |
+
next_question = user_profile['questions'][user_profile['current_q']]['question']
|
| 99 |
+
chat_history.append((message, f"Next question: {next_question}"))
|
| 100 |
+
else:
|
| 101 |
+
chat_history.append((message, "Interview complete! Type 'feedback' if you'd like an analysis."))
|
| 102 |
+
return chat_history
|
| 103 |
+
|
| 104 |
+
# General fallback chat using LLM if no interview ongoing
|
| 105 |
messages = [
|
| 106 |
{"role": "system", "content": f"You are a professional interviewer conducting a {user_profile['interview_type']} interview for a candidate in the {user_profile['field']} field."}
|
| 107 |
]
|
| 108 |
+
for q, a in chat_history:
|
| 109 |
+
messages.append({"role": "user", "content": q})
|
| 110 |
+
messages.append({"role": "assistant", "content": a})
|
| 111 |
+
messages.append({"role": "user", "content": message})
|
| 112 |
|
| 113 |
response = client.chat_completion(messages, max_tokens=150, stream=False)
|
| 114 |
bot_msg = response.choices[0].message.content
|
| 115 |
chat_history.append((message, bot_msg))
|
| 116 |
return chat_history
|
| 117 |
|
| 118 |
+
# ---- Feedback generator ----
|
| 119 |
def generate_feedback(user_profile):
|
| 120 |
feedback = []
|
| 121 |
questions = user_profile.get('questions', [])
|
| 122 |
answers = user_profile.get('user_answers', [])
|
| 123 |
|
| 124 |
+
num_questions = min(len(questions), len(answers))
|
|
|
|
| 125 |
if num_questions == 0:
|
| 126 |
+
return "No completed interview found."
|
| 127 |
|
| 128 |
for i in range(num_questions):
|
| 129 |
user_ans = answers[i]
|
|
|
|
| 134 |
else:
|
| 135 |
fb = f"Question {i+1}: β You missed some key points: {correct_answers[0]}"
|
| 136 |
feedback.append(fb)
|
|
|
|
| 137 |
return "\n".join(feedback)
|
| 138 |
|
| 139 |
+
# ---- Gradio interface ----
|
| 140 |
with gr.Blocks() as demo:
|
| 141 |
user_profile = gr.State({"interview_type": "", "field": ""})
|
| 142 |
chat_history = gr.State([])
|
|
|
|
| 145 |
|
| 146 |
gr.Markdown("### Step 1: Choose Interview Type")
|
| 147 |
with gr.Row():
|
| 148 |
+
btn1 = gr.Button("Technical")
|
| 149 |
+
btn2 = gr.Button("Competency-Based Interview")
|
| 150 |
+
btn3 = gr.Button("Case")
|
|
|
|
| 151 |
type_output = gr.Textbox(label="Bot response", interactive=False)
|
| 152 |
|
| 153 |
btn1.click(set_type, inputs=[gr.Textbox(value="Technical", visible=False), user_profile], outputs=[type_output, user_profile])
|
|
|
|
| 170 |
send_btn.click(lambda: "", None, msg, queue=False)
|
| 171 |
|
| 172 |
demo.launch()
|
|
|