Spaces:
Runtime error
Runtime error
Update app.py
Browse files12345671 unmodified line91011111213145 unmodified lines202122232423242527282930312627282930333435363738394041313243444546474849333435525336375638394041426143444546664748495051525354557456575859606162636480818283656667686970717273747576777887888990919293949596979899100101791021031041051061071081091108081823 unmodified lines86878812012189909192124931261279495969798132133134135991001011021034 unmodified lines108109110146111112113114151115116117118156119120158121122123124162125126164127128129130import gradio as grfrom huggingface_hub import InferenceClientimport reimport random
# ---- Load and parse questions from knowledge.txt ----def load_questions(file_path):1 unmodified line data = f.read()
question_blocks = re.split(r'Question:\s*', data)[1:]
questions = [] for block in question_blocks: parts = block.split('Possible Answers:')5 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 ----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", "deadlines", "teamwork", "leadership", "mistake", "conflict", "decision"])], '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"])] "testing", "financial", "automation", "analysis", "regression", "business", "stakeholder"])] }
# ---- Hugging Face Client ----client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
# ---- Interview type selection ----# ---- Logic functions ----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_profile
# ---- Save background ----def save_background(info, user_profile): user_profile["field"] = info return "Awesome! Type 'start' below to begin your interview.", user_profile
# ---- Main respond logic ----def respond(message, chat_history, user_profile): message_lower = 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': if message_lower == 'start': interview_type = user_profile['interview_type'] selected_questions = questions_by_type.get(interview_type, []) random.shuffle(selected_questions) selected_questions = selected_questions[:10]
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']}" user_profile['interview_in_progress'] = True
intro = f"Welcome to your {interview_type} interview for a {user_profile['field']} position. I will ask you up to 10 questions. Type 'stop' anytime to end." first_q = f"First question: {selected_questions[0]['question']}" chat_history.append((message, intro)) chat_history.append(("", first_q)) return chat_history
if message_lower == 'stop' and user_profile.get("interview_in_progress"): user_profile['interview_in_progress'] = False bot_msg = "Interview stopped. Type 'feedback' if you'd like me to analyze your answers. Thanks for interviewing with Intervu!" 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"): if user_profile.get("interview_in_progress"):
# --- 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)
3 unmodified lines 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."
user_profile['interview_in_progress'] = False bot_msg = "Interview complete! Type 'feedback' if you'd like me to analyze your answers. Thanks for interviewing with Intervu!" chat_history.append((message, bot_msg)) return chat_history
# Handle feedback request if message.strip().lower() == 'feedback': if message_lower == 'feedback': feedback = generate_feedback(user_profile) chat_history.append((message, feedback)) return chat_history
# Default fallback 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."} ] # fallback small talk messages = [{"role": "system", "content": f"You are a professional interviewer conducting a {user_profile['interview_type']} interview for a candidate in {user_profile['field']}."}] for q, a in chat_history: messages.append({"role": "user", "content": q}) messages.append({"role": "assistant", "content": a})4 unmodified lines chat_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', [])
for i, user_ans in enumerate(answers): correct_answers = questions[i]['answers'] match = any(ans.lower() in user_ans.lower() for ans in correct_answers) if match: fb = f"Question {i+1}: β
Good job! You covered key points." fb = f"Question {i+1}: β
Good job!" else: fb = f"Question {i+1}: β You missed some key points: {correct_answers[0]}" fb = f"Question {i+1}: β Missed some key points: {correct_answers[0]}" 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": "", "interview_in_progress": False}) chat_history = gr.State([])
gr.Markdown("# π€ Welcome to Intervu")app.py+36-73