File size: 11,490 Bytes
8f1528c
 
 
 
 
 
db77ec0
 
 
8f1528c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ce5b387
 
b9fc327
8f1528c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ce5b387
8f1528c
 
 
 
 
 
 
 
 
 
ce5b387
 
8f1528c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ce5b387
8f1528c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69b7570
 
8f1528c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b9fc327
8f1528c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
from langchain.chains import create_history_aware_retriever, create_retrieval_chain
from langchain.chains.combine_documents import create_stuff_documents_chain

from langchain_core.prompts import ChatPromptTemplate
from langchain_groq import ChatGroq
from langchain_google_genai import ChatGoogleGenerativeAI
from langchain_openai import ChatOpenAI


from langchain_huggingface import HuggingFaceEmbeddings
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_community.document_loaders import PyPDFLoader
from langchain.vectorstores import FAISS
import os
from dotenv import load_dotenv
import google.generativeai as genai
import pandas as pd
import json
from io import BytesIO
import tempfile


# Load environment variables
load_dotenv()



# CSS for background and alignment
# CSS for background with reduced transparency


# Set up embeddings
HF_TOKEN = os.getenv('HF_TOKEN')
embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")

# API keys
api_key = os.getenv('API_KEY')

llm = ChatGroq(groq_api_key=api_key, model_name="gemma2-9b-it")
llm_2 = ChatGroq(groq_api_key=api_key, model_name="gemma-7b-it")
   
#st.sidebar.title("Select AI Model")

# model_name = st.sidebar.selectbox("Select AI Model", [
#     "gemma-7b-it",
#     "gemma2-9b-it",
#     "llama-3.1-70b-versatile",
#     "llama-3.1-8b-instant",
#     "llama-3.2-1b-preview",
#     "llama-3.2-3b-preview",
#     "llama-3.2-90b-text-preview",
#     "llama-3.2-90b-vision-preview",
#     "llama-guard-3-8b",
#     "llama3-70b-8192",
#     "llama3-8b-8192",
#     "llama3-groq-70b-8192-tool-use-preview",
#     "llama3-groq-8b-8192-tool-use-preview",
#     "llava-v1.5-7b-4096-preview",
#     "mixtral-8x7b-32768"
# ])

st.title("πŸ•΅οΈ Recruitment AI πŸ€–")


def create_job_post(job_title, location, exp,salary_range,other_input):
    prompt = (
        f"Create a job opening post for platforms like Internshala, LinkedIn, and Naukri.com. "
        f"The post should include the job title: {job_title}, location: {location}, and required experience: {exp},salart range:{salary_range} and other input:{other_input}. "
        f"Make it attractive, include skills (if possible but the skills in boxes and highlight them) required, who can apply, benefits, and other necessary details. "
        f"The post should be 100-200 words."
    )

    try:
        # Replace 'model.generate_content' with the actual method to generate content
        response = llm_2.invoke(prompt)  # assuming 'model' is defined elsewhere
        return response.content
    except Exception as e:
        return f"Error generating response: {e}"



with st.form("job_post_form"):
    st.write("πŸ“ Fill in the details to create a job post ✍🏼 :")

    left, right = st.columns(2)
    with left:
        # Job title, location, and experience input fields
        job_title = st.text_input("Enter the job title you are looking for:")
        location = st.text_input("Enter the location you are looking for:")
    with right:
        exp = st.text_input("Enter the experience you are looking for:")
        salary_range = st.text_input("Enter the Salary range you are looking for:")

    other_input = st.text_input("Enter any other details you want to add in the job post:")


    # Form submission button
    submitted = st.form_submit_button("Create Job Post βœ…")

    if submitted:
        if job_title and location and exp:
            job_post = create_job_post(job_title, location, exp, salary_range, other_input)
            st.write("### Generated Job Post:")
            st.write(job_post)
        else:
            st.warning("Please fill in all required fields to generate the job post.")

job_post = create_job_post(job_title, location, exp, salary_range,other_input)




# Resume scoring section





jd=st.file_uploader("πŸ“ Upload Job Description πŸ“œ", type="pdf")
def get_jd(jd):
    try:
        # Use a temporary file to save the uploaded file
        with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as temp_pdf:
            temp_pdf.write(jd.getvalue())
            temp_pdf_path = temp_pdf.name

        # Load the PDF using PyPDFLoader
        loader = PyPDFLoader(temp_pdf_path)
        docs = loader.load()

    finally:
        # Remove the temporary file after processing
        if os.path.exists(temp_pdf_path):
            os.remove(temp_pdf_path)

    # Text splitting for embeddings
    text_splitter = RecursiveCharacterTextSplitter(chunk_size=5000, chunk_overlap=500)
    splits = text_splitter.split_documents(docs)

    # Create FAISS vectorstore for retrieval
    vectorstore = FAISS.from_documents(splits, embeddings)
    retriever = vectorstore.as_retriever()

    # Define prompt and QA chain
    system_prompt = (
        f"Extract the Job description from the uploaded file in proper format. Also keep the value of the Job Location in mind as it will be required Later to match with resumes "
    )

    qa_prompt = ChatPromptTemplate.from_messages(
        [
            ("system", system_prompt),
            ("human", "{context}\n{input}"),
        ]
    )

    # Create question-answering chain
    question_answer_chain = create_stuff_documents_chain(llm_2,qa_prompt)
    rag_chain = create_retrieval_chain(retriever, question_answer_chain)

    try:
        # Retrieve the job description using the chain
        response = rag_chain.invoke({
            "input": "Describe the job description in proper format"
        })
        job_description = response["answer"]

        return job_description

    except Exception as e:
        raise RuntimeError(f"Error retrieving job description: {e}")

