Spaces:
Sleeping
Sleeping
Create app.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160import gradio as grfrom huggingface_hub import InferenceClientimport reimport random
# uploading and cleaning the knowledge txt filedef 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')
# creating the questions based on each interviewquestions_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'])], '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"])] }
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
# setting up the users profiledef 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
def save_background(info, user_profile): user_profile["field"] = info return "Awesome! Type 'start' below to begin your interview.", user_profile
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
# interview process 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'] = [] 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 user_profile.get("interview_in_progress"): 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: 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
if message_lower == 'feedback': feedback = generate_feedback(user_profile) chat_history.append((message, feedback)) return chat_history
# starting the chatbot 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}) messages.append({"role": "user", "content": message})
response = client.chat_completion(messages, max_tokens=150, stream=False) bot_msg = response.choices[0].message.content chat_history.append((message, bot_msg)) return chat_history
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!" else: fb = f"Question {i+1}: ❌ Missed some key points: {correct_answers[0]}" feedback.append(fb) return "\n".join(feedback)
# creating the visual elementswith gr.Blocks() as demo: user_profile = gr.State({"interview_type": "", "field": "", "interview_in_progress": False}) chat_history = gr.State([])
gr.Markdown("# Welcome to Intervu") gr.Markdown('<p align="center"><img src="images.jpeg" width="200"></p>')
gr.Markdown("### Step 1: Choose Interview Type") with gr.Row(): with gr.Column(): btn1 = gr.Button("Technical") btn2 = gr.Button("Competency-Based Interview") btn3 = gr.Button("Case") type_output = gr.Textbox(label="Bot response", interactive=False)
btn1.click(set_type, inputs=[gr.Textbox(value="Technical", visible=False), user_profile], outputs=[type_output, user_profile]) btn2.click(set_type, inputs=[gr.Textbox(value="Competency-Based Interview", visible=False), user_profile], outputs=[type_output, user_profile]) btn3.click(set_type, inputs=[gr.Textbox(value="Case", 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") background_output = gr.Textbox(label="Bot response", interactive=False)
background_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") send_btn = gr.Button("Send")
send_btn.click(respond, inputs=[msg, chat_history, user_profile], outputs=[chatbot], queue=False) send_btn.click(lambda: "", None, msg, queue=False)
demo.launch()app.pyAdded+160