Spaces:
Running
Running
update the models routing
Browse files
__pycache__/config.cpython-312.pyc
CHANGED
|
Binary files a/__pycache__/config.cpython-312.pyc and b/__pycache__/config.cpython-312.pyc differ
|
|
|
__pycache__/main.cpython-312.pyc
CHANGED
|
Binary files a/__pycache__/main.cpython-312.pyc and b/__pycache__/main.cpython-312.pyc differ
|
|
|
config.py
CHANGED
|
@@ -13,13 +13,12 @@ from dotenv import load_dotenv
|
|
| 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
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
env_path = Path(__file__).parent / ".env"
|
| 14 |
load_dotenv(dotenv_path=env_path)
|
| 15 |
|
| 16 |
+
# Google Gemini
|
|
|
|
|
|
|
| 17 |
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY", "")
|
|
|
|
|
|
|
| 18 |
GEMINI_MODEL = "gemini-3.5-flash-lite"
|
| 19 |
|
| 20 |
MAX_OUTPUT_TOKENS = 4096
|
| 21 |
TEMPERATURE = 0.1
|
| 22 |
+
|
| 23 |
+
# LLM failover chain: first provider is used; on failure logs once and tries next
|
| 24 |
+
LLM_PROVIDERS = ["google", "fallback"]
|
main.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
from __future__ import annotations
|
| 2 |
|
|
|
|
| 3 |
import tempfile
|
| 4 |
import uuid
|
| 5 |
import httpx
|
|
@@ -9,6 +10,8 @@ from typing import Any, List, Optional
|
|
| 9 |
|
| 10 |
from contextlib import asynccontextmanager
|
| 11 |
|
|
|
|
|
|
|
| 12 |
from fastapi import FastAPI, File, Form, UploadFile
|
| 13 |
from fastapi.exceptions import RequestValidationError
|
| 14 |
from fastapi.responses import JSONResponse
|
|
@@ -21,6 +24,8 @@ from services.cv_converter import CVConverter
|
|
| 21 |
from services.job_matcher import JobInput, JobMatcher
|
| 22 |
from services.workers import CvWorker, QueueManager, CvTask
|
| 23 |
from services.new.cv_processor_v2 import CvProcessorV2
|
|
|
|
|
|
|
| 24 |
|
| 25 |
# ---------------------------------------------------------------------------
|
| 26 |
# Configuration
|
|
@@ -417,44 +422,73 @@ def batch_match_cv(payload: BatchMatchCVRequest):
|
|
| 417 |
|
| 418 |
|
| 419 |
# ---------------------------------------------------------------------------
|
| 420 |
-
# POST /v2/ping-llm — quick diagnostic
|
| 421 |
# ---------------------------------------------------------------------------
|
| 422 |
|
| 423 |
|
| 424 |
class PingLLMRequest(BaseModel):
|
| 425 |
-
prompt: str = Field(default="Say hello in one short sentence.", description="Prompt to send to
|
| 426 |
|
| 427 |
|
| 428 |
@app.post("/v2/ping-llm", response_model=APIResponse)
|
| 429 |
def ping_llm(payload: PingLLMRequest = PingLLMRequest()):
|
| 430 |
-
"""
|
|
|
|
|
|
|
|
|
|
| 431 |
t_start = datetime.now(timezone.utc)
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 436 |
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
|
| 440 |
-
|
| 441 |
-
|
| 442 |
-
|
| 443 |
-
|
| 444 |
-
|
| 445 |
-
|
| 446 |
-
|
| 447 |
-
|
| 448 |
-
|
| 449 |
-
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
|
| 453 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 454 |
|
| 455 |
|
| 456 |
# ---------------------------------------------------------------------------
|
| 457 |
-
# POST /v2/process-cv — LLM-powered extraction
|
| 458 |
# ---------------------------------------------------------------------------
|
| 459 |
|
| 460 |
|
|
@@ -462,9 +496,10 @@ def ping_llm(payload: PingLLMRequest = PingLLMRequest()):
|
|
| 462 |
async def process_cv_v2(file: UploadFile = File(...)):
|
| 463 |
"""
|
| 464 |
V2 CV processing endpoint.
|
| 465 |
-
Converts the uploaded file to Markdown then extracts all fields using
|
| 466 |
-
|
| 467 |
-
and a sections[] array that preserves
|
|
|
|
| 468 |
"""
|
| 469 |
if not file.filename:
|
| 470 |
return APIResponse(message="No file uploaded.", statusCode=400)
|
|
@@ -508,7 +543,8 @@ async def process_cv_v2(file: UploadFile = File(...)):
|
|
| 508 |
statusCode=200,
|
| 509 |
payload={
|
| 510 |
**payload,
|
| 511 |
-
"
|
|
|
|
| 512 |
"processed_at": t_end.isoformat(),
|
| 513 |
},
|
| 514 |
)
|
|
|
|
| 1 |
from __future__ import annotations
|
| 2 |
|
| 3 |
+
import logging
|
| 4 |
import tempfile
|
| 5 |
import uuid
|
| 6 |
import httpx
|
|
|
|
| 10 |
|
| 11 |
from contextlib import asynccontextmanager
|
| 12 |
|
| 13 |
+
logger = logging.getLogger(__name__)
|
| 14 |
+
|
| 15 |
from fastapi import FastAPI, File, Form, UploadFile
|
| 16 |
from fastapi.exceptions import RequestValidationError
|
| 17 |
from fastapi.responses import JSONResponse
|
|
|
|
| 24 |
from services.job_matcher import JobInput, JobMatcher
|
| 25 |
from services.workers import CvWorker, QueueManager, CvTask
|
| 26 |
from services.new.cv_processor_v2 import CvProcessorV2
|
| 27 |
+
from services.new.google.gemini_extractor import GeminiCvExtractor
|
| 28 |
+
from config import GEMINI_MODEL, LLM_PROVIDERS
|
| 29 |
|
| 30 |
# ---------------------------------------------------------------------------
|
| 31 |
# Configuration
|
|
|
|
| 422 |
|
| 423 |
|
| 424 |
# ---------------------------------------------------------------------------
|
| 425 |
+
# POST /v2/ping-llm — quick diagnostic: ping LLM provider with failover chain
|
| 426 |
# ---------------------------------------------------------------------------
|
| 427 |
|
| 428 |
|
| 429 |
class PingLLMRequest(BaseModel):
|
| 430 |
+
prompt: str = Field(default="Say hello in one short sentence.", description="Prompt to send to the LLM")
|
| 431 |
|
| 432 |
|
| 433 |
@app.post("/v2/ping-llm", response_model=APIResponse)
|
| 434 |
def ping_llm(payload: PingLLMRequest = PingLLMRequest()):
|
| 435 |
+
"""
|
| 436 |
+
Diagnostic endpoint. Picks the first provider in LLM_PROVIDERS; if it fails
|
| 437 |
+
logs once and tries the next until all exhausted.
|
| 438 |
+
"""
|
| 439 |
t_start = datetime.now(timezone.utc)
|
| 440 |
+
last_error: Optional[Exception] = None
|
| 441 |
+
|
| 442 |
+
for idx, provider in enumerate(LLM_PROVIDERS):
|
| 443 |
+
p = provider.lower().strip()
|
| 444 |
+
try:
|
| 445 |
+
if p == "google":
|
| 446 |
+
extractor = GeminiCvExtractor()
|
| 447 |
+
res = extractor.ping(prompt=payload.prompt)
|
| 448 |
+
model_name = res.get("model", GEMINI_MODEL)
|
| 449 |
+
reply = res.get("reply", "")
|
| 450 |
+
usage = res.get("usageMetadata", {})
|
| 451 |
+
elif p == "fallback":
|
| 452 |
+
model_name = "regex-fallback"
|
| 453 |
+
reply = "Fallback parser (regex chunker) is operational"
|
| 454 |
+
usage = {}
|
| 455 |
+
else:
|
| 456 |
+
logger.warning("Unknown provider '%s' in LLM_PROVIDERS. Skipping...", p)
|
| 457 |
+
continue
|
| 458 |
+
|
| 459 |
+
t_end = datetime.now(timezone.utc)
|
| 460 |
+
duration_ms = int((t_end - t_start).total_seconds() * 1000)
|
| 461 |
|
| 462 |
+
return APIResponse(
|
| 463 |
+
message=f"{p} ping successful",
|
| 464 |
+
statusCode=200,
|
| 465 |
+
payload={
|
| 466 |
+
"provider": p,
|
| 467 |
+
"model": model_name,
|
| 468 |
+
"prompt": payload.prompt,
|
| 469 |
+
"reply": reply,
|
| 470 |
+
"duration_ms": duration_ms,
|
| 471 |
+
"duration_seconds": round(duration_ms / 1000, 2),
|
| 472 |
+
"usageMetadata": usage,
|
| 473 |
+
},
|
| 474 |
+
)
|
| 475 |
+
except Exception as exc:
|
| 476 |
+
next_provider = LLM_PROVIDERS[idx + 1] if idx + 1 < len(LLM_PROVIDERS) else None
|
| 477 |
+
logger.warning(
|
| 478 |
+
"Provider '%s' ping failed: %s. %s",
|
| 479 |
+
p, exc,
|
| 480 |
+
f"Trying next provider '{next_provider}'..." if next_provider else "No more providers.",
|
| 481 |
+
)
|
| 482 |
+
last_error = exc
|
| 483 |
+
|
| 484 |
+
return APIResponse(
|
| 485 |
+
message=f"All providers failed. Last error: {last_error}",
|
| 486 |
+
statusCode=500,
|
| 487 |
+
)
|
| 488 |
|
| 489 |
|
| 490 |
# ---------------------------------------------------------------------------
|
| 491 |
+
# POST /v2/process-cv — LLM-powered extraction with provider failover
|
| 492 |
# ---------------------------------------------------------------------------
|
| 493 |
|
| 494 |
|
|
|
|
| 496 |
async def process_cv_v2(file: UploadFile = File(...)):
|
| 497 |
"""
|
| 498 |
V2 CV processing endpoint.
|
| 499 |
+
Converts the uploaded file to Markdown then extracts all fields using the
|
| 500 |
+
configured LLM_PROVIDERS failover chain (google → fallback).
|
| 501 |
+
Returns markdown, structured fields, and a sections[] array that preserves
|
| 502 |
+
the original CV heading titles.
|
| 503 |
"""
|
| 504 |
if not file.filename:
|
| 505 |
return APIResponse(message="No file uploaded.", statusCode=400)
|
|
|
|
| 543 |
statusCode=200,
|
| 544 |
payload={
|
| 545 |
**payload,
|
| 546 |
+
"provider": payload.get("provider", "unknown"),
|
| 547 |
+
"timing": {"total_ms": total_ms},
|
| 548 |
"processed_at": t_end.isoformat(),
|
| 549 |
},
|
| 550 |
)
|
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/cv_processor_v2.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
"""Orchestrates V2 CV processing: file → markdown →
|
| 2 |
|
| 3 |
from __future__ import annotations
|
| 4 |
|
|
@@ -7,6 +7,7 @@ import tempfile
|
|
| 7 |
from pathlib import Path
|
| 8 |
from typing import Any, Dict
|
| 9 |
|
|
|
|
| 10 |
from services.cv_chunker import chunk_cv
|
| 11 |
from services.new.cv_converter_v2 import CvConverterV2
|
| 12 |
from services.new.google.gemini_extractor import GeminiCvExtractor
|
|
@@ -21,7 +22,7 @@ logger = logging.getLogger(__name__)
|
|
| 21 |
|
| 22 |
|
| 23 |
class CvProcessorV2:
|
| 24 |
-
"""Processes CV using
|
| 25 |
|
| 26 |
def __init__(self) -> None:
|
| 27 |
self.converter = CvConverterV2()
|
|
@@ -29,8 +30,9 @@ class CvProcessorV2:
|
|
| 29 |
|
| 30 |
def process(self, file_bytes: bytes, filename: str) -> Dict[str, Any]:
|
| 31 |
"""
|
| 32 |
-
Convert file → markdown, then extract fields
|
| 33 |
-
|
|
|
|
| 34 |
"""
|
| 35 |
suffix = Path(filename).suffix.lower() if filename else ".pdf"
|
| 36 |
tmp_path = None
|
|
@@ -45,73 +47,82 @@ class CvProcessorV2:
|
|
| 45 |
if not conversion.success:
|
| 46 |
raise ValueError(conversion.error or "Conversion failed")
|
| 47 |
|
| 48 |
-
# Step 2 — Attempt Gemini extraction (handles up to 3 internal retries)
|
| 49 |
-
method_used = "llm"
|
| 50 |
-
extracted = None
|
| 51 |
-
try:
|
| 52 |
-
extracted = self.gemini_extractor.extract(conversion.markdown)
|
| 53 |
-
except Exception as gemini_err:
|
| 54 |
-
logger.warning(
|
| 55 |
-
"Gemini extraction failed after 3 retries (%s). Falling back to older regex chunk_cv...",
|
| 56 |
-
gemini_err,
|
| 57 |
-
)
|
| 58 |
-
method_used = "fallback"
|
| 59 |
-
|
| 60 |
warnings = list(conversion.warnings)
|
| 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 |
finally:
|
| 117 |
if tmp_path is not None:
|
|
|
|
| 1 |
+
"""Orchestrates V2 CV processing: file → markdown → LLM extraction → payload."""
|
| 2 |
|
| 3 |
from __future__ import annotations
|
| 4 |
|
|
|
|
| 7 |
from pathlib import Path
|
| 8 |
from typing import Any, Dict
|
| 9 |
|
| 10 |
+
from config import LLM_PROVIDERS
|
| 11 |
from services.cv_chunker import chunk_cv
|
| 12 |
from services.new.cv_converter_v2 import CvConverterV2
|
| 13 |
from services.new.google.gemini_extractor import GeminiCvExtractor
|
|
|
|
| 22 |
|
| 23 |
|
| 24 |
class CvProcessorV2:
|
| 25 |
+
"""Processes CV using the configured LLM_PROVIDERS failover chain."""
|
| 26 |
|
| 27 |
def __init__(self) -> None:
|
| 28 |
self.converter = CvConverterV2()
|
|
|
|
| 30 |
|
| 31 |
def process(self, file_bytes: bytes, filename: str) -> Dict[str, Any]:
|
| 32 |
"""
|
| 33 |
+
Convert file → markdown, then extract fields using LLM_PROVIDERS in order.
|
| 34 |
+
If a provider fails, logs once and tries the next. Last entry is always fallback.
|
| 35 |
+
Returns payload including 'method_used': 'llm' | 'fallback' and 'provider'.
|
| 36 |
"""
|
| 37 |
suffix = Path(filename).suffix.lower() if filename else ".pdf"
|
| 38 |
tmp_path = None
|
|
|
|
| 47 |
if not conversion.success:
|
| 48 |
raise ValueError(conversion.error or "Conversion failed")
|
| 49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
warnings = list(conversion.warnings)
|
| 51 |
|
| 52 |
+
# Step 2 — provider failover chain
|
| 53 |
+
last_error = None
|
| 54 |
+
for idx, provider in enumerate(LLM_PROVIDERS):
|
| 55 |
+
p = provider.lower().strip()
|
| 56 |
+
|
| 57 |
+
# --- GOOGLE (Gemini) ---
|
| 58 |
+
if p == "google":
|
| 59 |
+
try:
|
| 60 |
+
extracted = self.gemini_extractor.extract(conversion.markdown)
|
| 61 |
+
skills_found = set(s.lower() for s in extracted.get("skills", {}).get("items", []))
|
| 62 |
+
if not skills_found:
|
| 63 |
+
skills_found = _extract_skills(conversion.markdown, DEFAULT_SKILL_ALIASES)
|
| 64 |
+
inferred_category = _infer_category(
|
| 65 |
+
text=conversion.markdown,
|
| 66 |
+
skills=skills_found,
|
| 67 |
+
category_skills=DEFAULT_CATEGORY_SKILLS,
|
| 68 |
+
)
|
| 69 |
+
return {
|
| 70 |
+
"method_used": "llm",
|
| 71 |
+
"provider": "google",
|
| 72 |
+
"markdown": extracted.get("markdown", conversion.markdown),
|
| 73 |
+
"cv_title": extracted.get("cv_title", ""),
|
| 74 |
+
"seniority": extracted.get("seniority", "entry"),
|
| 75 |
+
"years_experience": extracted.get("years_experience", 0),
|
| 76 |
+
"category": inferred_category,
|
| 77 |
+
"completeness_score": extracted.get("completeness_score", 0),
|
| 78 |
+
"summary": extracted.get("summary", ""),
|
| 79 |
+
"contact": extracted.get("contact", {}),
|
| 80 |
+
"links": extracted.get("links", []),
|
| 81 |
+
"skills": extracted.get("skills", {}),
|
| 82 |
+
"experience": extracted.get("experience", []),
|
| 83 |
+
"education": extracted.get("education", []),
|
| 84 |
+
"projects": extracted.get("projects", []),
|
| 85 |
+
"awards": extracted.get("awards", []),
|
| 86 |
+
"raw_sections": extracted.get("raw_sections", []),
|
| 87 |
+
"usage_metadata": extracted.get("usage_metadata", {}),
|
| 88 |
+
"file_type": conversion.file_type,
|
| 89 |
+
"is_scanned": conversion.is_scanned,
|
| 90 |
+
"page_count": conversion.page_count,
|
| 91 |
+
"warnings": warnings,
|
| 92 |
+
}
|
| 93 |
+
except Exception as exc:
|
| 94 |
+
next_provider = LLM_PROVIDERS[idx + 1] if idx + 1 < len(LLM_PROVIDERS) else None
|
| 95 |
+
logger.warning(
|
| 96 |
+
"Provider 'google' extraction failed: %s. %s",
|
| 97 |
+
exc,
|
| 98 |
+
f"Trying next provider '{next_provider}'..." if next_provider else "No more providers.",
|
| 99 |
+
)
|
| 100 |
+
last_error = exc
|
| 101 |
+
|
| 102 |
+
# --- FALLBACK (regex chunker) ---
|
| 103 |
+
elif p == "fallback":
|
| 104 |
+
warnings.append("LLM service unavailable. Parsed using regex fallback.")
|
| 105 |
+
chunks = chunk_cv(conversion.markdown)
|
| 106 |
+
return {
|
| 107 |
+
"method_used": "fallback",
|
| 108 |
+
"provider": "fallback",
|
| 109 |
+
"markdown": conversion.markdown,
|
| 110 |
+
"cv_title": chunks.get("cv_title", ""),
|
| 111 |
+
"seniority": chunks.get("seniority", "entry"),
|
| 112 |
+
"years_experience": chunks.get("years_experience", 0),
|
| 113 |
+
"category": chunks.get("category", "Other"),
|
| 114 |
+
"completeness_score": chunks.get("completeness_score", 0),
|
| 115 |
+
"chunks": chunks.get("chunks", {}),
|
| 116 |
+
"file_type": conversion.file_type,
|
| 117 |
+
"is_scanned": conversion.is_scanned,
|
| 118 |
+
"page_count": conversion.page_count,
|
| 119 |
+
"warnings": warnings,
|
| 120 |
+
}
|
| 121 |
+
|
| 122 |
+
else:
|
| 123 |
+
logger.warning("Unknown provider '%s' in LLM_PROVIDERS. Skipping...", p)
|
| 124 |
+
|
| 125 |
+
raise RuntimeError(f"All extraction providers failed. Last error: {last_error}")
|
| 126 |
|
| 127 |
finally:
|
| 128 |
if tmp_path is not None:
|
services/new/google/__pycache__/gemini_extractor.cpython-312.pyc
CHANGED
|
Binary files a/services/new/google/__pycache__/gemini_extractor.cpython-312.pyc and b/services/new/google/__pycache__/gemini_extractor.cpython-312.pyc differ
|
|
|