File size: 11,986 Bytes
a1a2096
67588b7
 
03aa58e
a1a2096
 
 
03aa58e
 
67588b7
 
073ee45
 
 
67588b7
146b35d
a1a2096
 
7ddc3a3
 
 
 
a1a2096
d068091
5fdef64
d068091
7ddc3a3
03aa58e
a1a2096
242cb8f
67588b7
073ee45
242cb8f
67588b7
7ddc3a3
67588b7
d068091
5fdef64
7ddc3a3
 
 
67588b7
5fdef64
67588b7
a1a2096
03aa58e
7ddc3a3
 
67588b7
03aa58e
67588b7
 
5fdef64
7ddc3a3
 
 
 
 
03aa58e
7ddc3a3
 
 
03aa58e
 
 
 
7ddc3a3
 
03aa58e
7ddc3a3
03aa58e
073ee45
7ddc3a3
 
 
 
 
073ee45
 
 
 
 
 
 
 
 
 
 
146b35d
 
 
 
073ee45
 
 
 
 
7ddc3a3
073ee45
 
 
146b35d
 
 
 
 
 
073ee45
 
 
 
 
 
 
 
 
242cb8f
a1a2096
146b35d
a1a2096
67588b7
a1a2096
146b35d
67588b7
 
242cb8f
03aa58e
67588b7
242cb8f
03aa58e
242cb8f
67588b7
 
242cb8f
03aa58e
67588b7
 
242cb8f
 
03aa58e
242cb8f
 
 
 
 
 
67588b7
242cb8f
a1a2096
146b35d
a1a2096
03aa58e
 
 
 
 
 
 
a1a2096
7ddc3a3
 
a1a2096
 
 
 
 
7ddc3a3
a1a2096
 
 
67588b7
a1a2096
 
67588b7
a1a2096
 
 
 
67588b7
03aa58e
 
 
 
 
a1a2096
7ddc3a3
a1a2096
7ddc3a3
67588b7
 
073ee45
a1a2096
 
073ee45
a1a2096
67588b7
a1a2096
7ddc3a3
 
 
 
 
 
 
 
67588b7
03aa58e
a1a2096
7ddc3a3
a1a2096
7ddc3a3
67588b7
a1a2096
67588b7
a1a2096
 
67588b7
7ddc3a3
a1a2096
 
03aa58e
a1a2096
 
146b35d
a1a2096
 
073ee45
a1a2096
146b35d
 
 
 
03aa58e
 
146b35d
 
 
 
 
 
7ddc3a3
03aa58e
 
 
 
67588b7
073ee45
 
 
7ddc3a3
d068091
7ddc3a3
67588b7
03aa58e
146b35d
d068091
 
03aa58e
 
146b35d
 
03aa58e
7ddc3a3
 
67588b7
03aa58e
67588b7
03aa58e
146b35d
 
 
 
7ddc3a3
03aa58e
7ddc3a3
 
03aa58e
 
67588b7
7ddc3a3
073ee45
146b35d
7ddc3a3
 
 
073ee45
 
 
03aa58e
073ee45
 
 
03aa58e
073ee45
146b35d
 
 
 
 
a1a2096
67588b7
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
"""
AI Resume Studio โ€“ Hugging Face Space
Author: Oluwafemi Idiakhoa
Updated: 2025-06-27

Features
โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
1. Generate rรฉsumรฉ โ†’ Word & PDF downloads
2. Score rรฉsumรฉ vs. job description
3. AI Section Co-Pilot (rewrite, quantify, bulletizeโ€ฆ)
4. Cover-letter generator
5. Fetch any job description by URL:
   โ€ข LinkedIn via OAuth2 Jobs API
   โ€ข All other sites via HTML scraping
6. Multilingual export via Deep-Translator (DeepL backend)
7. Auto-populate Score tab from latest Resume & JD
"""

import os
import re
import time
import tempfile
import requests
import gradio as gr
import google.generativeai as genai
from dotenv import load_dotenv
from bs4 import BeautifulSoup
from docx import Document
from reportlab.lib.pagesizes import LETTER
from reportlab.pdfgen import canvas
from deep_translator import DeeplTranslator
from urllib.parse import urlparse

# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
# Load Secrets & Configure Clients
# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
load_dotenv()

# Gemini
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
genai.configure(api_key=GEMINI_API_KEY)
GEMINI = genai.GenerativeModel("gemini-1.5-pro-latest")

# DeepL via Deep-Translator
DEEPL_KEY = os.getenv("DEEPL_API_KEY")
def translate_text(text: str, tgt: str) -> str:
    if not DEEPL_KEY or tgt.upper() == "EN":
        return text
    try:
        return DeeplTranslator(api_key=DEEPL_KEY, target=tgt).translate(text)
    except Exception as e:
        return f"[Translation Error] {e}\n\n{text}"

