Spaces:
Runtime error
Runtime error
Update app.py
Browse files15 unmodified lines1617181920212223248 unmodified lines3334353637383912 unmodified lines525354555657583 unmodified lines626364656667682 unmodified lines7172737475767737 unmodified lines1151161171181191201215 unmodified lines1271281291301311321331341351361371381391401411421431441451461471 unmodified line14915015115215315415531 unmodified lines18718818919019119219319419519615 unmodified linesfor block in question_blocks:parts = block.split('Possible Answers:')question_text = parts[0].strip()answers_text = parts[1].strip()possible_answers = [ans.strip() for ans in re.split(r'\d+\.\s+', answers_text) if ans.strip()]questions.append({'question': question_text, 'answers': possible_answers})return questionsall_questions = load_questions('knowledge.txt')8 unmodified lines'Competency-Based Interview': [q for q in all_questions if any(keyword in q['question'].lower() for keyword in ["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 ["testing", "financial", "automation", "analysis", "regression", "business", "stakeholder"])]}# models12 unmodified linesexcept Exception as e:return f"β ERROR: {str(e)}"# setting up the users profile (step 1)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_profile3 unmodified linesuser_profile["field"] = inforeturn "Awesome! Type 'start' below to begin your interview.", user_profile# step 3def respond(message, chat_history, user_profile):message_lower = message.strip().lower()2 unmodified lineschat_history.append((message, bot_msg))return chat_history# interview processif message_lower == 'start':interview_type = user_profile['interview_type']selected_questions = questions_by_type.get(interview_type, [])37 unmodified lineschat_history.append((message, feedback))return chat_history# starting the chatbotmessages = [{"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})5 unmodified lineschat_history.append((message, bot_msg))return chat_historydef 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!"else:fb = f"Question {i+1}: β Missed some key points: {correct_answers[0]}"feedback.append(fb)return "\n".join(feedback)# handle audio inputdef handle_audio(audio_file, chat_history, user_profile):transcribed = transcribe_audio(audio_file)if transcribed.startswith("β"):1 unmodified linereturn chat_historyreturn respond(transcribed, chat_history, user_profile)# creating the visual elementswith gr.Blocks() as demo:user_profile = gr.State({"interview_type": "", "field": "", "interview_in_progress": False})chat_history = gr.State([])31 unmodified linessend_btn = gr.Button("Send Text")audio_btn = gr.Button("Send Audio")send_btn.click(respond, inputs=[msg, chat_history, user_profile], outputs=[chatbot], queue=False)send_btn.click(lambda: "", None, msg, queue=False)audio_btn.click(handle_audio, inputs=[audio_input, chat_history, user_profile], outputs=[chatbot], queue=False)demo.launch()15 unmodified lines161718192021228 unmodified lines3132333435363712 unmodified lines505152535455563 unmodified lines606162636465662 unmodified lines69707172737437 unmodified lines1121131141151161171185 unmodified lines1241251261271281291301311321331341351361371381391401411421431441451461471481491501 unmodified line15215315415515615715831 unmodified lines19019119219319419519619715 unmodified linesfor block in question_blocks:parts = block.split('Possible Answers:')question_text = parts[0].strip()questions.append({'question': question_text})return questionsall_questions = load_questions('knowledge.txt')8 unmodified lines'Competency-Based Interview': [q for q in all_questions if any(keyword in q['question'].lower() for keyword in ["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 ["testing", "financial", "automation", "analysis", "regression", "business", "stakeholder"])]}# models12 unmodified linesexcept Exception as e:return f"β ERROR: {str(e)}"# step 1def set_type(choice, user_profile):user_profile["interview_type"] = choicereturn "Great! Whatβs your background and what field/role are you aiming for?", user_profile3 unmodified linesuser_profile["field"] = inforeturn "Awesome! Type 'start' below to begin your interview.", user_profile# updated step 3def respond(message, chat_history, user_profile):message_lower = message.strip().lower()2 unmodified lineschat_history.append((message, bot_msg))return chat_historyif message_lower == 'start':interview_type = user_profile['interview_type']selected_questions = questions_by_type.get(interview_type, [])37 unmodified lineschat_history.append((message, feedback))return chat_history# chatbot fallbackmessages = [{"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})5 unmodified lineschat_history.append((message, bot_msg))return chat_history# updated feedback using modeldef generate_feedback(user_profile):questions = user_profile.get('questions', [])answers = user_profile.get('user_answers', [])feedback = []for i, (question, user_answer) in enumerate(zip(questions, answers)):prompt = (f"You are an expert interviewer. Please evaluate the following response:\n\n"f"Question: {question['question']}\n"f"Candidate's Answer: {user_answer}\n\n"f"Give a brief, constructive evaluation. Say whether it's a strong, okay, or weak answer and what could be improved.")response = client.text_generation(prompt, max_new_tokens=200)evaluation = response.strip()feedback.append(f"Question {i+1}:\n{evaluation}\n")return "\n".join(feedback)# handle audiodef handle_audio(audio_file, chat_history, user_profile):transcribed = transcribe_audio(audio_file)if transcribed.startswith("β"):1 unmodified linereturn chat_historyreturn respond(transcribed, chat_history, user_profile)# UIwith gr.Blocks() as demo:user_profile = gr.State({"interview_type": "", "field": "", "interview_in_progress": False})chat_history = gr.State([])31 unmodified linessend_btn = gr.Button("Send Text")audio_btn = gr.Button("Send Audio")send_btn.click(respond, inputs=[msg, chat_history, user_profile], outputs=[chatbot], queue=False)send_btn.click(lambda: "", None, msg, queue=False)audio_btn.click(handle_audio, inputs=[audio_input, chat_history, user_profile], outputs=[chatbot], queue=False)demo.launch()
app.py+23-22