Update parser_logic.py
Browse files- parser_logic.py +68 -70
parser_logic.py
CHANGED
|
@@ -1,71 +1,69 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import json
|
| 3 |
-
import re
|
| 4 |
-
import logging
|
| 5 |
-
import fitz # PyMuPDF
|
| 6 |
-
|
| 7 |
-
from dotenv import load_dotenv
|
| 8 |
-
|
| 9 |
-
# Configure Logging
|
| 10 |
-
logging.basicConfig(level=logging.INFO)
|
| 11 |
-
logger = logging.getLogger(__name__)
|
| 12 |
-
|
| 13 |
-
load_dotenv()
|
| 14 |
-
|
| 15 |
-
# Secure Configuration
|
| 16 |
-
api_key = os.getenv("GEMINI_API_KEY")
|
| 17 |
-
if not api_key:
|
| 18 |
-
logger.error("GEMINI_API_KEY not found in environment variables.")
|
| 19 |
-
raise ValueError("GEMINI_API_KEY is missing.")
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
def extract_text_from_stream(file_bytes: bytes) -> str:
|
| 25 |
-
"""Extracts raw text content from PDF bytes directly in memory."""
|
| 26 |
-
text = ""
|
| 27 |
-
try:
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
-
|
| 44 |
-
-
|
| 45 |
-
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
clean_json = re.sub(r'```json\s*|```', '', raw_output, flags=re.MULTILINE).strip()
|
| 64 |
-
|
| 65 |
-
return json.loads(clean_json)
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
except Exception as e:
|
| 70 |
-
logger.error(f"AI Processing Error: {e}")
|
| 71 |
return {"error": f"AI Processing failed: {str(e)}"}
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
import re
|
| 4 |
+
import logging
|
| 5 |
+
import fitz # PyMuPDF
|
| 6 |
+
from google import genai
|
| 7 |
+
from dotenv import load_dotenv
|
| 8 |
+
|
| 9 |
+
# Configure Logging
|
| 10 |
+
logging.basicConfig(level=logging.INFO)
|
| 11 |
+
logger = logging.getLogger(__name__)
|
| 12 |
+
|
| 13 |
+
load_dotenv()
|
| 14 |
+
|
| 15 |
+
# Secure Configuration
|
| 16 |
+
api_key = os.getenv("GEMINI_API_KEY")
|
| 17 |
+
if not api_key:
|
| 18 |
+
logger.error("GEMINI_API_KEY not found in environment variables.")
|
| 19 |
+
raise ValueError("GEMINI_API_KEY is missing.")
|
| 20 |
+
|
| 21 |
+
# NEW SDK INITIALIZATION
|
| 22 |
+
client = genai.Client(api_key=api_key)
|
| 23 |
+
|
| 24 |
+
def extract_text_from_stream(file_bytes: bytes) -> str:
|
| 25 |
+
"""Extracts raw text content from PDF bytes directly in memory."""
|
| 26 |
+
text = ""
|
| 27 |
+
try:
|
| 28 |
+
with fitz.open(stream=file_bytes, filetype="pdf") as doc:
|
| 29 |
+
for page in doc:
|
| 30 |
+
text += page.get_text()
|
| 31 |
+
except Exception as e:
|
| 32 |
+
logger.error(f"PDF Extraction Error: {e}")
|
| 33 |
+
raise ValueError("Failed to extract text from PDF. File may be corrupted.")
|
| 34 |
+
return text
|
| 35 |
+
|
| 36 |
+
def parse_resume_with_ai(resume_text: str) -> dict:
|
| 37 |
+
"""Uses GenAI to transform unstructured text into JSON."""
|
| 38 |
+
|
| 39 |
+
prompt = f"""
|
| 40 |
+
Acting as an expert recruiter, extract the following data from this resume text:
|
| 41 |
+
- name (string)
|
| 42 |
+
- email (string)
|
| 43 |
+
- phone (string)
|
| 44 |
+
- skills (array of strings)
|
| 45 |
+
- summary (string, max 2 sentences)
|
| 46 |
+
|
| 47 |
+
If a field is not found, return null or an empty list.
|
| 48 |
+
Return strictly valid JSON. Do not include markdown formatting.
|
| 49 |
+
|
| 50 |
+
Resume Text:
|
| 51 |
+
{resume_text[:10000]}
|
| 52 |
+
"""
|
| 53 |
+
|
| 54 |
+
try:
|
| 55 |
+
# NEW SDK CALL
|
| 56 |
+
response = client.models.generate_content(
|
| 57 |
+
model="gemini-1.5-flash",
|
| 58 |
+
contents=prompt
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
# Robust Cleaning
|
| 62 |
+
raw_output = response.text.strip()
|
| 63 |
+
clean_json = re.sub(r'```json\s*|```', '', raw_output, flags=re.MULTILINE).strip()
|
| 64 |
+
|
| 65 |
+
return json.loads(clean_json)
|
| 66 |
+
|
| 67 |
+
except Exception as e:
|
| 68 |
+
logger.error(f"AI Processing Error: {e}")
|
|
|
|
|
|
|
| 69 |
return {"error": f"AI Processing failed: {str(e)}"}
|