Spaces:
Running
Running
optimiizations ttrial
Browse files- services/job_matcher.py +224 -79
services/job_matcher.py
CHANGED
|
@@ -4,6 +4,7 @@ import re
|
|
| 4 |
from typing import Any, Dict, List, Optional, Set, Tuple
|
| 5 |
|
| 6 |
import numpy as np
|
|
|
|
| 7 |
from pydantic import BaseModel, Field
|
| 8 |
from sentence_transformers import SentenceTransformer
|
| 9 |
|
|
@@ -13,6 +14,13 @@ from services.taxonomy_client import (
|
|
| 13 |
SENIORITY_ORDER,
|
| 14 |
)
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
# ---------------------------------------------------------------------------
|
| 18 |
# Pydantic models
|
|
@@ -176,11 +184,19 @@ class JobMatcher:
|
|
| 176 |
Batch usage (same CV, many jobs β CV processed only once)
|
| 177 |
---------------------------------------------------------
|
| 178 |
results = matcher.predict_batch(list_of_job_dicts, cv_markdown)
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
"""
|
| 185 |
|
| 186 |
def __init__(
|
|
@@ -195,7 +211,7 @@ class JobMatcher:
|
|
| 195 |
self._title_model = SentenceTransformer("TechWolf/JobBERT-v3")
|
| 196 |
except Exception:
|
| 197 |
self._title_model = SentenceTransformer(title_model_name)
|
| 198 |
-
|
| 199 |
self._aliases: Dict[str, List[str]] = skill_aliases or DEFAULT_SKILL_ALIASES
|
| 200 |
self._category_skills: Dict[str, Set[str]] = category_skills or DEFAULT_CATEGORY_SKILLS
|
| 201 |
|
|
@@ -217,8 +233,12 @@ class JobMatcher:
|
|
| 217 |
) -> Dict[str, Any]:
|
| 218 |
"""
|
| 219 |
Predict the match between one job and one CV.
|
|
|
|
|
|
|
|
|
|
| 220 |
"""
|
| 221 |
-
|
|
|
|
| 222 |
cv_markdown=cv_markdown or "",
|
| 223 |
cv_title=cv_title or "Unknown",
|
| 224 |
cv_years=cv_years or 0,
|
|
@@ -228,7 +248,7 @@ class JobMatcher:
|
|
| 228 |
cv_skills_soft=cv_skills_soft or [],
|
| 229 |
cv_category=cv_category or "Other",
|
| 230 |
)
|
| 231 |
-
return
|
| 232 |
|
| 233 |
# ------------------------------------------------------------------
|
| 234 |
# Public: batch prediction (one CV vs. N jobs)
|
|
@@ -249,9 +269,10 @@ class JobMatcher:
|
|
| 249 |
"""
|
| 250 |
Match one CV against multiple jobs.
|
| 251 |
|
| 252 |
-
The CV is parsed and embedded **once**
|
| 253 |
-
|
| 254 |
-
|
|
|
|
| 255 |
"""
|
| 256 |
cv_data = self._parse_cv(
|
| 257 |
cv_markdown=cv_markdown,
|
|
@@ -263,7 +284,68 @@ class JobMatcher:
|
|
| 263 |
cv_skills_soft=cv_skills_soft,
|
| 264 |
cv_category=cv_category,
|
| 265 |
)
|
| 266 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 267 |
results.sort(
|
| 268 |
key=lambda x: float(x["match_score"].rstrip("%")),
|
| 269 |
reverse=True,
|
|
@@ -286,25 +368,34 @@ class JobMatcher:
|
|
| 286 |
cv_category: str,
|
| 287 |
) -> Dict[str, Any]:
|
| 288 |
"""
|
| 289 |
-
Return a dict holding all CV-level data needed by
|
| 290 |
"""
|
| 291 |
cv_skills_set = set(cv_skills_canonical)
|
| 292 |
cv_skills_technical_set = set(cv_skills_technical)
|
| 293 |
cv_skills_soft_set = set(cv_skills_soft)
|
| 294 |
|
| 295 |
cv_vec = self._embed(cv_markdown[:4000]) if cv_markdown else np.zeros(384, dtype="float32")
|
| 296 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 297 |
individual_skills_list = list(cv_skills_technical_set | cv_skills_soft_set)
|
| 298 |
if individual_skills_list:
|
| 299 |
contextualized_cv = [f"software development skill {s}" for s in individual_skills_list]
|
| 300 |
individual_skills_vecs = self._model.encode(
|
| 301 |
-
contextualized_cv, normalize_embeddings=True, show_progress_bar=False
|
| 302 |
)
|
| 303 |
else:
|
| 304 |
individual_skills_vecs = np.array([])
|
| 305 |
|
| 306 |
return {
|
| 307 |
"cv_title": cv_title,
|
|
|
|
| 308 |
"cv_years": cv_years,
|
| 309 |
"cv_seniority": cv_seniority,
|
| 310 |
"cv_skills": cv_skills_set,
|
|
@@ -317,49 +408,47 @@ class JobMatcher:
|
|
| 317 |
}
|
| 318 |
|
| 319 |
# ------------------------------------------------------------------
|
| 320 |
-
# Internal: score one job against pre-parsed CV data
|
|
|
|
| 321 |
# ------------------------------------------------------------------
|
| 322 |
|
| 323 |
-
def
|
| 324 |
self,
|
| 325 |
-
|
| 326 |
cv_data: Dict[str, Any],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 327 |
) -> Dict[str, Any]:
|
| 328 |
-
job = JobInput(**job_json)
|
| 329 |
-
|
| 330 |
cv_title = cv_data["cv_title"]
|
|
|
|
| 331 |
cv_years = cv_data["cv_years"]
|
| 332 |
cv_seniority = cv_data["cv_seniority"]
|
| 333 |
cv_skills = cv_data["cv_skills"]
|
| 334 |
-
cv_skills_technical = cv_data["cv_skills_technical"]
|
| 335 |
cv_skills_soft = cv_data["cv_skills_soft"]
|
| 336 |
cv_category = cv_data["cv_category"]
|
| 337 |
cv_vec = cv_data["cv_vec"]
|
| 338 |
-
individual_skills_list = cv_data["individual_skills_list"]
|
| 339 |
individual_skills_vecs = cv_data["individual_skills_vecs"]
|
| 340 |
|
| 341 |
-
# ββ
|
| 342 |
-
job_index_text = (
|
| 343 |
-
f"{job.job_title}\nCompany: {job.company_name}\n{job.description}\n"
|
| 344 |
-
f"Technical skills: {', '.join(job.skills_technical)}\n"
|
| 345 |
-
f"Soft skills: {', '.join(job.skills_soft)}"
|
| 346 |
-
)
|
| 347 |
-
job_vec = self._embed(job_index_text[:4000])
|
| 348 |
semantic = max(0.0, min(1.0, _cosine_similarity(cv_vec, job_vec)))
|
| 349 |
|
| 350 |
# ββ Skill scoring βββββββββββββββββββββββββββββββββββββββββββββββ
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
missing_technical,
|
| 361 |
individual_skills_vecs,
|
| 362 |
-
threshold=0.88
|
| 363 |
)
|
| 364 |
if semantic_matches:
|
| 365 |
matched_technical = sorted(set(matched_technical) | semantic_matches)
|
|
@@ -367,28 +456,28 @@ class JobMatcher:
|
|
| 367 |
|
| 368 |
matched_soft = sorted(cv_skills_soft & job_soft)
|
| 369 |
missing_soft = sorted(job_soft - cv_skills_soft)
|
| 370 |
-
|
| 371 |
extra = sorted(cv_skills - job_technical - job_soft)[:8]
|
| 372 |
|
| 373 |
tech_denom = min(len(job_technical), 12) if job_technical else 1
|
| 374 |
soft_denom = min(len(job_soft), 3) if job_soft else 1
|
| 375 |
-
|
| 376 |
tech_score = min(1.0, len(matched_technical) / tech_denom) if job_technical else 0.50
|
| 377 |
soft_score = min(1.0, len(matched_soft) / soft_denom) if job_soft else 0.50
|
| 378 |
-
|
| 379 |
skill = min(1.0, (tech_score * 0.80) + (soft_score * 0.20))
|
| 380 |
|
| 381 |
# ββ Component scores βββββββββββββββββββββββββββββββββββββββββββββ
|
| 382 |
cv_cat_lower = cv_category.strip().lower() if cv_category else ""
|
| 383 |
job_cat_lower = job.category.strip().lower() if job.category else ""
|
| 384 |
-
|
| 385 |
if cv_cat_lower and job_cat_lower and cv_cat_lower == job_cat_lower:
|
| 386 |
cat_score = 1.0
|
| 387 |
else:
|
| 388 |
cat_score = _soft_category_score(cv_skills, job.category, self._category_skills)
|
| 389 |
-
|
| 390 |
seniority, sen_reason = _seniority_fit(cv_seniority, job.seniority)
|
| 391 |
-
title = self.
|
| 392 |
experience = _experience_fit(cv_years, job.min_years_experience)
|
| 393 |
|
| 394 |
# ββ Weighted aggregation βββββββββββββββββββββββββββββββββββββββββ
|
|
@@ -408,11 +497,11 @@ class JobMatcher:
|
|
| 408 |
else:
|
| 409 |
cat_modifier = ((cat_score - 0.75) / 0.25) * 10.0
|
| 410 |
cat_modifier = max(0.0, min(10.0, cat_modifier))
|
| 411 |
-
|
| 412 |
final = base_final + cat_modifier
|
| 413 |
|
| 414 |
# Guardrails
|
| 415 |
-
|
| 416 |
if job_technical and not matched_technical:
|
| 417 |
final = min(final, 34.0)
|
| 418 |
if cv_cat_lower != job_cat_lower and skill < 0.30:
|
|
@@ -476,57 +565,113 @@ class JobMatcher:
|
|
| 476 |
# Private helpers
|
| 477 |
# ------------------------------------------------------------------
|
| 478 |
|
| 479 |
-
def
|
| 480 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 481 |
words = text.split()
|
| 482 |
if len(words) <= 180:
|
| 483 |
-
|
| 484 |
-
|
| 485 |
-
|
| 486 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 487 |
vectors = self._model.encode(
|
| 488 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 489 |
)
|
| 490 |
-
return np.
|
| 491 |
|
| 492 |
def _embed_title(self, text: str) -> np.ndarray:
|
| 493 |
-
"""Encode
|
| 494 |
vector = self._title_model.encode(
|
| 495 |
text, normalize_embeddings=True, show_progress_bar=False
|
| 496 |
)
|
| 497 |
return vector.astype("float32")
|
| 498 |
|
| 499 |
-
def
|
| 500 |
self,
|
| 501 |
missing_skills: List[str],
|
|
|
|
| 502 |
cv_skills_vecs: np.ndarray,
|
| 503 |
threshold: float = 0.72,
|
| 504 |
) -> Set[str]:
|
| 505 |
"""
|
| 506 |
-
|
| 507 |
-
|
| 508 |
-
|
|
|
|
| 509 |
"""
|
| 510 |
-
if not missing_skills or len(cv_skills_vecs) == 0:
|
| 511 |
return set()
|
| 512 |
-
|
| 513 |
-
contextualized_missing = [f"software development skill {s}" for s in missing_skills]
|
| 514 |
-
missing_vecs = self._model.encode(
|
| 515 |
-
contextualized_missing, normalize_embeddings=True, show_progress_bar=False
|
| 516 |
-
)
|
| 517 |
-
|
| 518 |
-
# missing_vecs is (M, 384), cv_skills_vecs is (N, 384)
|
| 519 |
-
sim_matrix = np.dot(missing_vecs, cv_skills_vecs.T)
|
| 520 |
-
|
| 521 |
matched: Set[str] = set()
|
| 522 |
-
for
|
| 523 |
-
|
| 524 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 525 |
return matched
|
| 526 |
|
| 527 |
-
def
|
| 528 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 529 |
return 0.35
|
| 530 |
-
|
| 531 |
-
job_vec = self._embed_title(job_title)
|
| 532 |
-
return max(0.2, min(1.0, _cosine_similarity(cv_vec, job_vec)))
|
|
|
|
| 4 |
from typing import Any, Dict, List, Optional, Set, Tuple
|
| 5 |
|
| 6 |
import numpy as np
|
| 7 |
+
import torch
|
| 8 |
from pydantic import BaseModel, Field
|
| 9 |
from sentence_transformers import SentenceTransformer
|
| 10 |
|
|
|
|
| 14 |
SENIORITY_ORDER,
|
| 15 |
)
|
| 16 |
|
| 17 |
+
# ---------------------------------------------------------------------------
|
| 18 |
+
# Step 4: Pin CPU thread count to the box's vCPU count. Left unpinned,
|
| 19 |
+
# PyTorch's default intra-op threading can oversubscribe a 2-vCPU host and
|
| 20 |
+
# cause contention between concurrent encode() calls.
|
| 21 |
+
# ---------------------------------------------------------------------------
|
| 22 |
+
torch.set_num_threads(2)
|
| 23 |
+
|
| 24 |
|
| 25 |
# ---------------------------------------------------------------------------
|
| 26 |
# Pydantic models
|
|
|
|
| 184 |
Batch usage (same CV, many jobs β CV processed only once)
|
| 185 |
---------------------------------------------------------
|
| 186 |
results = matcher.predict_batch(list_of_job_dicts, cv_markdown)
|
| 187 |
+
|
| 188 |
+
Performance notes (batched implementation)
|
| 189 |
+
-------------------------------------------
|
| 190 |
+
All model.encode() calls are batched ACROSS jobs, not per-job:
|
| 191 |
+
- 1 call embeds every job's semantic text
|
| 192 |
+
- 1 call embeds every job's title
|
| 193 |
+
- 1 call embeds the (deduplicated) set of missing technical skills
|
| 194 |
+
across *all* jobs, used for semantic skill fallback matching
|
| 195 |
+
- The CV title vector is computed once and cached (was previously
|
| 196 |
+
recomputed on every job).
|
| 197 |
+
This means a request with N jobs makes a fixed ~4 model.encode() calls
|
| 198 |
+
total instead of up to 4*N + 1, regardless of N. Scoring math and
|
| 199 |
+
output are unchanged β this is purely a batching optimization.
|
| 200 |
"""
|
| 201 |
|
| 202 |
def __init__(
|
|
|
|
| 211 |
self._title_model = SentenceTransformer("TechWolf/JobBERT-v3")
|
| 212 |
except Exception:
|
| 213 |
self._title_model = SentenceTransformer(title_model_name)
|
| 214 |
+
|
| 215 |
self._aliases: Dict[str, List[str]] = skill_aliases or DEFAULT_SKILL_ALIASES
|
| 216 |
self._category_skills: Dict[str, Set[str]] = category_skills or DEFAULT_CATEGORY_SKILLS
|
| 217 |
|
|
|
|
| 233 |
) -> Dict[str, Any]:
|
| 234 |
"""
|
| 235 |
Predict the match between one job and one CV.
|
| 236 |
+
|
| 237 |
+
Implemented as a thin wrapper around predict_batch([job]) so there
|
| 238 |
+
is a single scoring code path to keep in sync.
|
| 239 |
"""
|
| 240 |
+
results = self.predict_batch(
|
| 241 |
+
jobs=[job_json],
|
| 242 |
cv_markdown=cv_markdown or "",
|
| 243 |
cv_title=cv_title or "Unknown",
|
| 244 |
cv_years=cv_years or 0,
|
|
|
|
| 248 |
cv_skills_soft=cv_skills_soft or [],
|
| 249 |
cv_category=cv_category or "Other",
|
| 250 |
)
|
| 251 |
+
return results[0]
|
| 252 |
|
| 253 |
# ------------------------------------------------------------------
|
| 254 |
# Public: batch prediction (one CV vs. N jobs)
|
|
|
|
| 269 |
"""
|
| 270 |
Match one CV against multiple jobs.
|
| 271 |
|
| 272 |
+
The CV is parsed and embedded **once**. All job-level embeddings
|
| 273 |
+
(semantic text, title, missing-skill fallback) are computed in
|
| 274 |
+
batched encode() calls across every job in the request, then
|
| 275 |
+
scoring is done purely in numpy with no further model calls.
|
| 276 |
"""
|
| 277 |
cv_data = self._parse_cv(
|
| 278 |
cv_markdown=cv_markdown,
|
|
|
|
| 284 |
cv_skills_soft=cv_skills_soft,
|
| 285 |
cv_category=cv_category,
|
| 286 |
)
|
| 287 |
+
|
| 288 |
+
job_objs = [JobInput(**j) for j in jobs]
|
| 289 |
+
n = len(job_objs)
|
| 290 |
+
if n == 0:
|
| 291 |
+
return []
|
| 292 |
+
|
| 293 |
+
# ---- Batch 1: job semantic-text embeddings, ONE encode() call ----
|
| 294 |
+
job_texts = [self._build_job_index_text(j) for j in job_objs]
|
| 295 |
+
job_vecs = self._embed_batch_texts(job_texts)
|
| 296 |
+
|
| 297 |
+
# ---- Batch 2: job title embeddings, ONE encode() call ----
|
| 298 |
+
job_title_vecs = self._embed_title_batch([j.job_title for j in job_objs])
|
| 299 |
+
|
| 300 |
+
# ---- Precompute exact skill matches + collect missing technical
|
| 301 |
+
# skills across ALL jobs for a single batched semantic pass ----
|
| 302 |
+
cv_skills_technical_set = cv_data["cv_skills_technical"]
|
| 303 |
+
per_job_technical: List[Set[str]] = []
|
| 304 |
+
per_job_soft: List[Set[str]] = []
|
| 305 |
+
per_job_matched_technical: List[List[str]] = []
|
| 306 |
+
per_job_missing_technical: List[List[str]] = []
|
| 307 |
+
|
| 308 |
+
all_missing_skills: Set[str] = set()
|
| 309 |
+
for job in job_objs:
|
| 310 |
+
job_technical = {s.strip().lower() for s in job.skills_technical if s.strip()}
|
| 311 |
+
job_soft = {s.strip().lower() for s in job.skills_soft if s.strip()}
|
| 312 |
+
matched = sorted(cv_skills_technical_set & job_technical)
|
| 313 |
+
missing = sorted(job_technical - cv_skills_technical_set)
|
| 314 |
+
per_job_technical.append(job_technical)
|
| 315 |
+
per_job_soft.append(job_soft)
|
| 316 |
+
per_job_matched_technical.append(matched)
|
| 317 |
+
per_job_missing_technical.append(missing)
|
| 318 |
+
all_missing_skills.update(missing)
|
| 319 |
+
|
| 320 |
+
# ---- Batch 3: semantic skill-matching embeddings, ONE encode()
|
| 321 |
+
# call for the deduplicated union of missing skills across
|
| 322 |
+
# every job in the request ----
|
| 323 |
+
missing_skill_vecs: Dict[str, np.ndarray] = {}
|
| 324 |
+
individual_skills_vecs = cv_data["individual_skills_vecs"]
|
| 325 |
+
if all_missing_skills and len(individual_skills_vecs) > 0:
|
| 326 |
+
unique_missing = sorted(all_missing_skills)
|
| 327 |
+
contextualized = [f"software development skill {s}" for s in unique_missing]
|
| 328 |
+
vecs = self._model.encode(
|
| 329 |
+
contextualized, normalize_embeddings=True, show_progress_bar=False, batch_size=32
|
| 330 |
+
)
|
| 331 |
+
missing_skill_vecs = dict(zip(unique_missing, np.asarray(vecs)))
|
| 332 |
+
|
| 333 |
+
# ---- Score each job β pure numpy from here, no model calls ----
|
| 334 |
+
results = [
|
| 335 |
+
self._score_job(
|
| 336 |
+
job=job_objs[i],
|
| 337 |
+
cv_data=cv_data,
|
| 338 |
+
job_vec=job_vecs[i],
|
| 339 |
+
job_title_vec=job_title_vecs[i],
|
| 340 |
+
job_technical=per_job_technical[i],
|
| 341 |
+
job_soft=per_job_soft[i],
|
| 342 |
+
matched_technical=per_job_matched_technical[i],
|
| 343 |
+
missing_technical=per_job_missing_technical[i],
|
| 344 |
+
missing_skill_vecs=missing_skill_vecs,
|
| 345 |
+
)
|
| 346 |
+
for i in range(n)
|
| 347 |
+
]
|
| 348 |
+
|
| 349 |
results.sort(
|
| 350 |
key=lambda x: float(x["match_score"].rstrip("%")),
|
| 351 |
reverse=True,
|
|
|
|
| 368 |
cv_category: str,
|
| 369 |
) -> Dict[str, Any]:
|
| 370 |
"""
|
| 371 |
+
Return a dict holding all CV-level data needed by _score_job.
|
| 372 |
"""
|
| 373 |
cv_skills_set = set(cv_skills_canonical)
|
| 374 |
cv_skills_technical_set = set(cv_skills_technical)
|
| 375 |
cv_skills_soft_set = set(cv_skills_soft)
|
| 376 |
|
| 377 |
cv_vec = self._embed(cv_markdown[:4000]) if cv_markdown else np.zeros(384, dtype="float32")
|
| 378 |
+
|
| 379 |
+
# Step 1: compute the CV title vector once here instead of
|
| 380 |
+
# recomputing it inside every per-job title-similarity call.
|
| 381 |
+
cv_title_vec = (
|
| 382 |
+
self._embed_title(cv_title)
|
| 383 |
+
if cv_title and cv_title != "Unknown"
|
| 384 |
+
else None
|
| 385 |
+
)
|
| 386 |
+
|
| 387 |
individual_skills_list = list(cv_skills_technical_set | cv_skills_soft_set)
|
| 388 |
if individual_skills_list:
|
| 389 |
contextualized_cv = [f"software development skill {s}" for s in individual_skills_list]
|
| 390 |
individual_skills_vecs = self._model.encode(
|
| 391 |
+
contextualized_cv, normalize_embeddings=True, show_progress_bar=False, batch_size=32
|
| 392 |
)
|
| 393 |
else:
|
| 394 |
individual_skills_vecs = np.array([])
|
| 395 |
|
| 396 |
return {
|
| 397 |
"cv_title": cv_title,
|
| 398 |
+
"cv_title_vec": cv_title_vec,
|
| 399 |
"cv_years": cv_years,
|
| 400 |
"cv_seniority": cv_seniority,
|
| 401 |
"cv_skills": cv_skills_set,
|
|
|
|
| 408 |
}
|
| 409 |
|
| 410 |
# ------------------------------------------------------------------
|
| 411 |
+
# Internal: score one job against pre-parsed CV data + precomputed
|
| 412 |
+
# job-level vectors (no model calls happen in this method).
|
| 413 |
# ------------------------------------------------------------------
|
| 414 |
|
| 415 |
+
def _score_job(
|
| 416 |
self,
|
| 417 |
+
job: JobInput,
|
| 418 |
cv_data: Dict[str, Any],
|
| 419 |
+
job_vec: np.ndarray,
|
| 420 |
+
job_title_vec: np.ndarray,
|
| 421 |
+
job_technical: Set[str],
|
| 422 |
+
job_soft: Set[str],
|
| 423 |
+
matched_technical: List[str],
|
| 424 |
+
missing_technical: List[str],
|
| 425 |
+
missing_skill_vecs: Dict[str, np.ndarray],
|
| 426 |
) -> Dict[str, Any]:
|
|
|
|
|
|
|
| 427 |
cv_title = cv_data["cv_title"]
|
| 428 |
+
cv_title_vec = cv_data["cv_title_vec"]
|
| 429 |
cv_years = cv_data["cv_years"]
|
| 430 |
cv_seniority = cv_data["cv_seniority"]
|
| 431 |
cv_skills = cv_data["cv_skills"]
|
|
|
|
| 432 |
cv_skills_soft = cv_data["cv_skills_soft"]
|
| 433 |
cv_category = cv_data["cv_category"]
|
| 434 |
cv_vec = cv_data["cv_vec"]
|
|
|
|
| 435 |
individual_skills_vecs = cv_data["individual_skills_vecs"]
|
| 436 |
|
| 437 |
+
# ββ Semantic similarity (job embedding precomputed in the batch) ββ
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 438 |
semantic = max(0.0, min(1.0, _cosine_similarity(cv_vec, job_vec)))
|
| 439 |
|
| 440 |
# ββ Skill scoring βββββββββββββββββββββββββββββββββββββββββββββββ
|
| 441 |
+
matched_technical = list(matched_technical)
|
| 442 |
+
missing_technical = list(missing_technical)
|
| 443 |
+
|
| 444 |
+
# Semantic fallback for missing technical skills, using the
|
| 445 |
+
# precomputed (batched, deduplicated) missing-skill vector lookup.
|
| 446 |
+
if missing_technical and len(individual_skills_vecs) > 0 and missing_skill_vecs:
|
| 447 |
+
semantic_matches = self._semantic_skill_matches_precomputed(
|
| 448 |
+
missing_technical,
|
| 449 |
+
missing_skill_vecs,
|
|
|
|
| 450 |
individual_skills_vecs,
|
| 451 |
+
threshold=0.88,
|
| 452 |
)
|
| 453 |
if semantic_matches:
|
| 454 |
matched_technical = sorted(set(matched_technical) | semantic_matches)
|
|
|
|
| 456 |
|
| 457 |
matched_soft = sorted(cv_skills_soft & job_soft)
|
| 458 |
missing_soft = sorted(job_soft - cv_skills_soft)
|
| 459 |
+
|
| 460 |
extra = sorted(cv_skills - job_technical - job_soft)[:8]
|
| 461 |
|
| 462 |
tech_denom = min(len(job_technical), 12) if job_technical else 1
|
| 463 |
soft_denom = min(len(job_soft), 3) if job_soft else 1
|
| 464 |
+
|
| 465 |
tech_score = min(1.0, len(matched_technical) / tech_denom) if job_technical else 0.50
|
| 466 |
soft_score = min(1.0, len(matched_soft) / soft_denom) if job_soft else 0.50
|
| 467 |
+
|
| 468 |
skill = min(1.0, (tech_score * 0.80) + (soft_score * 0.20))
|
| 469 |
|
| 470 |
# ββ Component scores βββββββββββββββββββββββββββββββββββββββββββββ
|
| 471 |
cv_cat_lower = cv_category.strip().lower() if cv_category else ""
|
| 472 |
job_cat_lower = job.category.strip().lower() if job.category else ""
|
| 473 |
+
|
| 474 |
if cv_cat_lower and job_cat_lower and cv_cat_lower == job_cat_lower:
|
| 475 |
cat_score = 1.0
|
| 476 |
else:
|
| 477 |
cat_score = _soft_category_score(cv_skills, job.category, self._category_skills)
|
| 478 |
+
|
| 479 |
seniority, sen_reason = _seniority_fit(cv_seniority, job.seniority)
|
| 480 |
+
title = self._title_similarity_precomputed(cv_title, cv_title_vec, job_title_vec)
|
| 481 |
experience = _experience_fit(cv_years, job.min_years_experience)
|
| 482 |
|
| 483 |
# ββ Weighted aggregation βββββββββββββββββββββββββββββββββββββββββ
|
|
|
|
| 497 |
else:
|
| 498 |
cat_modifier = ((cat_score - 0.75) / 0.25) * 10.0
|
| 499 |
cat_modifier = max(0.0, min(10.0, cat_modifier))
|
| 500 |
+
|
| 501 |
final = base_final + cat_modifier
|
| 502 |
|
| 503 |
# Guardrails
|
| 504 |
+
|
| 505 |
if job_technical and not matched_technical:
|
| 506 |
final = min(final, 34.0)
|
| 507 |
if cv_cat_lower != job_cat_lower and skill < 0.30:
|
|
|
|
| 565 |
# Private helpers
|
| 566 |
# ------------------------------------------------------------------
|
| 567 |
|
| 568 |
+
def _build_job_index_text(self, job: JobInput) -> str:
|
| 569 |
+
"""Build the semantic index text for a job (same as before, factored out)."""
|
| 570 |
+
text = (
|
| 571 |
+
f"{job.job_title}\nCompany: {job.company_name}\n{job.description}\n"
|
| 572 |
+
f"Technical skills: {', '.join(job.skills_technical)}\n"
|
| 573 |
+
f"Soft skills: {', '.join(job.skills_soft)}"
|
| 574 |
+
)
|
| 575 |
+
return text[:4000]
|
| 576 |
+
|
| 577 |
+
@staticmethod
|
| 578 |
+
def _chunk_words(text: str) -> List[str]:
|
| 579 |
+
"""Split long text into overlapping ~180-word chunks (unchanged logic)."""
|
| 580 |
words = text.split()
|
| 581 |
if len(words) <= 180:
|
| 582 |
+
return [text]
|
| 583 |
+
step = 135
|
| 584 |
+
return [" ".join(words[i: i + 180]) for i in range(0, len(words), step)]
|
| 585 |
+
|
| 586 |
+
def _embed_batch_texts(self, texts: List[str]) -> np.ndarray:
|
| 587 |
+
"""
|
| 588 |
+
Embed a list of texts in a SINGLE batched encode() call.
|
| 589 |
+
|
| 590 |
+
Each text is chunked internally (same word-chunking-and-mean-pooling
|
| 591 |
+
behavior as the original per-item _embed()), but all chunks across
|
| 592 |
+
ALL input texts are flattened into one encode() call, then averaged
|
| 593 |
+
back per input text. This is the core batching win for step 2.
|
| 594 |
+
"""
|
| 595 |
+
if not texts:
|
| 596 |
+
return np.zeros((0, 384), dtype="float32")
|
| 597 |
+
|
| 598 |
+
all_parts: List[str] = []
|
| 599 |
+
boundaries: List[Tuple[int, int]] = []
|
| 600 |
+
for t in texts:
|
| 601 |
+
parts = self._chunk_words(t) if t else [""]
|
| 602 |
+
start = len(all_parts)
|
| 603 |
+
all_parts.extend(parts)
|
| 604 |
+
boundaries.append((start, len(all_parts)))
|
| 605 |
+
|
| 606 |
vectors = self._model.encode(
|
| 607 |
+
all_parts, normalize_embeddings=True, show_progress_bar=False, batch_size=32
|
| 608 |
+
)
|
| 609 |
+
vectors = np.asarray(vectors)
|
| 610 |
+
|
| 611 |
+
dim = vectors.shape[1] if vectors.ndim == 2 else 384
|
| 612 |
+
result = np.zeros((len(texts), dim), dtype="float32")
|
| 613 |
+
for i, (s, e) in enumerate(boundaries):
|
| 614 |
+
result[i] = np.mean(vectors[s:e], axis=0)
|
| 615 |
+
return result
|
| 616 |
+
|
| 617 |
+
def _embed(self, text: str) -> np.ndarray:
|
| 618 |
+
"""Encode a single text β normalised float32 vector (chunked internally)."""
|
| 619 |
+
return self._embed_batch_texts([text])[0]
|
| 620 |
+
|
| 621 |
+
def _embed_title_batch(self, titles: List[str]) -> np.ndarray:
|
| 622 |
+
"""Encode a list of titles in ONE batched encode() call with the title model."""
|
| 623 |
+
if not titles:
|
| 624 |
+
return np.zeros((0, 384), dtype="float32")
|
| 625 |
+
vectors = self._title_model.encode(
|
| 626 |
+
titles, normalize_embeddings=True, show_progress_bar=False, batch_size=32
|
| 627 |
)
|
| 628 |
+
return np.asarray(vectors).astype("float32")
|
| 629 |
|
| 630 |
def _embed_title(self, text: str) -> np.ndarray:
|
| 631 |
+
"""Encode a single title using the title model (used once per CV)."""
|
| 632 |
vector = self._title_model.encode(
|
| 633 |
text, normalize_embeddings=True, show_progress_bar=False
|
| 634 |
)
|
| 635 |
return vector.astype("float32")
|
| 636 |
|
| 637 |
+
def _semantic_skill_matches_precomputed(
|
| 638 |
self,
|
| 639 |
missing_skills: List[str],
|
| 640 |
+
missing_skill_vecs: Dict[str, np.ndarray],
|
| 641 |
cv_skills_vecs: np.ndarray,
|
| 642 |
threshold: float = 0.72,
|
| 643 |
) -> Set[str]:
|
| 644 |
"""
|
| 645 |
+
Same semantics as the original _semantic_skill_matches, but takes
|
| 646 |
+
precomputed vectors for the missing skills (built once per request
|
| 647 |
+
in a single batched encode() call in predict_batch) instead of
|
| 648 |
+
encoding them again per job.
|
| 649 |
"""
|
| 650 |
+
if not missing_skills or len(cv_skills_vecs) == 0 or not missing_skill_vecs:
|
| 651 |
return set()
|
| 652 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 653 |
matched: Set[str] = set()
|
| 654 |
+
for skill in missing_skills:
|
| 655 |
+
vec = missing_skill_vecs.get(skill)
|
| 656 |
+
if vec is None:
|
| 657 |
+
continue
|
| 658 |
+
sims = np.dot(cv_skills_vecs, vec)
|
| 659 |
+
if np.max(sims) >= threshold:
|
| 660 |
+
matched.add(skill)
|
| 661 |
return matched
|
| 662 |
|
| 663 |
+
def _title_similarity_precomputed(
|
| 664 |
+
self,
|
| 665 |
+
cv_title: str,
|
| 666 |
+
cv_title_vec: Optional[np.ndarray],
|
| 667 |
+
job_title_vec: np.ndarray,
|
| 668 |
+
) -> float:
|
| 669 |
+
"""
|
| 670 |
+
Same semantics as the original _title_similarity, but uses the
|
| 671 |
+
CV title vector cached once in cv_data and the job title vector
|
| 672 |
+
computed once per request in the batched title encode() call β
|
| 673 |
+
no model calls happen here.
|
| 674 |
+
"""
|
| 675 |
+
if not cv_title or cv_title == "Unknown" or cv_title_vec is None:
|
| 676 |
return 0.35
|
| 677 |
+
return max(0.2, min(1.0, _cosine_similarity(cv_title_vec, job_title_vec)))
|
|
|
|
|
|