Spaces:
Runtime error
Runtime error
Update app.py
Browse files19 unmodified lines2021222324232425262 unmodified lines2930313334353637383940323334434445464748493536375238394056414243445 unmodified lines50515253545556575859737460616263646 unmodified lines7172738788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012174757677787912480126127818283848586878889132909192939495969798991001011021031041051061071361371381391081091101111121131141151161171461181191201211221231521531241251551261271281294 unmodified lines1341351361661371381691391401411422 unmodified lines14514614717817918018114814915015115215316 unmodified lines17017117220419 unmodified lines
all_questions = load_questions('knowledge.txt')
# ---- Simple way to assign questions to interview types ----# You can replace this later with better tagging# ---- Tagging interview questions ----questions_by_type = { 'Technical': [q for q in all_questions if any(keyword in q['question'].lower() for keyword in [ 'function', 'linked list', 'url', 'rest', 'graphql', 'garbage', 'cap theorem', 'sql', 'hash table',2 unmodified lines 'sql injection', 'https', 'xss', 'hash', 'vulnerabilities'])], 'Competency-Based Interview': [q for q in all_questions if any(keyword in q['question'].lower() for keyword in [ "Debugging", "Learning Fast", "Deadlines", "Teamwork", "Leadership", "Mistake Recovery", "Conflict Management", "Decision Making"])], "Debugging","Learning Fast","Deadlines","Teamwork","Leadership","Mistake Recovery","Conflict Management","Decision Making"])], 'Case': [q for q in all_questions if any(keyword in q['question'].lower() for keyword in [ "A/B Testing", "Financial Modeling", "Automation", "Data Analysis", "Regression", "Business Opportunity", "Stakeholder Alignment"])] "A/B Testing","Financial Modeling","Automation","Data Analysis","Regression","Business Opportunity","Stakeholder Alignment"])] }
# ---- Hugging Face Client ----client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
# ---- Interview type selection ----# ---- Set interview type ----def set_type(choice, user_profile): user_profile["interview_type"] = choice return "Great! Whatβs your background and what field/role are you aiming for?", user_profile5 unmodified lines
# ---- Main respond logic ----def respond(message, chat_history, user_profile): message = message.strip().lower()
if not user_profile.get("interview_type") or not user_profile.get("field"): bot_msg = "Please finish steps 1 and 2 before starting the interview." chat_history.append((message, bot_msg)) return chat_history
# Start interview logic if message.strip().lower() == 'start': # Start interview if message == 'start': interview_type = user_profile['interview_type'] selected_questions = questions_by_type.get(interview_type, []) user_profile['questions'] = selected_questions6 unmodified lines chat_history.append((message, bot_msg)) return chat_history
# If interview is ongoing # if user_profile.get("questions"): # q_index = user_profile['current_q'] # user_profile['user_answers'].append(message)
# q_index += 1 # user_profile['current_q'] = q_index
# if q_index < len(user_profile['questions']): # bot_msg = f"Next question: {user_profile['questions'][q_index]['question']}" # else: # bot_msg = "Interview complete! Type 'feedback' if you'd like me to analyze your answers." # chat_history.append((message, bot_msg)) # return chat_history if user_profile.get("questions"):
# --- NEW STOP LOGIC --- if message.strip().lower() == 'stop': bot_msg = "Thank you for chatting with Intervu! The interview has been stopped. Type 'feedback' if you'd like me to analyze your answers." chat_history.append((message, bot_msg)) user_profile['questions'] = [] # clear questions list to stop return chat_history
# Existing interview logic continues here: q_index = user_profile['current_q'] user_profile['user_answers'].append(message)
q_index += 1 user_profile['current_q'] = q_index
if q_index < len(user_profile['questions']): bot_msg = f"Next question: {user_profile['questions'][q_index]['question']}" else: bot_msg = "Interview complete! Type 'feedback' if you'd like me to analyze your answers."
# Stop interview early if message == 'stop': user_profile['questions'] = [] bot_msg = "Interview stopped. Type 'start' to begin again or 'feedback' to get feedback." chat_history.append((message, bot_msg)) return chat_history
# Handle feedback request if message.strip().lower() == 'feedback': # Handle feedback if message == 'feedback': if not user_profile.get("questions"): chat_history.append((message, "No completed interview found. Please complete the interview first.")) return chat_history feedback = generate_feedback(user_profile) chat_history.append((message, feedback)) return chat_history
# Default fallback # Handle interview questions if user_profile.get("questions"): q_index = user_profile['current_q'] if q_index < len(user_profile['questions']): user_profile['user_answers'].append(message) user_profile['current_q'] += 1
if user_profile['current_q'] < len(user_profile['questions']): next_question = user_profile['questions'][user_profile['current_q']]['question'] chat_history.append((message, f"Next question: {next_question}")) else: chat_history.append((message, "Interview complete! Type 'feedback' if you'd like an analysis.")) return chat_history
# General fallback chat using LLM if no interview ongoing messages = [ {"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."} ] # for q, a in chat_history: # messages.append({"role": "user", "content": q}) # messages.append({"role": "assistant", "content": a}) # messages.append({"role": "user", "content": message}) for q, a in chat_history: messages.append({"role": "user", "content": q}) messages.append({"role": "assistant", "content": a}) messages.append({"role": "user", "content": message})
response = client.chat_completion(messages, max_tokens=150, stream=False) bot_msg = response.choices[0].message.content chat_history.append((message, bot_msg)) return chat_history
# ---- Simple feedback function (keyword based for now) ----# ---- Feedback generator ----def generate_feedback(user_profile): feedback = [] questions = user_profile.get('questions', []) answers = user_profile.get('user_answers', [])
num_questions = min(len(questions), len(answers)) # avoid index error
num_questions = min(len(questions), len(answers)) if num_questions == 0: return "No completed interview found. Please complete the interview first." return "No completed interview found."
for i in range(num_questions): user_ans = answers[i]4 unmodified lines else: fb = f"Question {i+1}: β You missed some key points: {correct_answers[0]}" feedback.append(fb)
return "\n".join(feedback)
# ---- Gradio Interface ----# ---- Gradio interface ----with gr.Blocks() as demo: user_profile = gr.State({"interview_type": "", "field": ""}) chat_history = gr.State([])2 unmodified lines
gr.Markdown("### Step 1: Choose Interview Type") with gr.Row(): with gr.Column(): btn1 = gr.Button("Technical") btn2 = gr.Button("Competency-Based Interview") btn3 = gr.Button("Case") btn1 = gr.Button("Technical") btn2 = gr.Button("Competency-Based Interview") btn3 = gr.Button("Case") type_output = gr.Textbox(label="Bot response", interactive=False)
btn1.click(set_type, inputs=[gr.Textbox(value="Technical", visible=False), user_profile], outputs=[type_output, user_profile])16 unmodified lines send_btn.click(lambda: "", None, msg, queue=False)
demo.launch()
app.py+43-75