ShubhamD95 commited on
Commit
1ab7e62
·
verified ·
1 Parent(s): 5b462e9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -35
app.py CHANGED
@@ -1,60 +1,59 @@
1
- from transformers import AutoTokenizer, AutoModelForCausalLM
2
  import os
3
  import gradio as gr
4
- import PyPDF2
5
  import docx2txt
 
 
 
6
 
7
- # Load token and model
8
- hf_token = os.environ.get("hf_space_token")
9
- model_name = "google/gemma-3-1b-it"
10
- tokenizer = AutoTokenizer.from_pretrained(model_name, token=hf_token)
11
- model = AutoModelForCausalLM.from_pretrained(model_name, token=hf_token)
12
 
13
- # Resume text extraction
14
- def extract_text_from_resume(file):
 
 
 
 
15
  if file is None:
16
- return "No file uploaded."
17
  ext = file.name.split('.')[-1].lower()
18
- if ext == "pdf":
19
  reader = PyPDF2.PdfReader(file)
20
  return "\n".join([page.extract_text() for page in reader.pages if page.extract_text()])
21
- elif ext == "docx":
22
  return docx2txt.process(file.name)
23
  else:
24
  return "Unsupported file format."
25
 
26
- # Function to generate LLM response
27
- def analyze_resume(job_description, resume_file, analyze_with_jd):
28
- resume_text = extract_text_from_resume(resume_file)
29
  if not resume_text.strip():
30
- return "Unable to extract resume content."
31
-
32
- if analyze_with_jd and job_description.strip():
33
- prompt = (
34
- f"Compare the following resume with this job description:\n\n"
35
- f"Job Description:\n{job_description}\n\n"
36
- f"Resume:\n{resume_text}\n\n"
37
- f"Give a short summary of how well this resume matches the job."
38
- )
39
- else:
40
- prompt = f"Summarize the following resume:\n\n{resume_text}"
41
 
42
- inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=2048)
43
- outputs = model.generate(**inputs, max_new_tokens=200, do_sample=True, temperature=0.7)
44
- return tokenizer.decode(outputs[0], skip_special_tokens=True)
45
 
46
- # Build Gradio UI
 
 
 
 
47
  with gr.Blocks() as demo:
48
  with gr.Row():
49
  with gr.Column():
50
  analyze_checkbox = gr.Checkbox(label="Analyze with Job Description", value=True)
51
- job_desc = gr.Textbox(label="Job Description", lines=8, placeholder="Paste job description here...")
52
  resume_file = gr.File(label="Upload Resume (PDF or DOCX)", file_types=[".pdf", ".docx"])
53
  with gr.Column():
54
- parsed_output = gr.Textbox(label="Gemma Analysis Output", lines=20)
55
 
56
- resume_file.change(fn=analyze_resume, inputs=[job_desc, resume_file, analyze_checkbox], outputs=parsed_output)
57
- job_desc.change(fn=analyze_resume, inputs=[job_desc, resume_file, analyze_checkbox], outputs=parsed_output)
 
58
 
59
- demo.launch()
 
 
60
 
 
 
 
1
  import os
2
  import gradio as gr
 
3
  import docx2txt
4
+ import PyPDF2
5
+ from keybert.llm import OpenAI
6
+ from keybert import KeyLLM
7
 
8
+ # Get OpenAI key from environment
9
+ api_key = os.environ.get("OPENAI_API_KEY")
 
 
 
10
 
11
+ # Setup OpenAI-backed KeyLLM model
12
+ llm = OpenAI(api_key=api_key)
13
+ kw_model = KeyLLM(llm)
14
+
15
+ # Function to extract text from uploaded resume
16
+ def extract_text(file):
17
  if file is None:
18
+ return ""
19
  ext = file.name.split('.')[-1].lower()
20
+ if ext == 'pdf':
21
  reader = PyPDF2.PdfReader(file)
22
  return "\n".join([page.extract_text() for page in reader.pages if page.extract_text()])
23
+ elif ext == 'docx':
24
  return docx2txt.process(file.name)
25
  else:
26
  return "Unsupported file format."
27
 
28
+ # Function to extract keywords using KeyLLM
29
+ def extract_keywords_from_resume(job_desc, resume_file, analyze_with_jd):
30
+ resume_text = extract_text(resume_file)
31
  if not resume_text.strip():
32
+ return "Could not extract text from resume."
 
 
 
 
 
 
 
 
 
 
33
 
34
+ # Create combined document if analysis is requested
35
+ document = job_desc + "\n\n" + resume_text if analyze_with_jd and job_desc else resume_text
 
36
 
37
+ # Use KeyLLM to extract keywords
38
+ keywords = kw_model.extract_keywords([document], check_vocab=True)
39
+ return ", ".join(keywords[0]) if keywords else "No keywords found."
40
+
41
+ # Gradio UI
42
  with gr.Blocks() as demo:
43
  with gr.Row():
44
  with gr.Column():
45
  analyze_checkbox = gr.Checkbox(label="Analyze with Job Description", value=True)
46
+ job_desc = gr.Textbox(label="Job Description", lines=6)
47
  resume_file = gr.File(label="Upload Resume (PDF or DOCX)", file_types=[".pdf", ".docx"])
48
  with gr.Column():
49
+ output_keywords = gr.Textbox(label="Extracted Keywords", lines=10)
50
 
51
+ resume_file.change(fn=extract_keywords_from_resume,
52
+ inputs=[job_desc, resume_file, analyze_checkbox],
53
+ outputs=output_keywords)
54
 
55
+ job_desc.change(fn=extract_keywords_from_resume,
56
+ inputs=[job_desc, resume_file, analyze_checkbox],
57
+ outputs=output_keywords)
58
 
59
+ demo.launch()