Spaces:
Running
Running
update to use gemini api to precess cv
Browse files- __pycache__/config.cpython-312.pyc +0 -0
- __pycache__/main.cpython-312.pyc +0 -0
- config.py +25 -0
- main.py +53 -15
- requirements.txt +2 -1
- services/new/__pycache__/cv_processor_v2.cpython-312.pyc +0 -0
- services/new/config.py +0 -68
- services/new/cv_processor_v2.py +9 -9
- services/new/google/__init__.py +1 -0
- services/new/google/__pycache__/__init__.cpython-312.pyc +0 -0
- services/new/google/__pycache__/config.cpython-312.pyc +0 -0
- services/new/google/__pycache__/gemini_extractor.cpython-312.pyc +0 -0
- services/new/google/gemini_extractor.py +219 -0
__pycache__/config.cpython-312.pyc
ADDED
|
Binary file (706 Bytes). View file
|
|
|
__pycache__/main.cpython-312.pyc
CHANGED
|
Binary files a/__pycache__/main.cpython-312.pyc and b/__pycache__/main.cpython-312.pyc differ
|
|
|
config.py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
config.py
|
| 3 |
+
=========
|
| 4 |
+
Global configuration for Job Processor (Gemini V2 pipeline).
|
| 5 |
+
Lives at processor root alongside .env.
|
| 6 |
+
"""
|
| 7 |
+
|
| 8 |
+
import os
|
| 9 |
+
from pathlib import Path
|
| 10 |
+
from dotenv import load_dotenv
|
| 11 |
+
|
| 12 |
+
# Load .env file from root
|
| 13 |
+
env_path = Path(__file__).parent / ".env"
|
| 14 |
+
load_dotenv(dotenv_path=env_path)
|
| 15 |
+
|
| 16 |
+
# ---------------------------------------------------------------------------
|
| 17 |
+
# Google Gemini Settings
|
| 18 |
+
# ---------------------------------------------------------------------------
|
| 19 |
+
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY", "")
|
| 20 |
+
|
| 21 |
+
# Default model: Gemini 3.5 Flash-Lite
|
| 22 |
+
GEMINI_MODEL = "gemini-3.5-flash-lite"
|
| 23 |
+
|
| 24 |
+
MAX_OUTPUT_TOKENS = 4096
|
| 25 |
+
TEMPERATURE = 0.1
|
main.py
CHANGED
|
@@ -417,44 +417,82 @@ def batch_match_cv(payload: BatchMatchCVRequest):
|
|
| 417 |
|
| 418 |
|
| 419 |
# ---------------------------------------------------------------------------
|
| 420 |
-
# POST /v2/ping-llm — quick diagnostic endpoint to test LLM & latency
|
| 421 |
# ---------------------------------------------------------------------------
|
| 422 |
|
| 423 |
|
| 424 |
class PingLLMRequest(BaseModel):
|
| 425 |
-
prompt: str = Field(default="Say hello in one short sentence.", description="Prompt to send to
|
| 426 |
-
max_tokens: Optional[int] = Field(default=64, description="Max tokens to generate")
|
| 427 |
|
| 428 |
|
| 429 |
@app.post("/v2/ping-llm", response_model=APIResponse)
|
| 430 |
def ping_llm(payload: PingLLMRequest = PingLLMRequest()):
|
| 431 |
-
"""Diagnostic endpoint to ping
|
| 432 |
t_start = datetime.now(timezone.utc)
|
| 433 |
try:
|
| 434 |
-
|
| 435 |
-
messages=[{"role": "user", "content": payload.prompt}],
|
| 436 |
-
max_tokens=payload.max_tokens or 64,
|
| 437 |
-
temperature=0.1,
|
| 438 |
-
)
|
| 439 |
-
reply = resp["choices"][0]["message"]["content"].strip()
|
| 440 |
t_end = datetime.now(timezone.utc)
|
| 441 |
duration_ms = int((t_end - t_start).total_seconds() * 1000)
|
| 442 |
|
| 443 |
-
active_cfg = cv_processor_v2.extractor._active_cfg or {}
|
| 444 |
return APIResponse(
|
| 445 |
-
message="
|
| 446 |
statusCode=200,
|
| 447 |
payload={
|
| 448 |
-
"model":
|
| 449 |
"prompt": payload.prompt,
|
| 450 |
-
"reply": reply,
|
| 451 |
"duration_ms": duration_ms,
|
| 452 |
"duration_seconds": round(duration_ms / 1000, 2),
|
|
|
|
| 453 |
},
|
| 454 |
)
|
| 455 |
except Exception as exc:
|
| 456 |
return APIResponse(
|
| 457 |
-
message=f"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 458 |
statusCode=500,
|
| 459 |
)
|
| 460 |
|
|
|
|
| 417 |
|
| 418 |
|
| 419 |
# ---------------------------------------------------------------------------
|
| 420 |
+
# POST /v2/ping-llm — quick diagnostic endpoint to test Gemini LLM & latency
|
| 421 |
# ---------------------------------------------------------------------------
|
| 422 |
|
| 423 |
|
| 424 |
class PingLLMRequest(BaseModel):
|
| 425 |
+
prompt: str = Field(default="Say hello in one short sentence.", description="Prompt to send to Gemini")
|
|
|
|
| 426 |
|
| 427 |
|
| 428 |
@app.post("/v2/ping-llm", response_model=APIResponse)
|
| 429 |
def ping_llm(payload: PingLLMRequest = PingLLMRequest()):
|
| 430 |
+
"""Diagnostic endpoint to ping Gemini API directly and measure latency."""
|
| 431 |
t_start = datetime.now(timezone.utc)
|
| 432 |
try:
|
| 433 |
+
res = cv_processor_v2.gemini_extractor.ping(prompt=payload.prompt)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 434 |
t_end = datetime.now(timezone.utc)
|
| 435 |
duration_ms = int((t_end - t_start).total_seconds() * 1000)
|
| 436 |
|
|
|
|
| 437 |
return APIResponse(
|
| 438 |
+
message="Gemini ping successful",
|
| 439 |
statusCode=200,
|
| 440 |
payload={
|
| 441 |
+
"model": res.get("model", ""),
|
| 442 |
"prompt": payload.prompt,
|
| 443 |
+
"reply": res.get("reply", ""),
|
| 444 |
"duration_ms": duration_ms,
|
| 445 |
"duration_seconds": round(duration_ms / 1000, 2),
|
| 446 |
+
"usageMetadata": res.get("usageMetadata", {}),
|
| 447 |
},
|
| 448 |
)
|
| 449 |
except Exception as exc:
|
| 450 |
return APIResponse(
|
| 451 |
+
message=f"Gemini ping error: {exc}",
|
| 452 |
+
statusCode=500,
|
| 453 |
+
)
|
| 454 |
+
|
| 455 |
+
|
| 456 |
+
# ---------------------------------------------------------------------------
|
| 457 |
+
# GET /v2/models — List models & inspect limits (context window, rate limits)
|
| 458 |
+
# ---------------------------------------------------------------------------
|
| 459 |
+
|
| 460 |
+
|
| 461 |
+
@app.get("/v2/models", response_model=APIResponse)
|
| 462 |
+
def list_models():
|
| 463 |
+
"""Lists available Gemini models and their input/output token limits."""
|
| 464 |
+
try:
|
| 465 |
+
data = cv_processor_v2.gemini_extractor.list_models()
|
| 466 |
+
return APIResponse(
|
| 467 |
+
message="Models fetched successfully",
|
| 468 |
+
statusCode=200,
|
| 469 |
+
payload=data,
|
| 470 |
+
)
|
| 471 |
+
except Exception as exc:
|
| 472 |
+
return APIResponse(
|
| 473 |
+
message=f"Failed to fetch models: {exc}",
|
| 474 |
+
statusCode=500,
|
| 475 |
+
)
|
| 476 |
+
|
| 477 |
+
|
| 478 |
+
# ---------------------------------------------------------------------------
|
| 479 |
+
# GET /v2/models/{model_name} — Inspect specific model token limits
|
| 480 |
+
# ---------------------------------------------------------------------------
|
| 481 |
+
|
| 482 |
+
|
| 483 |
+
@app.get("/v2/models/{model_name:path}", response_model=APIResponse)
|
| 484 |
+
def get_model_details(model_name: str):
|
| 485 |
+
"""Inspects a specific model's token limits and parameters."""
|
| 486 |
+
try:
|
| 487 |
+
data = cv_processor_v2.gemini_extractor.get_model(model_name)
|
| 488 |
+
return APIResponse(
|
| 489 |
+
message=f"Model {model_name} details fetched successfully",
|
| 490 |
+
statusCode=200,
|
| 491 |
+
payload=data,
|
| 492 |
+
)
|
| 493 |
+
except Exception as exc:
|
| 494 |
+
return APIResponse(
|
| 495 |
+
message=f"Failed to fetch model {model_name}: {exc}",
|
| 496 |
statusCode=500,
|
| 497 |
)
|
| 498 |
|
requirements.txt
CHANGED
|
@@ -37,4 +37,5 @@ optimum[onnxruntime]
|
|
| 37 |
onnxruntime
|
| 38 |
|
| 39 |
# In-process CV embedding cache (no external service required)
|
| 40 |
-
cachetools
|
|
|
|
|
|
| 37 |
onnxruntime
|
| 38 |
|
| 39 |
# In-process CV embedding cache (no external service required)
|
| 40 |
+
cachetools
|
| 41 |
+
python-dotenv
|
services/new/__pycache__/cv_processor_v2.cpython-312.pyc
CHANGED
|
Binary files a/services/new/__pycache__/cv_processor_v2.cpython-312.pyc and b/services/new/__pycache__/cv_processor_v2.cpython-312.pyc differ
|
|
|
services/new/config.py
DELETED
|
@@ -1,68 +0,0 @@
|
|
| 1 |
-
"""
|
| 2 |
-
config.py
|
| 3 |
-
=========
|
| 4 |
-
Model configuration for the V2 LLM-powered CV extraction pipeline.
|
| 5 |
-
|
| 6 |
-
The processor will attempt to use the model at index 0.
|
| 7 |
-
If its download fails, it falls back to index 1.
|
| 8 |
-
Put your preferred model first.
|
| 9 |
-
"""
|
| 10 |
-
|
| 11 |
-
from pathlib import Path
|
| 12 |
-
|
| 13 |
-
# ---------------------------------------------------------------------------
|
| 14 |
-
# Model priority list
|
| 15 |
-
# ---------------------------------------------------------------------------
|
| 16 |
-
# Each entry:
|
| 17 |
-
# repo_id — HuggingFace repo (must be ungated / publicly accessible)
|
| 18 |
-
# filename — exact GGUF filename inside that repo
|
| 19 |
-
# n_ctx — context window in tokens (input + output combined)
|
| 20 |
-
# n_threads — CPU threads to use during inference (match your vCPU count)
|
| 21 |
-
# max_tokens — max tokens the LLM may generate in one response
|
| 22 |
-
# ---------------------------------------------------------------------------
|
| 23 |
-
|
| 24 |
-
MODELS = [
|
| 25 |
-
{
|
| 26 |
-
"label": "Qwen2.5-1.5B-Instruct Q4_K_M",
|
| 27 |
-
"repo_id": "Qwen/Qwen2.5-1.5B-Instruct-GGUF",
|
| 28 |
-
"filename": "qwen2.5-1.5b-instruct-q4_k_m.gguf",
|
| 29 |
-
"n_ctx": 8192,
|
| 30 |
-
"n_threads": 2,
|
| 31 |
-
"max_tokens": 2048,
|
| 32 |
-
},
|
| 33 |
-
{
|
| 34 |
-
"label": "Phi-3-mini-4k Q4_K_M",
|
| 35 |
-
"repo_id": "bartowski/Phi-3-mini-4k-instruct-GGUF",
|
| 36 |
-
"filename": "Phi-3-mini-4k-instruct-Q4_K_M.gguf",
|
| 37 |
-
"n_ctx": 4096,
|
| 38 |
-
"n_threads": 2,
|
| 39 |
-
"max_tokens": 2048,
|
| 40 |
-
},
|
| 41 |
-
{
|
| 42 |
-
"label": "TinyLlama-1.1B Q4_K_M",
|
| 43 |
-
"repo_id": "TheBloke/TinyLlama-1.1B-Chat-v1.0-GGUF",
|
| 44 |
-
"filename": "tinyllama-1.1b-chat-v1.0.Q4_K_M.gguf",
|
| 45 |
-
"n_ctx": 2048,
|
| 46 |
-
"n_threads": 2,
|
| 47 |
-
"max_tokens": 1024,
|
| 48 |
-
},
|
| 49 |
-
]
|
| 50 |
-
|
| 51 |
-
# ---------------------------------------------------------------------------
|
| 52 |
-
# Cache directory — GGUF files are downloaded here once and reused
|
| 53 |
-
# ---------------------------------------------------------------------------
|
| 54 |
-
# On HuggingFace Spaces free tier the filesystem is ephemeral, so the model
|
| 55 |
-
# re-downloads on cold start. Point this to a mounted persistent volume
|
| 56 |
-
# (e.g. /data/model_cache) if you have one.
|
| 57 |
-
# ---------------------------------------------------------------------------
|
| 58 |
-
|
| 59 |
-
MODEL_CACHE_DIR = str(Path(__file__).parent.parent.parent / "model_cache")
|
| 60 |
-
|
| 61 |
-
# ---------------------------------------------------------------------------
|
| 62 |
-
# CV truncation limit
|
| 63 |
-
# ---------------------------------------------------------------------------
|
| 64 |
-
# Maximum characters of CV markdown sent to the LLM.
|
| 65 |
-
# 12,000 chars easily fits 2-4 page CVs comfortably into an 8k context window.
|
| 66 |
-
# ---------------------------------------------------------------------------
|
| 67 |
-
|
| 68 |
-
MAX_CV_CHARS = 12000
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
services/new/cv_processor_v2.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
"""Orchestrates V2 CV processing: file → markdown →
|
| 2 |
|
| 3 |
from __future__ import annotations
|
| 4 |
|
|
@@ -8,23 +8,22 @@ from pathlib import Path
|
|
| 8 |
from typing import Any, Dict
|
| 9 |
|
| 10 |
from services.new.cv_converter_v2 import CvConverterV2
|
| 11 |
-
from services.new.
|
| 12 |
|
| 13 |
logger = logging.getLogger(__name__)
|
| 14 |
|
| 15 |
|
| 16 |
class CvProcessorV2:
|
| 17 |
-
"""
|
| 18 |
|
| 19 |
def __init__(self) -> None:
|
| 20 |
self.converter = CvConverterV2()
|
| 21 |
-
self.
|
| 22 |
|
| 23 |
def process(self, file_bytes: bytes, filename: str) -> Dict[str, Any]:
|
| 24 |
"""
|
| 25 |
-
Convert file → markdown, then run
|
| 26 |
-
|
| 27 |
-
Raises ValueError on conversion failure.
|
| 28 |
"""
|
| 29 |
suffix = Path(filename).suffix.lower() if filename else ".pdf"
|
| 30 |
tmp_path = None
|
|
@@ -39,8 +38,8 @@ class CvProcessorV2:
|
|
| 39 |
if not conversion.success:
|
| 40 |
raise ValueError(conversion.error or "Conversion failed")
|
| 41 |
|
| 42 |
-
# Step 2 —
|
| 43 |
-
extracted = self.
|
| 44 |
|
| 45 |
return {
|
| 46 |
# ── LLM-extracted fields ──
|
|
@@ -59,6 +58,7 @@ class CvProcessorV2:
|
|
| 59 |
"projects": extracted.get("projects", []),
|
| 60 |
"awards": extracted.get("awards", []),
|
| 61 |
"sections": extracted.get("sections", []),
|
|
|
|
| 62 |
# ── Conversion metadata ──
|
| 63 |
"file_type": conversion.file_type,
|
| 64 |
"is_scanned": conversion.is_scanned,
|
|
|
|
| 1 |
+
"""Orchestrates V2 CV processing: file → markdown → Gemini extraction → payload."""
|
| 2 |
|
| 3 |
from __future__ import annotations
|
| 4 |
|
|
|
|
| 8 |
from typing import Any, Dict
|
| 9 |
|
| 10 |
from services.new.cv_converter_v2 import CvConverterV2
|
| 11 |
+
from services.new.google.gemini_extractor import GeminiCvExtractor
|
| 12 |
|
| 13 |
logger = logging.getLogger(__name__)
|
| 14 |
|
| 15 |
|
| 16 |
class CvProcessorV2:
|
| 17 |
+
"""Direct Gemini CV processor without local fallbacks."""
|
| 18 |
|
| 19 |
def __init__(self) -> None:
|
| 20 |
self.converter = CvConverterV2()
|
| 21 |
+
self.gemini_extractor = GeminiCvExtractor()
|
| 22 |
|
| 23 |
def process(self, file_bytes: bytes, filename: str) -> Dict[str, Any]:
|
| 24 |
"""
|
| 25 |
+
Convert file → markdown, then directly run Gemini extraction.
|
| 26 |
+
Raises ValueError on conversion failure or RuntimeError on API failure.
|
|
|
|
| 27 |
"""
|
| 28 |
suffix = Path(filename).suffix.lower() if filename else ".pdf"
|
| 29 |
tmp_path = None
|
|
|
|
| 38 |
if not conversion.success:
|
| 39 |
raise ValueError(conversion.error or "Conversion failed")
|
| 40 |
|
| 41 |
+
# Step 2 — direct Gemini extraction
|
| 42 |
+
extracted = self.gemini_extractor.extract(conversion.markdown)
|
| 43 |
|
| 44 |
return {
|
| 45 |
# ── LLM-extracted fields ──
|
|
|
|
| 58 |
"projects": extracted.get("projects", []),
|
| 59 |
"awards": extracted.get("awards", []),
|
| 60 |
"sections": extracted.get("sections", []),
|
| 61 |
+
"usage_metadata": extracted.get("usage_metadata", {}),
|
| 62 |
# ── Conversion metadata ──
|
| 63 |
"file_type": conversion.file_type,
|
| 64 |
"is_scanned": conversion.is_scanned,
|
services/new/google/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
# Google Gemini Client & Extractor
|
services/new/google/__pycache__/__init__.cpython-312.pyc
ADDED
|
Binary file (145 Bytes). View file
|
|
|
services/new/google/__pycache__/config.cpython-312.pyc
ADDED
|
Binary file (846 Bytes). View file
|
|
|
services/new/google/__pycache__/gemini_extractor.cpython-312.pyc
ADDED
|
Binary file (11.6 kB). View file
|
|
|
services/new/google/gemini_extractor.py
ADDED
|
@@ -0,0 +1,219 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
gemini_extractor.py
|
| 3 |
+
===================
|
| 4 |
+
CV field extraction using Google Gemini Interactions API.
|
| 5 |
+
Direct call with zero fallbacks.
|
| 6 |
+
"""
|
| 7 |
+
|
| 8 |
+
from __future__ import annotations
|
| 9 |
+
|
| 10 |
+
import json
|
| 11 |
+
import logging
|
| 12 |
+
import re
|
| 13 |
+
from typing import Any, Dict, List, Optional
|
| 14 |
+
import httpx
|
| 15 |
+
|
| 16 |
+
from config import (
|
| 17 |
+
GEMINI_API_KEY,
|
| 18 |
+
GEMINI_MODEL,
|
| 19 |
+
MAX_OUTPUT_TOKENS,
|
| 20 |
+
TEMPERATURE,
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
logger = logging.getLogger(__name__)
|
| 24 |
+
|
| 25 |
+
_SYSTEM_INSTRUCTION = (
|
| 26 |
+
"You are an expert CV/Resume parser. "
|
| 27 |
+
"Extract all information from the provided CV markdown and return a single valid JSON object. "
|
| 28 |
+
"Extract ONLY what is explicitly present in the CV — never invent or assume details."
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
_USER_PROMPT = """\
|
| 32 |
+
Parse the CV below and return a JSON object with EXACTLY this structure.
|
| 33 |
+
Use "" for missing strings and [] for missing arrays. Do not omit fields.
|
| 34 |
+
|
| 35 |
+
{{
|
| 36 |
+
"cv_title": "candidate professional title or most recent job title",
|
| 37 |
+
"seniority": "one of: intern | entry | mid | senior | lead",
|
| 38 |
+
"years_experience": 0,
|
| 39 |
+
"category": "one of: engineering | design | finance | marketing | sales | operations | healthcare | legal | education | hr | other",
|
| 40 |
+
"summary": "professional summary text or empty string",
|
| 41 |
+
"contact": {{"email": "", "phone": "", "location": ""}},
|
| 42 |
+
"links": [{{"label": "GitHub | LinkedIn | Portfolio | Website | Twitter", "url": "full URL"}}],
|
| 43 |
+
"skills": {{
|
| 44 |
+
"items": ["every skill as listed in CV"],
|
| 45 |
+
"technical": ["technical/hard skills only"],
|
| 46 |
+
"soft": ["soft/interpersonal skills only"]
|
| 47 |
+
}},
|
| 48 |
+
"experience": [{{"role": "", "company": "", "period": "", "responsibilities": []}}],
|
| 49 |
+
"education": [{{"institution": "", "degree": "", "period": ""}}],
|
| 50 |
+
"projects": [{{"name": "", "description": "", "tech_stack": [], "url": ""}}],
|
| 51 |
+
"awards": [{{"title": "", "year": "", "issuer": ""}}],
|
| 52 |
+
"sections": [{{"title": "EXACT heading as written in CV", "type": "summary | experience | education | skills | projects | awards | unknown", "content": "full section text"}}]
|
| 53 |
+
}}
|
| 54 |
+
|
| 55 |
+
Rules:
|
| 56 |
+
- sections: preserve the EXACT title of each section header from the CV so the UI keeps original naming.
|
| 57 |
+
- seniority: intern=0-1yr, entry=1-2yr, mid=2-5yr, senior=5-8yr, lead=8+yr or people manager.
|
| 58 |
+
- years_experience: integer from earliest start year to current year.
|
| 59 |
+
|
| 60 |
+
CV MARKDOWN:
|
| 61 |
+
{cv_markdown}"""
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
class GeminiCvExtractor:
|
| 65 |
+
"""Directly calls Google Gemini Interactions API without any fallback."""
|
| 66 |
+
|
| 67 |
+
def __init__(self, api_key: Optional[str] = None, model: Optional[str] = None):
|
| 68 |
+
self.api_key = api_key or GEMINI_API_KEY
|
| 69 |
+
self.model = model or GEMINI_MODEL
|
| 70 |
+
|
| 71 |
+
def _call_interactions(self, prompt: str, system_instruction: str = "") -> dict:
|
| 72 |
+
"""Call /v1beta/interactions directly."""
|
| 73 |
+
if not self.api_key or self.api_key == "YOUR_GEMINI_API_KEY_HERE":
|
| 74 |
+
raise ValueError("GEMINI_API_KEY is not set in processor/.env")
|
| 75 |
+
|
| 76 |
+
url = "https://generativelanguage.googleapis.com/v1beta/interactions"
|
| 77 |
+
headers = {
|
| 78 |
+
"x-goog-api-key": self.api_key,
|
| 79 |
+
"Content-Type": "application/json",
|
| 80 |
+
}
|
| 81 |
+
body: Dict[str, Any] = {
|
| 82 |
+
"model": self.model,
|
| 83 |
+
"input": prompt,
|
| 84 |
+
}
|
| 85 |
+
if system_instruction:
|
| 86 |
+
body["system_instruction"] = system_instruction
|
| 87 |
+
|
| 88 |
+
with httpx.Client(timeout=60.0) as client:
|
| 89 |
+
resp = client.post(url, headers=headers, json=body)
|
| 90 |
+
if resp.status_code != 200:
|
| 91 |
+
raise RuntimeError(f"Gemini Interactions API error ({resp.status_code}): {resp.text}")
|
| 92 |
+
|
| 93 |
+
data = resp.json()
|
| 94 |
+
reply = ""
|
| 95 |
+
for step in data.get("steps", []):
|
| 96 |
+
for item in step.get("content", []):
|
| 97 |
+
if item.get("type") == "text":
|
| 98 |
+
reply += item.get("text", "")
|
| 99 |
+
|
| 100 |
+
return {
|
| 101 |
+
"text": reply.strip(),
|
| 102 |
+
"usage": data.get("usage", {}),
|
| 103 |
+
}
|
| 104 |
+
|
| 105 |
+
def ping(self, prompt: str = "Say hello in one short sentence.") -> Dict[str, Any]:
|
| 106 |
+
"""Test API key and latency directly."""
|
| 107 |
+
res = self._call_interactions(prompt=prompt)
|
| 108 |
+
return {
|
| 109 |
+
"model": self.model,
|
| 110 |
+
"reply": res["text"],
|
| 111 |
+
"usageMetadata": res["usage"],
|
| 112 |
+
}
|
| 113 |
+
|
| 114 |
+
def list_models(self) -> Dict[str, Any]:
|
| 115 |
+
"""Call GET /v1beta/models directly."""
|
| 116 |
+
if not self.api_key or self.api_key == "YOUR_GEMINI_API_KEY_HERE":
|
| 117 |
+
raise ValueError("GEMINI_API_KEY is not set in processor/.env")
|
| 118 |
+
|
| 119 |
+
url = f"https://generativelanguage.googleapis.com/v1beta/models?key={self.api_key}"
|
| 120 |
+
with httpx.Client(timeout=30.0) as client:
|
| 121 |
+
resp = client.get(url)
|
| 122 |
+
if resp.status_code != 200:
|
| 123 |
+
raise RuntimeError(f"Failed to list models ({resp.status_code}): {resp.text}")
|
| 124 |
+
return resp.json()
|
| 125 |
+
|
| 126 |
+
def get_model(self, model_id: str) -> Dict[str, Any]:
|
| 127 |
+
"""Call GET /v1beta/models/{model_id} directly."""
|
| 128 |
+
if not self.api_key or self.api_key == "YOUR_GEMINI_API_KEY_HERE":
|
| 129 |
+
raise ValueError("GEMINI_API_KEY is not set in processor/.env")
|
| 130 |
+
|
| 131 |
+
clean_id = model_id.replace("models/", "")
|
| 132 |
+
url = f"https://generativelanguage.googleapis.com/v1beta/models/{clean_id}?key={self.api_key}"
|
| 133 |
+
with httpx.Client(timeout=30.0) as client:
|
| 134 |
+
resp = client.get(url)
|
| 135 |
+
if resp.status_code != 200:
|
| 136 |
+
raise RuntimeError(f"Failed to get model ({resp.status_code}): {resp.text}")
|
| 137 |
+
return resp.json()
|
| 138 |
+
|
| 139 |
+
def extract(self, markdown: str) -> Dict[str, Any]:
|
| 140 |
+
"""Extract structured CV profile and dynamic sections directly."""
|
| 141 |
+
prompt_text = _USER_PROMPT.format(cv_markdown=markdown)
|
| 142 |
+
res = self._call_interactions(
|
| 143 |
+
prompt=prompt_text,
|
| 144 |
+
system_instruction=_SYSTEM_INSTRUCTION,
|
| 145 |
+
)
|
| 146 |
+
|
| 147 |
+
data = self._parse_json(res["text"])
|
| 148 |
+
data = self._normalize(data)
|
| 149 |
+
data["completeness_score"] = self._completeness(data)
|
| 150 |
+
data["markdown"] = markdown
|
| 151 |
+
data["usage_metadata"] = res.get("usage", {})
|
| 152 |
+
return data
|
| 153 |
+
|
| 154 |
+
@staticmethod
|
| 155 |
+
def _parse_json(raw: str) -> Dict[str, Any]:
|
| 156 |
+
try:
|
| 157 |
+
return json.loads(raw.strip())
|
| 158 |
+
except json.JSONDecodeError:
|
| 159 |
+
match = re.search(r"\{.*\}", raw, re.DOTALL)
|
| 160 |
+
if match:
|
| 161 |
+
return json.loads(match.group())
|
| 162 |
+
raise ValueError(f"Could not parse valid JSON from Gemini output: {raw[:300]}")
|
| 163 |
+
|
| 164 |
+
@staticmethod
|
| 165 |
+
def _normalize(data: Dict[str, Any]) -> Dict[str, Any]:
|
| 166 |
+
data.setdefault("cv_title", "")
|
| 167 |
+
data.setdefault("seniority", "entry")
|
| 168 |
+
data.setdefault("years_experience", 0)
|
| 169 |
+
data.setdefault("category", "other")
|
| 170 |
+
data.setdefault("summary", "")
|
| 171 |
+
|
| 172 |
+
try:
|
| 173 |
+
data["years_experience"] = int(data["years_experience"])
|
| 174 |
+
except (TypeError, ValueError):
|
| 175 |
+
data["years_experience"] = 0
|
| 176 |
+
|
| 177 |
+
contact = data.get("contact")
|
| 178 |
+
if not isinstance(contact, dict):
|
| 179 |
+
data["contact"] = {"email": "", "phone": "", "location": ""}
|
| 180 |
+
else:
|
| 181 |
+
contact.setdefault("email", "")
|
| 182 |
+
contact.setdefault("phone", "")
|
| 183 |
+
contact.setdefault("location", "")
|
| 184 |
+
|
| 185 |
+
for key in ("links", "experience", "education", "projects", "awards", "sections"):
|
| 186 |
+
if not isinstance(data.get(key), list):
|
| 187 |
+
data[key] = []
|
| 188 |
+
|
| 189 |
+
skills = data.get("skills")
|
| 190 |
+
if not isinstance(skills, dict):
|
| 191 |
+
data["skills"] = {"items": [], "technical": [], "soft": []}
|
| 192 |
+
else:
|
| 193 |
+
for sub in ("items", "technical", "soft"):
|
| 194 |
+
if not isinstance(skills.get(sub), list):
|
| 195 |
+
skills[sub] = []
|
| 196 |
+
|
| 197 |
+
return data
|
| 198 |
+
|
| 199 |
+
@staticmethod
|
| 200 |
+
def _completeness(data: Dict[str, Any]) -> int:
|
| 201 |
+
score = 0
|
| 202 |
+
if isinstance(data.get("summary"), str) and len(data["summary"]) > 50:
|
| 203 |
+
score += 20
|
| 204 |
+
if data.get("experience"):
|
| 205 |
+
score += 25
|
| 206 |
+
if len(data.get("skills", {}).get("items", [])) >= 3:
|
| 207 |
+
score += 15
|
| 208 |
+
if data.get("education"):
|
| 209 |
+
score += 15
|
| 210 |
+
if data.get("contact", {}).get("email"):
|
| 211 |
+
score += 10
|
| 212 |
+
if data.get("years_experience", 0) > 0:
|
| 213 |
+
score += 5
|
| 214 |
+
if data.get("links"):
|
| 215 |
+
score += 5
|
| 216 |
+
title = data.get("cv_title", "")
|
| 217 |
+
if title and title.strip().lower() not in ("", "unknown"):
|
| 218 |
+
score += 5
|
| 219 |
+
return min(100, score)
|