# LinkedIn OAuth 2.0 (Client Credentials)
CLIENT_ID     = os.getenv("LINKEDIN_CLIENT_ID")
CLIENT_SECRET = os.getenv("LINKEDIN_CLIENT_SECRET")
_token_cache = {}

def get_linkedin_token():
    data = _token_cache.get("data", {})
    if data and data.get("expires_at", 0) > time.time():
        return data["access_token"]
    resp = requests.post(
        "https://www.linkedin.com/oauth/v2/accessToken",
        data={
            "grant_type":    "client_credentials",
            "client_id":     CLIENT_ID,
            "client_secret": CLIENT_SECRET,
        },
        timeout=10
    )
    resp.raise_for_status()
    payload = resp.json()
    payload["expires_at"] = time.time() + payload.get("expires_in", 0) - 60
    _token_cache["data"] = payload
    return payload["access_token"]

# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
# Job-Description Fetcher (LinkedIn API or Generic Scraping)
# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
def fetch_job_description(url: str) -> str:
    domain = urlparse(url).netloc.lower()
    if "linkedin.com" in domain:
        m = re.search(r"(?:jobs/view/|currentJobId=)(\d+)", url)
        if m:
            job_id = m.group(1)
            try:
                token = get_linkedin_token()
                api_url = f"https://api.linkedin.com/v2/jobPosts/{job_id}?projection=(description)"
                r = requests.get(api_url,
                                 headers={"Authorization": f"Bearer {token}"},
                                 timeout=10)
                r.raise_for_status()
                return r.json().get("description", "")
            except Exception:
                pass

    try:
        page = requests.get(url, headers={"User-Agent":"Mozilla/5.0"}, timeout=10)
        soup = BeautifulSoup(page.text, "html.parser")
        selectors = [
            "div.jobsearch-jobDescriptionText",
            "section.description",
            "div.jobs-description__content",
            "div#job-details",
            "article.jobPosting",
            "div.jd-container",
        ]
        for sel in selectors:
            block = soup.select_one(sel)
            if block and block.get_text(strip=True):
                return block.get_text(" ", strip=True)
        text = soup.get_text(" ", strip=True)
        return text[:5000] + ("โ€ฆ" if len(text) > 5000 else "")
    except Exception as e:
        return f"[Error fetching job description] {e}"

# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
# AI & File Utilities
# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
def ask_gemini(prompt: str, temp: float = 0.6) -> str:
    try:
        return GEMINI.generate_content(prompt, generation_config={"temperature": temp}).text.strip()
    except Exception as e:
        return f"[Gemini Error] {e}"

def save_docx(text: str) -> str:
    f = tempfile.NamedTemporaryFile(delete=False, suffix=".docx")
    doc = Document()
    for line in text.splitlines():
        doc.add_paragraph(line)
    doc.save(f.name)
    return f.name

def save_pdf(text: str) -> str:
    f = tempfile.NamedTemporaryFile(delete=False, suffix=".pdf")
    c = canvas.Canvas(f.name, pagesize=LETTER)
    width, height = LETTER
    y = height - 72
    for line in text.splitlines():
        c.drawString(72, y, line)
        y -= 14
        if y < 72:
            c.showPage()
            y = height - 72
    c.save()
    return f.name

# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
# Core AI Logic
# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
LANGS = {
    "EN": "English", "DE": "German",  "FR": "French",   "ES": "Spanish",
    "IT": "Italian", "NL": "Dutch",   "PT": "Portuguese","PL": "Polish",
    "JA": "Japanese","ZH": "Chinese"
}

def generate_resume(name, email, phone, summary, exp, edu, skills, lang):
    prompt = f"""
Create a professional rรฉsumรฉ in Markdown without first-person pronouns.
Output language: {LANGS[lang]}

Name: {name}
Email: {email}
Phone: {phone}

Professional Summary:
{summary}

Experience:
{exp}

Education:
{edu}

Skills:
{skills}
"""
    md = ask_gemini(prompt)
    return translate_text(md, lang)

def generate_and_export(name, email, phone, summary, exp, edu, skills, lang):
    md = generate_resume(name, email, phone, summary, exp, edu, skills, lang)
    return md, save_docx(md), save_pdf(md)

def score_resume(resume_md, jd):
    prompt = f"""
Evaluate this rรฉsumรฉ against the job description. Return compact Markdown:

### Match Score
<0โ€“100>

### Suggestions
- โ€ฆ
"""
    return ask_gemini(prompt, temp=0.4)

