CR7CAD commited on
Commit
c6d228e
·
verified ·
1 Parent(s): 0d4f4dd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +177 -111
app.py CHANGED
@@ -1,15 +1,45 @@
1
  import os
2
  import tempfile
3
  import re
4
- import time
5
  import streamlit as st
6
  import docx
7
  import textract
8
  from sentence_transformers import SentenceTransformer, util
9
  from transformers import pipeline
 
10
 
11
  #####################################
12
- # Function: Extract Text from File
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  #####################################
14
  def extract_text_from_file(file_obj):
15
  """
@@ -23,67 +53,70 @@ def extract_text_from_file(file_obj):
23
  if ext == ".docx":
24
  try:
25
  document = docx.Document(file_obj)
26
- text = "\n".join([para.text for para in document.paragraphs])
 
27
  except Exception as e:
28
  text = f"Error processing DOCX file: {e}"
29
  elif ext == ".doc":
30
  try:
31
- # textract requires a file name; save the file temporarily.
32
  with tempfile.NamedTemporaryFile(delete=False, suffix=".doc") as tmp:
33
  tmp.write(file_obj.read())
34
- tmp.flush()
35
  tmp_filename = tmp.name
36
  text = textract.process(tmp_filename).decode("utf-8")
 
 
37
  except Exception as e:
38
  text = f"Error processing DOC file: {e}"
39
- finally:
40
- try:
41
- os.remove(tmp_filename)
42
- except Exception:
43
- pass
44
  else:
45
  text = "Unsupported file type."
46
  return text
47
 
48
  #####################################
49
- # Function: Summarize Resume Text using a Transformer Model
50
  #####################################
51
- @st.cache_resource(show_spinner=False)
52
- def load_summarizer():
53
- """
54
- Loads the summarization pipeline using a transformer model.
55
- We use the model "google/pegasus-xsum" for summarization.
56
- """
57
- return pipeline("summarization", model="google/pegasus-xsum")
58
-
59
- def summarize_resume_text(resume_text):
60
  """
61
- Generates a concise summary of the resume text using the summarization model.
62
- If the resume text is very long, we trim it to avoid hitting the model's maximum input size.
63
  """
64
- summarizer = load_summarizer()
65
 
66
- # Trim resume_text if it's too long
67
- max_input_length = 1024 # adjust as needed
 
 
68
  if len(resume_text) > max_input_length:
69
- st.info(f"Resume text is longer than {max_input_length} characters. Trimming text for summarization...")
70
- resume_text = resume_text[:max_input_length]
 
 
 
 
 
 
 
 
 
 
 
 
71
 
72
- # Generate summary
73
- summary_result = summarizer(resume_text, max_length=150, min_length=40, do_sample=False)
74
- candidate_summary = summary_result[0]['summary_text']
75
  return candidate_summary
76
 
77
  #####################################
78
- # Function: Compare Candidate Summary to Company Prompt
79
  #####################################
80
- def compute_suitability(candidate_summary, company_prompt, model):
81
  """
82
  Compute the cosine similarity between candidate summary and company prompt embeddings.
83
  Returns a score in the range [0, 1].
84
  """
85
- candidate_embed = model.encode(candidate_summary, convert_to_tensor=True)
86
- company_embed = model.encode(company_prompt, convert_to_tensor=True)
 
 
 
 
87
  cosine_sim = util.cos_sim(candidate_embed, company_embed)
88
  score = float(cosine_sim.item())
89
  return score
@@ -91,95 +124,128 @@ def compute_suitability(candidate_summary, company_prompt, model):
91
  #####################################
92
  # Main Resume Processing Logic
93
  #####################################
94
- def process_resume(file_obj):
95
  """
96
  Extracts text from the uploaded file and then generates a summary
97
  using a text summarization model.
98
  """
99
- st.info("Extracting text from resume...")
100
- resume_text = extract_text_from_file(file_obj)
 
 
 
 
 
 
 
 
 
 
 
101
 
102
- # Check if resume_text is valid
103
- if not resume_text or resume_text.strip() == "":
104
- st.error("No text could be extracted. Please check your resume file!")
105
- return ""
106
-
107
- st.info(f"Text extraction complete. Extracted {len(resume_text)} characters.")
108
- time.sleep(0.5) # slight delay to let the user read the info message
109
-
110
- st.info("Generating candidate summary, please wait...")
111
- candidate_summary = summarize_resume_text(resume_text)
112
- st.info("Candidate summary generated.")
113
  return candidate_summary
114
 
115
  #####################################
116
- # Load the Sentence-BERT Model (Semantic Similarity Model)
117
- #####################################
118
- @st.cache_resource(show_spinner=False)
119
- def load_sbert_model():
120
- # This loads the Sentence-BERT model "all-MiniLM-L6-v2"
121
- return SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
122
-
123
- # Load Sentence-BERT model for computing semantic similarity.
124
- sbert_model = load_sbert_model()
125
-
126
- #####################################
127
- # Streamlit Interface
128
  #####################################
129
- st.title("Resume Analyzer and Company Suitability Checker")
130
- st.markdown(
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
131
  """
