Spaces:
Paused
Paused
fix: E5 semantic relevance pre-check + score cap + stricter grading prompt
Browse files
app.py
CHANGED
|
@@ -211,6 +211,22 @@ def check_english(text: str) -> bool:
|
|
| 211 |
return True # Fail open on very short / ambiguous text
|
| 212 |
|
| 213 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 214 |
def evaluate_and_format(question_text, candidate_answer, user_role, user_sector, user_interviewer, user_level):
|
| 215 |
if not candidate_answer.strip():
|
| 216 |
return "", "Please type an answer before submitting."
|
|
@@ -229,22 +245,41 @@ def evaluate_and_format(question_text, candidate_answer, user_role, user_sector,
|
|
| 229 |
"π« **Invalid Answer Detected**\n\nYour submission appears to contain instructions directed at the AI grader rather than a genuine interview answer.\n\nPlease provide a real response to the interview question."
|
| 230 |
)
|
| 231 |
|
| 232 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 233 |
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
4. If the candidate's answer is very short, vague, or a variation of "I don't know", you MUST give a Grade of 1/10 and explicitly state they failed to provide an answer in the Cons.
|
| 239 |
-
5. A concise answer is NOT a bad answer. Judge by correctness and relevance, NOT by length.
|
| 240 |
|
| 241 |
-
|
| 242 |
-
- 9-10: The answer is correct, demonstrates clear understanding, and covers the key points. It does NOT need to be perfect or exhaustive. A real interviewer would be impressed.
|
| 243 |
-
- 7-8: The answer is decent but has noticeable gaps in reasoning or misses important concepts.
|
| 244 |
-
- 4-6: The answer is partially correct but shows weak understanding or is too surface-level.
|
| 245 |
-
- 1-3: The answer is mostly wrong, irrelevant, or the candidate did not attempt it.
|
| 246 |
|
| 247 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 248 |
|
| 249 |
GRADING EXAMPLES (use these to calibrate your scoring):
|
| 250 |
|
|
@@ -295,13 +330,17 @@ Example Answer:
|
|
| 295 |
outputs = generator(messages, max_new_tokens=400, temperature=0.15, do_sample=True)
|
| 296 |
raw_feedback = outputs[0]['generated_text'][-1]['content']
|
| 297 |
|
| 298 |
-
#
|
| 299 |
match = re.search(r'Grade:\s*(\d+)', raw_feedback)
|
| 300 |
if match:
|
| 301 |
model_score = int(match.group(1))
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 305 |
if word_count >= 40:
|
| 306 |
min_grade = 4
|
| 307 |
elif word_count >= 20:
|
|
@@ -310,10 +349,10 @@ Example Answer:
|
|
| 310 |
min_grade = 2
|
| 311 |
else:
|
| 312 |
min_grade = 1
|
| 313 |
-
|
| 314 |
if model_score < min_grade:
|
| 315 |
-
|
| 316 |
-
raw_feedback = re.sub(r'Grade:\s*\d+', f'Grade: {
|
| 317 |
|
| 318 |
score_html = create_circular_progress(raw_feedback)
|
| 319 |
feedback_html = format_feedback_html(re.sub(r'Grade:.*?\n', '', raw_feedback).strip())
|
|
|
|
| 211 |
return True # Fail open on very short / ambiguous text
|
| 212 |
|
| 213 |
|
| 214 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 215 |
+
# RELEVANCE PRE-CHECK: Use E5 to catch nonsense/off-topic answers
|
| 216 |
+
# before wasting a model call
|
| 217 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 218 |
+
|
| 219 |
+
def check_relevance(question: str, answer: str) -> float:
|
| 220 |
+
"""Returns cosine similarity [0-1] between question and answer embeddings."""
|
| 221 |
+
try:
|
| 222 |
+
q_emb = final_model.encode([f"query: {question}"], normalize_embeddings=True)
|
| 223 |
+
a_emb = final_model.encode([f"passage: {answer}"], normalize_embeddings=True)
|
| 224 |
+
sim = float(cosine_similarity(q_emb, a_emb)[0][0])
|
| 225 |
+
return sim
|
| 226 |
+
except Exception:
|
| 227 |
+
return 1.0 # fail open
|
| 228 |
+
|
| 229 |
+
|
| 230 |
def evaluate_and_format(question_text, candidate_answer, user_role, user_sector, user_interviewer, user_level):
|
| 231 |
if not candidate_answer.strip():
|
| 232 |
return "", "Please type an answer before submitting."
|
|
|
|
| 245 |
"π« **Invalid Answer Detected**\n\nYour submission appears to contain instructions directed at the AI grader rather than a genuine interview answer.\n\nPlease provide a real response to the interview question."
|
| 246 |
)
|
| 247 |
|
| 248 |
+
# ββ Guard 3: Semantic Relevance Check (E5) βββββββββββββββββββββββββββββββ
|
| 249 |
+
relevance_score = check_relevance(question_text, candidate_answer)
|
| 250 |
+
word_count = len(candidate_answer.split())
|
| 251 |
+
|
| 252 |
+
# Instant fail: clearly irrelevant or gibberish (very short + off-topic)
|
| 253 |
+
if relevance_score < 0.25 or (word_count <= 6 and relevance_score < 0.40):
|
| 254 |
+
instant_feedback = format_feedback_html(
|
| 255 |
+
"Pros:\n- None identified\nCons:\n- The answer does not address the question at all.\n"
|
| 256 |
+
"- Please read the question carefully and provide a relevant response."
|
| 257 |
+
)
|
| 258 |
+
return create_circular_progress("Grade: 1"), instant_feedback
|
| 259 |
|
| 260 |
+
# Low relevance: cap the maximum score the model can give
|
| 261 |
+
max_score_from_relevance = None
|
| 262 |
+
if relevance_score < 0.40:
|
| 263 |
+
max_score_from_relevance = 3 # hard cap for low-relevance answers
|
|
|
|
|
|
|
| 264 |
|
| 265 |
+
system_prompt = f"""You are a {user_interviewer} evaluating a {user_level} {user_role} candidate in the {user_sector} sector.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 266 |
|
| 267 |
+
CRITICAL RULES:
|
| 268 |
+
1. READ THE CANDIDATE'S ANSWER CAREFULLY. You MUST base your evaluation ONLY on what is literally written in [BEGIN CANDIDATE ANSWER]. Do NOT imagine or infer content that is not there.
|
| 269 |
+
2. Before deciding on a grade, mentally ask yourself: "Did the candidate actually say anything relevant to the question?" If the answer is "no" or "barely", the grade MUST be 1-2.
|
| 270 |
+
3. Speak DIRECTLY to the candidate using "you" and "your". Never use the word "candidate".
|
| 271 |
+
4. Do NOT penalize the candidate for constraints mentioned in the [INTERVIEW QUESTION] itself.
|
| 272 |
+
5. You MUST generate an Example Answer at the very end. Keep it 2 sentences max.
|
| 273 |
+
6. If the answer is vague, nonsensical, off-topic, a single sentence with no substance, or a variation of "I don't know", you MUST give a Grade of 1/10.
|
| 274 |
+
7. A genuinely concise but CORRECT answer is fine. Judge correctness and relevance, NOT length.
|
| 275 |
+
|
| 276 |
+
GRADING SCALE (follow strictly):
|
| 277 |
+
- 9-10: Correct, shows clear understanding, covers key points. A real interviewer would be impressed.
|
| 278 |
+
- 7-8: Decent but noticeable gaps in reasoning or missing important concepts.
|
| 279 |
+
- 4-6: Partially correct but weak understanding or too surface-level.
|
| 280 |
+
- 1-3: Mostly wrong, irrelevant, or the candidate did not attempt to answer.
|
| 281 |
+
|
| 282 |
+
IMPORTANT: Only list a Pro if the candidate ACTUALLY SAID something that demonstrates that strength. Do NOT invent Pros based on what a good answer would say.
|
| 283 |
|
| 284 |
GRADING EXAMPLES (use these to calibrate your scoring):
|
| 285 |
|
|
|
|
| 330 |
outputs = generator(messages, max_new_tokens=400, temperature=0.15, do_sample=True)
|
| 331 |
raw_feedback = outputs[0]['generated_text'][-1]['content']
|
| 332 |
|
| 333 |
+
# ββ Post-processing: apply score caps βββββββββββββββββββββββββββββββββββββ
|
| 334 |
match = re.search(r'Grade:\s*(\d+)', raw_feedback)
|
| 335 |
if match:
|
| 336 |
model_score = int(match.group(1))
|
| 337 |
+
|
| 338 |
+
# Cap from semantic relevance check
|
| 339 |
+
if max_score_from_relevance is not None and model_score > max_score_from_relevance:
|
| 340 |
+
model_score = max_score_from_relevance
|
| 341 |
+
raw_feedback = re.sub(r'Grade:\s*\d+', f'Grade: {model_score}', raw_feedback)
|
| 342 |
+
|
| 343 |
+
# Safety floor: prevent unreasonably low grades for substantive answers
|
| 344 |
if word_count >= 40:
|
| 345 |
min_grade = 4
|
| 346 |
elif word_count >= 20:
|
|
|
|
| 349 |
min_grade = 2
|
| 350 |
else:
|
| 351 |
min_grade = 1
|
| 352 |
+
|
| 353 |
if model_score < min_grade:
|
| 354 |
+
model_score = min_grade
|
| 355 |
+
raw_feedback = re.sub(r'Grade:\s*\d+', f'Grade: {model_score}', raw_feedback)
|
| 356 |
|
| 357 |
score_html = create_circular_progress(raw_feedback)
|
| 358 |
feedback_html = format_feedback_html(re.sub(r'Grade:.*?\n', '', raw_feedback).strip())
|