Spaces:
Runtime error
Runtime error
Update app.py
Browse files19 unmodified lines20212223242526272 unmodified lines3031323334353637383940414243444546474849505152535455565758595 unmodified lines656667686970717273747576776 unmodified lines848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571584 unmodified lines1631641651661671681691701711722 unmodified lines17517617717817918018118218318416 unmodified lines20120220320419 unmodified linesall_questions = load_questions('knowledge.txt')# ---- Simple way to assign questions to interview types ----# You can replace this later with better taggingquestions_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"])],'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"])]}# ---- Hugging Face Client ----client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")# ---- Interview type selection ----def set_type(choice, user_profile):user_profile["interview_type"] = choicereturn "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):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 logicif message.strip().lower() == 'start':interview_type = user_profile['interview_type']selected_questions = questions_by_type.get(interview_type, [])user_profile['questions'] = selected_questions6 unmodified lineschat_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_historyif 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 stopreturn chat_history# Existing interview logic continues here:q_index = user_profile['current_q']user_profile['user_answers'].append(message)q_index += 1user_profile['current_q'] = q_indexif 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# Handle feedback requestif message.strip().lower() == 'feedback':feedback = generate_feedback(user_profile)chat_history.append((message, feedback))return chat_history# Default fallbackmessages = [{"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})response = client.chat_completion(messages, max_tokens=150, stream=False)bot_msg = response.choices[0].message.contentchat_history.append((message, bot_msg))return chat_history# ---- Simple feedback function (keyword based for now) ----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 errorif num_questions == 0:return "No completed interview found. Please complete the interview first."for i in range(num_questions):user_ans = answers[i]4 unmodified lineselse:fb = f"Question {i+1}: β You missed some key points: {correct_answers[0]}"feedback.append(fb)return "\n".join(feedback)# ---- Gradio Interface ----with gr.Blocks() as demo:user_profile = gr.State({"interview_type": "", "field": ""})chat_history = gr.State([])2 unmodified linesgr.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")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 linessend_btn.click(lambda: "", None, msg, queue=False)demo.launch()19 unmodified lines202122232425262 unmodified lines293031323334353637383940414243445 unmodified lines5051525354555657585960616263646 unmodified lines71727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281294 unmodified lines1341351361371381391401411422 unmodified lines14514614714814915015115215316 unmodified lines17017117219 unmodified linesall_questions = load_questions('knowledge.txt')# ---- 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"])],'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"])]}# ---- Hugging Face Client ----client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")# ---- Set interview type ----def set_type(choice, user_profile):user_profile["interview_type"] = choicereturn "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 interviewif message == 'start':interview_type = user_profile['interview_type']selected_questions = questions_by_type.get(interview_type, [])user_profile['questions'] = selected_questions6 unmodified lineschat_history.append((message, bot_msg))return chat_history# Stop interview earlyif 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 feedbackif message == 'feedback':if not user_profile.get("questions"):chat_history.append((message, "No completed interview found. Please complete the interview first."))return chat_historyfeedback = generate_feedback(user_profile)chat_history.append((message, feedback))return chat_history# Handle interview questionsif 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'] += 1if 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 ongoingmessages = [{"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})response = client.chat_completion(messages, max_tokens=150, stream=False)bot_msg = response.choices[0].message.contentchat_history.append((message, bot_msg))return chat_history# ---- 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))if num_questions == 0:return "No completed interview found."for i in range(num_questions):user_ans = answers[i]4 unmodified lineselse:fb = f"Question {i+1}: β You missed some key points: {correct_answers[0]}"feedback.append(fb)return "\n".join(feedback)# ---- Gradio interface ----with gr.Blocks() as demo:user_profile = gr.State({"interview_type": "", "field": ""})chat_history = gr.State([])2 unmodified linesgr.Markdown("### Step 1: Choose Interview Type")with gr.Row():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 linessend_btn.click(lambda: "", None, msg, queue=False)demo.launch()
app.py+43-75