Gemma / app.py
ShubhamD95's picture
Update app.py
5c93693 verified
raw
history blame
2.45 kB
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()