Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,143 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
import gradio as gr
|
3 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
4 |
+
import torch
|
5 |
+
import os
|
6 |
+
|
7 |
+
# Set up device
|
8 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
9 |
+
|
10 |
+
hf_token = os.environ["HF_TOKEN"]
|
11 |
+
|
12 |
+
# Load model and tokenizer from local files
|
13 |
+
model_path = "Mohinikathro/T5" # Assuming all files are in the root directory
|
14 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
15 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_path)
|
16 |
+
|
17 |
+
# Move model to the appropriate device
|
18 |
+
model.to(device)
|
19 |
+
|
20 |
+
# ------------------- SYSTEM PROMPT -------------------
|
21 |
+
system_prompt = """
|
22 |
+
You are conducting a mock technical interview. The candidate's experience level can be entry-level, mid-level, or senior-level. Generate questions and follow-up questions based on the domain and the candidate's experience level. Consider these aspects:
|
23 |
+
1. The question should be relevant to the domain (e.g., software engineering, machine learning) and appropriate for the candidate's experience level.
|
24 |
+
2. For follow-up questions, analyze the candidate's last response and ask questions that probe deeper into their understanding, challenge their approach, or request clarification.
|
25 |
+
3. The follow-up question should aim to explore the candidate's depth of knowledge and ability to adapt.
|
26 |
+
4. Ensure each question is unique and does not repeat previously asked questions.
|
27 |
+
5. Ensure each question covers a different sub-topic within the domain, avoiding redundancy.
|
28 |
+
6. If no clear follow-up can be derived, generate a fresh, related question from a different aspect of the domain.
|
29 |
+
Important: Ensure that each question is clear, concise, and allows the candidate to demonstrate their technical and communicative abilities effectively.
|
30 |
+
"""
|
31 |
+
|
32 |
+
# Define sub-topic categories for different domains
|
33 |
+
subtopic_keywords = {
|
34 |
+
"data analysis": ["data cleaning", "missing data", "EDA", "visualization"],
|
35 |
+
"machine learning": ["supervised learning", "overfitting", "hyperparameter tuning"],
|
36 |
+
"software engineering": ["code optimization", "design patterns", "database design"],
|
37 |
+
}
|
38 |
+
|
39 |
+
def identify_subtopic(question, domain):
|
40 |
+
"""Identify the sub-topic of a question using predefined keywords."""
|
41 |
+
domain = domain.lower()
|
42 |
+
if domain in subtopic_keywords:
|
43 |
+
for subtopic in subtopic_keywords[domain]:
|
44 |
+
if subtopic in question.lower():
|
45 |
+
return subtopic
|
46 |
+
return None
|
47 |
+
|
48 |
+
# Tracking asked questions
|
49 |
+
def generate_question(prompt, domain, state=None):
|
50 |
+
"""
|
51 |
+
Generates a unique question based on the prompt and domain.
|
52 |
+
Uses 'state' to track uniqueness in the conversation session.
|
53 |
+
"""
|
54 |
+
full_prompt = system_prompt + "\n" + prompt
|
55 |
+
inputs = tokenizer(full_prompt, return_tensors="pt").to(device)
|
56 |
+
|
57 |
+
outputs = model.generate(
|
58 |
+
inputs["input_ids"],
|
59 |
+
max_new_tokens=50,
|
60 |
+
num_return_sequences=1,
|
61 |
+
no_repeat_ngram_size=2,
|
62 |
+
top_k=30,
|
63 |
+
top_p=0.9,
|
64 |
+
temperature=0.7,
|
65 |
+
do_sample=True,
|
66 |
+
pad_token_id=tokenizer.eos_token_id,
|
67 |
+
)
|
68 |
+
|
69 |
+
question = tokenizer.decode(outputs[0], skip_special_tokens=True).strip()
|
70 |
+
|
71 |
+
# Ensure question ends with a question mark
|
72 |
+
if not question.endswith("?"):
|
73 |
+
question += "?"
|
74 |
+
|
75 |
+
# Identify the subtopic
|
76 |
+
subtopic = identify_subtopic(question, domain)
|
77 |
+
|
78 |
+
# Check for uniqueness
|
79 |
+
if state is not None:
|
80 |
+
if (question not in state["asked_questions"] and
|
81 |
+
(subtopic is None or subtopic not in state["asked_subtopics"])):
|
82 |
+
state["asked_questions"].add(question)
|
83 |
+
if subtopic:
|
84 |
+
state["asked_subtopics"].add(subtopic)
|
85 |
+
return question
|
86 |
+
return question # Fallback
|
87 |
+
|
88 |
+
# Initialize conversation state
|
89 |
+
def reset_state(domain, company, level):
|
90 |
+
return {
|
91 |
+
"domain": domain,
|
92 |
+
"company": company,
|
93 |
+
"level": level,
|
94 |
+
"asked_questions": set(),
|
95 |
+
"asked_subtopics": set(),
|
96 |
+
"conversation": [] # List of (speaker, message) tuples
|
97 |
+
}
|
98 |
+
|
99 |
+
def start_interview(domain, company, level):
|
100 |
+
state = reset_state(domain, company, level)
|
101 |
+
prompt = f"Domain: {domain}. Candidate experience level: {level}. Generate the first question:"
|
102 |
+
|
103 |
+
question = generate_question(prompt, domain, state)
|
104 |
+
state["conversation"].append(("Interviewer", question))
|
105 |
+
return state["conversation"], state
|
106 |
+
|
107 |
+
def submit_response(candidate_response, state):
|
108 |
+
state["conversation"].append(("Candidate", candidate_response))
|
109 |
+
prompt = (f"Domain: {state['domain']}. Candidate's last response: {candidate_response}. Generate a follow-up question:")
|
110 |
+
|
111 |
+
question = generate_question(prompt, state["domain"], state)
|
112 |
+
state["conversation"].append(("Interviewer", question))
|
113 |
+
return state["conversation"], state
|
114 |
+
|
115 |
+
# ----------- Gradio UI -----------
|
116 |
+
|
117 |
+
with gr.Blocks() as demo:
|
118 |
+
gr.Markdown("# Interactive AI-Powered Mock Interview")
|
119 |
+
|
120 |
+
with gr.Row():
|
121 |
+
domain_input = gr.Textbox(label="Domain", placeholder="e.g. Software Engineering")
|
122 |
+
company_input = gr.Textbox(label="Company (Optional)", placeholder="e.g. Google")
|
123 |
+
level_input = gr.Dropdown(
|
124 |
+
label="Experience Level",
|
125 |
+
choices=["Entry-Level", "Mid-Level", "Senior-Level"],
|
126 |
+
value="Entry-Level"
|
127 |
+
)
|
128 |
+
|
129 |
+
start_button = gr.Button("Start Interview")
|
130 |
+
chatbot = gr.Chatbot(label="Interview Conversation")
|
131 |
+
|
132 |
+
with gr.Row():
|
133 |
+
response_input = gr.Textbox(label="Your Response")
|
134 |
+
submit_button = gr.Button("Submit")
|
135 |
+
clear_button = gr.Button("Clear Chat")
|
136 |
+
|
137 |
+
state = gr.State() # Holds session data
|
138 |
+
|
139 |
+
start_button.click(start_interview, inputs=[domain_input, company_input, level_input], outputs=[chatbot, state])
|
140 |
+
submit_button.click(submit_response, inputs=[response_input, state], outputs=[chatbot, state]).then(lambda: "", None, response_input)
|
141 |
+
clear_button.click(lambda: ([], None), outputs=[chatbot, state])
|
142 |
+
|
143 |
+
demo.launch()
|