132
- Upload your resume file in **.doc** or **.docx** format. The app performs the following tasks:
133
- 1. Extracts text from the resume.
134
- 2. Uses a transformer-based text summarization model (**google/pegasus-xsum**) to generate a concise candidate summary.
135
- 3. Compares the candidate summary with a company profile (using Sentence-BERT) to produce a suitability score.
136
- """
137
- )
138
-
139
- # File uploader for resume
140
- uploaded_file = st.file_uploader("Upload Resume", type=["doc", "docx"])
141
 
142
- # Button to process the resume and store the summary in session state.
143
- if st.button("Process Resume"):
144
- if uploaded_file is None:
145
- st.error("Please upload a resume file first.")
146
- else:
147
- with st.spinner("Processing resume..."):
148
- candidate_summary = process_resume(uploaded_file)
149
- if candidate_summary: # only if summary is generated
150
- st.session_state["candidate_summary"] = candidate_summary
151
- if candidate_summary:
 
 
 
 
 
 
 
 
152
  st.subheader("Candidate Summary")
153
- st.markdown(candidate_summary)
 
 
 
 
 
 
 
 
 
 
 
154
 
155
- # Pre-defined company prompt for Google LLC.
156
- default_company_prompt = (
157
- "Google LLC, a global leader in technology and innovation, specializes in internet services, cloud computing, "
158
- "artificial intelligence, and software development. As part of Alphabet Inc., Google seeks candidates with strong "
159
- "problem-solving skills, adaptability, and collaboration abilities. Technical roles require proficiency in programming "
160
- "languages such as Python, Java, C++, Go, or JavaScript, with expertise in data structures, algorithms, and system design. "
161
- "Additionally, skills in AI, cybersecurity, UX/UI design, and digital marketing are highly valued. Google fosters a culture "
162
- "of innovation, expecting candidates to demonstrate creativity, analytical thinking, and a passion for cutting-edge technology."
163
- )
164
 
165
- # Company prompt text area.
166
- company_prompt = st.text_area(
167
- "Enter company details:",
168
- value=default_company_prompt,
169
- height=150,
170
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
171
 
172
- # Button to compute the suitability score.
173
- if st.button("Compute Suitability Score"):
174
- if "candidate_summary" not in st.session_state:
175
- st.error("Please process the resume first!")
176
- else:
177
- candidate_summary = st.session_state["candidate_summary"]
178
- if candidate_summary.strip() == "":
179
- st.error("Candidate summary is empty; please check your resume file.")
180
- elif company_prompt.strip() == "":
181
- st.error("Please enter the company information.")
182
- else:
183
- with st.spinner("Computing suitability score..."):
184
- score = compute_suitability(candidate_summary, company_prompt, sbert_model)
185
- st.success(f"Suitability Score: {score:.2f} (range 0 to 1)")
 
1
  import os
2
  import tempfile
3
  import re
 
4
  import streamlit as st
5
  import docx
6
  import textract
7
  from sentence_transformers import SentenceTransformer, util
8
  from transformers import pipeline
9
+ import threading
10
 
11
  #####################################
12
+ # Load Models - Optimized with Threading
13
+ #####################################
14
+ @st.cache_resource(show_spinner=False)
15
+ def load_models():
16
+ """
17
+ Load all models in parallel using threading to speed up initialization
18
+ """
19
+ models = {}
20
+
21
+ def load_summarizer_thread():
22
+ models['summarizer'] = pipeline("summarization", model="google/pegasus-xsum", device=0 if st.session_state.get('use_gpu', False) else -1)
23
+
24
+ def load_sbert_thread():
25
+ models['sbert'] = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2", device='cuda' if st.session_state.get('use_gpu', False) else 'cpu')
26
+
27
+ # Start threads to load models in parallel
28
+ threads = [
29
+ threading.Thread(target=load_summarizer_thread),
30
+ threading.Thread(target=load_sbert_thread)
31
+ ]
32
+
33
+ for thread in threads:
34
+ thread.start()
35
+
36
+ for thread in threads:
37
+ thread.join()
38
+
39
+ return models
40
+
41
+ #####################################
42
+ # Function: Extract Text from File - Optimized
43
  #####################################
44
  def extract_text_from_file(file_obj):
45
  """
 
