earlsab
commited on
Commit
·
ceba096
1
Parent(s):
fd3b959
add handler
Browse files- README.md +5 -1
- handler.py +558 -0
- requirements.txt +4 -0
- skill_db_relax_20.json +0 -0
- token_dist.json +0 -0
README.md
CHANGED
|
@@ -5,4 +5,8 @@ language:
|
|
| 5 |
base_model:
|
| 6 |
- allenai/longformer-base-4096
|
| 7 |
pipeline_tag: text-classification
|
| 8 |
-
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
base_model:
|
| 6 |
- allenai/longformer-base-4096
|
| 7 |
pipeline_tag: text-classification
|
| 8 |
+
---
|
| 9 |
+
|
| 10 |
+
Model task/inference task used in endpoint in handler.py does not segment a resume. \
|
| 11 |
+
It returns the skills found in the experiences section (model files are used for choosing the section) of a resume. \
|
| 12 |
+
This is to make it easier to code for the spaces/demo found in earlsab/beyond-keywords
|
handler.py
ADDED
|
@@ -0,0 +1,558 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Dict, List, Any
|
| 2 |
+
from transformers import AutoModelForQuestionAnswering, AutoTokenizer, pipeline, LongformerTokenizer
|
| 3 |
+
import torch
|
| 4 |
+
import torch.nn as nn
|
| 5 |
+
import torch.nn.functional as F
|
| 6 |
+
import spacy
|
| 7 |
+
from spacy.matcher import PhraseMatcher
|
| 8 |
+
from transformers import LongformerModel
|
| 9 |
+
from skillNer.general_params import SKILL_DB
|
| 10 |
+
from skillNer.skill_extractor_class import SkillExtractor
|
| 11 |
+
import torch
|
| 12 |
+
from transformers import LongformerTokenizer
|
| 13 |
+
|
| 14 |
+
import torch
|
| 15 |
+
import torch.nn.functional as F
|
| 16 |
+
from transformers import LongformerTokenizer
|
| 17 |
+
import re
|
| 18 |
+
from datetime import datetime
|
| 19 |
+
|
| 20 |
+
Resume_num_labels = None
|
| 21 |
+
class EndpointHandler():
|
| 22 |
+
def __init__(self, path=""):
|
| 23 |
+
# Label mapping as provided
|
| 24 |
+
# Resume Label Mapping
|
| 25 |
+
self.Resume_label_map = {
|
| 26 |
+
"RT": 0, # Resume Title
|
| 27 |
+
"SST": 1, # Summary Section Title
|
| 28 |
+
"SSC": 2, # Summary Section Content
|
| 29 |
+
"AST": 3, # Accomplishments Section Title
|
| 30 |
+
"ASC": 4, # Accomplishments Section Content
|
| 31 |
+
"EDST": 5, # Education Section Title
|
| 32 |
+
"EDSC": 6, # Education Section Content
|
| 33 |
+
"SKST": 7, # Skills Section Title
|
| 34 |
+
"SKSC": 8, # Skills Section Content
|
| 35 |
+
"HST": 9, # Highlights Section Title
|
| 36 |
+
"HSC": 10, # Highlights Section Content
|
| 37 |
+
"CST": 11, # Certifications Section Title
|
| 38 |
+
"CSC": 12, # Certifications Section Content
|
| 39 |
+
"EST": 13, # Experience Section Title
|
| 40 |
+
"EJT": 14, # Experience Job Title
|
| 41 |
+
"EDT": 15, # Experience Date Range Title
|
| 42 |
+
"ECT": 16, # Experience Company Title
|
| 43 |
+
"EDC": 17 # Experience Description Content
|
| 44 |
+
}
|
| 45 |
+
global Resume_num_labels
|
| 46 |
+
self.Resume_num_labels = len(self.Resume_label_map)
|
| 47 |
+
Resume_num_labels = self.Resume_num_labels
|
| 48 |
+
|
| 49 |
+
self.Resume_labels = [
|
| 50 |
+
{"value": "RT", "label": "Resume Title"},
|
| 51 |
+
{"value": "SST", "label": "Summary Section Title"},
|
| 52 |
+
{"value": "SSC", "label": "Summary Section Content"},
|
| 53 |
+
{"value": "AST", "label": "Accomplishments Section Title"},
|
| 54 |
+
{"value": "ASC", "label": "Accomplishments Section Content"},
|
| 55 |
+
{"value": "EDST", "label": "Education Section Title"},
|
| 56 |
+
{"value": "EDSC", "label": "Education Section Content"},
|
| 57 |
+
{"value": "SKST", "label": "Skills Section Title"},
|
| 58 |
+
{"value": "SKSC", "label": "Skills Section Content"},
|
| 59 |
+
{"value": "HST", "label": "Highlights Section Title"},
|
| 60 |
+
{"value": "HSC", "label": "Highlights Section Content"},
|
| 61 |
+
{"value": "CST", "label": "Certifications Section Title"},
|
| 62 |
+
{"value": "CSC", "label": "Certifications Section Content"},
|
| 63 |
+
{"value": "EST", "label": "Experience Section Title"},
|
| 64 |
+
{"value": "EJT", "label": "Experience Job Title"},
|
| 65 |
+
{"value": "EDT", "label": "Experience Date Range Title"},
|
| 66 |
+
{"value": "ECT", "label": "Experience Company Title"},
|
| 67 |
+
{"value": "EDC", "label": "Experience Description Content"}
|
| 68 |
+
]
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
self.Resume_tokenizer = LongformerTokenizer.from_pretrained("allenai/longformer-base-4096")
|
| 72 |
+
self.Resume_tokenizer.cls_token
|
| 73 |
+
|
| 74 |
+
# Load model architecture
|
| 75 |
+
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 76 |
+
self.Resume_model = LongformerSentenceClassifier(num_labels=Resume_num_labels)
|
| 77 |
+
self.Resume_model.to(self.device)
|
| 78 |
+
# Load trained weights
|
| 79 |
+
self.Resume_model.load_state_dict(torch.load(path + "/ResumeSegmentClassifier8thEpochV3.pth", map_location=self.device))
|
| 80 |
+
|
| 81 |
+
# Set model to evaluation mode
|
| 82 |
+
self.Resume_model.eval()
|
| 83 |
+
nlp = spacy.load("en_core_web_lg")
|
| 84 |
+
self.skill_extractor = SkillExtractor(nlp, SKILL_DB, PhraseMatcher)
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
def predict_resume_sections(self, model, text, tokenizer, device):
|
| 88 |
+
model.eval()
|
| 89 |
+
|
| 90 |
+
# Tokenize text and get input tensors
|
| 91 |
+
encoding = tokenizer(
|
| 92 |
+
text,
|
| 93 |
+
return_tensors="pt",
|
| 94 |
+
truncation=True,
|
| 95 |
+
padding="max_length",
|
| 96 |
+
max_length=4096
|
| 97 |
+
)
|
| 98 |
+
|
| 99 |
+
input_ids = encoding["input_ids"].to(device)
|
| 100 |
+
attention_mask = encoding["attention_mask"].to(device)
|
| 101 |
+
|
| 102 |
+
# Identify `[CLS]` positions (assuming each sentence starts with `[CLS]`)
|
| 103 |
+
cls_positions = (input_ids == tokenizer.cls_token_id).nonzero(as_tuple=True)[1]
|
| 104 |
+
cls_positions = cls_positions.unsqueeze(0).to(device) # Shape: (1, num_sentences)
|
| 105 |
+
|
| 106 |
+
# Create global attention mask (Longformer requires at least 1 global attention token)
|
| 107 |
+
global_attention_mask = torch.zeros_like(input_ids)
|
| 108 |
+
global_attention_mask[:, cls_positions] = 1 # Assign global attention to `[CLS]` tokens
|
| 109 |
+
|
| 110 |
+
# Run the model
|
| 111 |
+
with torch.no_grad():
|
| 112 |
+
logits = model(
|
| 113 |
+
input_ids=input_ids,
|
| 114 |
+
attention_mask=attention_mask,
|
| 115 |
+
global_attention_mask=global_attention_mask,
|
| 116 |
+
cls_positions=cls_positions
|
| 117 |
+
) # Shape: (1, num_sentences, num_labels)
|
| 118 |
+
|
| 119 |
+
logits = logits.squeeze(0) # Shape: (num_sentences, num_labels)
|
| 120 |
+
probs = F.softmax(logits, dim=-1) # Convert logits to probabilities
|
| 121 |
+
predictions = torch.argmax(probs, dim=-1) # Get predicted label indices
|
| 122 |
+
|
| 123 |
+
return predictions.cpu().numpy() # Convert to NumPy array for easy use
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
def capture_sentences(self, lines):
|
| 128 |
+
combined_text = " ".join(lines) # Merge all lines into one string
|
| 129 |
+
sentences = re.split(r"(?<=\.)\s+|(?<=\!)\s+|(?<=\?)\s+", combined_text) # Split by ., !, ?
|
| 130 |
+
return [sentence.strip() for sentence in sentences if sentence.strip()] # Remove extra spaces
|
| 131 |
+
|
| 132 |
+
def extract_resume_sections(self, text):
|
| 133 |
+
lines = text.splitlines()
|
| 134 |
+
lines = [line for line in text.splitlines() if line.strip()]
|
| 135 |
+
text = lines
|
| 136 |
+
|
| 137 |
+
concatenated_text = " ".join(f"{self.Resume_tokenizer.cls_token} {sentence}" for sentence in text)
|
| 138 |
+
|
| 139 |
+
predictions = self.predict_resume_sections(self.Resume_model, concatenated_text, self.Resume_tokenizer, self.device)
|
| 140 |
+
return predictions, text
|
| 141 |
+
|
| 142 |
+
def extract_resume_roles(self, text):
|
| 143 |
+
lines = text.splitlines()
|
| 144 |
+
lines = [line for line in text.splitlines() if line.strip()]
|
| 145 |
+
text = lines
|
| 146 |
+
|
| 147 |
+
concatenated_text = " ".join(f"{self.Resume_tokenizer.cls_token} {sentence}" for sentence in text)
|
| 148 |
+
|
| 149 |
+
predictions = self.predict_resume_sections(self.Resume_model, concatenated_text, self.Resume_tokenizer, self.device)
|
| 150 |
+
|
| 151 |
+
# Array of roles
|
| 152 |
+
# [
|
| 153 |
+
# {"title": [], "description": []},
|
| 154 |
+
# {"title": [], "description": []}
|
| 155 |
+
# ]
|
| 156 |
+
roles = []
|
| 157 |
+
|
| 158 |
+
i = -1
|
| 159 |
+
for item in predictions[:len(predictions) - 1]:
|
| 160 |
+
# ----- do not touch -----
|
| 161 |
+
i+=1
|
| 162 |
+
# If role array is empty, insert new role
|
| 163 |
+
if len(roles) == 0 and self.Resume_labels[item]['value'] == "EJT":
|
| 164 |
+
roles.append({"title": [lines[i]], "description": []})
|
| 165 |
+
continue
|
| 166 |
+
# if len(roles) == 0 and Resume_labels[item]['value'] == "EDC":
|
| 167 |
+
# roles.append({"title": [], "description": [lines[i]]})
|
| 168 |
+
|
| 169 |
+
# If element is a title
|
| 170 |
+
if self.Resume_labels[item]['value'] == "EJT":
|
| 171 |
+
# If previous element doesn't have a description, append the title
|
| 172 |
+
if len(roles[len(roles) - 1]["description"]) < 1:
|
| 173 |
+
roles[len(roles) - 1]["title"].append(lines[i])
|
| 174 |
+
continue
|
| 175 |
+
# If previous element already has a description, create a new role
|
| 176 |
+
if len(roles[len(roles) - 1]["description"]) > 0:
|
| 177 |
+
roles.append({"title": [lines[i]], "description": []})
|
| 178 |
+
continue
|
| 179 |
+
|
| 180 |
+
# If element is a description, directly append to the last role in the array
|
| 181 |
+
if self.Resume_labels[item]['value'] == "EDC":
|
| 182 |
+
if roles:
|
| 183 |
+
roles[-1]["description"].append(lines[i])
|
| 184 |
+
else:
|
| 185 |
+
# Optionally, log the error or create a default role
|
| 186 |
+
print("Warning: Description found but no role header exists. Skipping this description.")
|
| 187 |
+
|
| 188 |
+
# Cleaning description
|
| 189 |
+
for item in roles:
|
| 190 |
+
sentences = self.capture_sentences(item['description'])
|
| 191 |
+
item['description'] = sentences
|
| 192 |
+
|
| 193 |
+
return roles
|
| 194 |
+
|
| 195 |
+
|
| 196 |
+
def parse_date(self, date_str):
|
| 197 |
+
"""Tries multiple formats to parse a date string into a datetime object.
|
| 198 |
+
|
| 199 |
+
- Returns the current date if 'present' or 'current' is given.
|
| 200 |
+
- Tries multiple formats and prompts if ambiguous.
|
| 201 |
+
"""
|
| 202 |
+
|
| 203 |
+
# Handle cases like "present", "current"
|
| 204 |
+
present_keywords = {"present", "current", "now", "today"}
|
| 205 |
+
if date_str.strip().lower() in present_keywords:
|
| 206 |
+
return datetime.today() # Return the current date
|
| 207 |
+
|
| 208 |
+
date_formats = [
|
| 209 |
+
("%b %Y", "MMM YYYY"), # Jun 2022
|
| 210 |
+
("%B %Y", "MMMM YYYY"), # June 2022
|
| 211 |
+
("%Y-%m-%d", "YYYY-MM-DD"), # 2022-06-01
|
| 212 |
+
("%Y/%m/%d", "YYYY/MM/DD"), # 2022/06/01
|
| 213 |
+
("%Y.%m.%d", "YYYY.MM.DD"), # 2022.06.01
|
| 214 |
+
("%d-%m-%Y", "DD-MM-YYYY"), # 01-06-2022
|
| 215 |
+
("%d/%m/%Y", "DD/MM/YYYY"), # 01/06/2022
|
| 216 |
+
("%d.%m.%Y", "DD.MM.YYYY"), # 01.06.2022
|
| 217 |
+
("%m-%d-%Y", "MM-DD-YYYY"), # 06-01-2022 (US format)
|
| 218 |
+
("%m/%d/%Y", "MM/DD/YYYY"), # 06/01/2022 (US format)
|
| 219 |
+
("%m.%d.%Y", "MM.DD.YYYY"), # 06.01.2022
|
| 220 |
+
("%d %b %Y", "DD MMM YYYY"), # 01 Jun 2022
|
| 221 |
+
("%d %B %Y", "DD MMMM YYYY"), # 01 June 2022
|
| 222 |
+
("%b-%d-%Y", "MMM-DD-YYYY"), # Jun-01-2022
|
| 223 |
+
("%b/%d/%Y", "MMM/DD/YYYY"), # Jun/01/2022
|
| 224 |
+
("%B-%d-%Y", "MMMM-DD-YYYY"), # June-01-2022
|
| 225 |
+
("%B/%d/%Y", "MMMM/DD/YYYY"), # June/01/2022
|
| 226 |
+
("%d-%b-%Y", "DD-MMM-YYYY"), # 01-Jun-2022
|
| 227 |
+
("%d/%b/%Y", "DD/MMM/YYYY"), # 01/Jun/2022
|
| 228 |
+
("%d-%B-%Y", "DD-MMMM-YYYY"), # 01-June-2022
|
| 229 |
+
("%d/%B/%Y", "DD/MMMM/YYYY"), # 01/June/2022
|
| 230 |
+
("%Y", "YYYY"), # 2022 (Only Year)
|
| 231 |
+
("%m/%Y", "MM/YYYY"), # 06/2022
|
| 232 |
+
("%m-%Y", "MM-YYYY"), # 06-2022
|
| 233 |
+
("%m.%Y", "MM.YYYY"), # 06.2022
|
| 234 |
+
("%Y%m%d", "YYYYMMDD"), # 20220601 (Compact format)
|
| 235 |
+
("%d%m%Y", "DDMMYYYY"), # 01062022 (Compact format)
|
| 236 |
+
("%m%d%Y", "MMDDYYYY"), # 06012022 (Compact format)
|
| 237 |
+
("%Y-%b-%d", "YYYY-MMM-DD"), # 2022-Jun-01
|
| 238 |
+
("%Y/%b/%d", "YYYY/MMM/DD"), # 2022/Jun/01
|
| 239 |
+
("%Y-%B-%d", "YYYY-MMMM-DD"), # 2022-June-01
|
| 240 |
+
("%Y/%B/%d", "YYYY/MMMM/DD"), # 2022/June/01
|
| 241 |
+
("%d-%b-%y", "DD-MMM-YY"), # 01-Jun-22 (Two-digit year)
|
| 242 |
+
("%d/%b/%y", "DD/MMM/YY"), # 01/Jun/22 (Two-digit year)
|
| 243 |
+
("%d-%B-%y", "DD-MMMM-YY"), # 01-June-22 (Two-digit year)
|
| 244 |
+
("%d/%B/%y", "DD/MMMM/YY"), # 01/June/22 (Two-digit year)
|
| 245 |
+
("%d-%m-%y", "DD-MM-YY"), # 01-06-22 (Two-digit year)
|
| 246 |
+
("%d/%m/%y", "DD/MM/YY"), # 01/06/22 (Two-digit year)
|
| 247 |
+
("%m-%d-%y", "MM-DD-YY"), # 06-01-22 (US format with two-digit year)
|
| 248 |
+
("%m/%d/%y", "MM/DD/YY"), # 06/01/22 (US format with two-digit year)
|
| 249 |
+
("%A, %d %B %Y", "Day, DD MMMM YYYY"), # Wednesday, 01 June 2022
|
| 250 |
+
("%a, %d %b %Y", "Day Abbr, DD MMM YYYY"), # Wed, 01 Jun 2022
|
| 251 |
+
]
|
| 252 |
+
|
| 253 |
+
possible_dates = []
|
| 254 |
+
|
| 255 |
+
for fmt, fmt_name in date_formats:
|
| 256 |
+
try:
|
| 257 |
+
parsed_date = datetime.strptime(date_str, fmt)
|
| 258 |
+
possible_dates.append((parsed_date, fmt_name))
|
| 259 |
+
except ValueError:
|
| 260 |
+
continue # Try next format
|
| 261 |
+
|
| 262 |
+
# No valid format found
|
| 263 |
+
if not possible_dates:
|
| 264 |
+
# raise ValueError(f"Could not parse the date: {date_str}")
|
| 265 |
+
return []
|
| 266 |
+
|
| 267 |
+
# If only one valid interpretation, return it
|
| 268 |
+
if len(possible_dates) == 1:
|
| 269 |
+
return possible_dates[0][0] # Return datetime object
|
| 270 |
+
|
| 271 |
+
# If multiple interpretations exist, prompt user
|
| 272 |
+
print(f"Ambiguous date: '{date_str}' could mean:")
|
| 273 |
+
for idx, (date, fmt_name) in enumerate(possible_dates):
|
| 274 |
+
print(f"{idx + 1}. {date.strftime('%Y-%m-%d')} ({fmt_name})")
|
| 275 |
+
|
| 276 |
+
print("Defaulted to: ", possible_dates[0][1])
|
| 277 |
+
return possible_dates[0][0] # Return chosen date
|
| 278 |
+
|
| 279 |
+
def label_resume(self, text):
|
| 280 |
+
results = self.extract_resume_roles(text)
|
| 281 |
+
for item in results:
|
| 282 |
+
# Extracting dates
|
| 283 |
+
context = (" ".join(item["title"]))
|
| 284 |
+
date_started = "2020-01-01" # Random start date
|
| 285 |
+
date_ended = "2023-12-31" # Random end date
|
| 286 |
+
|
| 287 |
+
# Try parsing the dates; default to 0 for role_length if parsing fails.
|
| 288 |
+
try:
|
| 289 |
+
date_started_formatted = self.parse_date(date_started)
|
| 290 |
+
except ValueError:
|
| 291 |
+
date_started_formatted = None
|
| 292 |
+
|
| 293 |
+
# date_started_formatted = parse_date(date_started)
|
| 294 |
+
# date_ended_formatted = parse_date(date_ended)
|
| 295 |
+
try:
|
| 296 |
+
date_ended_formatted = self.parse_date(date_ended)
|
| 297 |
+
except ValueError:
|
| 298 |
+
date_ended_formatted = None
|
| 299 |
+
|
| 300 |
+
try:
|
| 301 |
+
role_length = self.extract_length(date_started_formatted, date_ended_formatted)
|
| 302 |
+
except:
|
| 303 |
+
role_length = 0
|
| 304 |
+
item["dates"] = {"date_started": date_started, "date_ended": date_ended}
|
| 305 |
+
item["role_length"] = role_length
|
| 306 |
+
|
| 307 |
+
# Extracting Skills
|
| 308 |
+
item["skills"] = []
|
| 309 |
+
seen = set()
|
| 310 |
+
annotations = self.skill_extractor.annotate(" ".join(item["description"]))
|
| 311 |
+
if 'results' in annotations and 'full_matches' in annotations['results']:
|
| 312 |
+
for result in annotations['results']['full_matches']:
|
| 313 |
+
# Standardizing the skill names
|
| 314 |
+
skill_info = SKILL_DB.get(result["skill_id"], {})
|
| 315 |
+
skill_name = skill_info.get('skill_name', 'Unknown Skill')
|
| 316 |
+
if skill_name not in seen:
|
| 317 |
+
seen.add(skill_name)
|
| 318 |
+
item["skills"].append({'name': skill_name, 'skill_id': result["skill_id"]})
|
| 319 |
+
if 'results' in annotations and 'ngram_scored' in annotations['results']:
|
| 320 |
+
for result in annotations['results']['ngram_scored']:
|
| 321 |
+
if result['score'] >= 1:
|
| 322 |
+
# Standardizing the skill names
|
| 323 |
+
skill_info = SKILL_DB.get(result["skill_id"], {})
|
| 324 |
+
skill_name = skill_info.get('skill_name', 'Unknown Skill')
|
| 325 |
+
if skill_name not in seen:
|
| 326 |
+
seen.add(skill_name)
|
| 327 |
+
item["skills"].append({'name': skill_name, 'skill_id': result["skill_id"]})
|
| 328 |
+
|
| 329 |
+
|
| 330 |
+
# for item in results:
|
| 331 |
+
# print(" -------- ROLE -------- ")
|
| 332 |
+
# print("Title: ", item["title"])
|
| 333 |
+
# print("Role Length: ", item["role_length"], " months")
|
| 334 |
+
# print("Dates: ", item["dates"])
|
| 335 |
+
# print("Skills: ", item["skills"])
|
| 336 |
+
# print("Description: ", item["description"])
|
| 337 |
+
# print("")
|
| 338 |
+
# print("")
|
| 339 |
+
|
| 340 |
+
return results
|
| 341 |
+
|
| 342 |
+
# SAMPLE OUTPUT
|
| 343 |
+
# [
|
| 344 |
+
# {
|
| 345 |
+
# "title": ['Full-stack Developer- Ospree.io - Jun. 2022 - Present (2 yrs, 1 mos)'],
|
| 346 |
+
# "role_length": 33 months,
|
| 347 |
+
# "dates": {'date_started': 'Jun 2022', 'date_ended': 'Present'},
|
| 348 |
+
# "skills": [{'name': 'Cascading Style Sheets (CSS)', 'skill_id': 'KS121F45VPV8C9W3QFYH'}, {'name': 'JavaScript (Programming Language)', 'skill_id': 'KS1200771D9CR9LB4MWW'}, {'name': 'React.js', 'skill_id': 'KSDJCA4E89LB98JAZ7LZ'}, {'name': 'React Redux', 'skill_id': 'KSQOOX1S2DYD0E1VVZ5X'}, {'name': 'Integration', 'skill_id': 'KS125716TLTGH6SDHJD1'}, {'name': 'Custom Backend', 'skill_id': 'KS7R8G2D52QH187SED9R'}, {'name': 'Architectural Design', 'skill_id': 'KS120MG6W03JCFGKFHHC'}, {'name': 'Python (Programming Language)', 'skill_id': 'KS125LS6N7WP4S6SFTCK'}, {'name': 'PostgreSQL', 'skill_id': 'KS125TB6YR6236RKM563'}, {'name': 'SQLAlchemy', 'skill_id': 'KS440WD72WMZLQT09C91'}]
|
| 349 |
+
# "description": ['● Acted as the sole frontend developer that developed the frontend of all core features for the product using raw css,', 'javascript, ReactJS, React Query, and Redux, ensuring seamless integration with backend endpoints.', '● Built 3 out of 4 of the core backend modules based on the architecture designed by the software architect, utilizing Python,', 'FastAPI, PostgreSQL, and SQLAlchemy while integrating each module with third-party APIs.', 'database, ensuring data accuracy and availability for application use.', "● Contributed significantly to the development of the product, which played a key role in the startup's acceptance into", 'T echstars and securing USD$120K in funding.'],
|
| 350 |
+
# }
|
| 351 |
+
# ]
|
| 352 |
+
|
| 353 |
+
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
|
| 354 |
+
"""
|
| 355 |
+
data args:
|
| 356 |
+
inputs (:obj: `str` | `PIL.Image` | `np.array`)
|
| 357 |
+
kwargs
|
| 358 |
+
Return:
|
| 359 |
+
A :obj:`list` | `dict`: will be serialized and returned
|
| 360 |
+
"""
|
| 361 |
+
text = data['inputs']
|
| 362 |
+
# predictions, text = self.extract_job_sections(text)
|
| 363 |
+
# requirements = self.extract_job_requirements(text)
|
| 364 |
+
label_resume = self.label_resume(text)
|
| 365 |
+
return label_resume
|
| 366 |
+
|
| 367 |
+
|
| 368 |
+
class LongformerSentenceClassifier(nn.Module):
|
| 369 |
+
def __init__(self, model_name="allenai/longformer-base-4096", num_labels=Resume_num_labels):
|
| 370 |
+
"""
|
| 371 |
+
Custom Longformer model for sentence classification.
|
| 372 |
+
|
| 373 |
+
Args:
|
| 374 |
+
model_name (str): Hugging Face Longformer model.
|
| 375 |
+
num_labels (int): Number of possible sentence labels.
|
| 376 |
+
"""
|
| 377 |
+
super(LongformerSentenceClassifier, self).__init__()
|
| 378 |
+
self.longformer = LongformerModel.from_pretrained(model_name)
|
| 379 |
+
self.classifier = nn.Linear(self.longformer.config.hidden_size, num_labels)
|
| 380 |
+
|
| 381 |
+
def forward(self, input_ids, attention_mask, global_attention_mask, cls_positions):
|
| 382 |
+
"""
|
| 383 |
+
Forward pass for sentence classification.
|
| 384 |
+
|
| 385 |
+
Args:
|
| 386 |
+
input_ids (Tensor): Tokenized input IDs, shape (batch_size, max_length)
|
| 387 |
+
attention_mask (Tensor): Attention mask, shape (batch_size, max_length)
|
| 388 |
+
global_attention_mask (Tensor): Global attention mask, shape (batch_size, max_length)
|
| 389 |
+
cls_positions (List[Tensor]): Indices of `[CLS]` tokens for each batch element.
|
| 390 |
+
"""
|
| 391 |
+
outputs = self.longformer(
|
| 392 |
+
input_ids=input_ids,
|
| 393 |
+
attention_mask=attention_mask,
|
| 394 |
+
global_attention_mask=global_attention_mask
|
| 395 |
+
)
|
| 396 |
+
|
| 397 |
+
last_hidden_state = outputs.last_hidden_state
|
| 398 |
+
cls_positions = cls_positions.view(input_ids.shape[0], -1)
|
| 399 |
+
cls_embeddings = last_hidden_state.gather(1, cls_positions.unsqueeze(-1).expand(-1, -1, last_hidden_state.size(-1)))
|
| 400 |
+
logits = self.classifier(cls_embeddings)
|
| 401 |
+
|
| 402 |
+
return logits
|
| 403 |
+
|
| 404 |
+
|
| 405 |
+
|
| 406 |
+
if __name__ == "__main__":
|
| 407 |
+
# init handler
|
| 408 |
+
my_handler = EndpointHandler(path=".")
|
| 409 |
+
|
| 410 |
+
# prepare sample payload
|
| 411 |
+
payload = {"inputs": """
|
| 412 |
+
CASHIER
|
| 413 |
+
|
| 414 |
+
|
| 415 |
+
Professional Summary
|
| 416 |
+
Results-oriented, strategic sales professional with two years in the Retail industry. Cashier who is highly energetic, outgoing and detail-oriented. Handles multiple responsibilities simultaneously while providing exceptional customer service. Reliable and friendly team member who quickly learns and masters new concepts and skills. Passionate about helping customers and creating a satisfying shopping experience.
|
| 417 |
+
|
| 418 |
+
|
| 419 |
+
Core Qualifications
|
| 420 |
+
• Excellent multi-tasker
|
| 421 |
+
• Strong communication skills
|
| 422 |
+
• Flexible schedule
|
| 423 |
+
• Proficient in MS Office
|
| 424 |
+
Cash handling accuracy
|
| 425 |
+
Mathematical aptitude
|
| 426 |
+
Organized
|
| 427 |
+
Time management
|
| 428 |
+
Detail-oriented
|
| 429 |
+
|
| 430 |
+
|
| 431 |
+
Experience
|
| 432 |
+
Cashier
|
| 433 |
+
October 2014 to Current
|
| 434 |
+
Company Name - City , State
|
| 435 |
+
• Receive payment by cash, check, credit cards, vouchers, or automatic debits.
|
| 436 |
+
• Issue receipts, refunds, credits, or change due to customers.
|
| 437 |
+
• Assist customers by providing information and resolving their complaints.
|
| 438 |
+
• Establish or identify prices of goods, services or admission, and tabulate bills using calculators, cash registers, or optical price scanners.
|
| 439 |
+
• Greet customers entering establishments.
|
| 440 |
+
• Answer customers' questions, and provide information on procedures or policies.
|
| 441 |
+
• Process merchandise returns and exchanges.
|
| 442 |
+
• Maintain clean and orderly checkout areas and complete other general cleaning duties, such as mopping floors and emptying trash cans.
|
| 443 |
+
• Stock shelves, and mark prices on shelves and items.
|
| 444 |
+
• Count money in cash drawers at the beginning of shifts to ensure that amounts are correct and that there is adequate change.
|
| 445 |
+
• Calculate total payments received during a time period, and reconcile this with total sales.
|
| 446 |
+
• Monitor checkout stations to ensure that they have adequate cash available and that they are staffed appropriately.
|
| 447 |
+
• Assist with duties in other areas of the store, such as monitoring fitting rooms or bagging and carrying out customers' items.
|
| 448 |
+
• Sort, count, and wrap currency and coins.
|
| 449 |
+
• Compute and record totals of transactions.
|
| 450 |
+
• Compile and maintain non-monetary reports and records.
|
| 451 |
+
• Weigh items sold by weight to determine prices.
|
| 452 |
+
• Cash checks for customers.
|
| 453 |
+
|
| 454 |
+
Inbound/Return
|
| 455 |
+
June 2014 to September 2014
|
| 456 |
+
Company Name - City , State
|
| 457 |
+
Changed equipment over to new product.Maintained proper stock levels on a line.Helped achieve company goals by supporting production workers.
|
| 458 |
+
|
| 459 |
+
Cashier
|
| 460 |
+
February 2014 to June 2014
|
| 461 |
+
Company Name - City , State
|
| 462 |
+
• Receive payment by cash, check, credit cards, vouchers, or automatic debits.
|
| 463 |
+
• Issue receipts, refunds, credits, or change due to customers.
|
| 464 |
+
• Assist customers by providing information and resolving their complaints.
|
| 465 |
+
• Establish or identify prices of goods, services or admission, and tabulate bills using calculators, cash registers, or optical price scanners.
|
| 466 |
+
• Greet customers entering establishments.
|
| 467 |
+
• Answer customers' questions, and provide information on procedures or policies.
|
| 468 |
+
• Process merchandise returns and exchanges.
|
| 469 |
+
• Maintain clean and orderly checkout areas and complete other general cleaning duties, such as mopping floors and emptying trash cans.
|
| 470 |
+
• Stock shelves, and mark prices on shelves and items.
|
| 471 |
+
• Count money in cash drawers at the beginning of shifts to ensure that amounts are correct and that there is adequate change.
|
| 472 |
+
• Calculate total payments received during a time period, and reconcile this with total sales.
|
| 473 |
+
• Monitor checkout stations to ensure that they have adequate cash available and that they are staffed appropriately.
|
| 474 |
+
• Assist with duties in other areas of the store, such as monitoring fitting rooms or bagging and carrying out customers' items.
|
| 475 |
+
• Sort, count, and wrap currency and coins.
|
| 476 |
+
• Compute and record totals of transactions.
|
| 477 |
+
• Compile and maintain non-monetary reports and records.
|
| 478 |
+
• Weigh items sold by weight to determine prices.
|
| 479 |
+
• Cash checks for customers.
|
| 480 |
+
|
| 481 |
+
Apparel Associate
|
| 482 |
+
January 2014 to February 2014
|
| 483 |
+
Company Name - City , State
|
| 484 |
+
• Greet customers and ascertain what each customer wants or needs.
|
| 485 |
+
• Describe merchandise and explain use, operation, and care of merchandise to customers.
|
| 486 |
+
• Recommend, select, and help locate or obtain merchandise based on customer needs and desires.
|
| 487 |
+
• Compute sales prices, total purchases and receive and process cash or credit payment.
|
| 488 |
+
• Answer questions regarding the store and its merchandise.
|
| 489 |
+
• Maintain knowledge of current sales and promotions, policies regarding payment and exchanges, and security practices.
|
| 490 |
+
• Maintain records related to sales.
|
| 491 |
+
• Watch for and recognize security risks and thefts, and know how to prevent or handle these situations.
|
| 492 |
+
• Inventory stock and requisition new stock.
|
| 493 |
+
• Help customers try on or fit merchandise.
|
| 494 |
+
• Clean shelves, counters, and tables.
|
| 495 |
+
• Exchange merchandise for customers and accept returns.
|
| 496 |
+
• Open and close cash registers, performing tasks such as counting money, separating charge slips, coupons, and vouchers, balancing cash drawers, and making deposits.
|
| 497 |
+
|
| 498 |
+
Apparel Associate
|
| 499 |
+
October 2013 to December 2013
|
| 500 |
+
Company Name - City , State
|
| 501 |
+
• Greet customers and ascertain what each customer wants or needs.
|
| 502 |
+
• Describe merchandise and explain use, operation, and care of merchandise to customers.
|
| 503 |
+
• Recommend, select, and help locate or obtain merchandise based on customer needs and desires.
|
| 504 |
+
• Compute sales prices, total purchases and receive and process cash or credit payment.
|
| 505 |
+
• Answer questions regarding the store and its merchandise.
|
| 506 |
+
• Maintain knowledge of current sales and promotions, policies regarding payment and exchanges, and security practices.
|
| 507 |
+
• Maintain records related to sales.
|
| 508 |
+
• Watch for and recognize security risks and thefts, and know how to prevent or handle these situations.
|
| 509 |
+
• Inventory stock and requisition new stock.
|
| 510 |
+
• Help customers try on or fit merchandise.
|
| 511 |
+
• Clean shelves, counters, and tables.
|
| 512 |
+
• Exchange merchandise for customers and accept returns.
|
| 513 |
+
• Open and close cash registers, performing tasks such as counting money, separating charge slips, coupons, and vouchers, balancing cash drawers, and making deposits.
|
| 514 |
+
|
| 515 |
+
Cashier
|
| 516 |
+
August 2012 to August 2013
|
| 517 |
+
Company Name - City , State
|
| 518 |
+
• Receive payment by cash, check, credit cards, vouchers, or automatic debits.
|
| 519 |
+
• Issue receipts, refunds, credits, or change due to customers.
|
| 520 |
+
• Assist customers by providing information and resolving their complaints.
|
| 521 |
+
• Establish or identify prices of goods, services or admission, and tabulate bills using calculators, cash registers, or optical price scanners.
|
| 522 |
+
• Greet customers entering establishments.
|
| 523 |
+
• Answer customers' questions, and provide information on procedures or policies.
|
| 524 |
+
• Process merchandise returns and exchanges.
|
| 525 |
+
• Maintain clean and orderly checkout areas and complete other general cleaning duties, such as mopping floors and emptying trash cans.
|
| 526 |
+
• Stock shelves, and mark prices on shelves and items.
|
| 527 |
+
• Count money in cash drawers at the beginning of shifts to ensure that amounts are correct and that there is adequate change.
|
| 528 |
+
• Calculate total payments received during a time period, and reconcile this with total sales.
|
| 529 |
+
• Monitor checkout stations to ensure that they have adequate cash available and that they are staffed appropriately.
|
| 530 |
+
• Assist with duties in other areas of the store, such as monitoring fitting rooms or bagging and carrying out customers' items.
|
| 531 |
+
• Sort, count, and wrap currency and coins.
|
| 532 |
+
• Compute and record totals of transactions.
|
| 533 |
+
• Compile and maintain non-monetary reports and records.
|
| 534 |
+
• Weigh items sold by weight to determine prices.
|
| 535 |
+
• Cash checks for customers.
|
| 536 |
+
|
| 537 |
+
|
| 538 |
+
Education
|
| 539 |
+
5 2013
|
| 540 |
+
Member of FFA, FCA, Pep Club, and mentoring children from one of the public elementary schools
|
| 541 |
+
|
| 542 |
+
|
| 543 |
+
Skills
|
| 544 |
+
• Calculators
|
| 545 |
+
• Cash registers
|
| 546 |
+
• Credit, debit, checks and money
|
| 547 |
+
• Inventory
|
| 548 |
+
• Sales, scanners, tables
|
| 549 |
+
"""}
|
| 550 |
+
# holiday_payload = {"inputs": "Today is a though day"}
|
| 551 |
+
|
| 552 |
+
# test the handler
|
| 553 |
+
non_holiday_pred=my_handler(payload)
|
| 554 |
+
# holiday_payload=my_handler(holiday_payload)
|
| 555 |
+
|
| 556 |
+
# show results
|
| 557 |
+
print(non_holiday_pred)
|
| 558 |
+
# print("holiday_payload", holiday_payload)
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
skillNer>=1.0.0
|
| 2 |
+
spacy>=3.7.2
|
| 3 |
+
en-core-web-lg @ https://github.com/explosion/spacy-models/releases/download/en_core_web_lg-3.7.0/en_core_web_lg-3.7.0-py3-none-any.whl
|
| 4 |
+
ipython>=8.12.0
|
skill_db_relax_20.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
token_dist.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|