Spaces:
Paused
Paused
Update resume_parser.py
Browse files- resume_parser.py +746 -234
resume_parser.py
CHANGED
|
@@ -1,247 +1,759 @@
|
|
| 1 |
"""
|
| 2 |
-
|
| 3 |
-
======
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
"""
|
| 8 |
|
| 9 |
-
import
|
| 10 |
-
import logging
|
| 11 |
-
import os
|
| 12 |
import re
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
import torch
|
| 16 |
-
from fastapi import FastAPI, HTTPException
|
| 17 |
-
from fastapi.responses import JSONResponse
|
| 18 |
-
from peft import PeftModel
|
| 19 |
-
from pydantic import BaseModel
|
| 20 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
|
| 21 |
-
|
| 22 |
-
logging.basicConfig(
|
| 23 |
-
level=logging.INFO,
|
| 24 |
-
format="%(asctime)s [%(levelname)s] %(message)s",
|
| 25 |
-
)
|
| 26 |
-
logger = logging.getLogger(__name__)
|
| 27 |
|
| 28 |
-
|
| 29 |
-
# Config
|
| 30 |
-
# ---------------------------------------------------------------------------
|
| 31 |
-
|
| 32 |
-
ADAPTER_REPO = "DarkSting/resume_parser"
|
| 33 |
-
BASE_MODEL = "google/gemma-4-E2B-it"
|
| 34 |
-
MAX_NEW_TOKENS = 800
|
| 35 |
-
|
| 36 |
-
SYSTEM_PROMPT = """You are an expert resume parser. Extract structured information from resume text and return ONLY valid JSON with no extra text, no markdown, no explanation.
|
| 37 |
-
|
| 38 |
-
Always follow this exact schema. Use null for missing fields and [] for missing lists:
|
| 39 |
-
|
| 40 |
-
{
|
| 41 |
-
"name": "Full Name or null",
|
| 42 |
-
"email": "email@example.com or null",
|
| 43 |
-
"phone": "phone number or null",
|
| 44 |
-
"skills": ["skill1", "skill2"],
|
| 45 |
-
"programming_languages": ["Python", "JavaScript"],
|
| 46 |
-
"experience": [
|
| 47 |
-
{
|
| 48 |
-
"job_title": "Job Title",
|
| 49 |
-
"company": "Company Name",
|
| 50 |
-
"start_date": "MM/YYYY or MM/DD/YYYY or null",
|
| 51 |
-
"end_date": "MM/YYYY or MM/DD/YYYY or Present or null",
|
| 52 |
-
"responsibilities": ["responsibility1", "responsibility2"]
|
| 53 |
-
}
|
| 54 |
-
],
|
| 55 |
-
"total_experience_years": 0.0,
|
| 56 |
-
"education": [
|
| 57 |
-
{
|
| 58 |
-
"degree": "Degree Name",
|
| 59 |
-
"field": "Field of Study or null",
|
| 60 |
-
"institution": "Institution Name or null",
|
| 61 |
-
"year": 2020
|
| 62 |
-
}
|
| 63 |
-
],
|
| 64 |
-
"certifications": ["cert1", "cert2"],
|
| 65 |
-
"projects": ["Project Name 1", "Project Name 2"],
|
| 66 |
-
"publications": ["publication1"],
|
| 67 |
-
"achievements": ["achievement1"],
|
| 68 |
-
"summary": "Brief professional summary or null"
|
| 69 |
-
}"""
|
| 70 |
-
|
| 71 |
-
# ---------------------------------------------------------------------------
|
| 72 |
-
# Global model state
|
| 73 |
-
# ---------------------------------------------------------------------------
|
| 74 |
-
|
| 75 |
-
model = None
|
| 76 |
-
tokenizer = None
|
| 77 |
-
load_error = None
|
| 78 |
-
|
| 79 |
-
# ---------------------------------------------------------------------------
|
| 80 |
-
# Model loading
|
| 81 |
-
# ---------------------------------------------------------------------------
|
| 82 |
-
|
| 83 |
-
def load_model():
|
| 84 |
-
global model, tokenizer, load_error
|
| 85 |
-
|
| 86 |
-
logger.info("Loading base model: %s", BASE_MODEL)
|
| 87 |
-
logger.info("Loading adapter : %s", ADAPTER_REPO)
|
| 88 |
-
|
| 89 |
-
try:
|
| 90 |
-
if torch.cuda.is_available() and torch.cuda.get_device_capability()[0] >= 8:
|
| 91 |
-
torch_dtype = torch.bfloat16
|
| 92 |
-
else:
|
| 93 |
-
torch_dtype = torch.float16
|
| 94 |
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
|
|
|
| 99 |
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
)
|
| 107 |
|
| 108 |
-
|
| 109 |
-
base_model = AutoModelForCausalLM.from_pretrained(BASE_MODEL, **model_kwargs)
|
| 110 |
-
|
| 111 |
-
logger.info("Loading LoRA adapter ...")
|
| 112 |
-
model = PeftModel.from_pretrained(base_model, ADAPTER_REPO)
|
| 113 |
-
model.eval()
|
| 114 |
-
|
| 115 |
-
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL)
|
| 116 |
-
logger.info("Model ready on device: %s", model.device)
|
| 117 |
-
|
| 118 |
-
except Exception as e:
|
| 119 |
-
load_error = str(e)
|
| 120 |
-
logger.exception("Failed to load model: %s", e)
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
# ---------------------------------------------------------------------------
|
| 124 |
-
# Lifespan — load model once at startup
|
| 125 |
-
# ---------------------------------------------------------------------------
|
| 126 |
-
|
| 127 |
-
@asynccontextmanager
|
| 128 |
-
async def lifespan(app: FastAPI):
|
| 129 |
-
load_model()
|
| 130 |
-
yield
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
# ---------------------------------------------------------------------------
|
| 134 |
-
# FastAPI app
|
| 135 |
-
# ---------------------------------------------------------------------------
|
| 136 |
-
|
| 137 |
-
app = FastAPI(
|
| 138 |
-
title="Smart Recruiter — Resume Parser",
|
| 139 |
-
description="Fine-tuned Gemma-4-E2B-it resume parser. POST raw resume text, get structured JSON.",
|
| 140 |
-
version="1.0.0",
|
| 141 |
-
lifespan=lifespan,
|
| 142 |
-
)
|
| 143 |
-
|
| 144 |
-
# ---------------------------------------------------------------------------
|
| 145 |
-
# Schemas
|
| 146 |
-
# ---------------------------------------------------------------------------
|
| 147 |
-
|
| 148 |
-
class AnalyzeRequest(BaseModel):
|
| 149 |
-
content: str
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
# ---------------------------------------------------------------------------
|
| 153 |
-
# Inference
|
| 154 |
-
# ---------------------------------------------------------------------------
|
| 155 |
-
|
| 156 |
-
def parse_resume(resume_text: str) -> dict:
|
| 157 |
-
messages = [
|
| 158 |
-
{"role": "system", "content": SYSTEM_PROMPT},
|
| 159 |
-
{
|
| 160 |
-
"role": "user",
|
| 161 |
-
"content": (
|
| 162 |
-
f"Parse this resume and return ONLY a JSON object:\n\n"
|
| 163 |
-
f"RESUME:\n{resume_text[:3000]}\n\n"
|
| 164 |
-
f"Return ONLY the JSON object, nothing else."
|
| 165 |
-
),
|
| 166 |
-
},
|
| 167 |
-
]
|
| 168 |
-
|
| 169 |
-
text = tokenizer.apply_chat_template(
|
| 170 |
-
messages,
|
| 171 |
-
tokenize=False,
|
| 172 |
-
add_generation_prompt=True,
|
| 173 |
-
)
|
| 174 |
-
|
| 175 |
-
inputs = tokenizer(text=text, return_tensors="pt").to(model.device)
|
| 176 |
-
|
| 177 |
-
with torch.no_grad():
|
| 178 |
-
output = model.generate(
|
| 179 |
-
**inputs,
|
| 180 |
-
max_new_tokens=MAX_NEW_TOKENS,
|
| 181 |
-
do_sample=False,
|
| 182 |
-
temperature=None,
|
| 183 |
-
top_p=None,
|
| 184 |
-
repetition_penalty=1.1,
|
| 185 |
-
pad_token_id=tokenizer.eos_token_id,
|
| 186 |
-
eos_token_id=tokenizer.eos_token_id,
|
| 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 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|
)
|
|
|
|
|
|
|
| 234 |
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
logger.info("Parse successful. Skills: %s", result.get("skills", []))
|
| 240 |
-
return JSONResponse(content=result)
|
| 241 |
-
|
| 242 |
-
except ValueError as e:
|
| 243 |
-
raise HTTPException(status_code=500, detail=str(e))
|
| 244 |
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
"""
|
| 2 |
+
resume_parser.py
|
| 3 |
+
================
|
| 4 |
+
Component 1 — Resume Parser Client
|
| 5 |
+
Smart Recruiter API
|
| 6 |
+
|
| 7 |
+
Flow:
|
| 8 |
+
PDF bytes ──► extract text locally
|
| 9 |
+
│ ATS-friendly PDF → pdfplumber (text layer)
|
| 10 |
+
│ Scanned/image PDF → pytesseract OCR (auto fallback)
|
| 11 |
+
│ Clean & normalise text
|
| 12 |
+
│
|
| 13 |
+
▼
|
| 14 |
+
POST multipart {"content": "<resume text>", "resume": <pdf bytes>}
|
| 15 |
+
│
|
| 16 |
+
HuggingFace endpoint (local model + ChatGPT verification pass)
|
| 17 |
+
https://SapunMendis-Resume-Parser.hf.space/analyze
|
| 18 |
+
│
|
| 19 |
+
▼
|
| 20 |
+
structured resume JSON ──► ScoringEngine
|
| 21 |
+
|
| 22 |
+
Note: ChatGPT verification/correction now happens server-side (in the HF
|
| 23 |
+
Space's app.py + chatgpt_verifier.py), not in this client. This client's
|
| 24 |
+
only job is to extract text locally, send both the text AND the original
|
| 25 |
+
PDF bytes to the HF endpoint, and normalise whatever comes back.
|
| 26 |
"""
|
| 27 |
|
| 28 |
+
import io
|
|
|
|
|
|
|
| 29 |
import re
|
| 30 |
+
import logging
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
+
import requests
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
+
try:
|
| 35 |
+
import pdfplumber
|
| 36 |
+
PDFPLUMBER_AVAILABLE = True
|
| 37 |
+
except ImportError:
|
| 38 |
+
PDFPLUMBER_AVAILABLE = False
|
| 39 |
|
| 40 |
+
try:
|
| 41 |
+
from pdf2image import convert_from_bytes
|
| 42 |
+
import pytesseract
|
| 43 |
+
OCR_AVAILABLE = True
|
| 44 |
+
except ImportError:
|
| 45 |
+
OCR_AVAILABLE = False
|
|
|
|
| 46 |
|
| 47 |
+
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
+
HF_ENDPOINT = "https://SapunMendis-Resume-Parser.hf.space/analyze"
|
| 50 |
+
REQUEST_TIMEOUT = (10, 300) # seconds — HF cold starts can be slow
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
class ResumeParserClient:
|
| 54 |
+
"""
|
| 55 |
+
Thin client around the HuggingFace resume-parsing endpoint.
|
| 56 |
+
|
| 57 |
+
The PDF text is extracted locally, then BOTH the plain text and the
|
| 58 |
+
original PDF bytes are POSTed (multipart/form-data) to the HF
|
| 59 |
+
endpoint, which runs the local model + a ChatGPT verification pass
|
| 60 |
+
server-side and returns the final corrected JSON.
|
| 61 |
+
|
| 62 |
+
If parsing fails for any reason, a graceful fallback with empty
|
| 63 |
+
fields is returned so the rest of the pipeline can still run.
|
| 64 |
+
"""
|
| 65 |
+
|
| 66 |
+
def __init__(self, endpoint: str = HF_ENDPOINT, timeout: int = REQUEST_TIMEOUT):
|
| 67 |
+
self.endpoint = endpoint
|
| 68 |
+
self.timeout = timeout
|
| 69 |
+
|
| 70 |
+
# ------------------------------------------------------------------
|
| 71 |
+
# Public
|
| 72 |
+
# ------------------------------------------------------------------
|
| 73 |
+
|
| 74 |
+
def parse_from_file(self, pdf_bytes: bytes) -> dict:
|
| 75 |
+
"""
|
| 76 |
+
Extract text from PDF bytes, then send BOTH the text and the raw
|
| 77 |
+
PDF bytes to the HF endpoint (which handles model parsing +
|
| 78 |
+
ChatGPT verification internally).
|
| 79 |
+
|
| 80 |
+
Parameters
|
| 81 |
+
----------
|
| 82 |
+
pdf_bytes : bytes Raw PDF content from multipart upload
|
| 83 |
+
|
| 84 |
+
Returns
|
| 85 |
+
-------
|
| 86 |
+
dict Structured resume data ready for ScoringEngine
|
| 87 |
+
"""
|
| 88 |
+
logger.info("[ResumeParser] Extracting text from PDF …")
|
| 89 |
+
text = self._extract_pdf_text(pdf_bytes)
|
| 90 |
+
|
| 91 |
+
if not text.strip():
|
| 92 |
+
logger.warning("[ResumeParser] PDF text extraction produced empty result.")
|
| 93 |
+
return self._fallback(error="empty_pdf_text")
|
| 94 |
+
|
| 95 |
+
logger.info("[ResumeParser] Extracted %d characters from PDF.", len(text))
|
| 96 |
+
return self._call_hf(text, resume_text=text, pdf_bytes=pdf_bytes)
|
| 97 |
+
|
| 98 |
+
def parse_from_text(self, text: str) -> dict:
|
| 99 |
+
"""
|
| 100 |
+
Send pre-extracted resume text to the HF endpoint (no PDF
|
| 101 |
+
available in this path — server-side verification falls back to
|
| 102 |
+
text-only mode automatically).
|
| 103 |
+
|
| 104 |
+
Parameters
|
| 105 |
+
----------
|
| 106 |
+
text : str Plain-text resume content
|
| 107 |
+
|
| 108 |
+
Returns
|
| 109 |
+
-------
|
| 110 |
+
dict Structured resume data
|
| 111 |
+
"""
|
| 112 |
+
if not text.strip():
|
| 113 |
+
return self._fallback(error="empty_text_input")
|
| 114 |
+
return self._call_hf(text, resume_text=text, pdf_bytes=None)
|
| 115 |
+
|
| 116 |
+
# ------------------------------------------------------------------
|
| 117 |
+
# PDF extraction
|
| 118 |
+
# ------------------------------------------------------------------
|
| 119 |
+
|
| 120 |
+
def _extract_pdf_text(self, pdf_bytes: bytes) -> str:
|
| 121 |
+
"""
|
| 122 |
+
Extract text from PDF bytes.
|
| 123 |
+
|
| 124 |
+
Strategy (auto-detected):
|
| 125 |
+
1. pdfplumber — for normal/ATS-friendly PDFs with a text layer
|
| 126 |
+
2. pytesseract — OCR fallback for scanned / image-based PDFs
|
| 127 |
+
|
| 128 |
+
Returns cleaned plain text, or empty string on failure.
|
| 129 |
+
|
| 130 |
+
Install dependencies:
|
| 131 |
+
pip install pdfplumber pdf2image pytesseract pillow
|
| 132 |
+
# Linux / Colab:
|
| 133 |
+
sudo apt-get install -y tesseract-ocr poppler-utils
|
| 134 |
+
"""
|
| 135 |
+
if not PDFPLUMBER_AVAILABLE:
|
| 136 |
+
logger.error("[ResumeParser] pdfplumber not installed. Run: pip install pdfplumber")
|
| 137 |
+
return ""
|
| 138 |
+
|
| 139 |
+
try:
|
| 140 |
+
if self._has_text_layer(pdf_bytes):
|
| 141 |
+
logger.info("[ResumeParser] Text layer detected → using pdfplumber.")
|
| 142 |
+
raw_text = self._extract_with_pdfplumber(pdf_bytes)
|
| 143 |
+
else:
|
| 144 |
+
logger.info("[ResumeParser] No text layer → falling back to OCR.")
|
| 145 |
+
raw_text = self._extract_with_ocr(pdf_bytes)
|
| 146 |
+
|
| 147 |
+
return self._clean_text(raw_text)
|
| 148 |
+
|
| 149 |
+
except Exception as e:
|
| 150 |
+
logger.error("[ResumeParser] PDF extraction error: %s", e)
|
| 151 |
+
return ""
|
| 152 |
+
|
| 153 |
+
def _has_text_layer(self, pdf_bytes: bytes, min_chars_per_page: int = 50) -> bool:
|
| 154 |
+
"""
|
| 155 |
+
Returns True if the PDF has a usable text layer (ATS-friendly),
|
| 156 |
+
False if it is scanned / image-based and needs OCR.
|
| 157 |
+
"""
|
| 158 |
+
with pdfplumber.open(io.BytesIO(pdf_bytes)) as pdf:
|
| 159 |
+
total_chars = sum(len((p.extract_text() or "").strip()) for p in pdf.pages)
|
| 160 |
+
avg_chars = total_chars / max(len(pdf.pages), 1)
|
| 161 |
+
return avg_chars >= min_chars_per_page
|
| 162 |
+
|
| 163 |
+
def _extract_with_pdfplumber(self, pdf_bytes: bytes) -> str:
|
| 164 |
+
"""Extract text from a text-layer PDF using pdfplumber."""
|
| 165 |
+
pages = []
|
| 166 |
+
with pdfplumber.open(io.BytesIO(pdf_bytes)) as pdf:
|
| 167 |
+
for page in pdf.pages:
|
| 168 |
+
text = page.extract_text(x_tolerance=2, y_tolerance=2)
|
| 169 |
+
if text:
|
| 170 |
+
pages.append(text.strip())
|
| 171 |
+
return "\n\n".join(pages)
|
| 172 |
+
|
| 173 |
+
def _extract_with_ocr(self, pdf_bytes: bytes, dpi: int = 300) -> str:
|
| 174 |
+
"""Extract text from a scanned PDF using pytesseract OCR."""
|
| 175 |
+
if not OCR_AVAILABLE:
|
| 176 |
+
logger.error(
|
| 177 |
+
"[ResumeParser] OCR dependencies missing. "
|
| 178 |
+
"Run: pip install pdf2image pytesseract "
|
| 179 |
+
"and: sudo apt-get install -y tesseract-ocr poppler-utils"
|
| 180 |
+
)
|
| 181 |
+
return ""
|
| 182 |
+
|
| 183 |
+
images = convert_from_bytes(pdf_bytes, dpi=dpi)
|
| 184 |
+
pages = []
|
| 185 |
+
for image in images:
|
| 186 |
+
text = pytesseract.image_to_string(image, config="--psm 6 --oem 3")
|
| 187 |
+
if text.strip():
|
| 188 |
+
pages.append(text.strip())
|
| 189 |
+
return "\n\n".join(pages)
|
| 190 |
+
|
| 191 |
+
@staticmethod
|
| 192 |
+
def _clean_text(text: str) -> str:
|
| 193 |
+
"""
|
| 194 |
+
Normalise unicode characters, remove OCR noise, collapse
|
| 195 |
+
excessive whitespace — so the model receives clean input.
|
| 196 |
+
|
| 197 |
+
Also fixes common pdfplumber issues with merged words and
|
| 198 |
+
section headers that lose their spaces:
|
| 199 |
+
"ITINFRASTRUCTURE" → "IT INFRASTRUCTURE"
|
| 200 |
+
"EDUCATION&CERTIFICATIONS" → "EDUCATION & CERTIFICATIONS"
|
| 201 |
+
"PROFESSIONALEXPERIENCE" → "PROFESSIONAL EXPERIENCE"
|
| 202 |
+
"""
|
| 203 |
+
if not text:
|
| 204 |
+
return ""
|
| 205 |
+
|
| 206 |
+
# Normalise unicode punctuation
|
| 207 |
+
text = text.replace("\u2013", "-").replace("\u2014", "-") # em/en dash
|
| 208 |
+
text = text.replace("\u2018", "'").replace("\u2019", "'") # curly quotes
|
| 209 |
+
text = text.replace("\u201c", '"').replace("\u201d", '"')
|
| 210 |
+
text = text.replace("\u2022", "-").replace("\u25cf", "-") # bullets
|
| 211 |
+
text = text.replace("\u00a0", " ") # non-breaking space
|
| 212 |
+
|
| 213 |
+
# Fix "&" merged into ALL-CAPS section headers:
|
| 214 |
+
# "EDUCATION&CERTIFICATIONS" → "EDUCATION & CERTIFICATIONS"
|
| 215 |
+
text = re.sub(r'([A-Z])&([A-Z])', r'\1 & \2', text)
|
| 216 |
+
|
| 217 |
+
# Fix merged CamelCase/TitleCase word boundaries:
|
| 218 |
+
# "TechnicalManager" → "Technical Manager"
|
| 219 |
+
text = re.sub(r'([A-Z]{2,})([A-Z][a-z])', r'\1 \2', text)
|
| 220 |
+
|
| 221 |
+
# Fix known fully-uppercase merged section headers
|
| 222 |
+
_SECTION_FIXES = {
|
| 223 |
+
'PROFESSIONALEXPERIENCE': 'PROFESSIONAL EXPERIENCE',
|
| 224 |
+
'WORKEXPERIENCE': 'WORK EXPERIENCE',
|
| 225 |
+
'SELECTACHIEVEMENTS': 'SELECT ACHIEVEMENTS',
|
| 226 |
+
'KEYACHIEVEMENTS': 'KEY ACHIEVEMENTS',
|
| 227 |
+
'ITINFRASTRUCTURE': 'IT INFRASTRUCTURE',
|
| 228 |
+
'TECHNICALSKILLS': 'TECHNICAL SKILLS',
|
| 229 |
+
'KEYSKILLS': 'KEY SKILLS',
|
| 230 |
+
'CORECOMPETENCIES': 'CORE COMPETENCIES',
|
| 231 |
+
'CAREERHISTORY': 'CAREER HISTORY',
|
| 232 |
+
'CAREEROBJECTIVE': 'CAREER OBJECTIVE',
|
| 233 |
+
'STATEUNIVERSITY': 'STATE UNIVERSITY',
|
| 234 |
+
}
|
| 235 |
+
for merged, fixed in _SECTION_FIXES.items():
|
| 236 |
+
text = text.replace(merged, fixed)
|
| 237 |
+
|
| 238 |
+
# Remove non-printable characters (OCR artifacts)
|
| 239 |
+
text = re.sub(r'[^\x20-\x7E\n]', ' ', text)
|
| 240 |
+
|
| 241 |
+
# Remove repeated separator lines (e.g. "------", "......")
|
| 242 |
+
text = re.sub(r'[.\-_=]{4,}', ' ', text)
|
| 243 |
+
|
| 244 |
+
# Collapse multiple spaces/tabs into one
|
| 245 |
+
text = re.sub(r'[ \t]+', ' ', text)
|
| 246 |
+
|
| 247 |
+
# Collapse more than 2 consecutive blank lines
|
| 248 |
+
text = re.sub(r'\n{3,}', '\n\n', text)
|
| 249 |
+
|
| 250 |
+
# Strip leading/trailing whitespace per line
|
| 251 |
+
lines = [line.strip() for line in text.splitlines()]
|
| 252 |
+
return "\n".join(lines).strip()
|
| 253 |
+
|
| 254 |
+
# ------------------------------------------------------------------
|
| 255 |
+
# HuggingFace API call
|
| 256 |
+
# ------------------------------------------------------------------
|
| 257 |
+
|
| 258 |
+
def _call_hf(self, text: str, resume_text: str = "", pdf_bytes: bytes = None) -> dict:
|
| 259 |
+
"""
|
| 260 |
+
POST the resume text (and, when available, the original PDF
|
| 261 |
+
bytes) to the HuggingFace endpoint.
|
| 262 |
+
|
| 263 |
+
The HF Space runs the local model AND the ChatGPT verification
|
| 264 |
+
pass server-side, so whatever JSON comes back is already final.
|
| 265 |
+
|
| 266 |
+
Sends multipart/form-data:
|
| 267 |
+
content : "<resume text>" (always)
|
| 268 |
+
resume : <pdf bytes> (only when available)
|
| 269 |
+
Expects back: structured resume JSON (already verified server-side)
|
| 270 |
+
"""
|
| 271 |
+
logger.info("[ResumeParser] Calling HuggingFace endpoint (pdf=%s) …", pdf_bytes is not None)
|
| 272 |
+
try:
|
| 273 |
+
files = None
|
| 274 |
+
if pdf_bytes:
|
| 275 |
+
files = {"resume": ("resume.pdf", pdf_bytes, "application/pdf")}
|
| 276 |
+
|
| 277 |
+
resp = requests.post(
|
| 278 |
+
self.endpoint,
|
| 279 |
+
data={"content": text},
|
| 280 |
+
files=files,
|
| 281 |
+
timeout=self.timeout,
|
| 282 |
+
)
|
| 283 |
+
resp.raise_for_status()
|
| 284 |
+
raw = resp.json()
|
| 285 |
+
logger.info("[ResumeParser] HF parse successful. parser_source=%s", raw.get("parser_source"))
|
| 286 |
+
return self._normalise(raw, resume_text=resume_text)
|
| 287 |
+
|
| 288 |
+
except requests.exceptions.Timeout:
|
| 289 |
+
logger.error("[ResumeParser] Request timed out after %ds.", self.timeout)
|
| 290 |
+
return self._fallback(error="timeout")
|
| 291 |
+
|
| 292 |
+
except requests.exceptions.HTTPError as e:
|
| 293 |
+
logger.error("[ResumeParser] HTTP error: %s", e)
|
| 294 |
+
return self._fallback(error=str(e))
|
| 295 |
+
|
| 296 |
+
except Exception as e:
|
| 297 |
+
logger.error("[ResumeParser] Unexpected error: %s", e)
|
| 298 |
+
return self._fallback(error=str(e))
|
| 299 |
+
|
| 300 |
+
# ------------------------------------------------------------------
|
| 301 |
+
# Private helpers
|
| 302 |
+
# ------------------------------------------------------------------
|
| 303 |
+
|
| 304 |
+
def _normalise(self, raw: dict, resume_text: str = "") -> dict:
|
| 305 |
+
"""
|
| 306 |
+
Normalise the HuggingFace model output into the schema expected
|
| 307 |
+
by ScoringEngine._score_resume().
|
| 308 |
+
|
| 309 |
+
For any field the model omits (name, education, projects),
|
| 310 |
+
post-processing regex fallbacks extract the value directly
|
| 311 |
+
from the raw resume text.
|
| 312 |
+
"""
|
| 313 |
+
experience_list = raw.get("experience") or []
|
| 314 |
+
|
| 315 |
+
# ── Model outputs ────────────────────────────────────────────────
|
| 316 |
+
name = (raw.get("name") or raw.get("candidate_name")
|
| 317 |
+
or raw.get("full_name") or "")
|
| 318 |
+
education = raw.get("education") or []
|
| 319 |
+
projects = self._to_list(
|
| 320 |
+
raw.get("projects") or raw.get("project_titles") or [])
|
| 321 |
+
certifications = self._to_list(raw.get("certifications") or [])
|
| 322 |
+
|
| 323 |
+
# ── Post-processing fallbacks (only when model missed the field) ─
|
| 324 |
+
if resume_text:
|
| 325 |
+
if not name:
|
| 326 |
+
name = self._fallback_extract_name(resume_text)
|
| 327 |
+
if not education:
|
| 328 |
+
education = self._fallback_extract_education(resume_text)
|
| 329 |
+
if not projects:
|
| 330 |
+
projects = self._fallback_extract_projects(resume_text)
|
| 331 |
+
|
| 332 |
+
return {
|
| 333 |
+
"name": name,
|
| 334 |
+
"skills": self._to_list(
|
| 335 |
+
raw.get("skills") or
|
| 336 |
+
raw.get("technical_skills") or []),
|
| 337 |
+
"programming_languages": self._to_list(
|
| 338 |
+
raw.get("programming_languages") or []),
|
| 339 |
+
"total_experience_years": self._calc_experience_years(experience_list),
|
| 340 |
+
"experience": experience_list,
|
| 341 |
+
"education": education,
|
| 342 |
+
"certifications": certifications,
|
| 343 |
+
"projects": projects,
|
| 344 |
+
"publications": self._to_list(raw.get("publications") or []),
|
| 345 |
+
"achievements": self._to_list(raw.get("achievements") or []),
|
| 346 |
+
"parser_source": raw.get("parser_source", "huggingface"),
|
| 347 |
+
"raw_parser_response": raw,
|
| 348 |
+
}
|
| 349 |
+
|
| 350 |
+
# ------------------------------------------------------------------
|
| 351 |
+
# Post-processing fallback extractors
|
| 352 |
+
# ------------------------------------------------------------------
|
| 353 |
+
|
| 354 |
+
@staticmethod
|
| 355 |
+
def _fallback_extract_name(text: str) -> str:
|
| 356 |
+
"""
|
| 357 |
+
Extract candidate name from the first line of the resume.
|
| 358 |
+
Handles all-caps, title case, and names with credentials.
|
| 359 |
+
e.g. "KARAN PRATAP SINGH" → "Karan Pratap Singh"
|
| 360 |
+
"PRIYA SILVA, B.Eng" → "Priya Silva"
|
| 361 |
+
"Maria A. Garcia" → "Maria A. Garcia"
|
| 362 |
+
"""
|
| 363 |
+
first_line = text.strip().splitlines()[0].strip()
|
| 364 |
+
lines = [l.strip() for l in text.strip().splitlines() if l.strip()]
|
| 365 |
+
|
| 366 |
+
def clean_name_candidate(raw):
|
| 367 |
+
# Strip after pipe, comma, email, phone or credential suffixes
|
| 368 |
+
raw = re.split(r'[|,]', raw)[0].strip()
|
| 369 |
+
# Remove trailing credentials like "M.ENG.", "PhD", "MBA"
|
| 370 |
+
raw = re.sub(r'\s*[,\|]\s*.*$', '', raw).strip()
|
| 371 |
+
return raw
|
| 372 |
+
|
| 373 |
+
# Try first line
|
| 374 |
+
name_part = clean_name_candidate(lines[0])
|
| 375 |
+
if re.match(r'^[A-Z][a-zA-Z\s\-\.]{2,50}$', name_part) and len(name_part.split()) >= 2:
|
| 376 |
+
return name_part.title() if name_part.isupper() else name_part
|
| 377 |
+
|
| 378 |
+
# Try combining first two lines (two-column PDF — name split across lines)
|
| 379 |
+
if len(lines) >= 2:
|
| 380 |
+
combined = clean_name_candidate(lines[0] + " " + lines[1])
|
| 381 |
+
if re.match(r'^[A-Z][a-zA-Z\s\-\.]{2,50}$', combined) and len(combined.split()) >= 2:
|
| 382 |
+
return combined.title() if combined.isupper() else combined
|
| 383 |
+
|
| 384 |
+
# Fallback: first line even if single word
|
| 385 |
+
if re.match(r'^[A-Z][a-zA-Z\s\-\.]{2,50}$', name_part):
|
| 386 |
+
return name_part.title() if name_part.isupper() else name_part
|
| 387 |
+
return ""
|
| 388 |
+
|
| 389 |
+
@staticmethod
|
| 390 |
+
def _fallback_extract_education(text: str) -> list:
|
| 391 |
+
"""
|
| 392 |
+
Extract education entries from the EDUCATION section.
|
| 393 |
+
Handles formats:
|
| 394 |
+
Format A: "BSc CS, University of X 2020" (inline institution)
|
| 395 |
+
Format B: "BSc CS 2018-2022 \\n SLIIT City" (institution next line)
|
| 396 |
+
Format C: "BSc CS \\n University \\n 2020" (year on own line)
|
| 397 |
+
Multiple degrees on consecutive lines.
|
| 398 |
+
"""
|
| 399 |
+
# Stop at known section headers only — not at every line starting with 3 uppercase letters
|
| 400 |
+
# (which would wrongly stop at "SRM Institute..." or "NSBM Green University...")
|
| 401 |
+
_SECTION_STOP = (r'(?=\n(?:EXPERIENCE|WORK EXPERIENCE|PROFESSIONAL EXPERIENCE|'
|
| 402 |
+
r'SKILLS|TECHNICAL SKILLS|PROJECTS|FREELANCING|PUBLICATIONS|'
|
| 403 |
+
r'ACHIEVEMENTS|CERTIFICATIONS|REFERENCES|SUMMARY|OBJECTIVE|'
|
| 404 |
+
r'CAREER|AWARDS|INTERESTS|LANGUAGES|VOLUNTEER)[\s\n]|$)')
|
| 405 |
+
|
| 406 |
+
edu_section = re.search(
|
| 407 |
+
r'EDUCATION(?:\s*[&/]\s*[\w\s]+)?\s*\n(.*?)' + _SECTION_STOP,
|
| 408 |
+
text, re.IGNORECASE | re.DOTALL
|
| 409 |
)
|
| 410 |
+
if not edu_section:
|
| 411 |
+
return []
|
| 412 |
+
|
| 413 |
+
lines = [l.strip() for l in edu_section.group(1).splitlines() if l.strip()]
|
| 414 |
+
|
| 415 |
+
DEGREE_KEYWORDS = [
|
| 416 |
+
'bachelor', 'master', 'phd', 'doctorate', 'bsc', 'msc', 'mba',
|
| 417 |
+
'b.tech', 'b.e', 'm.tech', 'diploma', 'associate', 'b.s', 'm.s',
|
| 418 |
+
'honours', 'hons', 'b.eng', 'm.eng', 'b.sc', 'm.sc', 'hnd',
|
| 419 |
+
'beng', 'meng', 'gce', 'a/l',
|
| 420 |
+
]
|
| 421 |
+
# 'engineering' and 'sciences' intentionally excluded —
|
| 422 |
+
# they appear in field-of-study strings like "Computer Science and Engineering"
|
| 423 |
+
INST_ENDINGS = ['university', 'universities', 'institute', 'institution',
|
| 424 |
+
'college', 'school', 'academy', 'polytechnic', 'faculty',
|
| 425 |
+
'technology', 'technologies']
|
| 426 |
+
INST_ACRONYMS = {'sliit', 'mit', 'nsbm', 'iit', 'nit', 'bits', 'vit',
|
| 427 |
+
'lpu', 'pearson'}
|
| 428 |
+
DEGREE_STARTS = ['bsc', 'msc', 'beng', 'meng', 'b.sc', 'm.sc', 'b.eng',
|
| 429 |
+
'm.eng', 'hnd', 'phd', 'mba', 'bachelor', 'master',
|
| 430 |
+
'diploma', 'gce', 'a/l', 'associate', 'honours']
|
| 431 |
+
|
| 432 |
+
def is_institution_line(line):
|
| 433 |
+
ll = line.lower().strip()
|
| 434 |
+
# Reject lines that start with a degree abbreviation
|
| 435 |
+
if any(ll.startswith(d) for d in DEGREE_STARTS):
|
| 436 |
+
return False
|
| 437 |
+
return (any(kw in ll for kw in INST_ENDINGS) or
|
| 438 |
+
any(ll.startswith(ac) for ac in INST_ACRONYMS) or
|
| 439 |
+
ll in INST_ACRONYMS)
|
| 440 |
+
|
| 441 |
+
def is_year_line(line):
|
| 442 |
+
return bool(re.match(r'^\d{4}\s*[-–]?\s*(\d{4}|present)?$',
|
| 443 |
+
line.strip(), re.IGNORECASE))
|
| 444 |
+
|
| 445 |
+
def extract_inline_institution(line):
|
| 446 |
+
"""
|
| 447 |
+
Extract institution from inline patterns like:
|
| 448 |
+
"HND in Software Engineering - Pearson (2020-2022)"
|
| 449 |
+
→ institution = "Pearson", year = 2022
|
| 450 |
+
Returns (institution, year) or ("", None)
|
| 451 |
+
"""
|
| 452 |
+
# Pattern: "Degree - Institution (year)" or "Degree - Institution year-year"
|
| 453 |
+
m = re.search(r'[-–]\s*([A-Z][^\d\n(]+?)\s*(?:\((\d{4})[-–]\d{4}\)|(\d{4})[-–]\d{4})', line)
|
| 454 |
+
if m:
|
| 455 |
+
inst_raw = m.group(1).strip().rstrip('.,')
|
| 456 |
+
yr_str = m.group(2) or m.group(3)
|
| 457 |
+
year = int(yr_str) if yr_str else None
|
| 458 |
+
return inst_raw, year
|
| 459 |
+
return "", None
|
| 460 |
+
|
| 461 |
+
results = []
|
| 462 |
+
i = 0
|
| 463 |
+
while i < len(lines):
|
| 464 |
+
line = lines[i]
|
| 465 |
+
|
| 466 |
+
# ── Format A: institution line BEFORE degree (two-column PDFs) ──
|
| 467 |
+
# e.g. "London Metropolitan University - March 2023 - February 2024"
|
| 468 |
+
# "BEng (Hons) Software Engineering (First class honours)"
|
| 469 |
+
if is_institution_line(line) and (i + 1) < len(lines):
|
| 470 |
+
nxt = lines[i + 1]
|
| 471 |
+
if any(kw in nxt.lower() for kw in DEGREE_KEYWORDS):
|
| 472 |
+
institution = ResumeParserClient._extract_institution_name(line)
|
| 473 |
+
yr = re.search(r'\b(20\d{2}|19\d{2})\b', line)
|
| 474 |
+
grad_year = int(yr.group(1)) if yr else None
|
| 475 |
+
degree_line = re.sub(r'\s+Overall\s+\w+$', '', nxt, flags=re.IGNORECASE).strip()
|
| 476 |
+
results.append({
|
| 477 |
+
"degree": degree_line,
|
| 478 |
+
"field": None,
|
| 479 |
+
"institution": institution,
|
| 480 |
+
"year": grad_year,
|
| 481 |
+
})
|
| 482 |
+
i += 2
|
| 483 |
+
continue
|
| 484 |
+
|
| 485 |
+
# ── Format B: degree line with optional inline institution ───────
|
| 486 |
+
if any(kw in line.lower() for kw in DEGREE_KEYWORDS):
|
| 487 |
+
year_match = re.search(r'(\d{4})\s*[-–]\s*(\d{4}|\bpresent\b)', line, re.IGNORECASE)
|
| 488 |
+
single_year = re.search(r'\b(20\d{2}|19\d{2})\b\s*$', line)
|
| 489 |
+
grad_year = None
|
| 490 |
+
|
| 491 |
+
if year_match:
|
| 492 |
+
end = year_match.group(2)
|
| 493 |
+
grad_year = int(end) if end.isdigit() else None
|
| 494 |
+
line = line[:year_match.start()].strip()
|
| 495 |
+
elif single_year:
|
| 496 |
+
grad_year = int(single_year.group(1))
|
| 497 |
+
line = line[:single_year.start()].strip()
|
| 498 |
+
|
| 499 |
+
# Check for inline institution after dash:
|
| 500 |
+
# "HND in Software Engineering - Pearson (2020-2022)"
|
| 501 |
+
inline_inst, inline_yr = extract_inline_institution(lines[i])
|
| 502 |
+
if inline_inst:
|
| 503 |
+
# Strip the "- Institution (year)" part from degree
|
| 504 |
+
degree_clean = re.sub(r'\s*[-–]\s*' + re.escape(inline_inst) + r'.*$',
|
| 505 |
+
'', lines[i]).strip()
|
| 506 |
+
degree_clean = re.sub(r'\s*[-–]\s*\w+\s+\(\d{4}[-–]\d{4}\).*$',
|
| 507 |
+
'', degree_clean).strip()
|
| 508 |
+
results.append({
|
| 509 |
+
"degree": degree_clean,
|
| 510 |
+
"field": None,
|
| 511 |
+
"institution": inline_inst,
|
| 512 |
+
"year": inline_yr or grad_year,
|
| 513 |
+
})
|
| 514 |
+
i += 1
|
| 515 |
+
continue
|
| 516 |
+
|
| 517 |
+
# Split by comma for degree / field / inline institution
|
| 518 |
+
parts = [p.strip() for p in line.split(',')]
|
| 519 |
+
degree = parts[0]
|
| 520 |
+
field = ""
|
| 521 |
+
institution = ""
|
| 522 |
+
|
| 523 |
+
if len(parts) >= 2:
|
| 524 |
+
last = parts[-1].strip()
|
| 525 |
+
if is_institution_line(last):
|
| 526 |
+
institution = ResumeParserClient._extract_institution_name(last)
|
| 527 |
+
field = parts[1] if len(parts) >= 3 else ""
|
| 528 |
+
else:
|
| 529 |
+
field = parts[1] if len(parts) > 1 else ""
|
| 530 |
+
|
| 531 |
+
# Look at following lines for institution / year
|
| 532 |
+
j = i + 1
|
| 533 |
+
while j < len(lines) and not institution:
|
| 534 |
+
nxt = lines[j]
|
| 535 |
+
if is_year_line(nxt):
|
| 536 |
+
if not grad_year:
|
| 537 |
+
yr = re.search(r'\b(20\d{2}|19\d{2})\b', nxt)
|
| 538 |
+
if yr: grad_year = int(yr.group(1))
|
| 539 |
+
j += 1
|
| 540 |
+
elif is_institution_line(nxt):
|
| 541 |
+
institution = ResumeParserClient._extract_institution_name(nxt)
|
| 542 |
+
j += 1
|
| 543 |
+
break
|
| 544 |
+
else:
|
| 545 |
+
break
|
| 546 |
+
|
| 547 |
+
i = j - 1 if j > i + 1 else i
|
| 548 |
+
results.append({
|
| 549 |
+
"degree": degree,
|
| 550 |
+
"field": field or None,
|
| 551 |
+
"institution": institution or None,
|
| 552 |
+
"year": grad_year,
|
| 553 |
+
})
|
| 554 |
+
i += 1
|
| 555 |
+
|
| 556 |
+
return results
|
| 557 |
+
|
| 558 |
+
@staticmethod
|
| 559 |
+
def _fallback_extract_projects(text: str) -> list:
|
| 560 |
+
"""
|
| 561 |
+
Extract project names from the PROJECTS section.
|
| 562 |
+
Handles bullet styles (•, -, *, ›), colon separators, and plain lines.
|
| 563 |
+
Returns just the project name (text before dash/colon description).
|
| 564 |
+
"""
|
| 565 |
+
_SECTION_STOP = (r'(?=\n(?:EXPERIENCE|WORK EXPERIENCE|PROFESSIONAL EXPERIENCE|'
|
| 566 |
+
r'SKILLS|TECHNICAL SKILLS|EDUCATION|PUBLICATIONS|'
|
| 567 |
+
r'ACHIEVEMENTS|CERTIFICATIONS|REFERENCES|SUMMARY|OBJECTIVE|'
|
| 568 |
+
r'CAREER|AWARDS|INTERESTS|LANGUAGES|VOLUNTEER)[\s\n]|$)')
|
| 569 |
+
|
| 570 |
+
proj_section = re.search(
|
| 571 |
+
r'(?:FREELANCING\s+)?PROJECTS?\s*\n(.*?)' + _SECTION_STOP,
|
| 572 |
+
text, re.IGNORECASE | re.DOTALL
|
| 573 |
)
|
| 574 |
+
if not proj_section:
|
| 575 |
+
return []
|
| 576 |
|
| 577 |
+
raw_lines = proj_section.group(1).splitlines()
|
| 578 |
+
has_bullets = any(
|
| 579 |
+
re.match(r'^[•\-\*›▪▸]', l.strip()) for l in raw_lines if l.strip()
|
| 580 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 581 |
|
| 582 |
+
bullets, current = [], ""
|
| 583 |
+
|
| 584 |
+
if has_bullets:
|
| 585 |
+
# Join continuation lines into their parent bullet
|
| 586 |
+
for line in raw_lines:
|
| 587 |
+
line = line.strip()
|
| 588 |
+
if not line:
|
| 589 |
+
continue
|
| 590 |
+
if re.match(r'^[•\-\*›▪▸]', line):
|
| 591 |
+
if current: bullets.append(current)
|
| 592 |
+
current = re.sub(r'^[•\-\*›▪▸]\s*', '', line)
|
| 593 |
+
elif current:
|
| 594 |
+
current += " " + line
|
| 595 |
+
if current: bullets.append(current)
|
| 596 |
+
else:
|
| 597 |
+
# No bullets — each non-empty line is a project entry
|
| 598 |
+
bullets = [l.strip() for l in raw_lines if l.strip()]
|
| 599 |
+
|
| 600 |
+
projects = []
|
| 601 |
+
for b in bullets:
|
| 602 |
+
# "SYSCONEX co-founder 2024 May to 2024 December" → "SYSCONEX co-founder"
|
| 603 |
+
year_in_middle = re.search(r'\s+\d{4}\s+\w+', b)
|
| 604 |
+
if year_in_middle:
|
| 605 |
+
projects.append(b[:year_in_middle.start()].strip())
|
| 606 |
+
continue
|
| 607 |
+
# "Name - description" or "Name: description"
|
| 608 |
+
m = re.match(r'^(.+?)\s*[-–:]\s*.+', b)
|
| 609 |
+
if m:
|
| 610 |
+
projects.append(m.group(1).strip())
|
| 611 |
+
continue
|
| 612 |
+
# No separator — take first 1-3 words as project name
|
| 613 |
+
# (handles "PaymentGateway API open source..." → "PaymentGateway API")
|
| 614 |
+
words = b.split()
|
| 615 |
+
if len(words) <= 3:
|
| 616 |
+
projects.append(b.strip())
|
| 617 |
+
else:
|
| 618 |
+
# Take up to first 3 words if they look like a name (title-case or camelcase)
|
| 619 |
+
name_words = []
|
| 620 |
+
for w in words:
|
| 621 |
+
if w[0].isupper() or w[0].isdigit():
|
| 622 |
+
name_words.append(w)
|
| 623 |
+
else:
|
| 624 |
+
break
|
| 625 |
+
projects.append(' '.join(name_words) if name_words else words[0])
|
| 626 |
+
|
| 627 |
+
return [p for p in projects if len(p) > 1]
|
| 628 |
+
|
| 629 |
+
@staticmethod
|
| 630 |
+
def _extract_institution_name(raw_inst: str) -> str:
|
| 631 |
+
"""
|
| 632 |
+
Extract institution name from a line that may contain
|
| 633 |
+
trailing location, year, or other noise.
|
| 634 |
+
|
| 635 |
+
Examples:
|
| 636 |
+
"SRM Institute of Science and Technology Delhi NCR, India"
|
| 637 |
+
→ "SRM Institute of Science and Technology"
|
| 638 |
+
"Stanford University 2019"
|
| 639 |
+
→ "Stanford University"
|
| 640 |
+
"NIBM Colombo, Sri Lanka"
|
| 641 |
+
→ "NIBM"
|
| 642 |
+
"Khalifa University, Abu Dhabi, UAE"
|
| 643 |
+
→ "Khalifa University"
|
| 644 |
+
"""
|
| 645 |
+
# Strip trailing year (e.g. "Stanford University 2019")
|
| 646 |
+
raw_inst = re.sub(r'\s+\b(19|20)\d{2}\b\s*$', '', raw_inst).strip()
|
| 647 |
+
|
| 648 |
+
# Double-space split — PDF column alignment artifact
|
| 649 |
+
if ' ' in raw_inst:
|
| 650 |
+
return raw_inst.split(' ')[0].strip()
|
| 651 |
+
|
| 652 |
+
INST_ENDINGS = [
|
| 653 |
+
'technology', 'technologies', 'university', 'universities',
|
| 654 |
+
'institute', 'institution', 'college', 'school', 'academy',
|
| 655 |
+
'polytechnic', 'faculty', 'sciences', 'engineering',
|
| 656 |
+
]
|
| 657 |
+
INST_ACRONYMS = {
|
| 658 |
+
'sliit', 'mit', 'nsbm', 'iit', 'nit', 'bits', 'vit', 'lpu',
|
| 659 |
+
'nibm', 'iisc', 'nus', 'ntu', 'uom', 'ucl', 'eth',
|
| 660 |
+
}
|
| 661 |
+
CONNECTORS = {'of', 'and', 'the', 'for', 'in', 'at', '&', 'de', 'la'}
|
| 662 |
+
|
| 663 |
+
tokens = raw_inst.split()
|
| 664 |
+
raw_lower = raw_inst.lower()
|
| 665 |
+
|
| 666 |
+
# Standalone acronym: only when no inst_ending keyword in line
|
| 667 |
+
if (tokens and tokens[0].lower() in INST_ACRONYMS and
|
| 668 |
+
not any(kw in raw_lower for kw in INST_ENDINGS)):
|
| 669 |
+
return tokens[0]
|
| 670 |
+
|
| 671 |
+
# Find last institution keyword token, then extend through connectors
|
| 672 |
+
best_end = -1
|
| 673 |
+
for i, tok in enumerate(tokens):
|
| 674 |
+
if tok.lower().rstrip('.,') in INST_ENDINGS:
|
| 675 |
+
best_end = i
|
| 676 |
+
|
| 677 |
+
if best_end >= 0:
|
| 678 |
+
end = best_end + 1
|
| 679 |
+
while end < len(tokens):
|
| 680 |
+
if tokens[end].lower() in CONNECTORS and end + 1 < len(tokens):
|
| 681 |
+
end += 2
|
| 682 |
+
else:
|
| 683 |
+
break
|
| 684 |
+
return ' '.join(tokens[:end]).strip()
|
| 685 |
+
|
| 686 |
+
# Fallback: strip after last comma
|
| 687 |
+
comma_idx = raw_inst.rfind(',')
|
| 688 |
+
return raw_inst[:comma_idx].strip() if comma_idx > 0 else raw_inst.strip()
|
| 689 |
+
|
| 690 |
+
@staticmethod
|
| 691 |
+
def _calc_experience_years(experience_list: list) -> float:
|
| 692 |
+
"""
|
| 693 |
+
Calculate total years of experience from a list of job entries.
|
| 694 |
+
Handles overlapping date ranges by merging intervals.
|
| 695 |
+
"""
|
| 696 |
+
from datetime import datetime
|
| 697 |
+
|
| 698 |
+
def parse_date(date_str: str):
|
| 699 |
+
if not date_str:
|
| 700 |
+
return None
|
| 701 |
+
date_str = date_str.strip()
|
| 702 |
+
if date_str.lower() == "present":
|
| 703 |
+
return datetime.today()
|
| 704 |
+
for fmt in ("%m/%d/%Y", "%m/%Y", "%B %Y", "%b %Y",
|
| 705 |
+
"%B %d, %Y", "%b %d, %Y", "%Y-%m-%d", "%Y"):
|
| 706 |
+
try:
|
| 707 |
+
return datetime.strptime(date_str, fmt)
|
| 708 |
+
except ValueError:
|
| 709 |
+
continue
|
| 710 |
+
return None
|
| 711 |
+
|
| 712 |
+
intervals = []
|
| 713 |
+
for job in experience_list:
|
| 714 |
+
start = parse_date(job.get("start_date", ""))
|
| 715 |
+
end = parse_date(job.get("end_date", ""))
|
| 716 |
+
if start and end and end >= start:
|
| 717 |
+
intervals.append((start, end))
|
| 718 |
+
|
| 719 |
+
if not intervals:
|
| 720 |
+
return 0.0
|
| 721 |
+
|
| 722 |
+
intervals.sort(key=lambda x: x[0])
|
| 723 |
+
merged = [intervals[0]]
|
| 724 |
+
for start, end in intervals[1:]:
|
| 725 |
+
if start <= merged[-1][1]:
|
| 726 |
+
merged[-1] = (merged[-1][0], max(merged[-1][1], end))
|
| 727 |
+
else:
|
| 728 |
+
merged.append((start, end))
|
| 729 |
+
|
| 730 |
+
total_days = sum((end - start).days for start, end in merged)
|
| 731 |
+
return round(total_days / 365.25, 1)
|
| 732 |
+
|
| 733 |
+
@staticmethod
|
| 734 |
+
def _to_list(value) -> list:
|
| 735 |
+
"""Ensure value is always a list."""
|
| 736 |
+
if isinstance(value, list):
|
| 737 |
+
return value
|
| 738 |
+
if isinstance(value, str):
|
| 739 |
+
return [v.strip() for v in value.split(",") if v.strip()]
|
| 740 |
+
return []
|
| 741 |
+
|
| 742 |
+
@staticmethod
|
| 743 |
+
def _fallback(error: str = "unknown") -> dict:
|
| 744 |
+
"""
|
| 745 |
+
Return a minimal valid dict so the pipeline can still run
|
| 746 |
+
using only GitHub + graph signals.
|
| 747 |
+
"""
|
| 748 |
+
logger.warning("[ResumeParser] Using fallback empty resume data (error: %s)", error)
|
| 749 |
+
return {
|
| 750 |
+
"name": "",
|
| 751 |
+
"skills": [],
|
| 752 |
+
"programming_languages": [],
|
| 753 |
+
"total_experience_years": 0,
|
| 754 |
+
"education": [],
|
| 755 |
+
"certifications": [],
|
| 756 |
+
"projects": [],
|
| 757 |
+
"parser_error": error,
|
| 758 |
+
"parser_source": "none",
|
| 759 |
+
}
|