Spaces:
Sleeping
Sleeping
File size: 7,315 Bytes
ff7b177 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 | """
Parse uploaded files:
- XLSX β list of CandidateData (same column schema as Sample_Candidate_Data.xlsx)
- PDF β raw text for JD parsing
"""
import ast
import re
import uuid
import logging
from io import BytesIO
from typing import Optional
import pandas as pd
import fitz # PyMuPDF
from app.models.domain import CandidateData, WorkExperience
logger = logging.getLogger(__name__)
# βββ Constants ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
REQUIRED_COLUMNS = {
"name", "parsed_skills", "parsed_summary",
"parsed_work_experience", "parsed_metadata_calculated_years_experience",
"engineer_type", "current_position",
}
ENGINEER_TYPE_MAP = {
"ai engineer": "AI Engineer",
"ai": "AI Engineer",
"ml engineer": "AI Engineer",
"machine learning engineer": "AI Engineer",
"backend engineer": "Backend Engineer",
"backend": "Backend Engineer",
"be": "Backend Engineer",
"frontend engineer": "Frontend Engineer",
"frontend": "Frontend Engineer",
"fe": "Frontend Engineer",
"full stack engineer": "Full Stack Engineer",
"fullstack": "Full Stack Engineer",
"full stack": "Full Stack Engineer",
"software engineer": "Software Engineer",
"sde": "Software Engineer",
"data engineer": "Data Engineer",
"devops engineer": "DevOps Engineer",
"devops": "DevOps Engineer",
}
RECENT_EXP_MAP = {
"companytype.product": "Product Company",
"companytype.service": "Service Company",
"a company that build its own technology products": "Product Company",
"product": "Product Company",
"service": "Service Company",
"both": "Both",
}
GEN_AI_POSITIVE = {
"6 months - 1 year", "1 to 1.5 years", "1.5 years - 2 years",
"2+ years", "only internship/project experience",
}
GEN_AI_NEGATIVE = {"i do not have gen ai experience"}
# βββ XLSX Parsing βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def parse_xlsx(file_bytes: bytes) -> tuple[list[CandidateData], dict[str, int]]:
"""Return (candidates, engineer_type_distribution)."""
df = pd.read_excel(BytesIO(file_bytes))
df.columns = [c.strip().lower().replace(" ", "_") for c in df.columns]
missing = REQUIRED_COLUMNS - set(df.columns)
if missing:
raise ValueError(f"XLSX missing required columns: {missing}")
candidates: list[CandidateData] = []
for idx, row in df.iterrows():
try:
candidate = _parse_row(row, idx)
if candidate:
candidates.append(candidate)
except Exception as e:
logger.warning(f"Skipping row {idx}: {e}")
dist: dict[str, int] = {}
for c in candidates:
dist[c.engineer_type] = dist.get(c.engineer_type, 0) + 1
return candidates, dist
def _parse_row(row: pd.Series, idx: int) -> Optional[CandidateData]:
def get(col: str, default=None):
v = row.get(col, default)
if v is None or (isinstance(v, float) and pd.isna(v)):
return default
return v
# ID
candidate_id = str(get("id") or get("submission_id") or f"cand_{idx}")
# Years
yoe = _parse_float(get("parsed_metadata_calculated_years_experience"))
if yoe is None:
yoe = _parse_float(get("years_of_experience"))
if yoe is None:
yoe = -1.0
# Engineer type
raw_type = str(get("engineer_type", "")).strip()
engineer_type = ENGINEER_TYPE_MAP.get(raw_type.lower(), raw_type or "Unknown")
# Work experience
raw_we = str(get("parsed_work_experience", "") or "")
work_experiences, we_text = _parse_work_experience(raw_we, candidate_id)
# Gen AI
gen_ai_raw = get("gen_ai_experience")
gen_ai = str(gen_ai_raw).strip() if gen_ai_raw else None
# Active looking
active_raw = get("is_actively_or_passively_looking")
if active_raw is not None:
active = str(active_raw).strip().lower() not in ("false", "no", "0", "nan", "none")
else:
active = None
flags: list[str] = []
if yoe == -1.0:
flags.append("years_unknown")
if not raw_we or raw_we == "nan":
flags.append("work_exp_unavailable")
if engineer_type == "Unknown":
flags.append("engineer_type_unknown")
return CandidateData(
id=candidate_id,
name=str(get("name", f"Candidate_{idx}")),
current_position=str(get("current_position", "")),
current_title=get("current_title"),
years_of_experience=yoe,
engineer_type=engineer_type,
gen_ai_experience=gen_ai,
parsed_skills=str(get("parsed_skills", "") or ""),
parsed_summary=str(get("parsed_summary", "") or ""),
parsed_work_experience_raw=raw_we,
parsed_work_experience_text=we_text,
work_experiences=work_experiences,
extracted_skills=[], # filled by skill_parser
most_recent_company=get("parsed_metadata_most_recent_company"),
notice_period=get("notice_period"),
is_actively_looking=active,
flags=flags,
penalties=[],
)
def _parse_work_experience(raw: str, cand_id: str) -> tuple[list[WorkExperience], str]:
if not raw or raw.strip() in ("", "nan", "None"):
return [], ""
entries: list[WorkExperience] = []
try:
parsed = ast.literal_eval("[" + raw + "]")
for item in parsed:
if isinstance(item, dict):
entries.append(WorkExperience(
company=str(item.get("company", "")),
role=str(item.get("role", "")),
duration=str(item.get("duration", "")),
description=str(item.get("description", "")),
))
except Exception:
logger.debug(f"ast.literal_eval failed for {cand_id}, using raw text")
entries.append(WorkExperience(
company="", role="", duration="",
description=raw[:1000],
))
text_parts = []
for e in entries:
text_parts.append(
f"{e.role} at {e.company} ({e.duration}): {e.description}"
)
return entries, " | ".join(text_parts)
def _parse_float(val) -> Optional[float]:
if val is None:
return None
try:
f = float(str(val).strip())
return f if f >= 0 else None
except (ValueError, TypeError):
return None
# βββ PDF Parsing ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def parse_pdf(file_bytes: bytes) -> str:
"""Extract full text from PDF, preserving newlines."""
text_parts: list[str] = []
try:
doc = fitz.open(stream=file_bytes, filetype="pdf")
for page in doc:
text_parts.append(page.get_text("text"))
doc.close()
except Exception as e:
raise ValueError(f"Failed to read PDF: {e}")
text = "\n".join(text_parts)
# Collapse 3+ blank lines to 2
text = re.sub(r"\n{3,}", "\n\n", text)
return text.strip()
|