Download discrimination_model.py from Tushar14/Lab_hr: direct link, hf CLI and curl.
- Browser
- Download file 2.65 kB
-
https://huggingface.co/spaces/Tushar14/Lab_hr/resolve/main/discrimination_model.py
- Command line
-
hf download hf://spaces/Tushar14/Lab_hr/discrimination_model.py
-
curl -L -o discrimination_model.py https://huggingface.co/spaces/Tushar14/Lab_hr/resolve/main/discrimination_model.py
2.65 kB
| """ | |
| Keyword-based fallback detector. | |
| Used automatically when no API key is configured. | |
| With an API key, the LLM in app.py takes over and this is bypassed. | |
| """ | |
| import re | |
| discrimination_rules = { | |
| "Gender": [ | |
| "male only", "female only", | |
| "waitress", "waiter", "cameriera", "cameriere", | |
| "salesman", "saleswoman", "salesgirl", | |
| "young lady", "hostess", "stewardess", | |
| "seamstress", "fairy hands", | |
| "he will", "she will", "his duties", "her duties", | |
| ], | |
| "Age": [ | |
| "under 30", "under 35", "under 25", | |
| "age not exceeding", "max age", | |
| "recent graduate only", "junior only", | |
| "young professional", "young candidate", | |
| ], | |
| "Origin": [ | |
| "italian only", "eu nationals only", | |
| "must be italian", "native english speaker only", | |
| "south american nationality", | |
| "residence near", "residence in", | |
| "must reside in", "local candidates only", | |
| ], | |
| "Mobility": [ | |
| "own means of transportation", | |
| "own vehicle required", | |
| "own car required", | |
| "own transport", | |
| "driving licence required", | |
| "driver's license required", | |
| "patente di guida", | |
| ], | |
| "Salary": [ | |
| "to be defined", "to be agreed", | |
| "commensurate with experience", | |
| "salary not specified", | |
| "compensation tbd", | |
| "retribuzione da definire", | |
| ], | |
| "Standing": [ | |
| "professional appearance", | |
| "smart appearance", | |
| "good appearance", | |
| "presentable", | |
| "bella presenza", | |
| ], | |
| "PPE": [ | |
| "own safety equipment", | |
| "provide own ppe", | |
| "own protective equipment", | |
| "own hard hat", | |
| "own boots", | |
| ], | |
| "Religion": [ | |
| "christian only", "muslim only", | |
| "catholic preferred", "must be christian", | |
| ], | |
| "Disability": [ | |
| "physically fit only", "able bodied", | |
| "no disabilities", "full physical fitness required", | |
| ], | |
| } | |
| def clean_text(text: str) -> str: | |
| text = str(text).lower() | |
| text = re.sub(r"[^\w\s]", " ", text) | |
| return text | |
| def detect_discrimination(text: str) -> tuple[str, str]: | |
| cleaned = clean_text(text) | |
| found_cats, found_kws = [], [] | |
| for category, keywords in discrimination_rules.items(): | |
| for kw in keywords: | |
| if kw in cleaned: | |
| if category not in found_cats: | |
| found_cats.append(category) | |
| found_kws.append(kw) | |
| if not found_cats: | |
| return "None", "No discriminatory keywords detected" | |
| return ", ".join(found_cats), "Keywords found: " + ", ".join(found_kws) |