Spaces:
Runtime error
Runtime error
Update app.py
Browse files1 unmodified line2345567811 unmodified lines202122232324252627282930262728293031323332333435353637383839404141424343444546474846495051495253515455565758595657606162606163646566676864656669706869707171727374757677787980818283848586747576777887888990919293948384959697989910010110290911031041059310610710810911011197981121131141151011161171181191041201211221237 unmodified lines13113213311813413513613714 unmodified lines1521531541391551561411571581591601 unmodified linefrom huggingface_hub import InferenceClientimport re
# ---- Load and parse questions from knowledge.txt ----# ---- Load and parse questions ----def load_questions(file_path): with open(file_path, 'r') as f: data = f.read()11 unmodified lines
all_questions = load_questions('knowledge.txt')
# ---- Tagging interview questions ----# ---- 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'])], '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"])], "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 ----# ---- HF Client ----client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
# ---- Set interview type ----# ---- Interview type selection ----def set_type(choice, user_profile): user_profile["interview_type"] = choice user_profile.update({ "interview_type": choice, "state": "idle" }) return "Great! Whatβs your background and what field/role are you aiming for?", user_profile
# ---- Save background ----# ---- Background ----def save_background(info, user_profile): user_profile["field"] = info return "Awesome! Type 'start' below to begin your interview.", user_profile return "Awesome! Type 'start' below when you're ready to begin your interview.", user_profile
# ---- Main respond logic ----# ---- Main Respond Logic ----def respond(message, chat_history, user_profile): message = message.strip().lower()
# Safety: Ensure interview type + field are set 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)) chat_history.append((message, "Please finish steps 1 and 2 first.")) return chat_history
# Start interview if message == 'start': state = user_profile.get("state", "idle")
# Handle start if message == 'start' and state == 'idle': interview_type = user_profile['interview_type'] selected_questions = questions_by_type.get(interview_type, []) user_profile['questions'] = selected_questions user_profile['current_q'] = 0 user_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)) chat_history.append((message, "No questions found for this interview type.")) return chat_history
user_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
# 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)) # Handle stop if 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 feedback if message == 'feedback': if not user_profile.get("questions"): chat_history.append((message, "No completed interview found. Please complete the interview first.")) if state != 'completed': chat_history.append((message, "Feedback is only available after completing the interview.")) return chat_history
feedback = generate_feedback(user_profile) chat_history.append((message, feedback)) return chat_history
# Handle interview questions if user_profile.get("questions"): # Handle interview question flow if state == 'running': q_index = user_profile['current_q'] if q_index < len(user_profile['questions']): questions = user_profile['questions']
if q_index < len(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'] if user_profile['current_q'] < len(questions): next_question = 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.")) user_profile['state'] = 'completed' chat_history.append((message, "β
Interview complete! You can type 'feedback' to receive an analysis.")) return chat_history
# General fallback chat using LLM if no interview ongoing # Handle idle/stopped fallback small talk using LLM 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."} ]7 unmodified lines chat_history.append((message, bot_msg)) return chat_history
# ---- Feedback generator ----# ---- Feedback ----def generate_feedback(user_profile): feedback = [] questions = user_profile.get('questions', [])14 unmodified lines feedback.append(fb) return "\n".join(feedback)
# ---- Gradio interface ----# ---- Gradio UI ----with gr.Blocks() as demo: user_profile = gr.State({"interview_type": "", "field": ""}) user_profile = gr.State({"interview_type": "", "field": "", "state": "idle"}) chat_history = gr.State([])
gr.Markdown("# π€ Welcome to Intervu")app.py+60-44