File size: 7,873 Bytes
f3c687b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from dotenv import load_dotenv

load_dotenv()
import base64
import streamlit as st
import os
import io
import textwrap
import tempfile
from PIL import Image
import fitz
import google.generativeai as genai
from prompts import (
    input_prompt1,
    input_prompt2,
    input_prompt3,
    input_prompt4,
    input_prompt5,
    input_prompt6,
    input_prompt7,
    input_prompt8,
)

genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))


def extract_field_from_description(description):
    return input_role


def construct_prompt(template, field):
    return template.replace("[User-Specified Field]", field)


def to_markdown(text):
    text = text.replace("•", "  *")
    return textwrap.indent(text, "> ", predicate=lambda _: True)


def get_gemini_response(input, pdf_content, prompt):
    model = genai.GenerativeModel("gemini-pro-vision")
    response = model.generate_content([input, pdf_content[0], prompt])
    #   return response.text
    return to_markdown(response.text)


def input_pdf_setup(uploaded_file):
    if uploaded_file is not None:
        file_content = uploaded_file.getvalue()
        if file_content:
            try:
                doc = fitz.open(stream=file_content, filetype="pdf")
                if len(doc) > 0:
                    page = doc.load_page(0)  # first page
                    pix = page.get_pixmap()
                    img_byte_arr = pix.tobytes("png")  # get bytes directly

                    pdf_parts = [
                        {
                            "mime_type": "image/png",
                            "data": base64.b64encode(img_byte_arr).decode(),
                        }
                    ]
                    return pdf_parts
                else:
                    raise ValueError("PDF does not contain any pages.")
            except Exception as e:
                st.error(f"Error processing PDF: {e}")
                raise e
            finally:
                doc.close()
        else:
            st.error("Uploaded file is empty or invalid.")
            raise ValueError("Uploaded file is empty or invalid.")
    else:
        st.error("No file uploaded.")
        raise FileNotFoundError("No file uploaded")


# Streamlit App

st.set_page_config(
    page_title="ATS Resume Expert",
    page_icon="🔥",
)
st.header("ATS Friendly Resume and Analysis")
input_name = st.text_input("Enter your name: ", key="name", placeholder="John Doe")
input_role = st.text_input(
    "Role you are applying for: ", key="role", placeholder="Software Engineer"
)

input_text = st.text_area(
    "Job Description: ", key="input", placeholder="Paste the job description here..."
)
uploaded_file = st.file_uploader("Upload your resume(PDF)", type=["pdf"])


if "input_text" not in st.session_state:
    st.session_state["input_text"] = input_text

if "input_name" not in st.session_state:
    st.session_state["input_name"] = input_name

if "input_role" not in st.session_state:
    st.session_state["input_role"] = input_role

if "input_text" not in st.session_state:
    st.session_state["input_text"] = input_text

if "uploaded_file" not in st.session_state:
    st.session_state["uploaded_file"] = uploaded_file


if uploaded_file is not None:
    st.write("PDF uploaded successfully!")

    # Prompts here

field = extract_field_from_description(input_text)


dynamic_prompt1 = construct_prompt(input_prompt1, field)
dynamic_prompt2 = construct_prompt(input_prompt2, field)
dynamic_prompt3 = construct_prompt(input_prompt3, field)
dynamic_prompt4 = construct_prompt(input_prompt4, field)
dynamic_prompt5 = construct_prompt(input_prompt5, field)
dynamic_prompt6 = construct_prompt(input_prompt6, field)
dynamic_prompt7 = construct_prompt(input_prompt7, field)
dynamic_prompt8 = construct_prompt(input_prompt8, field)
# Check if both the job description and the PDF file are provided
if input_text and uploaded_file:
    try:
        pdf_content = input_pdf_setup(uploaded_file)
        print(pdf_content)
        is_pdf_valid = True

    except Exception as e:
        st.error(f"Error processing the PDF file: {e}")
        is_pdf_valid = False

    if is_pdf_valid:
        # Process for each tab if PDF is valid
        field = extract_field_from_description(input_text)

        st.subheader("Name : " + input_name)
        st.subheader("Role : " + input_role)

        tab1, tab2, tab3, tab4, tab5, tab6, tab7, tab8 = st.tabs(
            [
                "HR/Human Resume Analysis",
                "ATS/AI Resume Analysis",
                "Skills Review",
                "ATS Friendly Resume",
                "Cover Letter",
                "LinkedIn",
                "Interview Questions",
                "Similar Companies",
            ]
        )

        with tab1:
            st.header("HR/Human - Detailed Resume Analysis")
            if uploaded_file is not None:
                # pdf_content = input_pdf_setup(uploaded_file)
                response = get_gemini_response(dynamic_prompt1, pdf_content, input_text)
                st.write(response)
            else:
                st.write("Please upload the resume")

        with tab2:
            st.header("ATS/AI - Detailed Resume Analysis")
            if uploaded_file is not None:
                # pdf_content = input_pdf_setup(uploaded_file)
                response = get_gemini_response(dynamic_prompt2, pdf_content, input_text)
                st.write(response)
            else:
                st.write("Please upload the resume")

        with tab3:
            st.header("Skills Recommended for the Role")
            if uploaded_file is not None:
                # pdf_content = input_pdf_setup(uploaded_file)
                response = get_gemini_response(dynamic_prompt3, pdf_content, input_text)
                st.write(response)
            else:
                st.write("Please upload the resume")
        with tab4:
            st.header("ATS Friendly and optimized Resume")
            if uploaded_file is not None:
                # pdf_content = input_pdf_setup(uploaded_file)
                response = get_gemini_response(dynamic_prompt4, pdf_content, input_text)
                st.write(response)
            else:
                st.write("Please upload the resume")
        with tab5:
            st.header("Cover Letter for your resume")
            if uploaded_file is not None:
                # pdf_content = input_pdf_setup(uploaded_file)
                response = get_gemini_response(dynamic_prompt5, pdf_content, input_text)
                st.write(response)
            else:
                st.write("Please upload the resume")
        with tab6:
            st.header("LinkedIn Profile Recommendations")
            if uploaded_file is not None:
                # pdf_content = input_pdf_setup(uploaded_file)
                response = get_gemini_response(dynamic_prompt6, pdf_content, input_text)
                st.write(response)
            else:
                st.write("Please upload the resume")
        with tab7:
            st.header("Interview Questions and Questions to ask the interviewer")
            if uploaded_file is not None:
                # pdf_content = input_pdf_setup(uploaded_file)
                response = get_gemini_response(dynamic_prompt7, pdf_content, input_text)
                st.write(response)
            else:
                st.write("Please upload the resume")
        with tab8:
            st.header("Similar companies you can apply for")
            if uploaded_file is not None:
                # pdf_content = input_pdf_setup(uploaded_file)
                response = get_gemini_response(dynamic_prompt8, pdf_content, input_text)
                st.write(response)
            else:
                st.write("Please upload the resume")

else:
    st.write("Please provide the job description and upload the resume PDF.")