Spaces:
Runtime error
Runtime error
Update app.py
Browse files1 unmodified line234567811 unmodified lines20212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061077 unmodified lines11511611711811912012114 unmodified lines1361371381391401411421431441 unmodified linefrom huggingface_hub import InferenceClientimport re# ---- Load and parse questions from knowledge.txt ----def load_questions(file_path):with open(file_path, 'r') as f:data = f.read()11 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','stack', 'queue', 'recursion', 'reverse', 'bfs', 'dfs', 'time complexity', 'binary search tree','web application', 'chat system', 'load balancing', 'caching', 'normalization', 'acid', 'indexing','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_profile# ---- Save background ----def save_background(info, user_profile):user_profile["field"] = inforeturn "Awesome! Type 'start' below to begin your interview.", user_profile# ---- 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_questionsuser_profile['current_q'] = 0user_profile['user_answers'] = []if not selected_questions:bot_msg = "No questions available for this interview type."else:bot_msg = f"First question: {selected_questions[0]['question']}"chat_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."}]7 unmodified lineschat_history.append((message, bot_msg))return chat_history# ---- Feedback generator ----def generate_feedback(user_profile):feedback = []questions = user_profile.get('questions', [])14 unmodified linesfeedback.append(fb)return "\n".join(feedback)# ---- Gradio interface ----with gr.Blocks() as demo:user_profile = gr.State({"interview_type": "", "field": ""})chat_history = gr.State([])gr.Markdown("# π€ Welcome to Intervu")1 unmodified line234567811 unmodified lines20212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221237 unmodified lines13113213313413513613714 unmodified lines1521531541551561571581591601 unmodified linefrom huggingface_hub import InferenceClientimport re# ---- Load and parse questions ----def load_questions(file_path):with open(file_path, 'r') as f:data = f.read()11 unmodified linesall_questions = load_questions('knowledge.txt')# ---- Categorize 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','stack', 'queue', 'recursion', 'reverse', 'bfs', 'dfs', 'time complexity', 'binary search tree','web application', 'chat system', 'load balancing', 'caching', 'normalization', 'acid', 'indexing','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"])]}# ---- HF Client ----client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")# ---- Interview type selection ----def set_type(choice, user_profile):user_profile.update({"interview_type": choice,"state": "idle"})return "Great! Whatβs your background and what field/role are you aiming for?", user_profile# ---- Background ----def save_background(info, user_profile):user_profile["field"] = inforeturn "Awesome! Type 'start' below when you're ready to begin your interview.", user_profile# ---- Main Respond Logic ----def respond(message, chat_history, user_profile):message = message.strip().lower()# Safety: Ensure interview type + field are setif not user_profile.get("interview_type") or not user_profile.get("field"):chat_history.append((message, "Please finish steps 1 and 2 first."))return chat_historystate = user_profile.get("state", "idle")# Handle startif message == 'start' and state == 'idle':interview_type = user_profile['interview_type']selected_questions = questions_by_type.get(interview_type, [])if not selected_questions:chat_history.append((message, "No questions found for this interview type."))return chat_historyuser_profile.update({'questions': selected_questions,'current_q': 0,'user_answers': [],'state': 'running'})intro_msg = f"Great, let's begin your {interview_type} interview for the {user_profile['field']} role.\nIβll be asking you a few questions to assess your knowledge. Take your time!"first_q = selected_questions[0]['question']chat_history.append((message, intro_msg))chat_history.append(("", f"First question: {first_q}"))return chat_history# Handle stopif message == 'stop' and state == 'running':user_profile["state"] = "stopped"chat_history.append((message, "Interview stopped. Type 'start' to begin again or 'feedback' if you'd like an analysis (if available)."))return chat_history# Handle feedbackif message == 'feedback':if state != 'completed':chat_history.append((message, "Feedback is only available after completing the interview."))return chat_historyfeedback = generate_feedback(user_profile)chat_history.append((message, feedback))return chat_history# Handle interview question flowif state == 'running':q_index = user_profile['current_q']questions = user_profile['questions']if q_index < len(questions):user_profile['user_answers'].append(message)user_profile['current_q'] += 1if user_profile['current_q'] < len(questions):next_question = questions[user_profile['current_q']]['question']chat_history.append((message, f"Next question: {next_question}"))else:user_profile['state'] = 'completed'chat_history.append((message, "β Interview complete! You can type 'feedback' to receive an analysis."))return chat_history# Handle idle/stopped fallback small talk using LLMmessages = [{"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."}]7 unmodified lineschat_history.append((message, bot_msg))return chat_history# ---- Feedback ----def generate_feedback(user_profile):feedback = []questions = user_profile.get('questions', [])14 unmodified linesfeedback.append(fb)return "\n".join(feedback)# ---- Gradio UI ----with gr.Blocks() as demo:user_profile = gr.State({"interview_type": "", "field": "", "state": "idle"})chat_history = gr.State([])gr.Markdown("# π€ Welcome to Intervu")
app.py+60-44