def refine_section(text, instr, lang):
    prompt = f"""
Apply the following instruction to this rรฉsumรฉ section. Respond in {LANGS[lang]}.

Instruction: {instr}
Section:
{text}
"""
    out = ask_gemini(prompt)
    return translate_text(out, lang)

def generate_cover_letter(resume_md, jd, tone, lang):
    prompt = f"""
Draft a one-page cover letter (max 300 words), in a {tone} tone, using {LANGS[lang]}.
Salutation: "Dear Hiring Manager,"

Rรฉsumรฉ:
{resume_md}

Job Description:
{jd}
"""
    letter = ask_gemini(prompt)
    return translate_text(letter, lang)

# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
# Gradio App Definition with State for Auto-Populate
# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
with gr.Blocks(title="AI Resume Studio") as demo:
    gr.Markdown("## ๐Ÿง  AI Resume Studio (Gemini ร— DeepL + Universal Job Fetcher)")

    # State to hold last-generated rรฉsumรฉ & JD
    resume_state = gr.State(value="")
    jd_state     = gr.State(value="")

    # Tab 1: Generate Rรฉsumรฉ
    with gr.Tab("๐Ÿ“„ Generate Rรฉsumรฉ"):
        with gr.Row():
            name_in, email_in, phone_in = (
                gr.Textbox(label="Name"),
                gr.Textbox(label="Email"),
                gr.Textbox(label="Phone"),
            )
        sum_in    = gr.Textbox(label="Professional Summary")
        exp_in    = gr.Textbox(label="Experience")
        edu_in    = gr.Textbox(label="Education")
        skills_in = gr.Textbox(label="Skills")
        lang_in   = gr.Dropdown(list(LANGS.keys()), value="EN", label="Language")

        md_out   = gr.Markdown(label="Rรฉsumรฉ (Markdown)")
        docx_out = gr.File(label="โฌ‡ Download .docx")
        pdf_out  = gr.File(label="โฌ‡ Download .pdf")
        btn_gen  = gr.Button("Generate")

        btn_gen.click(
            generate_and_export,
            inputs=[name_in, email_in, phone_in, sum_in, exp_in, edu_in, skills_in, lang_in],
            outputs=[md_out, docx_out, pdf_out, resume_state],
        )

    # Tab 2: Score Rรฉsumรฉ
    with gr.Tab("๐Ÿงฎ Score Rรฉsumรฉ Against Job"):
        res_in    = gr.Textbox(value=resume_state, label="Rรฉsumรฉ (Markdown)", lines=10)
        jd_in     = gr.Textbox(value=jd_state,     label="Job Description",   lines=8)
        score_out = gr.Markdown(label="Score & Suggestions")
        btn_score = gr.Button("Evaluate")
        btn_score.click(score_resume, inputs=[res_in, jd_in], outputs=score_out)

    # Tab 3: AI Section Co-Pilot
    with gr.Tab("โœ๏ธ AI Section Co-Pilot"):
        sec_in   = gr.Textbox(label="Section Text", lines=6)
        act_in   = gr.Radio(
            ["Rewrite", "Make More Concise", "Quantify Achievements", "Convert to Bullet Points"],
            label="Action"
        )
        lang_sec = gr.Dropdown(list(LANGS.keys()), value="EN", label="Language")
        sec_out  = gr.Textbox(label="AI Output", lines=6)
        btn_sec  = gr.Button("Apply")
        btn_sec.click(refine_section, inputs=[sec_in, act_in, lang_sec], outputs=sec_out)

    # Tab 4: Cover-Letter Generator
    with gr.Tab("๐Ÿ“ง Cover-Letter Generator"):
        cv_res   = gr.Textbox(label="Rรฉsumรฉ (Markdown)", lines=12)
        cv_jd    = gr.Textbox(label="Job Description",   lines=8)
        cv_tone  = gr.Radio(["Professional", "Friendly", "Enthusiastic"], label="Tone")
        cv_lang  = gr.Dropdown(list(LANGS.keys()), value="EN", label="Language")
        cv_out   = gr.Markdown(label="Cover Letter")
        btn_cv   = gr.Button("Generate")
        btn_cv.click(generate_cover_letter,
                     inputs=[cv_res, cv_jd, cv_tone, cv_lang],
                     outputs=cv_out)

    # Tab 5: Universal Job Description Fetcher
    with gr.Tab("๐ŸŒ Fetch Job Description"):
        url_in    = gr.Textbox(label="Job URL")
        jd_out    = gr.Textbox(label="Job Description", lines=12)
        btn_fetch = gr.Button("Fetch Description")
        btn_fetch.click(
            fetch_job_description,
            inputs=[url_in],
            outputs=[jd_out, jd_state],
        )

demo.launch(share=False)