53
  if ext == ".docx":
54
  try:
55
  document = docx.Document(file_obj)
56
+ # Use a list comprehension and join for better performance
57
+ text = "\n".join(para.text for para in document.paragraphs if para.text.strip())
58
  except Exception as e:
59
  text = f"Error processing DOCX file: {e}"
60
  elif ext == ".doc":
61
  try:
62
+ # Use a context manager for better file handling
63
  with tempfile.NamedTemporaryFile(delete=False, suffix=".doc") as tmp:
64
  tmp.write(file_obj.read())
 
65
  tmp_filename = tmp.name
66
  text = textract.process(tmp_filename).decode("utf-8")
67
+ # Clean up the temporary file immediately
68
+ os.unlink(tmp_filename)
69
  except Exception as e:
70
  text = f"Error processing DOC file: {e}"
 
 
 
 
 
71
  else:
72
  text = "Unsupported file type."
73
  return text
74
 
75
  #####################################
76
+ # Function: Summarize Resume Text - Optimized
77
  #####################################
78
+ def summarize_resume_text(resume_text, models):
 
 
 
 
 
 
 
 
79
  """
80
+ Generates a concise summary of the resume text using the pre-loaded summarization model.
 
81
  """
82
+ summarizer = models['summarizer']
83
 
84
+ # Optimize text processing - only use essential text
85
+ # Break text into chunks and summarize important parts
86
+ max_input_length = 1024 # PEGASUS-XSUM limit
87
+
88
  if len(resume_text) > max_input_length:
89
+ # Instead of simple trimming, extract key sections
90
+ chunks = [resume_text[i:i+max_input_length] for i in range(0, min(len(resume_text), 3*max_input_length), max_input_length)]
91
+ summaries = []
92
+
93
+ for chunk in chunks:
94
+ chunk_summary = summarizer(chunk, max_length=100, min_length=30, do_sample=False)[0]['summary_text']
95
+ summaries.append(chunk_summary)
96
+
97
+ candidate_summary = " ".join(summaries)
98
+ # Summarize again if combined summary is too long
99
+ if len(candidate_summary) > max_input_length:
100
+ candidate_summary = summarizer(candidate_summary[:max_input_length], max_length=150, min_length=40, do_sample=False)[0]['summary_text']
101
+ else:
102
+ candidate_summary = summarizer(resume_text, max_length=150, min_length=40, do_sample=False)[0]['summary_text']
103
 
 
 
 
104
  return candidate_summary
105
 
106
  #####################################
107
+ # Function: Compare Candidate Summary to Company Prompt - Optimized
108
  #####################################
109
+ def compute_suitability(candidate_summary, company_prompt, models):
110
  """
111
  Compute the cosine similarity between candidate summary and company prompt embeddings.
112
  Returns a score in the range [0, 1].
113
  """
114
+ sbert_model = models['sbert']
115
+
116
+ # Encode texts in parallel (if supported by model)
117
+ embeddings = sbert_model.encode([candidate_summary, company_prompt], convert_to_tensor=True)
118
+ candidate_embed, company_embed = embeddings[0], embeddings[1]
119
+
120
  cosine_sim = util.cos_sim(candidate_embed, company_embed)
121
  score = float(cosine_sim.item())
122
  return score
 
124
  #####################################
125
  # Main Resume Processing Logic
126
  #####################################
127
+ def process_resume(file_obj, models):
128
  """
129
  Extracts text from the uploaded file and then generates a summary
130
  using a text summarization model.
131
  """
132
+ with st.status("Processing resume...") as status:
133
+ status.update(label="Extracting text from resume...")
134
+ resume_text = extract_text_from_file(file_obj)
135
+
136
+ # Check if resume_text is valid
137
+ if not resume_text or resume_text.strip() == "":
138
+ status.update(label="Error: No text could be extracted", state="error")
139
+ return ""
140
+
141
+ status.update(label=f"Extracted {len(resume_text)} characters. Generating summary...")
142
+
143
+ candidate_summary = summarize_resume_text(resume_text, models)
144
+ status.update(label="Processing complete!", state="complete")
145
 
 
 
 
 
 
 
 
 
 
 
 
146
  return candidate_summary
147
 
148
  #####################################
149
+ # Streamlit Interface - Optimized
 
 
 
 
 
 
 
 
 
 
 
150
  #####################################
