File size: 5,040 Bytes
9f78ba7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import subprocess
import tempfile
import gradio as gr
from dotenv import load_dotenv
from pypdf import PdfReader
from docx import Document

from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser

load_dotenv()

# Load Resume Template
with open("resume_template.tex", "r", encoding="utf-8") as f:
    latex_template = f.read()


def extract_text_from_file(file):

    if file is None:
        return ""

    file_path = file

    if file_path.endswith(".txt"):
        with open(file_path, "r", encoding="utf-8") as f:
            return f.read()

    elif file_path.endswith(".pdf"):
        reader = PdfReader(file_path)
        text = ""

        for page in reader.pages:
            page_text = page.extract_text()
            if page_text:
                text += page_text + "\n"

        return text

    elif file_path.endswith(".docx"):
        doc = Document(file_path)

        text = "\n".join(
            paragraph.text
            for paragraph in doc.paragraphs
        )

        return text

    return ""


def generate_resume(jd_text, jd_file):

    uploaded_jd = extract_text_from_file(jd_file)

    final_jd = ""

    if jd_text and jd_text.strip():
        final_jd += jd_text

    if uploaded_jd:
        final_jd += "\n\n" + uploaded_jd

    if not final_jd.strip():
        raise gr.Error(
            "Please paste a Job Description or upload a file."
        )

    llm = ChatOpenAI(
        model="gpt-4.1-mini",
        temperature=0.2
    )

    prompt = ChatPromptTemplate.from_template("""

You are an ATS Resume Optimization Assistant.



Your task:

1. Analyze the job description.

2. Optimize the resume content for ATS.

3. Add relevant keywords naturally.

4. Keep all information truthful.

5. Do not invent experience.

6. Keep professional formatting.

7. Return ONLY resume content.



CURRENT RESUME:

{resume}



JOB DESCRIPTION:

{jd}

""")

    chain = prompt | llm | StrOutputParser()

    optimized_content = chain.invoke(
        {
            "resume": latex_template,
            "jd": final_jd
        }
    )

    final_tex = latex_template.replace(
        "{{PROJECTS}}",
        optimized_content
    )

    os.makedirs("output", exist_ok=True)

    tex_path = "output/tailored_resume.tex"

    with open(tex_path, "w", encoding="utf-8") as f:
        f.write(final_tex)

    try:

        subprocess.run(
            [
                "pdflatex",
                "-interaction=nonstopmode",
                "-output-directory=output",
                tex_path
            ],
            check=True
        )

        subprocess.run(
            [
                "pandoc",
                tex_path,
                "-o",
                "output/tailored_resume.docx"
            ],
            check=True
        )

    except subprocess.CalledProcessError as e:
        raise gr.Error(
            f"Resume generation failed: {str(e)}"
        )

    pdf_path = "output/tailored_resume.pdf"
    docx_path = "output/tailored_resume.docx"

    return (
        "βœ… Resume tailored successfully!",
        pdf_path,
        docx_path
    )


with gr.Blocks(
    theme=gr.themes.Soft(),
    title="AI Resume Tailor"
) as demo:

    gr.HTML(
        """

        <div style="text-align:center;padding:20px">

            <h1>πŸš€ AI Resume Tailor</h1>

            <p>

                Upload a Job Description or paste it below.

                Your LaTeX resume template will be optimized

                automatically for ATS.

            </p>

        </div>

        """
    )

    with gr.Row():

        with gr.Column():

            jd_text = gr.Textbox(
                label="Paste Job Description",
                lines=12,
                placeholder="""

Paste the job description here...



Example:

Looking for a Machine Learning Engineer with:

β€’ Python

β€’ SQL

β€’ AWS

β€’ GenAI

β€’ Docker

β€’ MLOps

"""
            )

            jd_file = gr.File(
                label="Upload JD File",
                file_types=[
                    ".pdf",
                    ".docx",
                    ".txt"
                ]
            )

            generate_btn = gr.Button(
                "✨ Generate ATS Resume",
                variant="primary",
                size="lg"
            )

    status = gr.Markdown()

    with gr.Row():

        pdf_output = gr.File(
            label="πŸ“„ Download PDF Resume"
        )

        docx_output = gr.File(
            label="πŸ“ Download DOCX Resume"
        )

    generate_btn.click(
        fn=generate_resume,
        inputs=[
            jd_text,
            jd_file
        ],
        outputs=[
            status,
            pdf_output,
            docx_output
        ]
    )

demo.launch()