if jd:
    job_description = get_jd(jd)
else:
    job_description=None


    # File uploader for PDF resumes






def process_pdfs_in_batches(files):
    batch_size = 4
    num_batches = (len(files) // batch_size) + (1 if len(files) % batch_size != 0 else 0)
    all_json_data = []

    for i in range(num_batches):
        batch = files[i * batch_size: (i + 1) * batch_size]  # Select a batch of files
        documents = []  # List to hold all document contents

        for j, uploaded_file in enumerate(batch):
            try:
                # Use a temporary file to save the uploaded file
                with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as temp_pdf:
                    temp_pdf.write(uploaded_file.getvalue())
                    temp_pdf_path = temp_pdf.name

                # Load the PDF using PyPDFLoader
                loader = PyPDFLoader(temp_pdf_path)
                docs = loader.load()
                documents.extend(docs)

            finally:
                # Remove the temporary file after processing
                if os.path.exists(temp_pdf_path):
                    os.remove(temp_pdf_path)

        # Text splitting for embeddings
        text_splitter = RecursiveCharacterTextSplitter(chunk_size=5000, chunk_overlap=500)
        splits = text_splitter.split_documents(documents)

        # Create FAISS vectorstore for retrieval
        vectorstore = FAISS.from_documents(splits, embeddings)
        retriever = vectorstore.as_retriever()

        # Define prompt and QA chain
        system_prompt = (
            f"You are a smart AI agent tasked with evaluating resumes against the job description: "
            f"Job Title: {job_title}, Location: {location}, Experience: {exp}. "
            f"Your evaluation should provide a score (0-100) for each resume based on skills, experience, and other factors. "
            f"Extract the following details from each uploaded PDF: Name, Contact Number, Email,Address and the calculated Score. "
            "Output must be a JSON array of dictionaries, where each dictionary has the keys: 'Name', 'Contact Number', 'Email', 'Address' and 'Score' "
            "Generate JSON data for, but present it as plain text in the response. Do not use a code block or any structured formatting like boxes."
            "Do not include extra line breaks or whitespace outside the JSON array. The JSON must start with [ and end with ]"
            f"if the Address of the candidate is too far (like more than 100 kms) away from {location} or the job location mentioned in the {job_description}, then the score should be 0."
        )

        qa_prompt = ChatPromptTemplate.from_messages(
            [
                ("system", system_prompt),
                ("human", "{context}\n{input}"),
            ]
        )

        # Create question-answering chain
        question_answer_chain = create_stuff_documents_chain(llm_2, qa_prompt)
        rag_chain = create_retrieval_chain(retriever, question_answer_chain)

        try:
            # Button for scoring resumes
            response = rag_chain.invoke({
                "input": (
                    "You are a smart AI agent tasked with evaluating resumes based on a job description. "
                    "Strictly follow the instructions below to generate the output:\n\n"
                    "1. Extract the following details from each resume: Name, Contact Number, Email, Address, and Score.\n"
                    "2. Output the data as a JSON array of dictionaries. Each dictionary must contain these keys: "
                    "'Name', 'Contact Number', 'Email', 'Address', and 'Score'.\n"
                    "3. The output must be plain text JSON without any extra formatting, syntax highlighting, or visual artifacts. "
                    "Do not include explanations, metadata, or anything other than the JSON array.\n"
                    "4. Ensure the JSON is well-formed and matches the number of resumes exactly.\n\n"
                    "Generate only the JSON array in text format, ensuring it adheres to this structure."
                    "Generate JSON data for, but present it as plain text in the response. Do not use a code block or any structured formatting like boxes."
                    f"if the Address of the candidate is too far (like more than 100 kms) away from {location} or the job location mentioned in the {job_description}, then the score should be 0.but dont include comparison in the output"
                )
            })


            json_data = json.loads(response["answer"])

            # Append the JSON data to the all_json_data list

            all_json_data.extend(json_data)
            sorted_data = sorted(all_json_data, key=lambda x: x["Score"], reverse=True)

            #st.write(json_data)

        except Exception as e:
            st.error(f"Error: {e}")

    # Once all batches are processed, you can use all_json_data as needed
    # For example, converting it into a DataFrame and displaying
    df = pd.DataFrame(sorted_data)

    st.dataframe(
        df.style
        # Highlight min values
        .set_table_styles([
            {'selector': 'thead th', 'props': [('background-color', '#4CAF50'), ('color', 'white')]},
            # Table header style
            {'selector': 'tbody td', 'props': [('border', '1px solid #ddd'), ('padding', '8px')]}  # Table body style
        ])
    )


st.write("πŸ“‚ Upload the resumes to score them πŸ“‘:")

uploaded_files = st.file_uploader("Choose PDF files", type="pdf", accept_multiple_files=True)

if st.button("Score Resumesβœ”οΈ"):
    if uploaded_files:
        process_pdfs_in_batches(uploaded_files)
    else:
        st.warning("Please upload files to score.")


# Call the batch processing function if files are uploaded