Snigdhapaul2003's picture
Update app.py
1d07404 verified
import groq
from jobspy import scrape_jobs
import pandas as pd
import json
from typing import List, Dict
import numpy as np
import time
import tempfile
def make_clickable(url):
return f"{url}"
def convert_prompt_to_parameters(client, prompt: str, location_pref) -> Dict[str, str]:
"""
Convert user input prompt to structured job search parameters using AI.
Args:
client: Groq AI client
prompt (str): User's job search description
Returns:
Dict[str, str]: Extracted search parameters with search_term and location
"""
system_prompt = f"""
You are a language decoder. Extract:
- search_term: job role/keywords (expand abbreviations)
- location: mentioned place or "{location_pref}"
Return only: [{{"search_term": "term", "location": "location"}}]
"""
response = client.chat.completions.create(
messages=[
{"role": "system", "content": system_prompt},
{"role": "user", "content": f"Extract from: {prompt}"}
],
max_tokens=1024,
model='llama3-8b-8192',
temperature=0.2
)
print("response: ", response.choices[0].message.content)
try:
print("*****************************************")
content = response.choices[0].message.content.strip()
# Find the first and last occurrence of JSON brackets
json_start = content.find("[")
json_end = content.rfind("]") + 1 # +1 to include the last bracket
# Ensure valid JSON presence
if json_start == -1 or json_end == -1:
raise ValueError("No valid JSON array found in LLM response.")
# Extract the JSON portion and parse it
json_str = content[json_start:json_end]
return json.loads(json_str)
except json.JSONDecodeError:
return {"search_term": prompt, "location": location_pref}
def get_job_data(search_params: Dict[str, str]) -> pd.DataFrame:
"""
Fetch job listings from multiple sources based on search parameters.
Args:
search_params (Dict[str, str]): Search parameters including term and location
Returns:
pd.DataFrame: Scraped job listings
"""
print("location: ", search_params["location"])
try:
return scrape_jobs(
site_name=["indeed", "linkedin", "glassdoor", "google"],
search_term=search_params["search_term"],
google_search_term=search_params["search_term"] + " jobs near " + search_params["location"],
location=search_params["location"],
results_wanted=60,
hours_old=72,
country_indeed=search_params["location"],
linkedin_fetch_description=True
)
except Exception as e:
return pd.DataFrame()
import json
import time
import pandas as pd
from typing import List, Dict
# Define weights (Adjust these as needed)
WEIGHTS = {
"qualification_match": 0.3,
"skills_match": 0.3,
"experience_match": 0.2,
"salary_match": 0.2
}
def analyze_job_batch(
client,
resume_json: Dict,
jobs_batch: List[Dict],
start_index: int,
retry_count: int = 0
) -> pd.DataFrame:
"""
Analyze a batch of jobs against the resume with retry logic.
Args:
client: Groq AI client
resume_json (Dict): Resume details in JSON format
jobs_batch (List[Dict]): Batch of job listings
start_index (int): Starting index of the batch
retry_count (int, optional): Number of retry attempts. Defaults to 0.
Returns:
pd.DataFrame: Job match analysis results with separate scores for qualification, skills, and experience.
"""
if retry_count >= 3:
return pd.DataFrame()
system_prompt = """
Rate resume-job matches based on qualifications, skills, and experience separately.
Ensure the candidate's experience level aligns with the job seniority (e.g., do not recommend senior-level jobs to freshers).
Consider job requirements, candidate qualifications, relevant skills, and years of experience.
Consider if the salary of the job matches the candidate's salary expectations. If the salary of the job is more than the expectation it is good. If no salary is mentioned, estimate it based on the job description and find the match.
Return only a JSON array:
[{"job_index": number, "qualification_match": 0-100, "skills_match": 0-100, "experience_match": 0-100, "qualification_reason": "brief reason based on qualifications", "skill_reason": "brief reason based on skill", "experience_reason": "brief reason based on experience", "salary_match": 0-100}]
"""
jobs_info = [
{
'index': idx + start_index,
'title': job['title'],
'desc': job.get('description', ''),
}
for idx, job in enumerate(jobs_batch)
]
analysis_prompt = f"""
Resume Details: {json.dumps(resume_json)}
Jobs: {json.dumps(jobs_info)}
"""
try:
response = client.generate_content([system_prompt, analysis_prompt])
print("Response overall: ", response)
print("Response: ", response.text)
content = response.text.strip()
# Find the first and last occurrence of JSON brackets
json_start = content.find("[")
json_end = content.rfind("]") + 1 # +1 to include the last bracket
# Ensure valid JSON presence
if json_start == -1 or json_end == -1:
raise ValueError("No valid JSON array found in LLM response.")
# Extract the JSON portion and parse it
json_str = content[json_start:json_end]
matches = json.loads(json_str)
# Compute match_score using weighted sum
for match in matches:
match["match_score"] = (
match["qualification_match"] * WEIGHTS["qualification_match"] +
match["skills_match"] * WEIGHTS["skills_match"] +
match["experience_match"] * WEIGHTS["experience_match"]+
match["salary_match"] * WEIGHTS["salary_match"]
)
# Convert to DataFrame and print
return pd.DataFrame(matches)
except Exception as e:
print(f"Error in analyze_job_batch: {str(e)}")
if retry_count < 3:
time.sleep(2)
return analyze_job_batch(client, resume_json, jobs_batch, start_index, retry_count + 1)
print(f"Batch {start_index} failed after retries: {str(e)}")
return pd.DataFrame()
def analyze_jobs_in_batches(
client,
resume: str,
jobs_df: pd.DataFrame,
batch_size: int = 3
) -> pd.DataFrame:
"""
Process job listings in batches and analyze match with resume.
Args:
client: Groq AI client
resume (str): Resume text
jobs_df (pd.DataFrame): DataFrame of job listings
batch_size (int, optional): Number of jobs to process in each batch. Defaults to 3.
Returns:
pd.DataFrame: Sorted job matches by match score
"""
all_matches = []
jobs_dict = jobs_df.to_dict('records')
print("jobs_dict: ", jobs_dict)
for i in range(0, len(jobs_dict), batch_size):
batch = jobs_dict[i:i + batch_size]
print("batch done", i)
batch_matches = analyze_job_batch(client, resume, batch, i)
print("batch matches", batch_matches)
if not batch_matches.empty:
all_matches.append(batch_matches)
# time.sleep(1) # Rate limiting
if all_matches:
final_matches = pd.concat(all_matches, ignore_index=True)
return final_matches.sort_values('match_score', ascending=False)
return pd.DataFrame()
import gradio as gr
import pandas as pd
import fitz
import os
from groq import Groq
import google.generativeai as genai
def extract_text_from_pdf(pdf_path):
"""Extracts text from a PDF file."""
text = ""
pdf_document = fitz.open(pdf_path)
for page in pdf_document:
text += page.get_text("text") + "\n"
pdf_document.close()
return text
def process_resume(file, job_description, location_pref, experience_years, yearly_salary_expectation):
if not file or not file.name.endswith('.pdf'):
return "Invalid file. Please upload a PDF.", None
resume_text = extract_text_from_pdf(file.name)
api_key = os.getenv("Groq_api_key") # Replace with actual API key
client = Groq(api_key=api_key)
response = client.chat.completions.create(
model="llama3-70b-8192",
messages=[
{"role": "system", "content": "You are an AI that extracts structured data from resumes."},
{"role": "user", "content": f"Convert the following resume text into a structured JSON format inside third bracket where each section is dynamically detected:\n\n{resume_text}, add years of experience as {experience_years}, salary expectation as {yearly_salary_expectation}"}
]
)
structured_data = response.choices[0].message.content.strip()
gemini_api_key = os.getenv("Gemini_api_key")
genai.configure(api_key=gemini_api_key)
model = genai.GenerativeModel("gemini-1.5-flash-latest")
client = Groq(api_key=api_key)
processed_params = convert_prompt_to_parameters(client, job_description, location_pref)
jobs_data = get_job_data(processed_params[0])
if not jobs_data.empty:
data = pd.DataFrame(jobs_data)
data = data[data['description'].notna()].reset_index(drop=True)
matches_df = analyze_jobs_in_batches(model, structured_data, data, batch_size=30)
print(matches_df)
# if not matches_df.empty:
# matched_jobs = data.iloc[matches_df['job_index']].copy()
# matched_jobs['match_score'] = matches_df['match_score']
# csv_path = "Job_list.csv"
# matched_jobs.to_csv(csv_path, index=False)
# return "Job matches found! Download the file below.", csv_path
# else:
# return "No suitable job matches found.", None
if not matches_df.empty:
matched_jobs = data.iloc[matches_df['job_index']].copy()
matched_jobs['match_score'] = matches_df['match_score']
matched_jobs["qualification_match"] = matches_df["qualification_match"]
matched_jobs["skills_match"] = matches_df["skills_match"]
matched_jobs["salary_match"] = matches_df["salary_match"]
matched_jobs["experience_match"] = matches_df["experience_match"]
matched_jobs["qualification_reason"] = matches_df["qualification_reason"]
matched_jobs["skill_reason"] = matches_df["skill_reason"]
matched_jobs["experience_reason"] = matches_df["experience_reason"]
print(f"Found {len(matched_jobs)} recommended matches!")
display_cols = ['site', 'job_url', 'title', 'company', 'location', 'match_score', 'qualification_match', 'qualification_reason', 'skills_match', 'skill_reason', 'experience_match', 'experience_reason', 'salary_match']
display_df = matched_jobs[matched_jobs['match_score'] > 50][display_cols].sort_values('match_score', ascending=False)
display_df['job_url'] = display_df['job_url'].apply(make_clickable)
print(display_df.to_string(index=False))
# csv_path = "Job_list.csv"
# display_df.to_csv(csv_path, index=False)
# return "βœ… Job matches found! Download your personalized job list below.", csv_path
# Create a temporary CSV file
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".csv", mode='w', newline='', encoding='utf-8')
display_df.to_csv(temp_file.name, index=False)
temp_file.close() # Close the file so it can be accessed later
return "βœ… Job matches found! Download your personalized job list below.", temp_file.name
else:
return "⚠️ No suitable job matches found.", None
else:
return "❌ No jobs found with the given parameters.", None
def gradio_interface():
with gr.Blocks() as demo:
gr.Markdown("""
# πŸš€ Smart Job Search with AI Matching
### Upload your resume and get AI-powered job recommendations!
""")
with gr.Row():
gr.Markdown("# πŸ“‚ Upload Resume")
with gr.Row():
resume_input = gr.File(label="Upload Resume (PDF)")
with gr.Row():
gr.Markdown("### πŸ“ Job Preferences")
with gr.Row():
# gr.Markdown("### πŸ“ Job Preferences")
job_desc_input = gr.Textbox(label="Job Role", placeholder="Enter desired job role")
# location_input = gr.Textbox(label="Preferred Location", placeholder="Enter location")
location_input = gr.Dropdown(
label="Preferred Location",
choices=[
"Argentina", "Australia", "Austria", "Bahrain", "Belgium", "Brazil", "Canada", "Chile",
"China", "Colombia", "Costa Rica", "Czech Republic", "Denmark", "Ecuador", "Egypt", "Finland",
"France", "Germany", "Greece", "Hong Kong", "Hungary", "India", "Indonesia", "Ireland",
"Israel", "Italy", "Japan", "Kuwait", "Luxembourg", "Malaysia", "Mexico", "Morocco",
"Netherlands", "New Zealand", "Nigeria", "Norway", "Oman", "Pakistan", "Panama", "Peru",
"Philippines", "Poland", "Portugal", "Qatar", "Romania", "Saudi Arabia", "Singapore",
"South Africa", "South Korea", "Spain", "Sweden", "Switzerland", "Taiwan", "Thailand",
"Turkey", "Ukraine", "United Arab Emirates", "UK", "USA", "Uruguay", "Venezuela", "Vietnam"
],
value=None
)
experience_input = gr.Number(label="Years of Experience", value=0)
salary_input = gr.Number(label="Expected Salary (Yearly)", value=100000)
submit_btn = gr.Button("πŸ” Find Jobs", elem_classes="primary-button")
output_text = gr.Textbox(label="Result", interactive=False)
download_link = gr.File(label="πŸ“₯ Download Job List")
submit_btn.click(
process_resume,
inputs=[resume_input, job_desc_input, location_input, experience_input, salary_input],
outputs=[output_text, download_link]
)
return demo
demo = gradio_interface()
demo.launch(debug=True, share=True)