Spaces:
Runtime error
Runtime error
Update app.py
Browse files123456789101112131415161718192021227 unmodified lines303132333435363738394041426 unmodified lines495051525354551 unmodified line575859606162633 unmodified lines676869import gradio as grfrom huggingface_hub import InferenceClientclient = InferenceClient("HuggingFaceH4/zephyr-7b-beta")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_profiledef save_background(info, user_profile):user_profile["field"] = inforeturn "Awesome! Type 'start' below to begin your interview.", user_profiledef respond(message, chat_history, user_profile):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_historymessages = [{"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_historywith gr.Blocks() as demo:user_profile = gr.State({"interview_type": "", "field": ""})chat_history = gr.State([])gr.Markdown("# π€ Welcome to Intervu")# Step 1gr.Markdown("### Step 1: Choose Interview Type")with gr.Row():with gr.Column():6 unmodified linesbtn2.click(set_type, inputs=[gr.Textbox(value="Technical", visible=False), user_profile], outputs=[type_output, user_profile])btn3.click(set_type, inputs=[gr.Textbox(value="College", visible=False), user_profile], outputs=[type_output, user_profile])# Step 2gr.Markdown("### Step 2: Enter Your Background")background = gr.Textbox(label="Your background and field/goal")background_btn = gr.Button("Submit")1 unmodified linebackground_btn.click(save_background, inputs=[background, user_profile], outputs=[background_output, user_profile])# Step 3gr.Markdown("### Step 3: Start Interview")chatbot = gr.Chatbot(label="Interview Bot")msg = gr.Textbox(label="Your message")3 unmodified linessend_btn.click(lambda: "", None, msg, queue=False)demo.launch()12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788897 unmodified lines9798991001011021031041051061071081091101111121131141151161171181191201211221231241256 unmodified lines1321331341351361371 unmodified line1391401411421431443 unmodified lines148149150151import gradio as grfrom 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()question_blocks = re.split(r'Question:\s*', data)[1:]questions = []for 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')# ---- Simple way to assign questions to interview types ----# You can replace this later with better taggingquestions_by_type = {'Technical': [q for q in all_questions if any(keyword in q['question'].lower() for keyword in ['function', 'algorithm', 'sql', 'system', 'java', 'programming', 'data', 'design', 'api', 'distributed', 'garbage', 'hash', 'stack', 'bfs', 'dfs'])],'Behavioral': [q for q in all_questions if any(keyword in q['question'].lower() for keyword in ['tell me about a time', 'describe a situation', 'example of', 'negotiation', 'lead', 'mistake', 'stakeholder'])],'College': [] # For now keep empty unless you add questions for this}# ---- Hugging Face Client ----client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")# ---- Interview type selection ----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):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 logicif message.strip().lower() == '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# If interview is ongoingif user_profile.get("questions"):q_index = user_profile['current_q']user_profile['user_answers'].append(message)q_index += 1user_profile['current_q'] = q_indexif 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# Handle feedback requestif message.strip().lower() == 'feedback':feedback = generate_feedback(user_profile)chat_history.append((message, feedback))return chat_history# Default fallbackmessages = [{"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# ---- 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."else:fb = f"Question {i+1}: β You missed some key points: {correct_answers[0]}"feedback.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")gr.Markdown("### Step 1: Choose Interview Type")with gr.Row():with gr.Column():6 unmodified linesbtn2.click(set_type, inputs=[gr.Textbox(value="Technical", visible=False), user_profile], outputs=[type_output, user_profile])btn3.click(set_type, inputs=[gr.Textbox(value="College", visible=False), user_profile], outputs=[type_output, user_profile])gr.Markdown("### Step 2: Enter Your Background")background = gr.Textbox(label="Your background and field/goal")background_btn = gr.Button("Submit")1 unmodified linebackground_btn.click(save_background, inputs=[background, user_profile], outputs=[background_output, user_profile])gr.Markdown("### Step 3: Start Interview")chatbot = gr.Chatbot(label="Interview Bot")msg = gr.Textbox(label="Your message")3 unmodified linessend_btn.click(lambda: "", None, msg, queue=False)demo.launch()
app.py+85-3