đ¤ HireWithAI - Smart Resume Screening System
AI-Powered Multi-Agent Recruitment Platform
Reduce 70% of time-to-hire with automated resume screening and ranking
""" HireWithAI - Smart Resume Screening System A CrewAI-powered multi-agent recruitment platform using GROQ API for fast inference. This single-file application includes three AI agents: 1. Resume Parser Agent - Extracts structured data from resumes 2. Skill Matcher Agent - Matches skills to job descriptions 3. Ranking Agent - Ranks candidates based on relevance Author: AI Developer License: MIT """ import streamlit as st import tempfile import os import json import pandas as pd from pathlib import Path from typing import Dict, List, Any import warnings import time import asyncio from datetime import datetime, timedelta # Suppress warnings warnings.filterwarnings("ignore") # Core imports for AI agents try: from crewai import Agent, Task, Crew, LLM from crewai.project import CrewBase, agent, crew, task from groq import Groq import spacy from spacy.matcher import PhraseMatcher import PyPDF2 import docx2txt import re import hashlib except ImportError as e: st.error(f"Missing required dependency: {e}") st.stop() # Configuration - Single model only GROQ_MODEL = "llama-3.1-8b-instant" MODEL_DISPLAY_NAME = "Llama 3.1 8B (Fastest)" # Rate limiting configuration RATE_LIMIT_DELAY = 3 # seconds between requests MAX_RETRIES = 3 BATCH_SIZE = 2 # Process resumes in smaller batches # Initialize session state if 'processed_resumes' not in st.session_state: st.session_state.processed_resumes = [] if 'ranked_candidates' not in st.session_state: st.session_state.ranked_candidates = [] if 'job_description' not in st.session_state: st.session_state.job_description = "" class RateLimitHandler: """Handle rate limiting for API calls""" def __init__(self, delay=RATE_LIMIT_DELAY): self.delay = delay self.last_request_time = 0 def wait_if_needed(self): """Wait if necessary to respect rate limits""" current_time = time.time() time_since_last_request = current_time - self.last_request_time if time_since_last_request < self.delay: sleep_time = self.delay - time_since_last_request st.info(f"âŗ Rate limit protection: waiting {sleep_time:.1f}s...") time.sleep(sleep_time) self.last_request_time = time.time() class ResumeProcessor: """Utility class for processing resume files""" @staticmethod def extract_text_from_pdf(file_buffer) -> str: """Extract text from PDF file""" try: pdf_reader = PyPDF2.PdfReader(file_buffer) text = "" for page in pdf_reader.pages: text += page.extract_text() + "\n" return text.strip() except Exception as e: st.error(f"Error reading PDF: {e}") return "" @staticmethod def extract_text_from_docx(file_buffer) -> str: """Extract text from DOCX file""" try: with tempfile.NamedTemporaryFile(delete=False, suffix='.docx') as tmp_file: tmp_file.write(file_buffer.read()) tmp_file.flush() text = docx2txt.process(tmp_file.name) os.unlink(tmp_file.name) return text.strip() except Exception as e: st.error(f"Error reading DOCX: {e}") return "" @staticmethod def extract_text_from_txt(file_buffer) -> str: """Extract text from TXT file""" try: return file_buffer.read().decode('utf-8').strip() except Exception as e: st.error(f"Error reading TXT: {e}") return "" class HireWithAICrew: """Main CrewAI multi-agent system for resume screening with rate limiting""" def __init__(self, groq_api_key: str): """Initialize the crew with GROQ API and rate limiting""" self.llm = LLM( model=f"groq/{GROQ_MODEL}", api_key=groq_api_key, temperature=0.1 ) self.rate_limiter = RateLimitHandler() # Initialize spaCy for NLP operations try: self.nlp = spacy.load("en_core_web_sm") except OSError: st.error("spaCy English model not found. Please install it with: python -m spacy download en_core_web_sm") st.stop() def create_resume_parser_agent(self) -> Agent: """Create the Resume Parser Agent""" return Agent( role='Resume Parser Specialist', goal='Extract key candidate information from resume text efficiently', backstory="""You are an expert resume parser focused on extracting essential candidate information quickly and accurately. You prioritize the most important details: name, contact info, skills, experience, and education.""", llm=self.llm, verbose=True, allow_delegation=False ) def create_skill_matcher_agent(self) -> Agent: """Create the Skill Matcher Agent""" return Agent( role='Skill Matching Expert', goal='Efficiently match candidate skills with job requirements', backstory="""You are a skilled matcher who quickly identifies relevant skills and calculates match percentages. You focus on the most critical skills and provide concise, actionable insights.""", llm=self.llm, verbose=True, allow_delegation=False ) def create_ranking_agent(self) -> Agent: """Create the Ranking Agent""" return Agent( role='Candidate Ranking Analyst', goal='Rank candidates efficiently based on key criteria', backstory="""You are a recruitment analyst who creates fast, accurate candidate rankings. You focus on the most important factors: skills match, experience relevance, and overall job fit.""", llm=self.llm, verbose=True, allow_delegation=False ) def create_concise_parsing_task(self, resume_text: str, filename: str) -> Task: """Create a more concise parsing task to reduce token usage""" # Truncate resume text if too long to save tokens max_chars = 2000 truncated_text = resume_text[:max_chars] + "..." if len(resume_text) > max_chars else resume_text return Task( description=f""" Extract key information from this resume in JSON format: Resume: {filename} Text: {truncated_text} Extract: 1. Name and contact (email, phone) 2. Key skills (top 5-8 most relevant) 3. Experience summary (years, key roles) 4. Education (degree, field) 5. Notable achievements Keep response concise and structured. """, expected_output="Concise JSON with essential candidate information", agent=self.create_resume_parser_agent() ) def create_concise_skill_matching_task(self, resume_data: str, job_description: str) -> Task: """Create a more concise skill matching task""" # Truncate job description if too long max_jd_chars = 1000 truncated_jd = job_description[:max_jd_chars] + "..." if len(job_description) > max_jd_chars else job_description return Task( description=f""" Analyze skill match between candidate and job: Job Requirements: {truncated_jd} Candidate Data: {resume_data} Provide: 1. Match percentage (0-100%) 2. Top 5 matching skills 3. Top 3 missing critical skills 4. Experience level fit (1-10) Keep analysis concise and focused. """, expected_output="Concise skill matching analysis in JSON format", agent=self.create_skill_matcher_agent() ) def safe_crew_execution(self, crew, max_retries=MAX_RETRIES): """Execute crew with retry logic for rate limits""" for attempt in range(max_retries): try: self.rate_limiter.wait_if_needed() result = crew.kickoff() return result except Exception as e: error_str = str(e).lower() if "rate limit" in error_str or "ratelimit" in error_str: if attempt < max_retries - 1: wait_time = (attempt + 1) * 5 # Progressive backoff st.warning(f"Rate limit hit. Retrying in {wait_time}s... (Attempt {attempt + 1}/{max_retries})") time.sleep(wait_time) continue else: st.error("Maximum retries reached. Please try again later or upgrade your Groq plan.") return None else: st.error(f"Error during processing: {e}") return None return None def process_resumes_with_batching(self, resumes_data: List[Dict], job_description: str) -> Dict: """Process resumes in smaller batches to avoid rate limits""" try: all_parsed_resumes = [] all_skill_analysis = [] # Process resumes in batches total_resumes = len(resumes_data) batches = [resumes_data[i:i + BATCH_SIZE] for i in range(0, total_resumes, BATCH_SIZE)] progress_bar = st.progress(0) progress_text = st.empty() for batch_idx, batch in enumerate(batches): progress_text.text(f"Processing batch {batch_idx + 1} of {len(batches)}...") # Step 1: Parse resumes in current batch for resume_idx, resume_data in enumerate(batch): overall_progress = (batch_idx * BATCH_SIZE + resume_idx) / total_resumes progress_bar.progress(overall_progress) parsing_task = self.create_concise_parsing_task( resume_data['text'], resume_data['filename'] ) parsing_crew = Crew( agents=[self.create_resume_parser_agent()], tasks=[parsing_task], verbose=False # Reduce verbosity to save tokens ) result = self.safe_crew_execution(parsing_crew) if result: all_parsed_resumes.append({ 'filename': resume_data['filename'], 'parsed_data': result.raw, 'original_text': resume_data['text'][:500] # Store only first 500 chars }) # Step 2: Skill matching for current batch for resume in all_parsed_resumes[-len(batch):]: # Only process newly added resumes skill_task = self.create_concise_skill_matching_task( resume['parsed_data'], job_description ) skill_crew = Crew( agents=[self.create_skill_matcher_agent()], tasks=[skill_task], verbose=False ) result = self.safe_crew_execution(skill_crew) if result: all_skill_analysis.append({ 'filename': resume['filename'], 'skill_analysis': result.raw, 'parsed_data': resume['parsed_data'] }) progress_bar.progress(1.0) progress_text.text("Finalizing rankings...") # Step 3: Final ranking (only if we have successful analyses) if all_skill_analysis: # Create a more concise ranking task ranking_task = Task( description=f""" Rank these candidates for the job. Provide top 5 ranked candidates with scores. Job: {job_description[:500]}... Candidates: {json.dumps([sa['skill_analysis'] for sa in all_skill_analysis[:5]], indent=1)} Provide concise ranking with: 1. Candidate name and rank 2. Overall score (0-100) 3. Key strengths (2-3 points) 4. Brief recommendation """, expected_output="Concise candidate ranking with top recommendations", agent=self.create_ranking_agent() ) ranking_crew = Crew( agents=[self.create_ranking_agent()], tasks=[ranking_task], verbose=False ) ranking_result = self.safe_crew_execution(ranking_crew) final_ranking = ranking_result.raw if ranking_result else "Ranking failed due to rate limits" else: final_ranking = "No candidates could be analyzed due to rate limits" return { 'parsed_resumes': all_parsed_resumes, 'skill_analysis': all_skill_analysis, 'final_ranking': final_ranking } except Exception as e: st.error(f"Error processing resumes: {e}") return {} def main(): """Main Streamlit application""" # Page config st.set_page_config( page_title="HireWithAI - Smart Resume Screening", page_icon="đ¤", layout="wide", initial_sidebar_state="expanded" ) # Custom CSS st.markdown(""" """, unsafe_allow_html=True) # Header st.markdown("""
AI-Powered Multi-Agent Recruitment Platform
Reduce 70% of time-to-hire with automated resume screening and ranking
đ¤ HireWithAI - Powered by CrewAI & GROQ API
Using {MODEL_DISPLAY_NAME} with Rate Limit Protection