151
+ def main():
152
+ st.set_page_config(page_title="Resume Analyzer", layout="wide")
153
+
154
+ # Initialize session state for GPU usage
155
+ if 'use_gpu' not in st.session_state:
156
+ st.session_state.use_gpu = False
157
+
158
+ # Only show sidebar settings on first run
159
+ with st.sidebar:
160
+ st.title("Settings")
161
+ if st.checkbox("Use GPU (if available)", value=st.session_state.use_gpu):
162
+ st.session_state.use_gpu = True
163
+ else:
164
+ st.session_state.use_gpu = False
165
+
166
+ st.info("Using GPU can significantly speed up model inference if available")
167
+
168
+ # Load models - this happens only once due to caching
169
+ with st.spinner("Loading AI models..."):
170
+ models = load_models()
171
+
172
+ st.title("Resume Analyzer and Company Suitability Checker")
173
+ st.markdown(
174
+ """
175
+ Upload your resume file in **.doc** or **.docx** format. The app performs the following tasks:
176
+ 1. Extracts text from the resume.
177
+ 2. Uses a transformer-based model to generate a concise candidate summary.
178
+ 3. Compares the candidate summary with a company profile to produce a suitability score.
179
  """
180
+ )
 
 
 
 
 
 
 
 
181
 
182
+ # Use columns for better layout
183
+ col1, col2 = st.columns([1, 1])
184
+
185
+ with col1:
186
+ # File uploader for resume
187
+ uploaded_file = st.file_uploader("Upload Resume", type=["doc", "docx"])
188
+
189
+ # Button to process the resume
190
+ if st.button("Process Resume", type="primary", use_container_width=True):
191
+ if uploaded_file is None:
192
+ st.error("Please upload a resume file first.")
193
+ else:
194
+ candidate_summary = process_resume(uploaded_file, models)
195
+ if candidate_summary: # only if summary is generated
196
+ st.session_state["candidate_summary"] = candidate_summary
197
+
198
+ # Display candidate summary if available
199
+ if "candidate_summary" in st.session_state:
200
  st.subheader("Candidate Summary")
201
+ st.markdown(st.session_state["candidate_summary"])
202
+
203
+ with col2:
204
+ # Pre-defined company prompt for Google LLC.
205
+ default_company_prompt = (
206
+ "Google LLC, a global leader in technology and innovation, specializes in internet services, cloud computing, "
207
+ "artificial intelligence, and software development. As part of Alphabet Inc., Google seeks candidates with strong "
208
+ "problem-solving skills, adaptability, and collaboration abilities. Technical roles require proficiency in programming "
209
+ "languages such as Python, Java, C++, Go, or JavaScript, with expertise in data structures, algorithms, and system design. "
210
+ "Additionally, skills in AI, cybersecurity, UX/UI design, and digital marketing are highly valued. Google fosters a culture "
211
+ "of innovation, expecting candidates to demonstrate creativity, analytical thinking, and a passion for cutting-edge technology."
212
+ )
213
 
214
+ # Company prompt text area.
215
+ company_prompt = st.text_area(
216
+ "Enter company details:",
217
+ value=default_company_prompt,
218
+ height=150,
219
+ )
 
 
 
220
 
221
+ # Button to compute the suitability score.
222
+ if st.button("Compute Suitability Score", type="primary", use_container_width=True):
223
+ if "candidate_summary" not in st.session_state:
224
+ st.error("Please process the resume first!")
225
+ else:
226
+ candidate_summary = st.session_state["candidate_summary"]
227
+ if candidate_summary.strip() == "":
228
+ st.error("Candidate summary is empty; please check your resume file.")
229
+ elif company_prompt.strip() == "":
230
+ st.error("Please enter the company information.")
231
+ else:
232
+ with st.spinner("Computing suitability score..."):
233
+ score = compute_suitability(candidate_summary, company_prompt, models)
234
+
235
+ # Display score with a progress bar for visual feedback
236
+ st.success(f"Suitability Score: {score:.2f} (range 0 to 1)")
237
+ st.progress(score)
238
+
239
+ # Add interpretation of score
240
+ if score > 0.75:
241
+ st.info("Excellent match! Your profile appears very well suited for this company.")
242
+ elif score > 0.5:
243
+ st.info("Good match. Your profile aligns with many aspects of the company's requirements.")
244
+ elif score > 0.3:
245
+ st.info("Moderate match. Consider highlighting more relevant skills or experience.")
246
+ else:
247
+ st.info("Low match. Your profile may need significant adjustments to better align with this company.")
248
 
249
+
250
+ if __name__ == "__main__":
251
+ main()