File size: 5,462 Bytes
df53b4b 8b3404a d905070 fccf350 df53b4b 0fbb17d c0ec921 df53b4b f5224a3 df53b4b f5224a3 df53b4b c0ec921 df53b4b f5224a3 df53b4b f5224a3 df53b4b f5224a3 df53b4b f5224a3 df53b4b f5224a3 df53b4b f5224a3 df53b4b f5224a3 df53b4b f5224a3 df53b4b fccf350 aa36863 d905070 aa36863 df53b4b d905070 f5224a3 d1f1b50 df53b4b d1f1b50 f5224a3 d1f1b50 df53b4b f5224a3 8b2be61 df53b4b d905070 df53b4b c741d40 c0ec921 df53b4b c741d40 f5224a3 aa36863 f0fc1b5 df53b4b c0ec921 d905070 bdb15a7 df53b4b f5224a3 df53b4b f5224a3 df53b4b bdb15a7 8b3404a df53b4b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | 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) |