Spaces:
Runtime error
Runtime error
Update app.py
Browse files12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788897 unmodified lines979899100101102103104105106107108109110111112113114115116117118119120121122391231241256 unmodified lines132133134521351361371 unmodified line139140141601421431443 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 questions
all_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"] = 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): 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': 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)) 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
# Handle feedback request if message.strip().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."} ]7 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." 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")
# Step 1 gr.Markdown("### Step 1: Choose Interview Type") with gr.Row(): with gr.Column():6 unmodified lines btn2.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 2 gr.Markdown("### Step 2: Enter Your Background") background = gr.Textbox(label="Your background and field/goal") background_btn = gr.Button("Submit")1 unmodified line
background_btn.click(save_background, inputs=[background, user_profile], outputs=[background_output, user_profile])
# Step 3 gr.Markdown("### Step 3: Start Interview") chatbot = gr.Chatbot(label="Interview Bot") msg = gr.Textbox(label="Your message")3 unmodified lines send_btn.click(lambda: "", None, msg, queue=False)
demo.launch()
app.py+85-3