Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -5,6 +5,7 @@ import os
|
|
5 |
import gradio_client.utils as client_utils
|
6 |
import sys
|
7 |
|
|
|
8 |
def _patched_json_schema_to_python_type(schema, defs=None, depth=0):
|
9 |
if depth > 100:
|
10 |
return "Any"
|
@@ -18,18 +19,19 @@ def _patched_json_schema_to_python_type(schema, defs=None, depth=0):
|
|
18 |
client_utils._json_schema_to_python_type = _patched_json_schema_to_python_type
|
19 |
sys.setrecursionlimit(10000)
|
20 |
|
21 |
-
#
|
22 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
23 |
|
24 |
-
#
|
25 |
-
|
26 |
-
# Load model and tokenizer
|
27 |
model_name = "AI-Mock-Interviewer/T5"
|
28 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
29 |
model = T5ForConditionalGeneration.from_pretrained(model_name)
|
30 |
|
|
|
|
|
|
|
31 |
|
32 |
-
# System
|
33 |
system_prompt = """
|
34 |
You are conducting a mock technical interview. Generate questions and follow-up questions based on the domain provided. Consider these aspects:
|
35 |
1. The question should be relevant to the domain (e.g., software engineering, machine learning).
|
@@ -41,7 +43,7 @@ You are conducting a mock technical interview. Generate questions and follow-up
|
|
41 |
Important: Ensure that each question is clear, concise, and allows the candidate to demonstrate their technical and communicative abilities effectively.
|
42 |
"""
|
43 |
|
44 |
-
#
|
45 |
subtopic_keywords = {
|
46 |
"data analysis": ["data cleaning", "missing data", "outliers", "feature engineering", "EDA", "trend analysis", "data visualization"],
|
47 |
"machine learning": ["supervised learning", "unsupervised learning", "model evaluation", "bias-variance tradeoff", "overfitting", "hyperparameter tuning"],
|
@@ -49,7 +51,6 @@ subtopic_keywords = {
|
|
49 |
}
|
50 |
|
51 |
def identify_subtopic(question, domain):
|
52 |
-
"""Identify the sub-topic of a question using predefined keywords."""
|
53 |
domain = domain.lower()
|
54 |
if domain in subtopic_keywords:
|
55 |
for subtopic in subtopic_keywords[domain]:
|
@@ -58,7 +59,6 @@ def identify_subtopic(question, domain):
|
|
58 |
return None
|
59 |
|
60 |
def generate_question(prompt, domain, state=None, max_attempts=10):
|
61 |
-
"""Generate a unique interview question while ensuring no repetition."""
|
62 |
attempts = 0
|
63 |
while attempts < max_attempts:
|
64 |
attempts += 1
|
@@ -73,15 +73,14 @@ def generate_question(prompt, domain, state=None, max_attempts=10):
|
|
73 |
top_p=0.9,
|
74 |
temperature=0.7,
|
75 |
do_sample=True,
|
76 |
-
pad_token_id=tokenizer.
|
77 |
)
|
78 |
question = tokenizer.decode(outputs[0], skip_special_tokens=True).strip()
|
79 |
if not question.endswith("?"):
|
80 |
question = question.split("?")[0] + "?"
|
81 |
-
|
82 |
subtopic = identify_subtopic(question, domain)
|
83 |
|
84 |
-
# Ensure uniqueness within the session state
|
85 |
if state:
|
86 |
if question not in state["asked_questions"] and (subtopic is None or subtopic not in state["asked_subtopics"]):
|
87 |
state["asked_questions"].add(question)
|
@@ -92,55 +91,49 @@ def generate_question(prompt, domain, state=None, max_attempts=10):
|
|
92 |
raise RuntimeError("Failed to generate a unique question after multiple attempts.")
|
93 |
|
94 |
def reset_state(domain, company):
|
95 |
-
"""Reset session state for a new interview."""
|
96 |
return {
|
97 |
"domain": domain,
|
98 |
"company": company,
|
99 |
"asked_questions": set(),
|
100 |
"asked_subtopics": set(),
|
101 |
-
"conversation": [] # List of
|
102 |
}
|
103 |
|
104 |
def start_interview(domain, company):
|
105 |
-
"""Start a new interview session."""
|
106 |
state = reset_state(domain, company)
|
107 |
prompt = f"Domain: {domain}. " + (f"Company: {company}. " if company else "") + "Generate the first question:"
|
108 |
question = generate_question(prompt, domain, state)
|
109 |
-
state["conversation"].append(
|
110 |
return state["conversation"], state
|
111 |
|
112 |
def submit_response(candidate_response, state):
|
113 |
-
"""
|
114 |
-
state["conversation"].append(("Candidate", candidate_response))
|
115 |
prompt = f"Domain: {state['domain']}. Candidate's last response: {candidate_response}. Generate a follow-up question with a new perspective:"
|
116 |
question = generate_question(prompt, state["domain"], state)
|
117 |
-
state["conversation"].append(
|
118 |
return state["conversation"], state
|
119 |
|
120 |
-
#
|
121 |
with gr.Blocks() as demo:
|
122 |
-
gr.Markdown("# Interactive Mock Interview")
|
123 |
-
|
124 |
with gr.Row():
|
125 |
domain_input = gr.Textbox(label="Domain")
|
126 |
company_input = gr.Textbox(label="Company (Optional)")
|
127 |
-
|
128 |
-
start_button = gr.Button("Start Interview")
|
129 |
-
chatbot = gr.Chatbot(label="Interview Conversation")
|
130 |
-
|
131 |
with gr.Row():
|
132 |
response_input = gr.Textbox(label="Your Response")
|
133 |
submit_button = gr.Button("Submit")
|
134 |
-
|
135 |
-
#
|
136 |
-
|
137 |
-
|
138 |
-
# Clicking start initializes the interview and shows the first question
|
139 |
start_button.click(start_interview, inputs=[domain_input, company_input], outputs=[chatbot, state])
|
140 |
-
|
141 |
-
# Submitting a response updates the conversation with a follow-up question
|
142 |
submit_button.click(submit_response, inputs=[response_input, state], outputs=[chatbot, state]).then(
|
143 |
-
lambda
|
144 |
)
|
145 |
|
146 |
-
demo.launch()
|
|
|
5 |
import gradio_client.utils as client_utils
|
6 |
import sys
|
7 |
|
8 |
+
# ======== Patch for Gradio JSON Schema Bug ========
|
9 |
def _patched_json_schema_to_python_type(schema, defs=None, depth=0):
|
10 |
if depth > 100:
|
11 |
return "Any"
|
|
|
19 |
client_utils._json_schema_to_python_type = _patched_json_schema_to_python_type
|
20 |
sys.setrecursionlimit(10000)
|
21 |
|
22 |
+
# ======== Setup Device ========
|
23 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
24 |
|
25 |
+
# ======== Load Model and Tokenizer ========
|
|
|
|
|
26 |
model_name = "AI-Mock-Interviewer/T5"
|
27 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
28 |
model = T5ForConditionalGeneration.from_pretrained(model_name)
|
29 |
|
30 |
+
# Fix for attention mask warning
|
31 |
+
if tokenizer.pad_token is None:
|
32 |
+
tokenizer.pad_token = tokenizer.eos_token
|
33 |
|
34 |
+
# ======== System Prompt ========
|
35 |
system_prompt = """
|
36 |
You are conducting a mock technical interview. Generate questions and follow-up questions based on the domain provided. Consider these aspects:
|
37 |
1. The question should be relevant to the domain (e.g., software engineering, machine learning).
|
|
|
43 |
Important: Ensure that each question is clear, concise, and allows the candidate to demonstrate their technical and communicative abilities effectively.
|
44 |
"""
|
45 |
|
46 |
+
# ======== Subtopic Keywords ========
|
47 |
subtopic_keywords = {
|
48 |
"data analysis": ["data cleaning", "missing data", "outliers", "feature engineering", "EDA", "trend analysis", "data visualization"],
|
49 |
"machine learning": ["supervised learning", "unsupervised learning", "model evaluation", "bias-variance tradeoff", "overfitting", "hyperparameter tuning"],
|
|
|
51 |
}
|
52 |
|
53 |
def identify_subtopic(question, domain):
|
|
|
54 |
domain = domain.lower()
|
55 |
if domain in subtopic_keywords:
|
56 |
for subtopic in subtopic_keywords[domain]:
|
|
|
59 |
return None
|
60 |
|
61 |
def generate_question(prompt, domain, state=None, max_attempts=10):
|
|
|
62 |
attempts = 0
|
63 |
while attempts < max_attempts:
|
64 |
attempts += 1
|
|
|
73 |
top_p=0.9,
|
74 |
temperature=0.7,
|
75 |
do_sample=True,
|
76 |
+
pad_token_id=tokenizer.pad_token_id,
|
77 |
)
|
78 |
question = tokenizer.decode(outputs[0], skip_special_tokens=True).strip()
|
79 |
if not question.endswith("?"):
|
80 |
question = question.split("?")[0] + "?"
|
81 |
+
|
82 |
subtopic = identify_subtopic(question, domain)
|
83 |
|
|
|
84 |
if state:
|
85 |
if question not in state["asked_questions"] and (subtopic is None or subtopic not in state["asked_subtopics"]):
|
86 |
state["asked_questions"].add(question)
|
|
|
91 |
raise RuntimeError("Failed to generate a unique question after multiple attempts.")
|
92 |
|
93 |
def reset_state(domain, company):
|
|
|
94 |
return {
|
95 |
"domain": domain,
|
96 |
"company": company,
|
97 |
"asked_questions": set(),
|
98 |
"asked_subtopics": set(),
|
99 |
+
"conversation": [] # List of dicts: {"role": ..., "content": ...}
|
100 |
}
|
101 |
|
102 |
def start_interview(domain, company):
|
|
|
103 |
state = reset_state(domain, company)
|
104 |
prompt = f"Domain: {domain}. " + (f"Company: {company}. " if company else "") + "Generate the first question:"
|
105 |
question = generate_question(prompt, domain, state)
|
106 |
+
state["conversation"].append({"role": "assistant", "content": question})
|
107 |
return state["conversation"], state
|
108 |
|
109 |
def submit_response(candidate_response, state):
|
110 |
+
state["conversation"].append({"role": "user", "content": candidate_response})
|
|
|
111 |
prompt = f"Domain: {state['domain']}. Candidate's last response: {candidate_response}. Generate a follow-up question with a new perspective:"
|
112 |
question = generate_question(prompt, state["domain"], state)
|
113 |
+
state["conversation"].append({"role": "assistant", "content": question})
|
114 |
return state["conversation"], state
|
115 |
|
116 |
+
# ======== Gradio Interface ========
|
117 |
with gr.Blocks() as demo:
|
118 |
+
gr.Markdown("# 🎙️ Interactive Mock Interview")
|
119 |
+
|
120 |
with gr.Row():
|
121 |
domain_input = gr.Textbox(label="Domain")
|
122 |
company_input = gr.Textbox(label="Company (Optional)")
|
123 |
+
|
124 |
+
start_button = gr.Button("🚀 Start Interview")
|
125 |
+
chatbot = gr.Chatbot(label="Interview Conversation", type="messages")
|
126 |
+
|
127 |
with gr.Row():
|
128 |
response_input = gr.Textbox(label="Your Response")
|
129 |
submit_button = gr.Button("Submit")
|
130 |
+
|
131 |
+
state = gr.State({}) # Session state holder
|
132 |
+
|
133 |
+
# Hook buttons to logic
|
|
|
134 |
start_button.click(start_interview, inputs=[domain_input, company_input], outputs=[chatbot, state])
|
|
|
|
|
135 |
submit_button.click(submit_response, inputs=[response_input, state], outputs=[chatbot, state]).then(
|
136 |
+
lambda: "", inputs=[], outputs=[response_input] # Clear textbox after submit
|
137 |
)
|
138 |
|
139 |
+
demo.launch()
|