Tushar14 commited on
Commit
f5224a3
Β·
verified Β·
1 Parent(s): 0819f83

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -29
app.py CHANGED
@@ -9,9 +9,9 @@ from discrimination_model import detect_discrimination
9
 
10
  # ── LLM backend selector ─────────────────────────────────────────────────────
11
  # Set ONE of these in Space Settings β†’ Variables and secrets:
12
- # ANTHROPIC_API_KEY β†’ uses Claude (Option A, higher accuracy)
13
- # HF_TOKEN β†’ uses Mistral via HF Inference API (Option B, free)
14
- # If neither is set, falls back to the original keyword-based rules.
15
 
16
  ANTHROPIC_KEY = os.environ.get("ANTHROPIC_API_KEY", "")
17
  HF_TOKEN = os.environ.get("HF_TOKEN", "")
@@ -25,17 +25,17 @@ Analyse the job posting and identify discriminatory elements from this list:
25
  - Salary: undisclosed or vague compensation (e.g. "to be defined", "commensurate with experience")
26
  - Standing: appearance, weight, height, or "professional look" requirements
27
  - PPE: candidate required to supply own safety equipment
28
- - Role clarity: vague/contradictory description masking discriminatory intent
29
  - None: if fully compliant
30
 
31
- Return ONLY valid JSON, no markdown, no extra text:
32
- {"discriminatory_elements": ["Element1"], "explanation": "brief reason per element"}"""
33
 
34
 
35
  def llm_detect(text: str) -> tuple[str, str]:
36
  """Call LLM backend. Returns (categories_str, explanation_str)."""
37
 
38
- # ── Option A: Claude ─────────────────────────────────────────────────────
39
  if ANTHROPIC_KEY:
40
  try:
41
  resp = requests.post(
@@ -46,7 +46,7 @@ def llm_detect(text: str) -> tuple[str, str]:
46
  "content-type": "application/json",
47
  },
48
  json={
49
- "model": "claude-haiku-4-5-20251001", # fast + cheap
50
  "max_tokens": 512,
51
  "system": SYSTEM_PROMPT,
52
  "messages": [{"role": "user", "content": f"Job posting:\n\n{text}"}],
@@ -62,33 +62,52 @@ def llm_detect(text: str) -> tuple[str, str]:
62
  except Exception as e:
63
  return "Error (Claude)", str(e)
64
 
65
- # ── Option B: HF Inference API (Mistral-7B) ──────────────────────────────
66
  if HF_TOKEN:
67
  try:
 
68
  prompt = (
69
- f"<s>[INST] {SYSTEM_PROMPT}\n\nJob posting:\n\n{text} [/INST]"
 
 
 
 
 
 
 
70
  )
71
  resp = requests.post(
72
- "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.3",
73
  headers={"Authorization": f"Bearer {HF_TOKEN}"},
74
- json={"inputs": prompt, "parameters": {"max_new_tokens": 300, "temperature": 0.1}},
 
 
 
 
 
 
 
75
  timeout=60,
76
  )
77
  resp.raise_for_status()
78
  output = resp.json()
79
  generated = output[0]["generated_text"] if isinstance(output, list) else str(output)
80
- # Extract JSON from the response
81
- match = re.search(r'\{.*\}', generated, re.DOTALL)
 
82
  if match:
83
  data = json.loads(match.group())
84
  elements = data.get("discriminatory_elements", ["None"])
85
  explanation = data.get("explanation", "")
86
  return ", ".join(elements), explanation
87
- return "Parsing error", generated[:200]
 
 
 
88
  except Exception as e:
89
- return "Error (HF)", str(e)
90
 
91
- # ── Fallback: keyword rules ───────────────────────────────────────────────
92
  return detect_discrimination(text)
93
 
94
 
@@ -98,21 +117,21 @@ def analyze_file(file):
98
 
99
  df = pd.read_excel(file)
100
 
101
- # Accept either column name convention
102
  text_col = None
103
  for candidate in ["Job post text", "job_post", "text", "job post"]:
104
  if candidate in df.columns:
105
  text_col = candidate
106
  break
107
  if text_col is None:
108
- return gr.update(value="❌ Could not find job text column. Expected: 'Job post text' or 'job_post'"), None
109
 
110
  categories, explanations = [], []
111
  for text in df[text_col]:
112
  cat, exp = llm_detect(str(text))
113
  categories.append(cat)
114
  explanations.append(exp)
115
- time.sleep(0.3) # gentle rate-limit buffer
116
 
117
  df["Discrimination_Type"] = categories
118
  df["Explanation"] = explanations
@@ -120,32 +139,35 @@ def analyze_file(file):
120
  output_path = "/tmp/results.xlsx"
121
  df.to_excel(output_path, index=False)
122
 
123
- backend = "Claude API" if ANTHROPIC_KEY else ("HF Inference API" if HF_TOKEN else "Keyword rules")
 
 
 
 
124
  print(f"Analysis complete β€” backend: {backend}, rows: {len(df)}")
125
 
126
- return df[["Job post ID" if "Job post ID" in df.columns else df.columns[0],
127
- text_col, "Discrimination_Type", "Explanation"]], output_path
128
 
129
 
130
  # ── Gradio UI ─────────────────────────────────────────────────────────────────
131
  backend_label = (
132
- "πŸ€– Claude API" if ANTHROPIC_KEY
133
- else ("πŸ€— HF Inference API (Mistral-7B)" if HF_TOKEN
134
- else "πŸ“‹ Keyword rules (no API key set)")
135
  )
136
 
137
  demo = gr.Interface(
138
  fn=analyze_file,
139
  inputs=gr.File(label="Upload Job Posts Excel (.xlsx)"),
140
  outputs=[
141
- gr.Dataframe(label=f"Results β€” backend: {backend_label}"),
142
  gr.File(label="Download results.xlsx"),
143
  ],
144
  title="πŸ” AI Job Post Discrimination Checker",
145
  description=(
146
  "Upload the job posts Excel file. "
147
- "The tool detects: **Gender Β· Age Β· Origin Β· Mobility Β· Salary Β· "
148
- "Standing Β· PPE Β· Role clarity**.\n\n"
149
  f"Active backend: `{backend_label}`"
150
  ),
151
  allow_flagging="never",
 
9
 
10
  # ── LLM backend selector ─────────────────────────────────────────────────────
11
  # Set ONE of these in Space Settings β†’ Variables and secrets:
12
+ # ANTHROPIC_API_KEY β†’ uses Claude Haiku (Option A, highest accuracy)
13
+ # HF_TOKEN β†’ uses Llama-3.2-1B (Option B, free, no extra cost)
14
+ # If neither is set, falls back to keyword-based rules (no API needed).
15
 
16
  ANTHROPIC_KEY = os.environ.get("ANTHROPIC_API_KEY", "")
17
  HF_TOKEN = os.environ.get("HF_TOKEN", "")
 
25
  - Salary: undisclosed or vague compensation (e.g. "to be defined", "commensurate with experience")
26
  - Standing: appearance, weight, height, or "professional look" requirements
27
  - PPE: candidate required to supply own safety equipment
28
+ - Role clarity: vague or contradictory role description
29
  - None: if fully compliant
30
 
31
+ Return ONLY valid JSON β€” no markdown fences, no extra text:
32
+ {"discriminatory_elements": ["Element1", "Element2"], "explanation": "brief reason per element"}"""
33
 
