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| 1 |
+
import gradio as gr
|
| 2 |
+
from huggingface_hub import InferenceClient
|
| 3 |
+
import re
|
| 4 |
+
import random
|
| 5 |
+
|
| 6 |
+
# uploading and cleaning the knowledge txt file
|
| 7 |
+
def load_questions(file_path):
|
| 8 |
+
with open(file_path, 'r') as f:
|
| 9 |
+
data = f.read()
|
| 10 |
+
|
| 11 |
+
question_blocks = re.split(r'Question:\s*', data)[1:]
|
| 12 |
+
questions = []
|
| 13 |
+
for block in question_blocks:
|
| 14 |
+
parts = block.split('Possible Answers:')
|
| 15 |
+
question_text = parts[0].strip()
|
| 16 |
+
answers_text = parts[1].strip()
|
| 17 |
+
possible_answers = [ans.strip() for ans in re.split(r'\d+\.\s+', answers_text) if ans.strip()]
|
| 18 |
+
questions.append({'question': question_text, 'answers': possible_answers})
|
| 19 |
+
return questions
|
| 20 |
+
|
| 21 |
+
all_questions = load_questions('knowledge.txt')
|
| 22 |
+
|
| 23 |
+
# creating the questions based on each interview
|
| 24 |
+
questions_by_type = {
|
| 25 |
+
'Technical': [q for q in all_questions if any(keyword in q['question'].lower() for keyword in [
|
| 26 |
+
'function', 'linked list', 'url', 'rest', 'graphql', 'garbage', 'cap theorem', 'sql', 'hash table',
|
| 27 |
+
'stack', 'queue', 'recursion', 'reverse', 'bfs', 'dfs', 'time complexity', 'binary search tree',
|
| 28 |
+
'web application', 'chat system', 'load balancing', 'caching', 'normalization', 'acid', 'indexing',
|
| 29 |
+
'sql injection', 'https', 'xss', 'hash', 'vulnerabilities'])],
|
| 30 |
+
'Competency-Based Interview': [q for q in all_questions if any(keyword in q['question'].lower() for keyword in [
|
| 31 |
+
"debugging", "learning", "deadlines", "teamwork", "leadership", "mistake", "conflict", "decision"])],
|
| 32 |
+
'Case': [q for q in all_questions if any(keyword in q['question'].lower() for keyword in [
|
| 33 |
+
"testing", "financial", "automation", "analysis", "regression", "business", "stakeholder"])]
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 37 |
+
|
| 38 |
+
# setting up the users profile
|
| 39 |
+
def set_type(choice, user_profile):
|
| 40 |
+
user_profile["interview_type"] = choice
|
| 41 |
+
return "Great! What’s your background and what field/role are you aiming for?", user_profile
|
| 42 |
+
|
| 43 |
+
def save_background(info, user_profile):
|
| 44 |
+
user_profile["field"] = info
|
| 45 |
+
return "Awesome! Type 'start' below to begin your interview.", user_profile
|
| 46 |
+
|
| 47 |
+
def respond(message, chat_history, user_profile):
|
| 48 |
+
message_lower = message.strip().lower()
|
| 49 |
+
|
| 50 |
+
if not user_profile.get("interview_type") or not user_profile.get("field"):
|
| 51 |
+
bot_msg = "Please finish steps 1 and 2 before starting the interview."
|
| 52 |
+
chat_history.append((message, bot_msg))
|
| 53 |
+
return chat_history
|
| 54 |
+
|
| 55 |
+
# interview process
|
| 56 |
+
if message_lower == 'start':
|
| 57 |
+
interview_type = user_profile['interview_type']
|
| 58 |
+
selected_questions = questions_by_type.get(interview_type, [])
|
| 59 |
+
random.shuffle(selected_questions)
|
| 60 |
+
selected_questions = selected_questions[:10]
|
| 61 |
+
|
| 62 |
+
user_profile['questions'] = selected_questions
|
| 63 |
+
user_profile['current_q'] = 0
|
| 64 |
+
user_profile['user_answers'] = []
|
| 65 |
+
user_profile['interview_in_progress'] = True
|
| 66 |
+
|
| 67 |
+
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."
|
| 68 |
+
first_q = f"First question: {selected_questions[0]['question']}"
|
| 69 |
+
chat_history.append((message, intro))
|
| 70 |
+
chat_history.append(("", first_q))
|
| 71 |
+
return chat_history
|
| 72 |
+
|
| 73 |
+
if message_lower == 'stop' and user_profile.get("interview_in_progress"):
|
| 74 |
+
user_profile['interview_in_progress'] = False
|
| 75 |
+
bot_msg = "Interview stopped. Type 'feedback' if you'd like me to analyze your answers. Thanks for interviewing with Intervu!"
|
| 76 |
+
chat_history.append((message, bot_msg))
|
| 77 |
+
return chat_history
|
| 78 |
+
|
| 79 |
+
if user_profile.get("interview_in_progress"):
|
| 80 |
+
q_index = user_profile['current_q']
|
| 81 |
+
user_profile['user_answers'].append(message)
|
| 82 |
+
|
| 83 |
+
q_index += 1
|
| 84 |
+
user_profile['current_q'] = q_index
|
| 85 |
+
|
| 86 |
+
if q_index < len(user_profile['questions']):
|
| 87 |
+
bot_msg = f"Next question: {user_profile['questions'][q_index]['question']}"
|
| 88 |
+
else:
|
| 89 |
+
user_profile['interview_in_progress'] = False
|
| 90 |
+
bot_msg = "Interview complete! Type 'feedback' if you'd like me to analyze your answers. Thanks for interviewing with Intervu!"
|
| 91 |
+
chat_history.append((message, bot_msg))
|
| 92 |
+
return chat_history
|
| 93 |
+
|
| 94 |
+
if message_lower == 'feedback':
|
| 95 |
+
feedback = generate_feedback(user_profile)
|
| 96 |
+
chat_history.append((message, feedback))
|
| 97 |
+
return chat_history
|
| 98 |
+
|
| 99 |
+
# starting the chatbot
|
| 100 |
+
messages = [{"role": "system", "content": f"You are a professional interviewer conducting a {user_profile['interview_type']} interview for a candidate in {user_profile['field']}."}]
|
| 101 |
+
for q, a in chat_history:
|
| 102 |
+
messages.append({"role": "user", "content": q})
|
| 103 |
+
messages.append({"role": "assistant", "content": a})
|
| 104 |
+
messages.append({"role": "user", "content": message})
|
| 105 |
+
|
| 106 |
+
response = client.chat_completion(messages, max_tokens=150, stream=False)
|
| 107 |
+
bot_msg = response.choices[0].message.content
|
| 108 |
+
chat_history.append((message, bot_msg))
|
| 109 |
+
return chat_history
|
| 110 |
+
|
| 111 |
+
def generate_feedback(user_profile):
|
| 112 |
+
feedback = []
|
| 113 |
+
questions = user_profile.get('questions', [])
|
| 114 |
+
answers = user_profile.get('user_answers', [])
|
| 115 |
+
for i, user_ans in enumerate(answers):
|
| 116 |
+
correct_answers = questions[i]['answers']
|
| 117 |
+
match = any(ans.lower() in user_ans.lower() for ans in correct_answers)
|
| 118 |
+
if match:
|
| 119 |
+
fb = f"Question {i+1}: ✅ Good job!"
|
| 120 |
+
else:
|
| 121 |
+
fb = f"Question {i+1}: ❌ Missed some key points: {correct_answers[0]}"
|
| 122 |
+
feedback.append(fb)
|
| 123 |
+
return "\n".join(feedback)
|
| 124 |
+
|
| 125 |
+
# creating the visual elements
|
| 126 |
+
with gr.Blocks() as demo:
|
| 127 |
+
user_profile = gr.State({"interview_type": "", "field": "", "interview_in_progress": False})
|
| 128 |
+
chat_history = gr.State([])
|
| 129 |
+
|
| 130 |
+
gr.Markdown("# Welcome to Intervu")
|
| 131 |
+
gr.Markdown('<p align="center"><img src="images.jpeg" width="200"></p>')
|
| 132 |
+
|
| 133 |
+
gr.Markdown("### Step 1: Choose Interview Type")
|
| 134 |
+
with gr.Row():
|
| 135 |
+
with gr.Column():
|
| 136 |
+
btn1 = gr.Button("Technical")
|
| 137 |
+
btn2 = gr.Button("Competency-Based Interview")
|
| 138 |
+
btn3 = gr.Button("Case")
|
| 139 |
+
type_output = gr.Textbox(label="Bot response", interactive=False)
|
| 140 |
+
|
| 141 |
+
btn1.click(set_type, inputs=[gr.Textbox(value="Technical", visible=False), user_profile], outputs=[type_output, user_profile])
|
| 142 |
+
btn2.click(set_type, inputs=[gr.Textbox(value="Competency-Based Interview", visible=False), user_profile], outputs=[type_output, user_profile])
|
| 143 |
+
btn3.click(set_type, inputs=[gr.Textbox(value="Case", visible=False), user_profile], outputs=[type_output, user_profile])
|
| 144 |
+
|
| 145 |
+
gr.Markdown("### Step 2: Enter Your Background")
|
| 146 |
+
background = gr.Textbox(label="Your background and field/goal")
|
| 147 |
+
background_btn = gr.Button("Submit")
|
| 148 |
+
background_output = gr.Textbox(label="Bot response", interactive=False)
|
| 149 |
+
|
| 150 |
+
background_btn.click(save_background, inputs=[background, user_profile], outputs=[background_output, user_profile])
|
| 151 |
+
|
| 152 |
+
gr.Markdown("### Step 3: Start Interview")
|
| 153 |
+
chatbot = gr.Chatbot(label="Interview Bot")
|
| 154 |
+
msg = gr.Textbox(label="Your message")
|
| 155 |
+
send_btn = gr.Button("Send")
|
| 156 |
+
|
| 157 |
+
send_btn.click(respond, inputs=[msg, chat_history, user_profile], outputs=[chatbot], queue=False)
|
| 158 |
+
send_btn.click(lambda: "", None, msg, queue=False)
|
| 159 |
+
|
| 160 |
+
demo.launch()
|