File size: 2,450 Bytes
98a1c89
f93c703
3c06ec2
5c93693
 
f93c703
5c93693
f93c703
98a1c89
3c06ec2
 
 
5c93693
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3c06ec2
 
5c93693
 
 
 
 
 
 
 
 
 
 
 
 
 
3c06ec2
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
from transformers import AutoTokenizer, AutoModelForCausalLM
import os
import gradio as gr
import PyPDF2
import docx2txt

# Load token and model
hf_token = os.environ.get("hf_space_token")
model_name = "google/gemma-3-1b-it"
tokenizer = AutoTokenizer.from_pretrained(model_name, token=hf_token)
model = AutoModelForCausalLM.from_pretrained(model_name, token=hf_token)

# Resume text extraction
def extract_text_from_resume(file):
    if file is None:
        return "No file uploaded."
    ext = file.name.split('.')[-1].lower()
    if ext == "pdf":
        reader = PyPDF2.PdfReader(file)
        return "\n".join([page.extract_text() for page in reader.pages if page.extract_text()])
    elif ext == "docx":
        return docx2txt.process(file.name)
    else:
        return "Unsupported file format."

# Function to generate LLM response
def analyze_resume(job_description, resume_file, analyze_with_jd):
    resume_text = extract_text_from_resume(resume_file)
    if not resume_text.strip():
        return "Unable to extract resume content."

    if analyze_with_jd and job_description.strip():
        prompt = (
            f"Compare the following resume with this job description:\n\n"
            f"Job Description:\n{job_description}\n\n"
            f"Resume:\n{resume_text}\n\n"
            f"Give a short summary of how well this resume matches the job."
        )
    else:
        prompt = f"Summarize the following resume:\n\n{resume_text}"

    inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=2048)
    outputs = model.generate(**inputs, max_new_tokens=200, do_sample=True, temperature=0.7)
    return tokenizer.decode(outputs[0], skip_special_tokens=True)

# Build Gradio UI
with gr.Blocks() as demo:
    with gr.Row():
        with gr.Column():
            analyze_checkbox = gr.Checkbox(label="Analyze with Job Description", value=True)
            job_desc = gr.Textbox(label="Job Description", lines=8, placeholder="Paste job description here...")
            resume_file = gr.File(label="Upload Resume (PDF or DOCX)", file_types=[".pdf", ".docx"])
        with gr.Column():
            parsed_output = gr.Textbox(label="Gemma Analysis Output", lines=20)

    resume_file.change(fn=analyze_resume, inputs=[job_desc, resume_file, analyze_checkbox], outputs=parsed_output)
    job_desc.change(fn=analyze_resume, inputs=[job_desc, resume_file, analyze_checkbox], outputs=parsed_output)

demo.launch()