import os import json import docx from pypdf import PdfReader from groq import Groq from dotenv import load_dotenv # Load environment variables load_dotenv() DEFAULT_MODEL = "llama-3.3-70b-versatile" DEFAULT_TEMPERATURE = 0.2 def build_application_context(target_role=None, experience_level=None, focus_area=None): """Create a short context block from user-facing personalization choices.""" context_lines = [] if target_role and target_role.strip(): context_lines.append(f"Target role: {target_role.strip()}") if experience_level and experience_level != "Not specified": context_lines.append(f"Experience level: {experience_level}") if focus_area: context_lines.append(f"Main focus requested by user: {focus_area}") return "\n".join(context_lines) if context_lines else "No extra user preferences provided." def extract_text_from_pdf(file_path): """Extract text from a PDF file using pypdf.""" text = "" try: reader = PdfReader(file_path) for page in reader.pages: page_text = page.extract_text() if page_text: text += page_text + "\n" except Exception as e: raise Exception(f"Error reading PDF file: {str(e)}") return text def extract_text_from_docx(file_path): """Extract text from a DOCX file using python-docx.""" text = "" try: doc = docx.Document(file_path) for para in doc.paragraphs: text += para.text + "\n" for table in doc.tables: for row in table.rows: for cell in row.cells: text += cell.text + " " text += "\n" except Exception as e: raise Exception(f"Error reading DOCX file: {str(e)}") return text def parse_resume_file(uploaded_file): """Parse the uploaded file based on its extension.""" filename = uploaded_file.name # Save file temporarily or read bytes directly # Since streamlit upload is a BytesIO-like object, we can write a temporary file or pass it to standard tools # Let's save it temporarily to read it temp_dir = os.path.join(os.path.dirname(__file__), "temp") os.makedirs(temp_dir, exist_ok=True) temp_path = os.path.join(temp_dir, filename) try: with open(temp_path, "wb") as f: f.write(uploaded_file.getbuffer()) if filename.endswith(".pdf"): text = extract_text_from_pdf(temp_path) elif filename.endswith(".docx"): text = extract_text_from_docx(temp_path) else: # Fallback to plain text with open(temp_path, "r", encoding="utf-8", errors="ignore") as f: text = f.read() finally: # Cleanup if os.path.exists(temp_path): os.remove(temp_path) return text def analyze_resume_ats( resume_text, job_desc_text, api_key=None, target_role=None, experience_level=None, focus_area=None ): """ Review a resume against a job description and return a structured JSON payload for the Streamlit results screen. """ # Use provided key or fall back to environment variable key = api_key or os.getenv("GROQ_API_KEY") if not key: raise ValueError("The analysis service is not configured yet.") client = Groq(api_key=key) application_context = build_application_context(target_role, experience_level, focus_area) system_prompt = ( "You are an expert Applicant Tracking System (ATS) reviewer, HR consultant, and resume coach. " "Your task is to perform a highly thorough, accurate, and professional ATS audit of the user's resume against " "the provided job description. You must output the analysis strictly in JSON format. Do not write any conversational " "text before or after the JSON payload. Ensure all JSON fields are populated accurately. " "Keep the wording practical and applicant-friendly. Do not mention model names, API providers, temperature, prompts, " "JSON schema details, or backend implementation in any user-facing response field." ) user_prompt = f""" Analyze the resume and job description below. Use the applicant preferences to make the recommendations more specific. --- APPLICANT PREFERENCES --- {application_context} --- RESUME --- {resume_text} --- JOB DESCRIPTION --- {job_desc_text} --- OUTPUT JSON SCHEMA --- You must return a JSON object with the following exact keys and types: {{ "ats_score": , "match_percentage": , "formatting_score": , "keyword_score": , "experience_score": , "skills_score": , "analysis_summary": "", "key_strengths": ["", "", ...], "critical_issues": ["", "", ...], "keyword_analysis": [ {{ "keyword": "", "status": "", "importance": "" }}, ... ], "skills_gap": [ {{ "skill": "", "category": "", "gap_description": "" }}, ... ], "ats_compatibility_checks": [ {{ "check": "", "status": "", "feedback": "" }}, ... ], "improvement_suggestions": ["", ...], "optimized_resume_snippets": [ {{ "original": "", "optimized": "", "rationale": "" }}, ... ], "interview_questions": [ {{ "question": "", "rationale": "" }}, ... ], "cover_letter": "" }} """ try: response = client.chat.completions.create( model=DEFAULT_MODEL, messages=[ {"role": "system", "content": system_prompt}, {"role": "user", "content": user_prompt} ], response_format={"type": "json_object"}, temperature=DEFAULT_TEMPERATURE ) result_content = response.choices[0].message.content return json.loads(result_content) except json.JSONDecodeError: raise Exception("The analysis returned an unexpected format. Please try again.") except Exception: raise Exception("Could not complete the analysis right now. Please try again in a minute.")