YaminiVJ commited on
Commit
8780fb9
·
verified ·
1 Parent(s): f5db9d5

Upload 10 files

Browse files
Files changed (10) hide show
  1. .dockerignore +64 -0
  2. .gitattributes +6 -0
  3. Dockerfile +43 -0
  4. app.py +12 -0
  5. app/config.py +38 -0
  6. app/embedding.py +1461 -0
  7. app/main.py +40 -0
  8. app/routes/review.py +175 -0
  9. app/supabase.py +106 -0
  10. requirements.txt +30 -0
.dockerignore ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Git
2
+ .git
3
+ .gitignore
4
+
5
+ # Python
6
+ __pycache__
7
+ *.pyc
8
+ *.pyo
9
+ *.pyd
10
+ .Python
11
+ env
12
+ pip-log.txt
13
+ pip-delete-this-directory.txt
14
+ .tox
15
+ .coverage
16
+ .coverage.*
17
+ .cache
18
+ nosetests.xml
19
+ coverage.xml
20
+ *.cover
21
+ *.log
22
+ .git
23
+ .mypy_cache
24
+ .pytest_cache
25
+ .hypothesis
26
+
27
+ # Virtual environments
28
+ venv/
29
+ env/
30
+ ENV/
31
+ env.bak/
32
+ venv.bak/
33
+
34
+ # IDE
35
+ .vscode/
36
+ .idea/
37
+ *.swp
38
+ *.swo
39
+ *~
40
+
41
+ # OS
42
+ .DS_Store
43
+ .DS_Store?
44
+ ._*
45
+ .Spotlight-V100
46
+ .Trashes
47
+ ehthumbs.db
48
+ Thumbs.db
49
+
50
+ # Project specific
51
+ uploads/
52
+ *.pdf
53
+ *.db
54
+ models/
55
+ *.bin
56
+ *.safetensors
57
+ secrets.json
58
+ credentials.json
59
+
60
+ # Documentation
61
+ README.md
62
+ *.md
63
+ HF-SPACES-CHECKLIST.md
64
+ env.example
.gitattributes ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ *.py linguist-language=Python
2
+ *.md linguist-language=Markdown
3
+ *.txt linguist-language=Text
4
+ *.json linguist-language=JSON
5
+ *.yml linguist-language=YAML
6
+ *.yaml linguist-language=YAML
Dockerfile ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Use Python 3.11 slim image for smaller size
2
+ FROM python:3.11-slim
3
+
4
+ # Set working directory
5
+ WORKDIR /app
6
+
7
+ # Set environment variables
8
+ ENV PYTHONDONTWRITEBYTECODE=1
9
+ ENV PYTHONUNBUFFERED=1
10
+ ENV PORT=8000
11
+
12
+ # Install system dependencies
13
+ RUN apt-get update && apt-get install -y \
14
+ gcc \
15
+ g++ \
16
+ curl \
17
+ && rm -rf /var/lib/apt/lists/*
18
+
19
+ # Copy requirements first for better caching
20
+ COPY requirements.txt .
21
+
22
+ # Install Python dependencies
23
+ RUN pip install --no-cache-dir -r requirements.txt
24
+
25
+ # Download spaCy model
26
+ RUN python -m spacy download en_core_web_sm
27
+
28
+ # Copy application code
29
+ COPY . .
30
+
31
+ # Create non-root user for security
32
+ RUN adduser --disabled-password --gecos '' appuser && chown -R appuser:appuser /app
33
+ USER appuser
34
+
35
+ # Expose port
36
+ EXPOSE 7860
37
+
38
+ # Health check (update to port 7860)
39
+ HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 \
40
+ CMD curl -f http://localhost:7860/health || exit 1
41
+
42
+ # Run the FastAPI application (not Gradio)
43
+ CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "7860"]
app.py ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Hugging Face Spaces entry point for AI Resume Reviewer
3
+ """
4
+ import os
5
+ import logging
6
+ from app.main import app
7
+
8
+ # Configure logging
9
+ logging.basicConfig(level=logging.INFO)
10
+ logger = logging.getLogger(__name__)
11
+
12
+ # (Remove all code in this file or delete the file if not needed for Gradio)
app/config.py ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Configuration settings for the application
3
+ """
4
+ from pydantic_settings import BaseSettings
5
+ from typing import Optional
6
+
7
+ class Settings(BaseSettings):
8
+ """Application settings"""
9
+
10
+ # Supabase configuration
11
+ SUPABASE_URL: Optional[str] = None
12
+ SUPABASE_KEY: Optional[str] = None
13
+
14
+ # API Keys for LLM Services
15
+ GROQ_API_KEY: Optional[str] = None
16
+ COHERE_API_KEY: Optional[str] = None
17
+ TOGETHER_API_KEY: Optional[str] = None
18
+ HUGGINGFACE_API_KEY: Optional[str] = None
19
+ OPENROUTER_API_KEY: Optional[str] = None
20
+
21
+ # LLM Model configuration
22
+ LLM_MODEL_NAME: str = "mistralai/Mistral-7B-Instruct-v0.1"
23
+ PARSER_MODEL_NAME: str = "llama3-8b-8192"
24
+ LLM_FEEDBACK_MODEL_NAME: str = "mistralai/mistral-7b-instruct:free"
25
+
26
+ # Embedding model configuration
27
+ EMBEDDING_MODEL_NAME: str = "all-MiniLM-L6-v2"
28
+
29
+ # API configuration
30
+ MAX_FILE_SIZE: int = 10 * 1024 * 1024 # 10MB
31
+ ALLOWED_FILE_TYPES: list = ["application/pdf"]
32
+
33
+ class Config:
34
+ env_file = ".env"
35
+ case_sensitive = True
36
+
37
+ # Global settings instance
38
+ settings = Settings()
app/embedding.py ADDED
@@ -0,0 +1,1461 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Comprehensive Resume and Job Description Matching System
3
+ Implements all phases: PDF extraction, LLM ensemble, semantic similarity, skills extraction, and multi-layer validation.
4
+ Enhanced with Final Similarity Score calculation.
5
+ """
6
+ import os
7
+ import re
8
+ import io
9
+ import pdfplumber
10
+ import fitz # PyMuPDF
11
+ import numpy as np
12
+ from typing import Optional, List, Dict, Any
13
+ from sentence_transformers import SentenceTransformer, util
14
+ from transformers import DistilBertTokenizer, DistilBertModel
15
+ import torch
16
+ import requests
17
+ import spacy
18
+ from fuzzywuzzy import fuzz
19
+ from fuzzywuzzy import process as fuzzy_process
20
+ from dotenv import load_dotenv
21
+ import time
22
+
23
+ # Load environment variables from .env file
24
+ load_dotenv()
25
+
26
+ # Load spaCy model for NER
27
+ try:
28
+ nlp = spacy.load("en_core_web_sm")
29
+ except Exception:
30
+ import subprocess
31
+ subprocess.run(["python", "-m", "spacy", "download", "en_core_web_sm"])
32
+ nlp = spacy.load("en_core_web_sm")
33
+
34
+ # ========== Phase 1: Enhanced PDF Processing ==========
35
+ class PDFExtractor:
36
+ """Extracts text from PDFs using pdfplumber and PyMuPDF as fallback. OCR removed."""
37
+ @staticmethod
38
+ def extract_text(pdf_bytes: bytes) -> str:
39
+ # Try pdfplumber first
40
+ try:
41
+ with pdfplumber.open(io.BytesIO(pdf_bytes)) as pdf:
42
+ text = "\n".join(page.extract_text() or '' for page in pdf.pages)
43
+ if text.strip():
44
+ return PDFExtractor.clean_text(text)
45
+ except Exception:
46
+ pass
47
+ # Fallback to PyMuPDF
48
+ try:
49
+ doc = fitz.open(stream=pdf_bytes, filetype="pdf")
50
+ text = "\n".join(page.get_text() for page in doc)
51
+ doc.close()
52
+ if text.strip():
53
+ return PDFExtractor.clean_text(text)
54
+ except Exception:
55
+ pass
56
+ # If both fail, return empty string
57
+ return ""
58
+
59
+ @staticmethod
60
+ def clean_text(text: str) -> str:
61
+ # Remove excessive whitespace, fix line breaks, basic formatting recovery
62
+ text = re.sub(r'\s+', ' ', text)
63
+ text = re.sub(r'\n+', '\n', text)
64
+ return text.strip()
65
+
66
+ # ========== Phase 2: Advanced LLM Integration ==========
67
+ class ImprovedLLMEnsemble:
68
+ """Smart LLM ensemble with fallback strategy instead of calling all APIs"""
69
+ def __init__(self, groq_api_key: Optional[str] = None, cohere_api_key: Optional[str] = None):
70
+ self.groq_api_key = groq_api_key
71
+ self.cohere_api_key = cohere_api_key
72
+ self.llm_endpoints = [
73
+ ("Groq (Llama3-70B)", self.query_groq),
74
+ ("Together (Mixtral)", self.query_huggingface),
75
+ ("Together (CodeLlama)", self.query_together),
76
+ ("Cohere", self.query_cohere)
77
+ ]
78
+ self.success_rates = {}
79
+ self.response_times = {}
80
+
81
+ def query_groq(self, prompt: str) -> Optional[str]:
82
+ print("[LLMEnsemble] Calling Groq API...")
83
+ if not self.groq_api_key:
84
+ print("[LLMEnsemble] Groq API key not provided. Skipping Groq API call.")
85
+ return None
86
+ url = "https://api.groq.com/openai/v1/chat/completions"
87
+ headers = {"Authorization": f"Bearer {self.groq_api_key}", "Content-Type": "application/json"}
88
+ data = {
89
+ "model": "llama3-70b-8192", # Best free Groq model
90
+ "messages": [{"role": "user", "content": prompt}],
91
+ "temperature": 0.1,
92
+ "max_tokens": 1000
93
+ }
94
+ try:
95
+ r = requests.post(url, headers=headers, json=data, timeout=30)
96
+ print(f"[LLMEnsemble] Groq API response status: {r.status_code}")
97
+ if r.status_code == 200:
98
+ print("[LLMEnsemble] Groq API returned a response.")
99
+ return r.json()["choices"][0]["message"]["content"]
100
+ except Exception as e:
101
+ print(f"[LLMEnsemble] Groq API call failed: {e}")
102
+ return None
103
+
104
+ def query_huggingface(self, prompt: str) -> Optional[str]:
105
+ # Now using Together AI API for Mixtral-8x7B-Instruct-v0.1
106
+ print("[LLMEnsemble] Calling Together AI API for Mixtral-8x7B-Instruct-v0.1...")
107
+ together_api_key = os.getenv("TOGETHER_API_KEY")
108
+ url = "https://api.together.xyz/v1/chat/completions"
109
+ headers = {"Content-Type": "application/json"}
110
+ if together_api_key:
111
+ headers["Authorization"] = f"Bearer {together_api_key}"
112
+ data = {
113
+ "model": "mistralai/Mixtral-8x7B-Instruct-v0.1",
114
+ "messages": [{"role": "user", "content": prompt}],
115
+ "temperature": 0.1,
116
+ "max_tokens": 1000
117
+ }
118
+ try:
119
+ r = requests.post(url, headers=headers, json=data, timeout=30)
120
+ print(f"[LLMEnsemble] Together AI (Mixtral) API response status: {r.status_code}")
121
+ if r.status_code == 200:
122
+ print("[LLMEnsemble] Together AI (Mixtral) API returned a response.")
123
+ return r.json()["choices"][0]["message"]["content"]
124
+ except Exception as e:
125
+ print(f"[LLMEnsemble] Together AI (Mixtral) API call failed: {e}")
126
+ return None
127
+
128
+ def query_together(self, prompt: str) -> Optional[str]:
129
+ print("[LLMEnsemble] Calling Together.ai API...")
130
+ url = "https://api.together.xyz/v1/chat/completions"
131
+ headers = {"Content-Type": "application/json"}
132
+ data = {
133
+ "model": "codellama/CodeLlama-34b-Instruct-hf",
134
+ "messages": [{"role": "user", "content": prompt}],
135
+ "temperature": 0.1,
136
+ "max_tokens": 1000
137
+ }
138
+ try:
139
+ r = requests.post(url, headers=headers, json=data, timeout=30)
140
+ print(f"[LLMEnsemble] Together.ai API response status: {r.status_code}")
141
+ if r.status_code == 200:
142
+ print("[LLMEnsemble] Together.ai API returned a response.")
143
+ return r.json()["choices"][0]["message"]["content"]
144
+ except Exception as e:
145
+ print(f"[LLMEnsemble] Together.ai API call failed: {e}")
146
+ return None
147
+
148
+ def query_cohere(self, prompt: str) -> Optional[str]:
149
+ print("[LLMEnsemble] Calling Cohere API...")
150
+ if not self.cohere_api_key:
151
+ print("[LLMEnsemble] Cohere API key not provided. Skipping Cohere API call.")
152
+ return None
153
+ url = "https://api.cohere.ai/v1/generate"
154
+ headers = {"Authorization": f"Bearer {self.cohere_api_key}", "Content-Type": "application/json"}
155
+ data = {"model": "command", "prompt": prompt, "max_tokens": 500}
156
+ try:
157
+ r = requests.post(url, headers=headers, json=data, timeout=30)
158
+ print(f"[LLMEnsemble] Cohere API response status: {r.status_code}")
159
+ if r.status_code == 200:
160
+ print("[LLMEnsemble] Cohere API returned a response.")
161
+ return r.json()["generations"][0]["text"]
162
+ except Exception as e:
163
+ print(f"[LLMEnsemble] Cohere API call failed: {e}")
164
+ return None
165
+
166
+ def advanced_prompt(self, resume_text: str, job_description: str) -> str:
167
+ # Chain-of-thought, few-shot, JSON schema
168
+ return f"""
169
+ Analyze the following resume and job description for compatibility. Provide a JSON with:
170
+ - compatibility_score (0-100)
171
+ - strengths (list)
172
+ - gaps (list)
173
+ - recommendations (list)
174
+
175
+ RESUME: {resume_text[:2000]}
176
+ JOB DESCRIPTION: {job_description[:2000]}
177
+ """
178
+
179
+ def get_smart_response(self, resume_text: str, job_description: str, strategy: str = "fallback") -> Dict[str, Any]:
180
+ prompt = self.advanced_prompt(resume_text, job_description)
181
+ if strategy == "fallback":
182
+ return self._fallback_strategy(prompt)
183
+ elif strategy == "ensemble":
184
+ return self._ensemble_strategy(prompt)
185
+ elif strategy == "best":
186
+ return self._best_api_strategy(prompt)
187
+ else:
188
+ raise ValueError(f"Unknown strategy: {strategy}")
189
+
190
+ def _fallback_strategy(self, prompt: str) -> Dict[str, Any]:
191
+ for api_name, api_func in self.llm_endpoints:
192
+ print(f"[LLM] Trying {api_name}...")
193
+ try:
194
+ start_time = time.time()
195
+ response = api_func(prompt)
196
+ response_time = time.time() - start_time
197
+ if response:
198
+ parsed_response = self._parse_and_validate_response(response)
199
+ if parsed_response:
200
+ self._update_success_rate(api_name, True, response_time)
201
+ print(f"[LLM] ✅ {api_name} succeeded in {response_time:.2f}s")
202
+ return {**parsed_response, "api_used": api_name, "response_time": response_time}
203
+ self._update_success_rate(api_name, False, response_time)
204
+ except Exception as e:
205
+ print(f"[LLM] ❌ {api_name} failed: {e}")
206
+ self._update_success_rate(api_name, False, 0)
207
+ continue
208
+ print("[LLM] ⚠️ All APIs failed, using default response")
209
+ return self._get_default_response()
210
+
211
+ def _ensemble_strategy(self, prompt: str, max_apis: int = 2) -> Dict[str, Any]:
212
+ responses = []
213
+ apis_called = 0
214
+ for api_name, api_func in self.llm_endpoints:
215
+ if apis_called >= max_apis:
216
+ break
217
+ try:
218
+ response = api_func(prompt)
219
+ if response:
220
+ parsed = self._parse_and_validate_response(response)
221
+ if parsed:
222
+ responses.append({**parsed, "api_name": api_name})
223
+ apis_called += 1
224
+ except Exception as e:
225
+ print(f"[LLM] {api_name} failed: {e}")
226
+ continue
227
+ if not responses:
228
+ return self._get_default_response()
229
+ return self._aggregate_responses(responses)
230
+
231
+ def _best_api_strategy(self, prompt: str) -> Dict[str, Any]:
232
+ if not self.success_rates:
233
+ return self._fallback_strategy(prompt)
234
+ best_api = max(self.success_rates.keys(), key=lambda x: self.success_rates[x]["success_rate"] - sum(self.response_times.get(x, [10]))/max(len(self.response_times.get(x, [1])),1))
235
+ api_func = None
236
+ for api_name, func in self.llm_endpoints:
237
+ if api_name == best_api:
238
+ api_func = func
239
+ break
240
+ if api_func:
241
+ try:
242
+ response = api_func(prompt)
243
+ if response:
244
+ parsed = self._parse_and_validate_response(response)
245
+ if parsed:
246
+ return {**parsed, "api_used": best_api}
247
+ except Exception:
248
+ pass
249
+ return self._fallback_strategy(prompt)
250
+
251
+ def _parse_and_validate_response(self, response: str) -> Optional[Dict[str, Any]]:
252
+ try:
253
+ import json
254
+ import re
255
+ json_match = re.search(r'\{.*\}', response, re.DOTALL)
256
+ if not json_match:
257
+ return None
258
+ json_str = json_match.group()
259
+ parsed = json.loads(json_str)
260
+ required_fields = ["compatibility_score", "strengths", "gaps", "recommendations"]
261
+ if not all(field in parsed for field in required_fields):
262
+ return None
263
+ score = parsed.get("compatibility_score", 0)
264
+ if not (0 <= score <= 100):
265
+ return None
266
+ return parsed
267
+ except Exception as e:
268
+ print(f"[LLM] JSON parsing failed: {e}")
269
+ return None
270
+
271
+ def _aggregate_responses(self, responses: List[Dict[str, Any]]) -> Dict[str, Any]:
272
+ if len(responses) == 1:
273
+ return responses[0]
274
+ scores = [r.get("compatibility_score", 0) for r in responses]
275
+ avg_score = sum(scores) / len(scores)
276
+ all_strengths = []
277
+ all_gaps = []
278
+ all_recommendations = []
279
+ for r in responses:
280
+ all_strengths.extend(r.get("strengths", []))
281
+ all_gaps.extend(r.get("gaps", []))
282
+ all_recommendations.extend(r.get("recommendations", []))
283
+ def dedupe_list(lst):
284
+ seen = set()
285
+ result = []
286
+ for item in lst:
287
+ if item not in seen:
288
+ seen.add(item)
289
+ result.append(item)
290
+ return result
291
+ return {
292
+ "compatibility_score": avg_score,
293
+ "strengths": dedupe_list(all_strengths),
294
+ "gaps": dedupe_list(all_gaps),
295
+ "recommendations": dedupe_list(all_recommendations),
296
+ "apis_used": [r.get("api_name", "unknown") for r in responses],
297
+ "ensemble_size": len(responses)
298
+ }
299
+
300
+ def _update_success_rate(self, api_name: str, success: bool, response_time: float):
301
+ if api_name not in self.success_rates:
302
+ self.success_rates[api_name] = {"successes": 0, "total": 0}
303
+ self.success_rates[api_name]["total"] += 1
304
+ if success:
305
+ self.success_rates[api_name]["successes"] += 1
306
+ total = self.success_rates[api_name]["total"]
307
+ successes = self.success_rates[api_name]["successes"]
308
+ self.success_rates[api_name]["success_rate"] = successes / total
309
+ if response_time > 0:
310
+ if api_name not in self.response_times:
311
+ self.response_times[api_name] = []
312
+ self.response_times[api_name].append(response_time)
313
+ self.response_times[api_name] = self.response_times[api_name][-10:]
314
+
315
+ def _get_default_response(self) -> Dict[str, Any]:
316
+ return {
317
+ "compatibility_score": 50,
318
+ "strengths": ["Unable to analyze - API unavailable"],
319
+ "gaps": ["Unable to analyze - API unavailable"],
320
+ "recommendations": ["Please try again later or check API keys"],
321
+ "api_used": "default",
322
+ "error": "All LLM APIs failed"
323
+ }
324
+
325
+ def get_api_stats(self) -> Dict[str, Any]:
326
+ stats = {}
327
+ for api_name in self.success_rates:
328
+ stats[api_name] = {
329
+ "success_rate": self.success_rates[api_name]["success_rate"],
330
+ "total_calls": self.success_rates[api_name]["total"],
331
+ "avg_response_time": sum(self.response_times.get(api_name, [0])) / len(self.response_times.get(api_name, [1]))
332
+ }
333
+ return stats
334
+
335
+ # ========== Phase 3: BERT-Based Semantic Enhancement ==========
336
+ class EnhancedBERTSemanticEngine:
337
+ """
338
+ Enhanced BERT engine with specialized models for resume/job matching
339
+ """
340
+ def __init__(self, resume_bert_model: Optional[str] = None, load_specialized_models: bool = True):
341
+ self.semantic_model = SentenceTransformer('all-MiniLM-L6-v2')
342
+ self.distilbert_tokenizer = DistilBertTokenizer.from_pretrained('distilbert-base-uncased')
343
+ self.distilbert_model = DistilBertModel.from_pretrained('distilbert-base-uncased')
344
+ self.resume_bert_model = None
345
+ self.resume_model_name = None
346
+ if resume_bert_model:
347
+ self.resume_bert_model = self._load_resume_model(resume_bert_model)
348
+ self.resume_model_name = resume_bert_model
349
+ if self.resume_bert_model is None and load_specialized_models:
350
+ self.resume_bert_model, self.resume_model_name = self._load_best_available_resume_model()
351
+ self.specialized_models = {}
352
+ if load_specialized_models:
353
+ self._load_specialized_models()
354
+
355
+ def _load_resume_model(self, model_name: str) -> Optional[SentenceTransformer]:
356
+ try:
357
+ print(f"[BERT] Loading resume-specific model: {model_name}")
358
+ model = SentenceTransformer(model_name)
359
+ print(f"[BERT] ✅ Successfully loaded: {model_name}")
360
+ return model
361
+ except Exception as e:
362
+ print(f"[BERT] ❌ Failed to load {model_name}: {e}")
363
+ return None
364
+
365
+ def _load_best_available_resume_model(self) -> tuple[Optional[SentenceTransformer], Optional[str]]:
366
+ candidate_models = [
367
+ "sentence-transformers/all-mpnet-base-v2",
368
+ "sentence-transformers/all-roberta-large-v1",
369
+ "nlpaueb/legal-bert-base-uncased",
370
+ "ProsusAI/finbert",
371
+ "sentence-transformers/multi-qa-mpnet-base-dot-v1",
372
+ "sentence-transformers/all-distilroberta-v1",
373
+ "sentence-transformers/paraphrase-mpnet-base-v2"
374
+ ]
375
+ for model_name in candidate_models:
376
+ model = self._load_resume_model(model_name)
377
+ if model is not None:
378
+ print(f"[BERT] 🎯 Using {model_name} as resume-specific model")
379
+ return model, model_name
380
+ print("[BERT] ⚠️ No specialized resume model could be loaded")
381
+ return None, None
382
+
383
+ def _load_specialized_models(self):
384
+ specialized_candidates = {
385
+ "business_model": [
386
+ "sentence-transformers/all-mpnet-base-v2",
387
+ "ProsusAI/finbert"
388
+ ],
389
+ "technical_model": [
390
+ "sentence-transformers/all-roberta-large-v1",
391
+ "microsoft/codebert-base"
392
+ ],
393
+ "quality_model": [
394
+ "sentence-transformers/paraphrase-mpnet-base-v2",
395
+ "sentence-transformers/multi-qa-mpnet-base-dot-v1"
396
+ ]
397
+ }
398
+ for category, models in specialized_candidates.items():
399
+ for model_name in models:
400
+ try:
401
+ model = SentenceTransformer(model_name)
402
+ self.specialized_models[category] = {
403
+ 'model': model,
404
+ 'name': model_name
405
+ }
406
+ print(f"[BERT] ✅ Loaded {category}: {model_name}")
407
+ break
408
+ except Exception as e:
409
+ print(f"[BERT] ❌ Failed to load {model_name}: {e}")
410
+ continue
411
+
412
+ def semantic_similarity(self, text1: str, text2: str) -> float:
413
+ emb1 = self.semantic_model.encode(text1, convert_to_tensor=True)
414
+ emb2 = self.semantic_model.encode(text2, convert_to_tensor=True)
415
+ score = float(util.pytorch_cos_sim(emb1, emb2).item())
416
+ return score
417
+
418
+ def resume_specific_similarity(self, text1: str, text2: str) -> Optional[float]:
419
+ if self.resume_bert_model:
420
+ try:
421
+ emb1 = self.resume_bert_model.encode(text1, convert_to_tensor=True)
422
+ emb2 = self.resume_bert_model.encode(text2, convert_to_tensor=True)
423
+ score = float(util.pytorch_cos_sim(emb1, emb2).item())
424
+ print(f"[BERT] Resume-specific similarity: {score:.4f} using {self.resume_model_name}")
425
+ return score
426
+ except Exception as e:
427
+ print(f"[BERT] Error in resume-specific similarity: {e}")
428
+ return None
429
+ print("[BERT] No resume-specific model available")
430
+ return None
431
+
432
+ def ensemble_resume_similarity(self, resume_text: str, job_description: str) -> Dict[str, Any]:
433
+ scores = {}
434
+ model_details = {}
435
+ try:
436
+ scores['semantic_base'] = self.semantic_similarity(resume_text, job_description)
437
+ model_details['semantic_base'] = 'all-MiniLM-L6-v2'
438
+ except Exception as e:
439
+ print(f"[BERT] Error with base semantic model: {e}")
440
+ resume_score = self.resume_specific_similarity(resume_text, job_description)
441
+ if resume_score is not None:
442
+ scores['resume_specific'] = resume_score
443
+ model_details['resume_specific'] = self.resume_model_name
444
+ for category, model_info in self.specialized_models.items():
445
+ try:
446
+ model = model_info['model']
447
+ emb1 = model.encode(resume_text, convert_to_tensor=True)
448
+ emb2 = model.encode(job_description, convert_to_tensor=True)
449
+ score = float(util.pytorch_cos_sim(emb1, emb2).item()) # Convert numpy.float to Python float
450
+ scores[category] = score
451
+ model_details[category] = model_info['name']
452
+ print(f"[BERT] {category} similarity: {score:.4f}")
453
+ except Exception as e:
454
+ print(f"[BERT] Error with {category} model: {e}")
455
+ continue
456
+
457
+ # Automatically select the best model based on highest similarity score
458
+ best_model = self._select_best_model(scores, model_details)
459
+
460
+ ensemble_score = self._calculate_ensemble_score(scores)
461
+ domain_analysis = self._analyze_domain_suitability(resume_text, job_description)
462
+ return {
463
+ 'individual_scores': scores,
464
+ 'model_details': model_details,
465
+ 'ensemble_score': ensemble_score,
466
+ 'domain_analysis': domain_analysis,
467
+ 'models_used': len(scores),
468
+ 'primary_resume_model': self.resume_model_name,
469
+ 'best_model': best_model,
470
+ 'confidence': self._calculate_confidence(scores)
471
+ }
472
+
473
+ def _select_best_model(self, scores: Dict[str, float], model_details: Dict[str, str]) -> Dict[str, Any]:
474
+ """Automatically select the best model based on highest similarity score"""
475
+ if not scores:
476
+ return {"model_name": "none", "score": 0.0, "category": "none"}
477
+
478
+ # Find the model with the highest score
479
+ best_category = max(scores.keys(), key=lambda k: scores[k])
480
+ best_score = scores[best_category]
481
+ best_model_name = model_details.get(best_category, best_category)
482
+
483
+ print(f"[BERT] 🎯 Best model selected: {best_category} ({best_model_name}) with score: {best_score:.4f}")
484
+
485
+ return {
486
+ "model_name": best_model_name,
487
+ "score": best_score,
488
+ "category": best_category,
489
+ "all_scores": scores,
490
+ "all_models": model_details
491
+ }
492
+
493
+ def _calculate_ensemble_score(self, scores: Dict[str, float]) -> float:
494
+ if not scores:
495
+ return 0.0
496
+ weights = {
497
+ 'semantic_base': 0.2,
498
+ 'resume_specific': 0.35,
499
+ 'business_model': 0.25,
500
+ 'technical_model': 0.15,
501
+ 'quality_model': 0.2
502
+ }
503
+ weighted_sum = 0.0
504
+ total_weight = 0.0
505
+ for score_type, score in scores.items():
506
+ weight = weights.get(score_type, 0.1)
507
+ weighted_sum += weight * score
508
+ total_weight += weight
509
+ return weighted_sum / total_weight if total_weight > 0 else np.mean(list(scores.values()))
510
+
511
+ def _analyze_domain_suitability(self, resume_text: str, job_description: str) -> Dict[str, Any]:
512
+ resume_lower = resume_text.lower()
513
+ job_lower = job_description.lower()
514
+ tech_keywords = [
515
+ 'python', 'java', 'javascript', 'programming', 'software', 'developer',
516
+ 'algorithm', 'database', 'api', 'framework', 'cloud', 'machine learning',
517
+ 'ai', 'data science', 'devops', 'kubernetes', 'docker'
518
+ ]
519
+ business_keywords = [
520
+ 'finance', 'accounting', 'business', 'management', 'strategy', 'marketing',
521
+ 'sales', 'consulting', 'operations', 'project management', 'leadership'
522
+ ]
523
+ legal_keywords = [
524
+ 'legal', 'compliance', 'regulation', 'policy', 'governance', 'audit',
525
+ 'risk management', 'contract', 'intellectual property'
526
+ ]
527
+ tech_score = sum(1 for keyword in tech_keywords if keyword in resume_lower or keyword in job_lower)
528
+ business_score = sum(1 for keyword in business_keywords if keyword in resume_lower or keyword in job_lower)
529
+ legal_score = sum(1 for keyword in legal_keywords if keyword in resume_lower or keyword in job_lower)
530
+ max_score = max(tech_score, business_score, legal_score)
531
+ if max_score == 0:
532
+ primary_domain = 'general'
533
+ elif tech_score == max_score:
534
+ primary_domain = 'technical'
535
+ elif business_score == max_score:
536
+ primary_domain = 'business'
537
+ else:
538
+ primary_domain = 'legal'
539
+ return {
540
+ 'primary_domain': primary_domain,
541
+ 'domain_scores': {
542
+ 'technical': tech_score,
543
+ 'business': business_score,
544
+ 'legal': legal_score
545
+ },
546
+ 'specialization_strength': float(max_score / (len(resume_text.split()) + len(job_description.split())) * 1000) # Convert to Python float
547
+ }
548
+
549
+ def _calculate_confidence(self, scores: Dict[str, float]) -> float:
550
+ if not scores:
551
+ return 0.0
552
+ model_confidence = min(len(scores) / 4.0, 1.0)
553
+ score_values = list(scores.values())
554
+ if len(score_values) > 1:
555
+ consistency = 1.0 - min(float(np.std(score_values)), 0.5) * 2 # Convert numpy.float to Python float
556
+ else:
557
+ consistency = 0.7
558
+ resume_model_bonus = 0.1 if 'resume_specific' in scores else 0.0
559
+ return min(1.0, (model_confidence * 0.4 + consistency * 0.5 + resume_model_bonus + 0.1))
560
+
561
+ def context_embedding(self, text: str) -> np.ndarray:
562
+ inputs = self.distilbert_tokenizer(text, return_tensors="pt", truncation=True, max_length=512)
563
+ with torch.no_grad():
564
+ outputs = self.distilbert_model(**inputs)
565
+ cls_embedding = outputs.last_hidden_state[:, 0, :].cpu().numpy()
566
+ return cls_embedding[0].tolist() # Convert numpy array to list
567
+
568
+ def skills_similarity(self, skills1: List[str], skills2: List[str]) -> float:
569
+ if not skills1 or not skills2:
570
+ return 0.0
571
+ model_to_use = self.resume_bert_model if self.resume_bert_model else self.semantic_model
572
+ emb1 = model_to_use.encode(skills1, convert_to_tensor=True)
573
+ emb2 = model_to_use.encode(skills2, convert_to_tensor=True)
574
+ sim_matrix = util.pytorch_cos_sim(emb1, emb2)
575
+ best_sim_1 = float(sim_matrix.max(dim=1).values.mean().item()) # Convert numpy.float to Python float
576
+ best_sim_2 = float(sim_matrix.max(dim=0).values.mean().item()) # Convert numpy.float to Python float
577
+ semantic_skill_sim = (best_sim_1 + best_sim_2) / 2
578
+ from fuzzywuzzy import fuzz
579
+ fuzzy_matches = 0
580
+ total_comparisons = 0
581
+ for skill1 in skills1:
582
+ for skill2 in skills2:
583
+ similarity_ratio = fuzz.ratio(skill1.lower(), skill2.lower()) / 100.0
584
+ fuzzy_matches += similarity_ratio
585
+ total_comparisons += 1
586
+ fuzzy_skill_sim = fuzzy_matches / total_comparisons if total_comparisons > 0 else 0.0
587
+ set1 = set(skill.lower() for skill in skills1)
588
+ set2 = set(skill.lower() for skill in skills2)
589
+ jaccard_sim = len(set1.intersection(set2)) / len(set1.union(set2)) if set1.union(set2) else 0.0
590
+ final_score = (0.5 * semantic_skill_sim + 0.3 * fuzzy_skill_sim + 0.2 * jaccard_sim)
591
+ skill_coverage_bonus = min(len(set1.intersection(set2)) / max(len(set1), len(set2), 1) * 0.1, 0.2)
592
+ return min(1.0, final_score + skill_coverage_bonus)
593
+
594
+ def get_model_info(self) -> Dict[str, Any]:
595
+ return {
596
+ 'primary_semantic_model': 'all-MiniLM-L6-v2',
597
+ 'resume_specific_model': self.resume_model_name,
598
+ 'specialized_models': {k: v['name'] for k, v in self.specialized_models.items()},
599
+ 'total_models_loaded': 1 + (1 if self.resume_bert_model else 0) + len(self.specialized_models),
600
+ 'resume_model_available': self.resume_bert_model is not None
601
+ }
602
+
603
+ # ========== Phase 4: Dynamic Skills System ==========
604
+ class SkillsExtractor:
605
+ """Extracts and matches skills using NER, fuzzy matching, and context classification."""
606
+ def __init__(self, skill_db: Optional[List[str]] = None):
607
+ # Optionally load a dynamic skills database
608
+ self.skill_db = skill_db or [
609
+ # Programming Languages
610
+ "python", "java", "javascript", "typescript", "c++", "c#", "php", "ruby", "go", "rust", "scala", "kotlin", "swift",
611
+ # Web Technologies
612
+ "react", "angular", "vue", "html", "css", "sass", "less", "bootstrap", "tailwind", "jquery",
613
+ # Backend Frameworks
614
+ "django", "flask", "fastapi", "spring", "express", "node.js", "laravel", "rails",
615
+ # Databases
616
+ "sql", "mysql", "postgresql", "mongodb", "redis", "elasticsearch", "sqlite", "oracle",
617
+ # Cloud & DevOps
618
+ "aws", "azure", "gcp", "docker", "kubernetes", "jenkins", "git", "terraform", "ansible",
619
+ # Data Science & ML
620
+ "machine learning", "deep learning", "ai", "data science", "nlp", "computer vision",
621
+ "pandas", "numpy", "scikit-learn", "tensorflow", "pytorch", "keras", "jupyter",
622
+ # Other Technologies
623
+ "api", "rest", "graphql", "microservices", "agile", "scrum", "ci/cd", "testing",
624
+ "linux", "bash", "powershell", "nginx", "apache", "redis", "rabbitmq"
625
+ ]
626
+
627
+ def extract_skills(self, text: str) -> List[str]:
628
+ doc = nlp(text)
629
+ # Extract entities labeled as ORG, PRODUCT, SKILL, etc.
630
+ skills = [ent.text for ent in doc.ents if ent.label_ in ("ORG", "PRODUCT", "SKILL", "WORK_OF_ART")]
631
+
632
+ # Enhanced fuzzy matching with better thresholds
633
+ matched_skills = set()
634
+ text_lower = text.lower()
635
+
636
+ for skill in self.skill_db:
637
+ skill_lower = skill.lower()
638
+ # Multiple matching strategies
639
+ if (skill_lower in text_lower or
640
+ fuzz.partial_ratio(skill_lower, text_lower) > 80 or
641
+ fuzz.token_sort_ratio(skill_lower, text_lower) > 85):
642
+ matched_skills.add(skill)
643
+
644
+ # Add NER skills if they are close to known skills
645
+ for s in skills:
646
+ if len(s) > 2: # Avoid very short matches
647
+ match, score = fuzzy_process.extractOne(s, self.skill_db)
648
+ if score > 75: # Lower threshold for better recall
649
+ matched_skills.add(match)
650
+
651
+ # Add common programming languages and technologies that might be missed
652
+ tech_patterns = [
653
+ r'\b(python|java|javascript|js|react|angular|vue|node|sql|mysql|postgresql|mongodb|aws|azure|gcp|docker|kubernetes|git|html|css|api|rest|graphql|machine learning|ml|ai|data science|pandas|numpy|scikit-learn|tensorflow|pytorch|django|flask|spring|express)\b'
654
+ ]
655
+
656
+ for pattern in tech_patterns:
657
+ matches = re.findall(pattern, text_lower)
658
+ for match in matches:
659
+ if match in [s.lower() for s in self.skill_db]:
660
+ matched_skills.add(next(s for s in self.skill_db if s.lower() == match))
661
+
662
+ return list(matched_skills)
663
+
664
+ def classify_skill_context(self, text: str, skill: str) -> str:
665
+ # Simple context classification: required, optional, mentioned
666
+ text = text.lower()
667
+ skill = skill.lower()
668
+ if f"required: {skill}" in text or f"must have {skill}" in text:
669
+ return "required"
670
+ elif f"preferred: {skill}" in text or f"nice to have {skill}" in text:
671
+ return "optional"
672
+ elif skill in text:
673
+ return "mentioned"
674
+ return "none"
675
+
676
+ # ========== CORRECTED Skills Extractor ==========
677
+ class ImprovedSkillsExtractor(SkillsExtractor):
678
+ """Enhanced skills extraction with better matching and AI/ML focus"""
679
+ def __init__(self, skill_db: Optional[List[str]] = None):
680
+ super().__init__(skill_db)
681
+
682
+ # Enhanced skill patterns from JavaScript code
683
+ self.skill_patterns = {
684
+ # AI/ML Core Skills
685
+ 'llm': ['llm', 'large language model', 'language model', 'gpt', 'chatgpt'],
686
+ 'langchain': ['langchain', 'lang chain', 'langchain framework'],
687
+ 'crewai': ['crewai', 'crew ai', 'crew-ai'],
688
+ 'autogen': ['autogen', 'auto gen', 'auto-gen'],
689
+ 'openai': ['openai', 'open ai', 'gpt-4', 'gpt4', 'chatgpt', 'gpt-3'],
690
+ 'claude': ['claude', 'anthropic', 'claude-3', 'claude3'],
691
+ 'mistral': ['mistral', 'mistral-7b', 'mistral-8x7b'],
692
+ 'nlp': ['nlp', 'natural language processing', 'text processing', 'text analysis'],
693
+ 'vector_search': ['vector search', 'faiss', 'pinecone', 'embeddings', 'similarity search', 'vector database'],
694
+ 'speech_to_text': ['speech to text', 'whisper', 'speech recognition', 'asr', 'audio processing'],
695
+ 'machine_learning': ['machine learning', 'ml', 'ai', 'artificial intelligence', 'predictive modeling'],
696
+ 'transformers': ['transformers', 'bert', 'attention', 'hugging face', 'huggingface', 'transformer models'],
697
+ 'pytorch': ['pytorch', 'torch', 'pytorch lightning'],
698
+ 'tensorflow': ['tensorflow', 'tf', 'keras'],
699
+ 'python': ['python', 'python3', 'python 3'],
700
+ 'deep_learning': ['deep learning', 'neural networks', 'cnn', 'rnn', 'lstm', 'transformer'],
701
+
702
+ # Technical Skills
703
+ 'api_integration': ['api', 'rest api', 'integration', 'web services', 'microservices'],
704
+ 'caching': ['caching', 'redis', 'memcached', 'cache'],
705
+ 'optimization': ['optimization', 'performance tuning', 'performance optimization'],
706
+ 'frontend': ['frontend', 'react', 'javascript', 'typescript', 'vue', 'angular'],
707
+ 'backend': ['backend', 'node.js', 'express', 'fastapi', 'flask', 'django', 'spring'],
708
+ 'databases': ['database', 'sql', 'mongodb', 'postgresql', 'mysql', 'redis'],
709
+ 'cloud': ['aws', 'azure', 'gcp', 'cloud', 'amazon web services', 'google cloud'],
710
+ 'docker': ['docker', 'containerization', 'containers'],
711
+ 'git': ['git', 'version control', 'github', 'gitlab'],
712
+
713
+ # Soft Skills
714
+ 'collaboration': ['collaborate', 'team work', 'cross-functional', 'teamwork'],
715
+ 'research': ['research', 'prototyping', 'experimentation', 'r&d'],
716
+ 'problem_solving': ['problem solving', 'analytical', 'debugging', 'troubleshooting']
717
+ }
718
+
719
+ self.skill_relations = {
720
+ 'llm': ['machine_learning', 'nlp', 'transformers', 'openai', 'claude'],
721
+ 'langchain': ['llm', 'python', 'api_integration'],
722
+ 'vector_search': ['machine_learning', 'python', 'databases'],
723
+ 'nlp': ['machine_learning', 'python', 'transformers'],
724
+ 'machine_learning': ['python', 'pytorch', 'tensorflow', 'deep_learning']
725
+ }
726
+
727
+ # Skill importance levels
728
+ self.high_importance = ['llm', 'langchain', 'crewai', 'nlp', 'python', 'machine_learning']
729
+ self.medium_importance = ['vector_search', 'openai', 'claude', 'transformers', 'pytorch']
730
+
731
+ def extract_skills(self, text: str) -> List[str]:
732
+ """Enhanced skill extraction with pattern matching"""
733
+ text_lower = text.lower()
734
+ found_skills = set()
735
+
736
+ # Use parent class method for basic extraction
737
+ basic_skills = super().extract_skills(text)
738
+ found_skills.update(basic_skills)
739
+
740
+ # Enhanced pattern matching
741
+ for skill, patterns in self.skill_patterns.items():
742
+ for pattern in patterns:
743
+ if pattern in text_lower:
744
+ found_skills.add(skill)
745
+ break
746
+
747
+ # Add variations and related terms
748
+ for skill in list(found_skills):
749
+ if skill in self.skill_relations:
750
+ # Add related skills that might be mentioned
751
+ for related_skill in self.skill_relations[skill]:
752
+ if any(pattern in text_lower for pattern in self.skill_patterns.get(related_skill, [])):
753
+ found_skills.add(related_skill)
754
+
755
+ return list(found_skills)
756
+
757
+ def get_skill_importance(self, skill: str) -> str:
758
+ """Determine skill importance level"""
759
+ if skill in self.high_importance:
760
+ return 'high'
761
+ elif skill in self.medium_importance:
762
+ return 'medium'
763
+ return 'low'
764
+
765
+ def calculate_skills_match(self, resume_skills: List[str], job_skills: List[str]) -> float:
766
+ """Calculate skills match score with importance weighting"""
767
+ if not job_skills:
768
+ return 0.0
769
+
770
+ total_score = 0.0
771
+ weight_sum = 0.0
772
+
773
+ for job_skill in job_skills:
774
+ importance = self.get_skill_importance(job_skill)
775
+ weight = 3 if importance == 'high' else 2 if importance == 'medium' else 1
776
+
777
+ if job_skill in resume_skills:
778
+ total_score += weight * 1.0 # Perfect match
779
+ else:
780
+ # Check for related skills
781
+ related_score = self.get_related_skill_score(job_skill, resume_skills)
782
+ total_score += weight * related_score
783
+
784
+ weight_sum += weight
785
+
786
+ return total_score / weight_sum if weight_sum > 0 else 0.0
787
+
788
+ def get_related_skill_score(self, target_skill: str, available_skills: List[str]) -> float:
789
+ """Get score for related skills when exact match not found"""
790
+ related_skills = self.skill_relations.get(target_skill, [])
791
+ match_count = sum(1 for skill in related_skills if skill in available_skills)
792
+
793
+ return min(0.7, match_count * 0.3) if match_count > 0 else 0.0
794
+
795
+ def generate_skills_diagnostics(self, resume_skills: List[str], job_skills: List[str]) -> Dict[str, Any]:
796
+ """Generate diagnostic information about skills matching"""
797
+ resume_set = set(resume_skills)
798
+ job_set = set(job_skills)
799
+
800
+ # Calculate direct matches and missing skills
801
+ direct_matches = resume_set.intersection(job_set)
802
+ missing_skills = job_set - resume_set
803
+
804
+ # Find missing critical skills
805
+ critical_missing = [
806
+ skill for skill in job_skills
807
+ if self.get_skill_importance(skill) == 'high' and skill not in resume_set
808
+ ]
809
+
810
+ # Find related skills that could compensate
811
+ related_compensations = {}
812
+ for missing_skill in critical_missing:
813
+ related = self.skill_relations.get(missing_skill, [])
814
+ available_related = [skill for skill in related if skill in resume_set]
815
+ if available_related:
816
+ related_compensations[missing_skill] = available_related
817
+
818
+ return {
819
+ 'skills_gap': len(job_set) - len(direct_matches),
820
+ 'critical_skills_missing': critical_missing,
821
+ 'related_compensations': related_compensations,
822
+ 'coverage_percentage': len(direct_matches) / len(job_set) if job_set else 0,
823
+ 'resume_skills_count': len(resume_skills),
824
+ 'job_skills_count': len(job_skills),
825
+ # Add fields that frontend expects
826
+ 'direct_matches': list(direct_matches),
827
+ 'direct_match_count': len(direct_matches),
828
+ 'total_job_skills': len(job_set),
829
+ 'missing_skills': list(missing_skills)
830
+ }
831
+
832
+ def generate_skills_recommendations(self, diagnostics: Dict[str, Any]) -> List[Dict[str, str]]:
833
+ """Generate recommendations based on skills diagnostics"""
834
+ recommendations = []
835
+
836
+ if diagnostics['critical_skills_missing']:
837
+ missing_skills = diagnostics['critical_skills_missing']
838
+ recommendations.append({
839
+ 'type': 'critical',
840
+ 'title': 'Critical Skills Gap',
841
+ 'description': f"Missing key skills: {', '.join(missing_skills)}. Consider highlighting related experience or pursuing certifications."
842
+ })
843
+
844
+ if diagnostics['coverage_percentage'] < 0.5:
845
+ recommendations.append({
846
+ 'type': 'coverage',
847
+ 'title': 'Low Skills Coverage',
848
+ 'description': f"Only {diagnostics['coverage_percentage']*100:.1f}% of required skills found. Focus on acquiring missing core competencies."
849
+ })
850
+
851
+ if diagnostics['related_compensations']:
852
+ comp_skills = list(diagnostics['related_compensations'].keys())
853
+ recommendations.append({
854
+ 'type': 'compensation',
855
+ 'title': 'Related Skills Available',
856
+ 'description': f"While missing {', '.join(comp_skills)}, you have related skills that could demonstrate transferable knowledge."
857
+ })
858
+
859
+ return recommendations
860
+
861
+ # ========== CORRECTED Multi-Layer Validation ==========
862
+ class CorrectedMultiLayerValidator:
863
+ """Fixed scoring system with proper weighting and skill matching"""
864
+ def __init__(self):
865
+ # Adjusted weights - LLM gets higher weight since it's most accurate
866
+ self.similarity_weights_with_resume_bert = {
867
+ "semantic_score": 0.20, # Reduced - often too conservative
868
+ "skills_score": 0.30, # Increased - critical for tech roles
869
+ "llm_score": 0.40, # Increased - most comprehensive
870
+ "resume_bert_score": 0.10 # Reduced - good but not always reliable
871
+ }
872
+ self.similarity_weights_without_resume_bert = {
873
+ "semantic_score": 0.25, # Slightly higher when no resume BERT
874
+ "skills_score": 0.35, # Higher importance
875
+ "llm_score": 0.40, # Primary scorer
876
+ "resume_bert_score": 0.0
877
+ }
878
+ # Confidence calculation weights
879
+ self.confidence_weights = {
880
+ "semantic_score": 0.25,
881
+ "skills_score": 0.35,
882
+ "llm_score": 0.30,
883
+ "resume_bert_score": 0.10
884
+ }
885
+ def calculate_final_similarity(self, scores: Dict[str, float]) -> Dict[str, Any]:
886
+ """Enhanced final similarity calculation with score validation"""
887
+ # Validate and potentially adjust individual scores
888
+ adjusted_scores = self._validate_and_adjust_scores(scores)
889
+
890
+ # Check if LLM score is high and adjust weights accordingly
891
+ llm_score = adjusted_scores.get("llm_score", 0)
892
+ if llm_score > 1.0: # Convert to 0-1 range for comparison
893
+ llm_score = llm_score / 100.0
894
+
895
+ # Choose weights based on resume BERT availability and LLM score
896
+ has_resume_bert = adjusted_scores.get("resume_bert_score") is not None
897
+
898
+ # If LLM score is very high (>= 80%), give it more weight
899
+ if llm_score >= 0.8:
900
+ print(f"[VALIDATOR] High LLM score ({llm_score:.1%}) detected, applying LLM-dominant weighting")
901
+ if has_resume_bert:
902
+ weights = {
903
+ "semantic_score": 0.15, # Reduced
904
+ "skills_score": 0.25, # Reduced
905
+ "llm_score": 0.50, # Increased significantly
906
+ "resume_bert_score": 0.10 # Same
907
+ }
908
+ else:
909
+ weights = {
910
+ "semantic_score": 0.15, # Reduced
911
+ "skills_score": 0.25, # Reduced
912
+ "llm_score": 0.60, # Increased significantly
913
+ "resume_bert_score": 0.0
914
+ }
915
+ else:
916
+ # Use normal weights
917
+ weights = (self.similarity_weights_with_resume_bert if has_resume_bert
918
+ else self.similarity_weights_without_resume_bert)
919
+
920
+ total_score = 0.0
921
+ total_weight = 0.0
922
+ used_components = []
923
+ component_contributions = {}
924
+ for score_type, weight in weights.items():
925
+ if adjusted_scores.get(score_type) is not None and weight > 0:
926
+ score_value = adjusted_scores[score_type]
927
+ # Normalize LLM score to 0-1 range if it's in 0-100 range
928
+ if score_type == "llm_score" and score_value > 1.0:
929
+ score_value = score_value / 100.0
930
+ contribution = weight * score_value
931
+ total_score += contribution
932
+ total_weight += weight
933
+ used_components.append(score_type)
934
+ component_contributions[score_type] = {
935
+ 'score': score_value,
936
+ 'weight': weight,
937
+ 'contribution': contribution
938
+ }
939
+ # Normalize by actual weights used
940
+ if total_weight > 0:
941
+ final_score = total_score / total_weight
942
+ else:
943
+ final_score = 0.0
944
+ # Ensure score is in valid range
945
+ final_score = max(0.0, min(1.0, final_score))
946
+ return {
947
+ "score": final_score,
948
+ "percentage": final_score * 100,
949
+ "weights_used": weights,
950
+ "components_used": used_components,
951
+ "component_contributions": component_contributions,
952
+ "original_scores": scores,
953
+ "adjusted_scores": adjusted_scores,
954
+ "has_resume_bert": has_resume_bert,
955
+ "total_weight_used": total_weight,
956
+ "llm_dominant": llm_score >= 0.8
957
+ }
958
+ def _validate_and_adjust_scores(self, scores: Dict[str, float]) -> Dict[str, float]:
959
+ """Validate scores and apply corrections for known issues"""
960
+ adjusted = scores.copy()
961
+
962
+ # LLM score validation and adjustment
963
+ llm_score = scores.get("llm_score", 0)
964
+ if llm_score > 1.0: # Convert to 0-1 range for comparison
965
+ llm_score = llm_score / 100.0
966
+
967
+ # If LLM score is very high (>= 80%), it should dominate the final score
968
+ if llm_score >= 0.8:
969
+ print(f"[VALIDATOR] High LLM score detected ({llm_score:.1%}), applying dominance adjustment")
970
+ # Boost other scores to align with LLM assessment
971
+ if scores.get("skills_score") is not None:
972
+ skills_score = scores["skills_score"]
973
+ if llm_score - skills_score > 0.2: # 20% difference threshold
974
+ adjustment_factor = min(0.25, (llm_score - skills_score) * 0.6)
975
+ adjusted["skills_score"] = min(1.0, skills_score + adjustment_factor)
976
+ print(f"[VALIDATOR] Adjusted skills score from {skills_score:.3f} to {adjusted['skills_score']:.3f}")
977
+
978
+ if scores.get("semantic_score") is not None:
979
+ semantic_score = scores["semantic_score"]
980
+ if llm_score - semantic_score > 0.25: # 25% difference threshold
981
+ adjustment = min(0.2, (llm_score - semantic_score) * 0.5)
982
+ adjusted["semantic_score"] = min(1.0, semantic_score + adjustment)
983
+ print(f"[VALIDATOR] Adjusted semantic score from {semantic_score:.3f} to {adjusted['semantic_score']:.3f}")
984
+
985
+ # Regular validation for non-high LLM scores
986
+ else:
987
+ # Skills score validation and adjustment
988
+ if scores.get("skills_score") is not None:
989
+ skills_score = scores["skills_score"]
990
+ # If skills score seems too low compared to LLM assessment, adjust upward
991
+ if llm_score - skills_score > 0.15: # 15% difference threshold
992
+ adjustment_factor = min(0.15, (llm_score - skills_score) * 0.5)
993
+ adjusted["skills_score"] = min(1.0, skills_score + adjustment_factor)
994
+ print(f"[VALIDATOR] Adjusted skills score from {skills_score:.3f} to {adjusted['skills_score']:.3f}")
995
+
996
+ # Semantic score validation
997
+ if scores.get("semantic_score") is not None:
998
+ semantic_score = scores["semantic_score"]
999
+ # If semantic score is very low but other scores are high, apply correction
1000
+ other_scores = [s for k, s in scores.items()
1001
+ if k != "semantic_score" and s is not None]
1002
+ if other_scores:
1003
+ avg_other = np.mean([s if s <= 1.0 else s/100.0 for s in other_scores])
1004
+ if avg_other - semantic_score > 0.2: # 20% difference
1005
+ adjustment = min(0.1, (avg_other - semantic_score) * 0.3)
1006
+ adjusted["semantic_score"] = min(1.0, semantic_score + adjustment)
1007
+ print(f"[VALIDATOR] Adjusted semantic score from {semantic_score:.3f} to {adjusted['semantic_score']:.3f}")
1008
+
1009
+ return adjusted
1010
+ def get_similarity_category(self, score: float) -> str:
1011
+ """Updated categorization to be more realistic"""
1012
+ if score >= 0.85:
1013
+ return "Excellent Match (85-100%)"
1014
+ elif score >= 0.70:
1015
+ return "Good Match (70-84%)"
1016
+ elif score >= 0.55:
1017
+ return "Fair Match (55-69%)"
1018
+ elif score >= 0.40:
1019
+ return "Poor Match (40-54%)"
1020
+ else:
1021
+ return "Very Poor Match (<40%)"
1022
+ def enhanced_skills_analysis(self, resume_skills: List[str], job_skills: List[str]) -> Dict[str, Any]:
1023
+ """Detailed skills analysis to debug scoring issues"""
1024
+ resume_lower = [s.lower().strip() for s in resume_skills]
1025
+ job_lower = [s.lower().strip() for s in job_skills]
1026
+ # Direct matches
1027
+ direct_matches = set(resume_lower).intersection(set(job_lower))
1028
+ # Fuzzy matches
1029
+ fuzzy_matches = []
1030
+ for job_skill in job_lower:
1031
+ if job_skill not in direct_matches:
1032
+ for resume_skill in resume_lower:
1033
+ if resume_skill not in direct_matches:
1034
+ similarity = fuzz.ratio(job_skill, resume_skill)
1035
+ if similarity >= 80: # High similarity threshold
1036
+ fuzzy_matches.append((job_skill, resume_skill, similarity))
1037
+ # Calculate various metrics
1038
+ total_job_skills = len(job_lower)
1039
+ direct_match_count = len(direct_matches)
1040
+ fuzzy_match_count = len(fuzzy_matches)
1041
+ # Coverage metrics
1042
+ direct_coverage = direct_match_count / total_job_skills if total_job_skills > 0 else 0
1043
+ total_coverage = (direct_match_count + fuzzy_match_count) / total_job_skills if total_job_skills > 0 else 0
1044
+ # Missing critical skills
1045
+ missing_skills = set(job_lower) - direct_matches
1046
+ for fuzzy_job, _, _ in fuzzy_matches:
1047
+ missing_skills.discard(fuzzy_job)
1048
+ return {
1049
+ "direct_matches": list(direct_matches),
1050
+ "direct_match_count": direct_match_count,
1051
+ "fuzzy_matches": fuzzy_matches,
1052
+ "fuzzy_match_count": fuzzy_match_count,
1053
+ "total_job_skills": total_job_skills,
1054
+ "direct_coverage": direct_coverage,
1055
+ "total_coverage": total_coverage,
1056
+ "missing_skills": list(missing_skills),
1057
+ "resume_skills_count": len(resume_lower),
1058
+ "skill_density": len(resume_lower) / total_job_skills if total_job_skills > 0 else 0
1059
+ }
1060
+ def confidence_score(self, scores: Dict[str, float]) -> float:
1061
+ # Weighted average for confidence calculation
1062
+ total = 0.0
1063
+ total_weight = 0.0
1064
+ for k, v in scores.items():
1065
+ if v is not None and k in self.confidence_weights:
1066
+ weight = self.confidence_weights[k]
1067
+ score_value = v if v <= 1.0 else v / 100.0 # Normalize if needed
1068
+ total += weight * score_value
1069
+ total_weight += weight
1070
+ return total / total_weight if total_weight > 0 else 0.0
1071
+
1072
+ def detect_anomaly(self, scores: Dict[str, float]) -> bool:
1073
+ # Flag if scores are inconsistent
1074
+ vals = []
1075
+ for k, v in scores.items():
1076
+ if v is not None:
1077
+ # Normalize scores to 0-1 range for comparison
1078
+ normalized_v = v if v <= 1.0 else v / 100.0
1079
+ vals.append(normalized_v)
1080
+ if len(vals) < 2:
1081
+ return False
1082
+ std = float(np.std(vals)) # Convert numpy.float to Python float
1083
+ return bool(std > 0.3) # Convert numpy.bool to Python bool
1084
+
1085
+ # ========== UPDATED Main Matcher Class ==========
1086
+ class CorrectedResumeJobMatcher(EnhancedBERTSemanticEngine): # Inherit from EnhancedBERTSemanticEngine
1087
+ """Enhanced matcher with corrected scoring"""
1088
+ def __init__(self, groq_api_key: Optional[str] = None, cohere_api_key: Optional[str] = None,
1089
+ resume_bert_model: Optional[str] = None, skill_db: Optional[List[str]] = None,
1090
+ load_specialized_models: bool = True):
1091
+ self.pdf_extractor = PDFExtractor()
1092
+ self.llm_ensemble = ImprovedLLMEnsemble(groq_api_key=groq_api_key, cohere_api_key=cohere_api_key)
1093
+ self.bert_engine = EnhancedBERTSemanticEngine(
1094
+ resume_bert_model=resume_bert_model,
1095
+ load_specialized_models=load_specialized_models
1096
+ )
1097
+ self.skills_extractor = ImprovedSkillsExtractor(skill_db=skill_db)
1098
+ self.validator = CorrectedMultiLayerValidator()
1099
+
1100
+ def match(self, resume_pdf_bytes: bytes, job_description: str) -> Dict[str, Any]:
1101
+ resume_text = self.pdf_extractor.extract_text(resume_pdf_bytes)
1102
+ resume_skills = self.skills_extractor.extract_skills(resume_text)
1103
+ job_skills = self.skills_extractor.extract_skills(job_description)
1104
+
1105
+ # Enhanced skills analysis with diagnostics
1106
+ skills_diagnostics = self.skills_extractor.generate_skills_diagnostics(resume_skills, job_skills)
1107
+ skills_recommendations = self.skills_extractor.generate_skills_recommendations(skills_diagnostics)
1108
+
1109
+ # Calculate scores
1110
+ ensemble_result = self.bert_engine.ensemble_resume_similarity(resume_text, job_description)
1111
+ semantic_score = self.bert_engine.semantic_similarity(resume_text, job_description)
1112
+ skills_score = self.bert_engine.skills_similarity(resume_skills, job_skills)
1113
+ resume_bert_score = self.bert_engine.resume_specific_similarity(resume_text, job_description)
1114
+ llm_result = self.llm_ensemble.get_smart_response(resume_text, job_description)
1115
+ llm_score = llm_result.get("compatibility_score", 50)
1116
+
1117
+ # Enhanced skills matching with importance weighting
1118
+ enhanced_skills_score = self.skills_extractor.calculate_skills_match(resume_skills, job_skills)
1119
+
1120
+ scores = {
1121
+ "semantic_score": semantic_score,
1122
+ "skills_score": skills_score,
1123
+ "enhanced_skills_score": enhanced_skills_score,
1124
+ "llm_score": llm_score,
1125
+ "resume_bert_score": resume_bert_score
1126
+ }
1127
+
1128
+ final_similarity_result = self.validator.calculate_final_similarity(scores)
1129
+ final_score = final_similarity_result["score"]
1130
+ similarity_category = self.validator.get_similarity_category(final_score)
1131
+ confidence = self.validator.confidence_score(scores)
1132
+ anomaly = self.validator.detect_anomaly(scores)
1133
+
1134
+ # Generate comprehensive diagnostics
1135
+ diagnostics = {
1136
+ "skills_diagnostics": skills_diagnostics,
1137
+ "skills_recommendations": skills_recommendations,
1138
+ "semantic_weakness": semantic_score < 0.3,
1139
+ "llm_discrepancy": abs((llm_score / 100) - final_score) > 0.15,
1140
+ "score_consistency": self._check_score_consistency(scores),
1141
+ "critical_gaps": skills_diagnostics.get("critical_skills_missing", []),
1142
+ "coverage_analysis": {
1143
+ "skills_coverage": skills_diagnostics.get("coverage_percentage", 0),
1144
+ "semantic_alignment": semantic_score,
1145
+ "llm_assessment": llm_score / 100
1146
+ }
1147
+ }
1148
+
1149
+ return {
1150
+ "final_similarity_score": final_score,
1151
+ "final_similarity_percentage": final_score * 100,
1152
+ "similarity_category": similarity_category,
1153
+ "final_similarity_details": final_similarity_result,
1154
+ "skills_analysis": skills_diagnostics,
1155
+ "skills_recommendations": skills_recommendations,
1156
+ "ensemble_analysis": ensemble_result,
1157
+ "model_info": self.bert_engine.get_model_info(),
1158
+ "resume_text": resume_text,
1159
+ "resume_skills": resume_skills,
1160
+ "job_skills": job_skills,
1161
+ "semantic_score": semantic_score,
1162
+ "skills_score": skills_score,
1163
+ "enhanced_skills_score": enhanced_skills_score,
1164
+ "resume_bert_score": resume_bert_score,
1165
+ "llm_score": llm_score,
1166
+ "llm_details": llm_result,
1167
+ "confidence": confidence,
1168
+ "anomaly": anomaly,
1169
+ "component_scores": scores,
1170
+ "diagnostics": diagnostics,
1171
+ "debug_info": {
1172
+ "expected_score_range": "85-90%",
1173
+ "score_adjustments_made": final_similarity_result.get("adjusted_scores", {}),
1174
+ "primary_scoring_component": "llm_score" if llm_score > 80 else "enhanced_skills_score",
1175
+ "skills_importance_analysis": self._analyze_skills_importance(resume_skills, job_skills)
1176
+ }
1177
+ }
1178
+
1179
+ def _check_score_consistency(self, scores: Dict[str, float]) -> Dict[str, Any]:
1180
+ """Check if component scores are consistent"""
1181
+ score_values = [v for v in scores.values() if v is not None]
1182
+ if len(score_values) < 2:
1183
+ return {"consistent": True, "std": 0.0}
1184
+
1185
+ std = float(np.std(score_values)) # Convert numpy.float to Python float
1186
+ mean = float(np.mean(score_values)) # Convert numpy.float to Python float
1187
+ cv = float(std / mean if mean > 0 else 0) # Convert numpy.float to Python float
1188
+
1189
+ return {
1190
+ "consistent": bool(cv < 0.3), # Convert numpy.bool to Python bool
1191
+ "std": std,
1192
+ "mean": mean,
1193
+ "coefficient_of_variation": cv
1194
+ }
1195
+
1196
+ def _analyze_skills_importance(self, resume_skills: List[str], job_skills: List[str]) -> Dict[str, Any]:
1197
+ """Analyze skills by importance level"""
1198
+ resume_high = [s for s in resume_skills if self.skills_extractor.get_skill_importance(s) == 'high']
1199
+ resume_medium = [s for s in resume_skills if self.skills_extractor.get_skill_importance(s) == 'medium']
1200
+ resume_low = [s for s in resume_skills if self.skills_extractor.get_skill_importance(s) == 'low']
1201
+
1202
+ job_high = [s for s in job_skills if self.skills_extractor.get_skill_importance(s) == 'high']
1203
+ job_medium = [s for s in job_skills if self.skills_extractor.get_skill_importance(s) == 'medium']
1204
+ job_low = [s for s in job_skills if self.skills_extractor.get_skill_importance(s) == 'low']
1205
+
1206
+ return {
1207
+ "resume_skills_by_importance": {
1208
+ "high": resume_high,
1209
+ "medium": resume_medium,
1210
+ "low": resume_low
1211
+ },
1212
+ "job_skills_by_importance": {
1213
+ "high": job_high,
1214
+ "medium": job_medium,
1215
+ "low": job_low
1216
+ },
1217
+ "high_importance_match": len(set(resume_high).intersection(set(job_high))),
1218
+ "medium_importance_match": len(set(resume_medium).intersection(set(job_medium)))
1219
+ }
1220
+
1221
+ if __name__ == "__main__":
1222
+ print("=== CORRECTED Resume-Job Matcher ===")
1223
+ resume_pdf_path = input("Enter the path to the resume PDF file: ").strip()
1224
+ job_description_path = input("Enter the path to the job description text file: ").strip()
1225
+ # Automatically select the best BERT model based on similarity scores
1226
+ resume_bert_model = None
1227
+ try:
1228
+ with open(resume_pdf_path, "rb") as f:
1229
+ resume_pdf_bytes = f.read()
1230
+ except Exception as e:
1231
+ print(f"Error reading resume PDF: {e}")
1232
+ exit(1)
1233
+ try:
1234
+ with open(job_description_path, "r", encoding="utf-8") as f:
1235
+ job_description = f.read()
1236
+ except Exception as e:
1237
+ print(f"Error reading job description: {e}")
1238
+ exit(1)
1239
+ groq_api_key = os.getenv("GROQ_API_KEY")
1240
+ cohere_api_key = os.getenv("COHERE_API_KEY")
1241
+ matcher = CorrectedResumeJobMatcher(
1242
+ groq_api_key=groq_api_key,
1243
+ cohere_api_key=cohere_api_key,
1244
+ resume_bert_model=resume_bert_model
1245
+ )
1246
+ result = matcher.match(resume_pdf_bytes, job_description)
1247
+ print("\n" + "="*60)
1248
+ print(" FINAL MATCHING RESULTS (CORRECTED)")
1249
+ print("="*60)
1250
+ final_score = result["final_similarity_score"]
1251
+ final_percentage = result["final_similarity_percentage"]
1252
+ category = result["similarity_category"]
1253
+ print(f"\n🎯 FINAL SIMILARITY SCORE: {final_score:.4f} ({final_percentage:.2f}%)")
1254
+ print(f"📊 MATCH CATEGORY: {category}")
1255
+ print(f"🔍 CONFIDENCE LEVEL: {result['confidence']:.3f}")
1256
+ print(f"⚠️ ANOMALY DETECTED: {result['anomaly']}")
1257
+ print(f"\n" + "-"*50)
1258
+ print("COMPONENT BREAKDOWN:")
1259
+ print(f"├── Semantic Similarity: {result['semantic_score']:.3f} ({result['semantic_score']*100:.1f}%)")
1260
+ print(f"├── Skills Matching: {result['skills_score']:.3f} ({result['skills_score']*100:.1f}%)")
1261
+ print(f"├── Enhanced Skills Score: {result['enhanced_skills_score']:.3f} ({result['enhanced_skills_score']*100:.1f}%)")
1262
+ print(f"├── Resume-BERT Score: {result['resume_bert_score']:.3f} ({result['resume_bert_score']*100:.1f}%)")
1263
+ print(f"└── LLM Assessment: {result['llm_score']:.3f} ({result['llm_score']*100:.1f}%)")
1264
+ weights_info = result["final_similarity_details"]
1265
+ print(f"\n" + "-"*50)
1266
+ print("WEIGHTING STRATEGY:")
1267
+ if weights_info.get("llm_dominant", False):
1268
+ print("🎯 LLM-DOMINANT WEIGHTING (High LLM score detected)")
1269
+ for component, weight in weights_info["weights_used"].items():
1270
+ if weight > 0 and component in weights_info["components_used"]:
1271
+ print(f"├── {component}: {weight*100:.0f}%")
1272
+ print(f"\n" + "-"*50)
1273
+ print("📊 DETAILED PROFESSIONAL ANALYSIS")
1274
+ print("-"*50)
1275
+
1276
+ # Enhanced Model Analysis
1277
+ model_info = result["model_info"]
1278
+ print(f"\n🤖 AI MODEL ANALYSIS:")
1279
+ print(f"├── Primary Semantic Model: {model_info.get('primary_semantic_model', 'N/A')}")
1280
+ print(f"├── Resume-Specific Model: {model_info.get('resume_specific_model', 'N/A')}")
1281
+ print(f"├── Total Models Loaded: {model_info.get('total_models_loaded', 0)}")
1282
+ print(f"└── Resume Model Available: {'✅ Yes' if model_info.get('resume_model_available') else '❌ No'}")
1283
+
1284
+ # Best Model Selection
1285
+ ensemble_analysis = result.get("ensemble_analysis", {})
1286
+ best_model = ensemble_analysis.get("best_model", {})
1287
+ if best_model:
1288
+ print(f"\n🎯 OPTIMAL MODEL SELECTION:")
1289
+ print(f"├── Best Model: {best_model.get('model_name', 'N/A')}")
1290
+ print(f"├── Category: {best_model.get('category', 'N/A')}")
1291
+ print(f"├── Score: {best_model.get('score', 0):.4f}")
1292
+ print(f"└── All Model Scores:")
1293
+ all_scores = best_model.get('all_scores', {})
1294
+ for model, score in all_scores.items():
1295
+ print(f" • {model}: {score:.4f}")
1296
+
1297
+ # Enhanced LLM Analysis
1298
+ llm_details = result["llm_details"]
1299
+ print(f"\n🧠 LLM INTELLIGENCE ANALYSIS:")
1300
+ print(f"├── API Used: {llm_details.get('api_used', 'N/A')}")
1301
+ print(f"├── Response Time: {llm_details.get('response_time', 0):.2f}s")
1302
+ print(f"├── Compatibility Score: {llm_details.get('compatibility_score', 0)}/100")
1303
+ print(f"└── Analysis Quality: {'High' if llm_details.get('response_time', 0) < 5 else 'Medium'}")
1304
+
1305
+ if llm_details.get('strengths'):
1306
+ print(f"\n💪 KEY STRENGTHS IDENTIFIED:")
1307
+ for i, strength in enumerate(llm_details['strengths'][:5], 1):
1308
+ print(f" {i}. {strength}")
1309
+
1310
+ if llm_details.get('gaps'):
1311
+ print(f"\n🎯 AREAS FOR IMPROVEMENT:")
1312
+ for i, gap in enumerate(llm_details['gaps'][:5], 1):
1313
+ print(f" {i}. {gap}")
1314
+
1315
+ if llm_details.get('recommendations'):
1316
+ print(f"\n💡 STRATEGIC RECOMMENDATIONS:")
1317
+ for i, rec in enumerate(llm_details['recommendations'][:5], 1):
1318
+ print(f" {i}. {rec}")
1319
+
1320
+ # Enhanced Skills Analysis
1321
+ print(f"\n🔧 ENHANCED SKILLS ANALYSIS:")
1322
+ skills_analysis = result["skills_analysis"]
1323
+ print(f"├── Skills Coverage: {skills_analysis['coverage_percentage']*100:.1f}%")
1324
+ print(f"├── Skills Gap: {skills_analysis['skills_gap']}")
1325
+ print(f"├── Resume Skills: {skills_analysis['resume_skills_count']}")
1326
+ print(f"├── Job Requirements: {skills_analysis['job_skills_count']}")
1327
+ print(f"└── Critical Skills Missing: {len(skills_analysis['critical_skills_missing'])}")
1328
+
1329
+ # Show skills by importance
1330
+ importance_analysis = result["debug_info"]["skills_importance_analysis"]
1331
+ print(f"\n📊 SKILLS BY IMPORTANCE:")
1332
+ print(f"├── High Importance Matches: {importance_analysis['high_importance_match']}")
1333
+ print(f"├── Medium Importance Matches: {importance_analysis['medium_importance_match']}")
1334
+ print(f"├── Resume High-Value Skills: {len(importance_analysis['resume_skills_by_importance']['high'])}")
1335
+ print(f"└── Job High-Value Requirements: {len(importance_analysis['job_skills_by_importance']['high'])}")
1336
+
1337
+ if skills_analysis['critical_skills_missing']:
1338
+ print(f"\n❌ CRITICAL SKILLS MISSING:")
1339
+ for skill in skills_analysis['critical_skills_missing']:
1340
+ print(f" • {skill}")
1341
+
1342
+ if skills_analysis.get('related_compensations'):
1343
+ print(f"\n🔄 RELATED SKILLS COMPENSATION:")
1344
+ for missing, related in skills_analysis['related_compensations'].items():
1345
+ print(f" • Missing '{missing}' but have: {', '.join(related)}")
1346
+
1347
+ # Show skills recommendations
1348
+ if result.get("skills_recommendations"):
1349
+ print(f"\n💡 SKILLS RECOMMENDATIONS:")
1350
+ for rec in result["skills_recommendations"]:
1351
+ print(f" • {rec['title']}: {rec['description']}")
1352
+
1353
+ # Professional Assessment
1354
+ print(f"\n📈 PROFESSIONAL ASSESSMENT:")
1355
+ confidence = result['confidence']
1356
+ anomaly = result['anomaly']
1357
+
1358
+ if confidence > 0.8:
1359
+ confidence_level = "High"
1360
+ elif confidence > 0.6:
1361
+ confidence_level = "Medium"
1362
+ else:
1363
+ confidence_level = "Low"
1364
+
1365
+ print(f"├── Assessment Confidence: {confidence_level} ({confidence:.1%})")
1366
+ print(f"├── Data Consistency: {'✅ Good' if not anomaly else '⚠️ Inconsistent'}")
1367
+ print(f"├── Recommendation: ", end="")
1368
+
1369
+ if final_percentage >= 80:
1370
+ print("Strongly Recommended")
1371
+ elif final_percentage >= 65:
1372
+ print("Recommended with Minor Concerns")
1373
+ elif final_percentage >= 50:
1374
+ print("Consider with Development Plan")
1375
+ else:
1376
+ print("Not Recommended")
1377
+
1378
+ print(f"└── Next Steps: ", end="")
1379
+ if final_percentage >= 80:
1380
+ print("Proceed to interview")
1381
+ elif final_percentage >= 65:
1382
+ print("Schedule technical assessment")
1383
+ elif final_percentage >= 50:
1384
+ print("Request additional training/certifications")
1385
+ else:
1386
+ print("Consider alternative roles or candidates")
1387
+
1388
+ # Detailed Component Analysis
1389
+ print(f"\n🔍 DETAILED COMPONENT ANALYSIS:")
1390
+ print("-" * 50)
1391
+ print(f"📊 SEMANTIC SIMILARITY ANALYSIS:")
1392
+ print(f" • Score: {result['semantic_score']:.3f} ({result['semantic_score']*100:.1f}%)")
1393
+ print(f" • Model Used: {model_info.get('primary_semantic_model', 'N/A')}")
1394
+ print(f" • Analysis: {'Strong semantic alignment' if result['semantic_score'] > 0.7 else 'Moderate alignment' if result['semantic_score'] > 0.5 else 'Weak alignment'}")
1395
+
1396
+ print(f"\n🔧 SKILLS MATCHING ANALYSIS:")
1397
+ print(f" • Basic Skills Score: {result['skills_score']:.3f} ({result['skills_score']*100:.1f}%)")
1398
+ print(f" • Enhanced Skills Score: {result['enhanced_skills_score']:.3f} ({result['enhanced_skills_score']*100:.1f}%)")
1399
+ print(f" • Skills Coverage: {skills_analysis['coverage_percentage']*100:.1f}%")
1400
+ print(f" • Direct Matches: {skills_analysis['direct_match_count']}/{skills_analysis['total_job_skills']}")
1401
+ print(f" • Missing Skills: {len(skills_analysis['missing_skills'])}")
1402
+
1403
+ print(f"\n🤖 RESUME-BERT ANALYSIS:")
1404
+ print(f" • Score: {result['resume_bert_score']:.3f} ({result['resume_bert_score']*100:.1f}%)")
1405
+ print(f" • Model Used: {model_info.get('resume_specific_model', 'N/A')}")
1406
+ print(f" • Analysis: {'Strong resume-specific alignment' if result['resume_bert_score'] > 0.7 else 'Moderate alignment' if result['resume_bert_score'] > 0.5 else 'Weak alignment'}")
1407
+
1408
+ print(f"\n🧠 LLM INTELLIGENCE ANALYSIS:")
1409
+ print(f" • Score: {result['llm_score']:.1f}/100")
1410
+ print(f" • API Used: {llm_details.get('api_used', 'N/A')}")
1411
+ print(f" • Response Time: {llm_details.get('response_time', 0):.2f}s")
1412
+ print(f" • Analysis Quality: {'High' if llm_details.get('response_time', 0) < 5 else 'Medium'}")
1413
+
1414
+ # Score Adjustments Made
1415
+ if result.get("debug_info", {}).get("score_adjustments_made"):
1416
+ print(f"\n⚙️ SCORE ADJUSTMENTS APPLIED:")
1417
+ adjustments = result["debug_info"]["score_adjustments_made"]
1418
+ for component, original_score in result["component_scores"].items():
1419
+ if component in adjustments and adjustments[component] != original_score:
1420
+ print(f" • {component}: {original_score:.3f} → {adjustments[component]:.3f}")
1421
+
1422
+ # Weighting Strategy Details
1423
+ print(f"\n⚖️ WEIGHTING STRATEGY DETAILS:")
1424
+ print(f" • Strategy Used: {'LLM-Dominant' if weights_info.get('llm_dominant') else 'Standard'}")
1425
+ print(f" • Total Weight Used: {weights_info.get('total_weight_used', 0):.2f}")
1426
+ print(f" • Components Used: {', '.join(weights_info.get('components_used', []))}")
1427
+
1428
+ # Model Performance Analysis
1429
+ print(f"\n🎯 MODEL PERFORMANCE ANALYSIS:")
1430
+ if best_model:
1431
+ print(f" • Best Model: {best_model.get('model_name', 'N/A')}")
1432
+ print(f" • Best Score: {best_model.get('score', 0):.4f}")
1433
+ print(f" • Category: {best_model.get('category', 'N/A')}")
1434
+ print(f" • All Model Scores:")
1435
+ all_scores = best_model.get('all_scores', {})
1436
+ for model, score in all_scores.items():
1437
+ print(f" - {model}: {score:.4f}")
1438
+
1439
+ # Skills Importance Analysis
1440
+ importance_analysis = result["debug_info"]["skills_importance_analysis"]
1441
+ print(f"\n📊 SKILLS IMPORTANCE BREAKDOWN:")
1442
+ print(f" • High Importance Matches: {importance_analysis['high_importance_match']}")
1443
+ print(f" • Medium Importance Matches: {importance_analysis['medium_importance_match']}")
1444
+ print(f" • Resume High-Value Skills: {len(importance_analysis['resume_skills_by_importance']['high'])}")
1445
+ print(f" • Job High-Value Requirements: {len(importance_analysis['job_skills_by_importance']['high'])}")
1446
+
1447
+ # Confidence and Anomaly Analysis
1448
+ print(f"\n🔍 CONFIDENCE & ANOMALY ANALYSIS:")
1449
+ print(f" • Confidence Score: {confidence:.3f}")
1450
+ print(f" • Confidence Level: {confidence_level}")
1451
+ print(f" • Anomaly Detected: {anomaly}")
1452
+ print(f" • Score Consistency: {'✅ Consistent' if not anomaly else '⚠️ Inconsistent'}")
1453
+
1454
+ print(f"\n" + "="*60)
1455
+ print("📋 EXECUTIVE SUMMARY")
1456
+ print("="*60)
1457
+ print(f"🎯 Final Score: {final_percentage:.1f}% ({category})")
1458
+ print(f"🔍 Confidence: {confidence_level}")
1459
+ print(f"⚡ Key Strength: {'Technical Skills' if result['skills_score'] > 0.7 else 'Experience' if result['resume_bert_score'] > 0.6 else 'LLM Assessment'}")
1460
+ print(f"⚠️ Primary Gap: {'Missing Skills' if skills_analysis['missing_skills'] else 'Experience Level' if result['resume_bert_score'] < 0.5 else 'Semantic Match'}")
1461
+ print("="*60)
app/main.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ FastAPI main application entry point for AI Resume Reviewer
3
+ """
4
+ from fastapi import FastAPI
5
+ from fastapi.middleware.cors import CORSMiddleware
6
+ from app.routes import review
7
+ from app.config import settings
8
+
9
+ # Create FastAPI app instance
10
+ app = FastAPI(
11
+ title="AI Resume Reviewer API",
12
+ description="Backend API for AI-powered resume review and job matching",
13
+ version="1.0.0"
14
+ )
15
+
16
+ # Add CORS middleware
17
+ app.add_middleware(
18
+ CORSMiddleware,
19
+ allow_origins=["*"], # In production, specify exact origins
20
+ allow_credentials=True,
21
+ allow_methods=["*"],
22
+ allow_headers=["*"],
23
+ )
24
+
25
+ # Include routers
26
+ app.include_router(review.router, prefix="/api/v1", tags=["review"])
27
+
28
+ @app.get("/")
29
+ async def root():
30
+ """Health check endpoint"""
31
+ return {"message": "AI Resume Reviewer API is running!"}
32
+
33
+ @app.get("/health")
34
+ async def health_check():
35
+ """Health check endpoint"""
36
+ return {"status": "healthy"}
37
+
38
+ if __name__ == "__main__":
39
+ import uvicorn
40
+ uvicorn.run(app, host="0.0.0.0", port=8000)
app/routes/review.py ADDED
@@ -0,0 +1,175 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ FastAPI routes for resume review functionality
3
+ """
4
+ from fastapi import APIRouter, UploadFile, File, Form, HTTPException
5
+ from fastapi.responses import JSONResponse
6
+ from app.embedding import CorrectedResumeJobMatcher
7
+ from app.supabase import supabase_service
8
+ from app.config import settings
9
+ import logging
10
+ import os
11
+
12
+ logger = logging.getLogger(__name__)
13
+
14
+ router = APIRouter()
15
+
16
+ @router.post("/match", response_model=dict)
17
+ async def match_resume_job(
18
+ file: UploadFile = File(..., description="PDF resume file"),
19
+ job_description: str = Form(..., description="Job description text", min_length=50)
20
+ ):
21
+ """
22
+ Enhanced resume-job matching using the CorrectedResumeJobMatcher
23
+
24
+ Args:
25
+ file: PDF file upload
26
+ job_description: Job description as form data
27
+
28
+ Returns:
29
+ dict: Comprehensive matching results with detailed analysis
30
+ """
31
+ try:
32
+ # Validate file type
33
+ if file.content_type not in settings.ALLOWED_FILE_TYPES:
34
+ raise HTTPException(
35
+ status_code=400,
36
+ detail=f"Invalid file type. Only PDF files are allowed."
37
+ )
38
+
39
+ # Check file size
40
+ file_content = await file.read()
41
+ if len(file_content) > settings.MAX_FILE_SIZE:
42
+ raise HTTPException(
43
+ status_code=400,
44
+ detail=f"File size exceeds maximum allowed size of {settings.MAX_FILE_SIZE/1024/1024}MB"
45
+ )
46
+
47
+ # Initialize the enhanced matcher
48
+ groq_api_key = os.getenv("GROQ_API_KEY")
49
+ cohere_api_key = os.getenv("COHERE_API_KEY")
50
+
51
+ matcher = CorrectedResumeJobMatcher(
52
+ groq_api_key=groq_api_key,
53
+ cohere_api_key=cohere_api_key,
54
+ resume_bert_model=None # Auto-select best model
55
+ )
56
+
57
+ # Perform the matching analysis
58
+ result = matcher.match(file_content, job_description)
59
+
60
+ # Print detailed analysis to terminal
61
+ print("\n" + "="*80)
62
+ print("🔍 API REQUEST ANALYSIS RESULTS")
63
+ print("="*80)
64
+
65
+ final_score = result["final_similarity_score"]
66
+ final_percentage = result["final_similarity_percentage"]
67
+ category = result["similarity_category"]
68
+
69
+ print(f"\n🎯 FINAL MATCH SCORE: {final_score:.4f} ({final_percentage:.2f}%)")
70
+ print(f"📊 CATEGORY: {category}")
71
+ print(f"🔍 CONFIDENCE: {result['confidence']:.3f}")
72
+ print(f"⚠️ ANOMALY: {result['anomaly']}")
73
+
74
+ # Component scores
75
+ print(f"\n📊 COMPONENT SCORES:")
76
+ print(f" • Semantic Similarity: {result['semantic_score']:.3f} ({result['semantic_score']*100:.1f}%)")
77
+ print(f" • Skills Matching: {result['skills_score']:.3f} ({result['skills_score']*100:.1f}%)")
78
+ print(f" • Enhanced Skills: {result['enhanced_skills_score']:.3f} ({result['enhanced_skills_score']*100:.1f}%)")
79
+ print(f" • Resume-BERT: {result['resume_bert_score']:.3f} ({result['resume_bert_score']*100:.1f}%)")
80
+ print(f" • LLM Assessment: {result['llm_score']:.1f}/100")
81
+
82
+ # LLM Details
83
+ if result.get('llm_details'):
84
+ llm_details = result['llm_details']
85
+ print(f"\n🧠 LLM ANALYSIS:")
86
+ print(f" • API Used: {llm_details.get('api_used', 'N/A')}")
87
+ print(f" • Response Time: {llm_details.get('response_time', 0):.2f}s")
88
+ print(f" • Compatibility: {llm_details.get('compatibility_score', 0)}/100")
89
+
90
+ if llm_details.get('strengths'):
91
+ print(f" • Key Strengths: {len(llm_details['strengths'])} identified")
92
+ if llm_details.get('gaps'):
93
+ print(f" • Areas for Improvement: {len(llm_details['gaps'])} identified")
94
+ if llm_details.get('recommendations'):
95
+ print(f" • Recommendations: {len(llm_details['recommendations'])} provided")
96
+
97
+ # Skills Analysis
98
+ if result.get('skills_analysis'):
99
+ skills_analysis = result['skills_analysis']
100
+ print(f"\n🔧 SKILLS ANALYSIS:")
101
+ print(f" • Coverage: {skills_analysis['coverage_percentage']*100:.1f}%")
102
+ print(f" • Direct Matches: {skills_analysis['direct_match_count']}/{skills_analysis['total_job_skills']}")
103
+ print(f" • Missing Skills: {len(skills_analysis['missing_skills'])}")
104
+ print(f" • Critical Skills Missing: {len(skills_analysis['critical_skills_missing'])}")
105
+
106
+ # Model Info
107
+ if result.get('model_info'):
108
+ model_info = result['model_info']
109
+ print(f"\n🤖 MODEL INFO:")
110
+ print(f" • Primary Model: {model_info.get('primary_semantic_model', 'N/A')}")
111
+ print(f" • Resume Model: {model_info.get('resume_specific_model', 'N/A')}")
112
+ print(f" • Total Models: {model_info.get('total_models_loaded', 0)}")
113
+
114
+ print("\n" + "="*80)
115
+
116
+ # Store results in database if Supabase is configured
117
+ try:
118
+ # Extract resume text for storage
119
+ resume_text = matcher.pdf_extractor.extract_text(file_content)
120
+
121
+ # Create a summary for storage
122
+ feedback = f"Match Score: {result['final_similarity_percentage']:.1f}% - {result['similarity_category']}"
123
+
124
+ # Store in database
125
+ await supabase_service.insert_resume_review(
126
+ resume_text=resume_text,
127
+ job_description=job_description,
128
+ match_score=result['final_similarity_percentage'],
129
+ feedback=feedback
130
+ )
131
+ except Exception as e:
132
+ logger.warning(f"Failed to store results in database: {str(e)}")
133
+ # Continue without failing the request
134
+
135
+ return result
136
+
137
+ except Exception as e:
138
+ logger.error(f"Resume matching failed: {str(e)}")
139
+ raise HTTPException(
140
+ status_code=500,
141
+ detail=f"Failed to process resume matching: {str(e)}"
142
+ )
143
+
144
+ @router.get("/reviews")
145
+ async def get_recent_reviews():
146
+ """
147
+ Get recent resume reviews
148
+
149
+ Returns:
150
+ list: Recent resume reviews
151
+ """
152
+ try:
153
+ reviews = await supabase_service.get_resume_reviews(limit=10)
154
+ return {"reviews": reviews}
155
+ except Exception as e:
156
+ logger.error(f"Error retrieving reviews: {str(e)}")
157
+ raise HTTPException(
158
+ status_code=500,
159
+ detail="Failed to retrieve reviews"
160
+ )
161
+
162
+ @router.get("/health")
163
+ async def health_check():
164
+ """
165
+ Health check for the review service
166
+
167
+ Returns:
168
+ dict: Health status
169
+ """
170
+ return {
171
+ "status": "healthy",
172
+ "embedding_model": settings.EMBEDDING_MODEL_NAME,
173
+ "llm_model": settings.LLM_MODEL_NAME,
174
+ "supabase_connected": supabase_service.client is not None
175
+ }
app/supabase.py ADDED
@@ -0,0 +1,106 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Supabase client setup and database operations
3
+ """
4
+ from supabase import create_client, Client
5
+ from datetime import datetime
6
+ from typing import Optional, Dict, Any
7
+ import logging
8
+ from app.config import settings
9
+
10
+ logger = logging.getLogger(__name__)
11
+
12
+ class SupabaseService:
13
+ """Service for Supabase database operations"""
14
+
15
+ def __init__(self):
16
+ """Initialize Supabase client"""
17
+ self.client: Optional[Client] = None
18
+ self._setup_client()
19
+
20
+ def _setup_client(self):
21
+ """Setup Supabase client"""
22
+ try:
23
+ if settings.SUPABASE_URL and settings.SUPABASE_KEY:
24
+ self.client = create_client(settings.SUPABASE_URL, settings.SUPABASE_KEY)
25
+ logger.info("Supabase client initialized successfully")
26
+ else:
27
+ logger.warning("Supabase credentials not provided - database operations will be disabled")
28
+ except Exception as e:
29
+ logger.error(f"Failed to initialize Supabase client: {str(e)}")
30
+ self.client = None
31
+
32
+ async def insert_resume_review(
33
+ self,
34
+ resume_text: str,
35
+ job_description: str,
36
+ match_score: float,
37
+ feedback: str
38
+ ) -> Optional[Dict[str, Any]]:
39
+ """
40
+ Insert resume review into database
41
+
42
+ Args:
43
+ resume_text: Extracted resume text
44
+ job_description: Job description
45
+ match_score: Similarity score
46
+ feedback: AI-generated feedback
47
+
48
+ Returns:
49
+ Dict with inserted record or None if failed
50
+ """
51
+ if not self.client:
52
+ logger.warning("Supabase client not available - skipping database insert")
53
+ return None
54
+
55
+ try:
56
+ data = {
57
+ "resume_text": resume_text,
58
+ "job_description": job_description,
59
+ "match_score": match_score,
60
+ "feedback": feedback,
61
+ "created_at": datetime.utcnow().isoformat()
62
+ }
63
+
64
+ result = self.client.table("resume_reviews").insert(data).execute()
65
+
66
+ if result.data:
67
+ logger.info("Resume review stored in database successfully")
68
+ return result.data[0]
69
+ else:
70
+ logger.error("Failed to insert resume review - no data returned")
71
+ return None
72
+
73
+ except Exception as e:
74
+ logger.error(f"Database insert failed: {str(e)}")
75
+ return None
76
+
77
+ async def get_resume_reviews(self, limit: int = 10) -> list:
78
+ """
79
+ Get recent resume reviews
80
+
81
+ Args:
82
+ limit: Maximum number of reviews to return
83
+
84
+ Returns:
85
+ List of recent reviews
86
+ """
87
+ if not self.client:
88
+ logger.warning("Supabase client not available - returning empty list")
89
+ return []
90
+
91
+ try:
92
+ result = self.client.table("resume_reviews").select("*").order("created_at", desc=True).limit(limit).execute()
93
+
94
+ if result.data:
95
+ logger.info(f"Retrieved {len(result.data)} recent reviews")
96
+ return result.data
97
+ else:
98
+ logger.info("No recent reviews found")
99
+ return []
100
+
101
+ except Exception as e:
102
+ logger.error(f"Failed to retrieve reviews: {str(e)}")
103
+ return []
104
+
105
+ # Global service instance
106
+ supabase_service = SupabaseService()
requirements.txt ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Core Framework
2
+ fastapi
3
+ uvicorn[standard]
4
+ python-multipart
5
+
6
+ # Essential ML Libraries
7
+ sentence-transformers
8
+ transformers
9
+ torch
10
+ numpy
11
+ scikit-learn
12
+
13
+ # PDF Processing
14
+ pdfplumber
15
+ PyMuPDF
16
+
17
+ # Text Processing
18
+ spacy
19
+ fuzzywuzzy
20
+ python-Levenshtein
21
+
22
+ # HTTP and Environment
23
+ requests
24
+ python-dotenv
25
+
26
+ # Database (Optional)
27
+ supabase
28
+
29
+ # Hugging Face Hub
30
+ huggingface-hub