34
 
35
  def llm_detect(text: str) -> tuple[str, str]:
36
  """Call LLM backend. Returns (categories_str, explanation_str)."""
37
 
38
+ # ── Option A: Claude Haiku ───────────────────────────────────────────────
39
  if ANTHROPIC_KEY:
40
  try:
41
  resp = requests.post(
 
46
  "content-type": "application/json",
47
  },
48
  json={
49
+ "model": "claude-haiku-4-5-20251001",
50
  "max_tokens": 512,
51
  "system": SYSTEM_PROMPT,
52
  "messages": [{"role": "user", "content": f"Job posting:\n\n{text}"}],
 
62
  except Exception as e:
63
  return "Error (Claude)", str(e)
64
 
65
+ # ── Option B: Llama-3.2-1B-Instruct via HF Inference API ────────────────
66
  if HF_TOKEN:
67
  try:
68
+ # Llama-3 chat template
69
  prompt = (
70
+ "<|begin_of_text|>"
71
+ "<|start_header_id|>system<|end_header_id|>\n"
72
+ f"{SYSTEM_PROMPT}"
73
+ "<|eot_id|>"
74
+ "<|start_header_id|>user<|end_header_id|>\n"
75
+ f"Job posting:\n\n{text}"
76
+ "<|eot_id|>"
77
+ "<|start_header_id|>assistant<|end_header_id|>\n"
78
  )
79
  resp = requests.post(
80
+ "https://api-inference.huggingface.co/models/meta-llama/Llama-3.2-1B-Instruct",
81
  headers={"Authorization": f"Bearer {HF_TOKEN}"},
82
+ json={
83
+ "inputs": prompt,
84
+ "parameters": {
85
+ "max_new_tokens": 300,
86
+ "temperature": 0.1,
87
+ "return_full_text": False,
88
+ },
89
+ },
90
  timeout=60,
91
  )
92
  resp.raise_for_status()
93
  output = resp.json()
94
  generated = output[0]["generated_text"] if isinstance(output, list) else str(output)
95
+
96
+ # Pull the first JSON object out of the response
97
+ match = re.search(r'\{.*?\}', generated, re.DOTALL)
98
  if match:
99
  data = json.loads(match.group())
100
  elements = data.get("discriminatory_elements", ["None"])
101
  explanation = data.get("explanation", "")
102
  return ", ".join(elements), explanation
103
+
104
+ # If model didn't return valid JSON, surface the raw text for debug
105
+ return "Parsing error", generated[:300]
106
+
107
  except Exception as e:
108
+ return "Error (Llama)", str(e)
109
 
110
+ # ── Fallback: keyword rules (no API key needed) ───────────────────────────
111
  return detect_discrimination(text)
112
 
113
 
 
117
 
118
  df = pd.read_excel(file)
119
 
120
+ # Accept either column name used in the exercise file
121
  text_col = None
122
  for candidate in ["Job post text", "job_post", "text", "job post"]:
123
  if candidate in df.columns:
124
  text_col = candidate
125
  break
126
  if text_col is None:
127
+ raise ValueError("Could not find job text column. Expected: 'Job post text' or 'job_post'")
128
 
129
  categories, explanations = [], []
130
  for text in df[text_col]:
131
  cat, exp = llm_detect(str(text))
132
  categories.append(cat)
133
  explanations.append(exp)
134
+ time.sleep(0.3) # gentle rate-limit buffer
135
 
136
  df["Discrimination_Type"] = categories
137
  df["Explanation"] = explanations
 
139
  output_path = "/tmp/results.xlsx"
140
  df.to_excel(output_path, index=False)
141
 
142
+ backend = (
143
+ "Claude Haiku" if ANTHROPIC_KEY
144
+ else ("Llama-3.2-1B" if HF_TOKEN
145
+ else "Keyword rules")
146
+ )
147
  print(f"Analysis complete β€” backend: {backend}, rows: {len(df)}")
148
 
149
+ id_col = "Job post ID" if "Job post ID" in df.columns else df.columns[0]
150
+ return df[[id_col, text_col, "Discrimination_Type", "Explanation"]], output_path
151
 
152
 
153
  # ── Gradio UI ─────────────────────────────────────────────────────────────────
154
  backend_label = (
155
+ "πŸ€– Claude Haiku" if ANTHROPIC_KEY
156
+ else ("πŸ¦™ Llama-3.2-1B" if HF_TOKEN
157
+ else "πŸ“‹ Keyword rules")
158
  )
159
 
160
  demo = gr.Interface(
161
  fn=analyze_file,
162
  inputs=gr.File(label="Upload Job Posts Excel (.xlsx)"),
163
  outputs=[
164
+ gr.Dataframe(label=f"Results β€” {backend_label}"),
165
  gr.File(label="Download results.xlsx"),
166
  ],
167
  title="πŸ” AI Job Post Discrimination Checker",
168
  description=(
169
  "Upload the job posts Excel file. "
170
+ "Detects: **Gender Β· Age Β· Origin Β· Mobility Β· Salary Β· Standing Β· PPE Β· Role clarity**\n\n"
 
171
  f"Active backend: `{backend_label}`"
172
  ),
173
  allow_flagging="never",