CR7CAD commited on
Commit
6637415
·
verified ·
1 Parent(s): 50528fd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -31
app.py CHANGED
@@ -25,7 +25,7 @@ def extract_text_from_file(file_obj):
25
  text = f"Error processing DOCX file: {e}"
26
  elif ext == ".doc":
27
  try:
28
- # textract requires a filename; solve this using a temporary file.
29
  with tempfile.NamedTemporaryFile(delete=False, suffix=".doc") as tmp:
30
  tmp.write(file_obj.read())
31
  tmp.flush()
@@ -40,7 +40,6 @@ def extract_text_from_file(file_obj):
40
  pass
41
  else:
42
  text = "Unsupported file type."
43
-
44
  return text
45
 
46
  #####################################
@@ -48,10 +47,10 @@ def extract_text_from_file(file_obj):
48
  #####################################
49
  def extract_basic_resume_info(text):
50
  """
51
- Parse the extracted text to summarize basic info:
52
  - Name
53
  - Age
54
- - Job Experience (years or descriptive)
55
  - Skills
56
  - Education
57
 
@@ -65,44 +64,50 @@ def extract_basic_resume_info(text):
65
  "Education": None,
66
  }
67
 
68
- # Extract Name (e.g., "Name: John Doe")
69
- name_match = re.search(r"[Nn]ame[:\-]\s*([A-Za-z\s]+)", text)
70
  if name_match:
71
  info["Name"] = name_match.group(1).strip()
72
  else:
73
- # Fallback: heuristic, assume the first two or three capitalized words are the candidate name.
74
  potential_names = re.findall(r"\b[A-Z][a-z]+(?:\s+[A-Z][a-z]+){1,2}\b", text)
75
  if potential_names:
76
  info["Name"] = potential_names[0]
77
 
78
  # Extract Age (e.g., "Age: 28")
79
- age_match = re.search(r"[Aa]ge[:\-]\s*(\d{1,2})", text)
80
  if age_match:
81
  info["Age"] = age_match.group(1)
82
-
83
- # Extract Job Experience (e.g., "5 years of experience")
84
- exp_match = re.search(r"(\d+)\s+(years|yrs)\s+(?:of\s+)?experience", text, re.IGNORECASE)
85
- if exp_match:
86
- info["Job Experience"] = f"{exp_match.group(1)} {exp_match.group(2)}"
 
 
 
87
  else:
88
- # Attempt to capture a descriptive work experience line via a labeled section.
89
- exp_line = re.search(r"(Experience|Work History)[:\-]\s*(.+)", text, re.IGNORECASE)
90
- if exp_line:
91
- info["Job Experience"] = exp_line.group(2).strip()
92
 
93
  # Extract Skills (e.g., "Skills: Python, Java, SQL")
94
- # This is a simple pattern and might require refinement for your resume formats.
95
  skills_match = re.search(r"(Skills|Technical Skills)[:\-]\s*(.+)", text, re.IGNORECASE)
96
  if skills_match:
97
- # Cleanup skills by removing any trailing or extra characters.
98
  skills_str = skills_match.group(2).strip()
99
  info["Skills"] = skills_str.rstrip(".")
100
-
101
- # Extract Education (e.g., "Education: B.Sc in Computer Science")
102
- edu_match = re.search(r"Education[:\-]\s*(.+)", text, re.IGNORECASE)
103
  if edu_match:
104
- edu_str = edu_match.group(1).strip()
105
- info["Education"] = edu_str.rstrip(".")
 
 
 
 
 
106
 
107
  return info
108
 
@@ -111,7 +116,7 @@ def extract_basic_resume_info(text):
111
  #####################################
112
  def summarize_basic_info(info):
113
  """
114
- Combine the extracted basic resume information into a cohesive paragraph.
115
  """
116
  parts = []
117
 
@@ -124,13 +129,13 @@ def summarize_basic_info(info):
124
  parts.append(f"aged {info['Age']}")
125
 
126
  if info.get("Job Experience"):
127
- parts.append(f"with {info['Job Experience']} of work experience")
128
 
129
  if info.get("Skills"):
130
  parts.append(f"skilled in {info['Skills']}")
131
 
132
  if info.get("Education"):
133
- parts.append(f"and educated with a background in {info['Education']}")
134
 
135
  summary_paragraph = ", ".join(parts) + "."
136
  return summary_paragraph
@@ -145,7 +150,7 @@ def process_resume(file_obj):
145
  resume_text = extract_text_from_file(file_obj)
146
  # Extract basic info from the text.
147
  basic_info = extract_basic_resume_info(resume_text)
148
- # Create a summary paragraph from the extracted info.
149
  summary_paragraph = summarize_basic_info(basic_info)
150
  return resume_text, summary_paragraph
151
 
@@ -154,8 +159,8 @@ def process_resume(file_obj):
154
  #####################################
155
  st.title("Resume Basic Information Summary")
156
  st.markdown("""
