File size: 14,453 Bytes
b862851
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2de4126
b862851
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e9e0183
b862851
 
 
 
 
 
 
 
 
 
 
1d07404
 
 
b862851
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
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)