Spaces:
Runtime error
Runtime error
Update app.py
Browse files41 unmodified lines424344454647484 unmodified lines535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212330 unmodified lines15415515615715815916041 unmodified linesdef 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_profile4 unmodified lines# ---- 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."}]30 unmodified lines# ---- 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")41 unmodified lines42434445464748495051524 unmodified lines5758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712830 unmodified lines15916016116216316416516616716841 unmodified linesdef set_type(choice, user_profile):user_profile.update({"interview_type": choice,"state": "idle","questions": [],"current_q": 0,"user_answers": [],"field": ""})return "Great! Whatβs your background and what field/role are you aiming for?", user_profile4 unmodified lines# ---- Main Respond Logic ----def respond(message, chat_history, user_profile):msg = message.strip().lower()state = user_profile.get("state", "idle")# Safety: ensure setupif 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_history# START commandif msg == "start" and state == "idle":selected_questions = questions_by_type.get(user_profile['interview_type'], [])if not selected_questions:chat_history.append((message, "No questions found for this interview type."))return chat_historyuser_profile["questions"] = selected_questionsuser_profile["current_q"] = 0user_profile["user_answers"] = []user_profile["state"] = "intro"intro_msg = f"Great, let's begin your {user_profile['interview_type']} interview for the {user_profile['field']} role.\nI'll be asking you several questions to assess your knowledge. Feel free to take your time answering."chat_history.append((message, intro_msg))chat_history.append(("", "Type 'next' when you're ready to begin."))return chat_history# INTRO β waiting for user to say "next"if msg == "next" and state == "intro":user_profile["state"] = "interviewing"first_question = user_profile['questions'][0]['question']chat_history.append((message, f"First question: {first_question}"))return chat_history# STOP anytime during interviewingif msg == "stop" and state in ["intro", "interviewing"]:user_profile["state"] = "stopped"chat_history.append((message, "Interview stopped. You may type 'start' to begin again."))return chat_history# FEEDBACK commandif msg == "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# INTERVIEWING STATE LOGICif state == "interviewing":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_q = questions[user_profile["current_q"]]["question"]chat_history.append((message, f"Next question: {next_q}"))else:user_profile["state"] = "completed"chat_history.append((message, "β Interview complete! Type 'feedback' if you'd like a performance analysis."))return chat_history# ANY OTHER MESSAGE (idle, stopped, etc) β small talk fallback 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."}]30 unmodified lines# ---- Gradio UI ----with gr.Blocks() as demo:user_profile = gr.State({"interview_type": "", "field": "", "state": "idle","questions": [], "current_q": 0, "user_answers": []})chat_history = gr.State([])gr.Markdown("# π€ Welcome to Intervu")
app.py+46-38