157
- Upload your resume file in **.doc** or **.docx** format. The app will extract the document's content and generate a summary paragraph
158
- highlighting the candidate’s name, age, job experience, skills, and education.
159
  """)
160
 
161
  uploaded_file = st.file_uploader("Upload Resume", type=["doc", "docx"])
@@ -167,7 +172,7 @@ if st.button("Process Resume"):
167
  with st.spinner("Processing resume..."):
168
  resume_text, summary_paragraph = process_resume(uploaded_file)
169
 
170
- st.subheader("Summary of Basic Information")
171
  st.markdown(summary_paragraph)
172
 
173
  st.subheader("Full Extracted Resume Text")
 
25
  text = f"Error processing DOCX file: {e}"
26
  elif ext == ".doc":
27
  try:
28
+ # textract requires a file name; save the file temporarily.
29
  with tempfile.NamedTemporaryFile(delete=False, suffix=".doc") as tmp:
30
  tmp.write(file_obj.read())
31
  tmp.flush()
 
40
  pass
41
  else:
42
  text = "Unsupported file type."
 
43
  return text
44
 
45
  #####################################
 
47
  #####################################
48
  def extract_basic_resume_info(text):
49
  """
50
+ Parse the extracted text to extract/summarize:
51
  - Name
52
  - Age
53
+ - Job Experience (capturing the block under the "experience" section)
54
  - Skills
55
  - Education
56
 
 
64
  "Education": None,
65
  }
66
 
67
+ # Extract Name (e.g., "Name: John Doe" or from heuristics)
68
+ name_match = re.search(r"[Nn]ame[:\-]\s*([A-Za-z\s,]+)", text)
69
  if name_match:
70
  info["Name"] = name_match.group(1).strip()
71
  else:
72
+ # Heuristic: Assume the first line or a line with two or three capitalized words is the candidate's name.
73
  potential_names = re.findall(r"\b[A-Z][a-z]+(?:\s+[A-Z][a-z]+){1,2}\b", text)
74
  if potential_names:
75
  info["Name"] = potential_names[0]
76
 
77
  # Extract Age (e.g., "Age: 28")
78
+ age_match = re.search(r"[Aa]ge[:\-]\s*(\d{1,3})", text)
79
  if age_match:
80
  info["Age"] = age_match.group(1)
81
+
82
+ # Extract Job Experience using the "experience" section.
83
+ # This regex captures everything after the word "experience" until the next section heading (e.g., "additional information" or "skills")
84
+ experience_match = re.search(r"experience\s*(.*?)(?:\n\s*\n|additional information|$)", text, re.IGNORECASE | re.DOTALL)
85
+ if experience_match:
86
+ # Clean up the extracted block by removing any extra whitespace or newlines.
87
+ job_experience = experience_match.group(1).strip()
88
+ info["Job Experience"] = " ".join(job_experience.split())
89
  else:
90
+ # Fallback if a labeled section isn't found.
91
+ exp_match = re.search(r"(\d+)\s+(years|yrs)\s+(?:of\s+)?experience", text, re.IGNORECASE)
92
+ if exp_match:
93
+ info["Job Experience"] = f"{exp_match.group(1)} {exp_match.group(2)}"
94
 
95
  # Extract Skills (e.g., "Skills: Python, Java, SQL")
 
96
  skills_match = re.search(r"(Skills|Technical Skills)[:\-]\s*(.+)", text, re.IGNORECASE)
97
  if skills_match:
 
98
  skills_str = skills_match.group(2).strip()
99
  info["Skills"] = skills_str.rstrip(".")
100
+
101
+ # Extract Education (e.g., "Education: ...")
102
+ edu_match = re.search(r"education\s*(.*?)(?:\n\s*\n|experience|$)", text, re.IGNORECASE | re.DOTALL)
103
  if edu_match:
104
+ education_block = edu_match.group(1).strip()
105
+ info["Education"] = " ".join(education_block.split())
106
+ else:
107
+ # Fallback: search for lines starting with common degree words.
108
+ edu_match = re.search(r"(Bachelor|Master|B\.Sc|M\.Sc|Ph\.D)[^\n]+", text)
109
+ if edu_match:
110
+ info["Education"] = edu_match.group(0)
111
 
112
  return info
113
 
 
116
  #####################################
117
  def summarize_basic_info(info):
118
  """
119
+ Combine the extracted resume elements into a concise summary paragraph.
120
  """
121
  parts = []
122
 
 
129
  parts.append(f"aged {info['Age']}")
130
 
131
  if info.get("Job Experience"):
132
+ parts.append(f"with job experience: {info['Job Experience']}")
133
 
134
  if info.get("Skills"):
135
  parts.append(f"skilled in {info['Skills']}")
136
 
137
  if info.get("Education"):
138
+ parts.append(f"and educated in {info['Education']}")
139
 
140
  summary_paragraph = ", ".join(parts) + "."
141
  return summary_paragraph
 
150
  resume_text = extract_text_from_file(file_obj)
151
  # Extract basic info from the text.
152
  basic_info = extract_basic_resume_info(resume_text)
153
+ # Create a summary paragraph from the basic info.
154
  summary_paragraph = summarize_basic_info(basic_info)
155
  return resume_text, summary_paragraph
156
 
 
159
  #####################################
160
  st.title("Resume Basic Information Summary")
161
  st.markdown("""
162
+ Upload your resume file in **.doc** or **.docx** format. The app extracts key details such as name, age, job experience, skills,
163
+ and education, then summarizes them into a single paragraph.
164
  """)
165
 
166
  uploaded_file = st.file_uploader("Upload Resume", type=["doc", "docx"])
 
172
  with st.spinner("Processing resume..."):
173
  resume_text, summary_paragraph = process_resume(uploaded_file)
174
 
175
+ st.subheader("Summary Paragraph")
176
  st.markdown(summary_paragraph)
177
 
178
  st.subheader("Full Extracted Resume Text")