Download app.py from Tushar14/Lab_hr: direct link, hf CLI and curl.
- Browser
- Download file 5.46 kB
-
https://huggingface.co/spaces/Tushar14/Lab_hr/resolve/main/app.py
- Command line
-
hf download hf://spaces/Tushar14/Lab_hr/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Tushar14/Lab_hr/resolve/main/app.py
5.46 kB
| import os | |
| import re | |
| import json | |
| import time | |
| import gradio as gr | |
| import pandas as pd | |
| import requests | |
| from discrimination_model import detect_discrimination | |
| # ββ LLM backend selector βββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| HF_TOKEN = os.environ.get("HF_TOKEN", "") | |
| SYSTEM_PROMPT = """You are an expert HR compliance analyst. | |
| Analyse the job posting and identify discriminatory elements from this list: | |
| - Gender: gendered job titles or language implying a preferred gender | |
| - Age: explicit age limits or age-suggestive language | |
| - Origin: nationality, ethnicity, or geographic residency requirements | |
| - Mobility: unjustified driving licence or own vehicle requirement | |
| - Salary: undisclosed or vague compensation (e.g. "to be defined", "commensurate with experience") | |
| - Standing: appearance, weight, height, or "professional look" requirements | |
| - PPE: candidate required to supply own safety equipment | |
| - Role clarity: vague or contradictory role description | |
| - None: if fully compliant | |
| Return ONLY valid JSON β no markdown fences, no extra text: | |
| {"discriminatory_elements": ["Element1", "Element2"], "explanation": "brief reason per element"}""" | |
| def llm_detect(text: str) -> tuple[str, str]: | |
| """Call LLM backend. Returns (categories_str, explanation_str).""" | |
| # ββ Option A: Llama-3.2-1B-Instruct via HF Inference API ββββββββββββββββ | |
| if HF_TOKEN: | |
| try: | |
| # Llama-3 chat template | |
| prompt = ( | |
| "<|begin_of_text|>" | |
| "<|start_header_id|>system<|end_header_id|>\n" | |
| f"{SYSTEM_PROMPT}" | |
| "<|eot_id|>" | |
| "<|start_header_id|>user<|end_header_id|>\n" | |
| f"Job posting:\n\n{text}" | |
| "<|eot_id|>" | |
| "<|start_header_id|>assistant<|end_header_id|>\n" | |
| ) | |
| resp = requests.post( | |
| "https://api-inference.huggingface.co/models/meta-llama/Llama-3.2-1B-Instruct", | |
| headers={"Authorization": f"Bearer {HF_TOKEN}"}, | |
| json={ | |
| "inputs": prompt, | |
| "parameters": { | |
| "max_new_tokens": 300, | |
| "temperature": 0.1, | |
| "return_full_text": False, | |
| }, | |
| }, | |
| timeout=60, | |
| ) | |
| resp.raise_for_status() | |
| output = resp.json() | |
| generated = output[0]["generated_text"] if isinstance(output, list) else str(output) | |
| # Pull the first JSON object out of the response | |
| match = re.search(r'\{.*?\}', generated, re.DOTALL) | |
| if match: | |
| data = json.loads(match.group()) | |
| elements = data.get("discriminatory_elements", ["None"]) | |
| explanation = data.get("explanation", "") | |
| return ", ".join(elements), explanation | |
| # If model didn't return valid JSON, surface the raw text for debug | |
| return "Parsing error", generated[:300] | |
| except Exception as e: | |
| return "Error (Llama)", str(e) | |
| # ββ Fallback: keyword rules (no API key needed) βββββββββββββββββββββββββββ | |
| return detect_discrimination(text) | |
| def analyze_file(file): | |
| if file is None: | |
| return None, None | |
| df = pd.read_excel(file) | |
| # Accept either column name used in the exercise file | |
| text_col = None | |
| for candidate in ["Job post text", "job_post", "text", "job post"]: | |
| if candidate in df.columns: | |
| text_col = candidate | |
| break | |
| if text_col is None: | |
| raise ValueError("Could not find job text column. Expected: 'Job post text' or 'job_post'") | |
| categories, explanations = [], [] | |
| for text in df[text_col]: | |
| cat, exp = llm_detect(str(text)) | |
| categories.append(cat) | |
| explanations.append(exp) | |
| time.sleep(0.3) # gentle rate-limit buffer | |
| df["Discrimination_Type"] = categories | |
| df["Explanation"] = explanations | |
| output_path = "/tmp/results.xlsx" | |
| df.to_excel(output_path, index=False) | |
| backend = "Llama-3.2-1B" if HF_TOKEN else "Keyword rules" | |
| print(f"Analysis complete β backend: {backend}, rows: {len(df)}") | |
| id_col = "Job post ID" if "Job post ID" in df.columns else df.columns[0] | |
| return df[[id_col, text_col, "Discrimination_Type", "Explanation"]], output_path | |
| # ββ Gradio UI βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| backend_label = "π¦ Llama-3.2-1B" if HF_TOKEN else "π Keyword rules" | |
| demo = gr.Interface( | |
| fn=analyze_file, | |
| inputs=gr.File(label="Upload Job Posts Excel (.xlsx)"), | |
| outputs=[ | |
| gr.Dataframe(label=f"Results β {backend_label}"), | |
| gr.File(label="Download results.xlsx"), | |
| ], | |
| title="π AI Job Post Discrimination Checker", | |
| description=( | |
| "Upload the job posts Excel file. " | |
| "Detects: **Gender Β· Age Β· Origin Β· Mobility Β· Salary Β· Standing Β· PPE Β· Role clarity**\n\n" | |
| f"Active backend: `{backend_label}`" | |
| ), | |
| allow_flagging="never", | |
| ) | |
| demo.launch(server_name="0.0.0.0", server_port=7860) |