Tushar14 commited on
Commit
df53b4b
Β·
verified Β·
1 Parent(s): 656f7df

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +129 -105
app.py CHANGED
@@ -1,130 +1,154 @@
 
 
 
 
1
  import gradio as gr
2
  import pandas as pd
3
  import requests
4
- import json
5
- import os
6
-
7
- HF_TOKEN = os.environ.get("HF_TOKEN")
8
-
9
- def analyze_with_llama(job_text):
10
- API_URL = "https://api-inference.huggingface.co/models/meta-llama/Llama-3.2-3B-Instruct"
11
-
12
- headers = {"Authorization": f"Bearer {HF_TOKEN}"}
13
-
14
- prompt = f"""<|system|>
15
- You are an HR compliance expert. Analyze job posts for discrimination.
16
- Respond ONLY with valid JSON, nothing else.
17
- </s>
18
- <|user|>
19
- Analyze this job post for discrimination. Categories to check:
20
- - Gender Discrimination
21
- - Age Discrimination
22
- - Nationality/Origin Discrimination
23
- - Religion Discrimination
24
- - Disability Discrimination
25
-
26
- Respond ONLY with this JSON format:
27
- {{"discrimination_found": true or false, "categories": ["list of categories found"], "keywords": ["discriminatory words/phrases found"], "explanation": "brief explanation"}}
28
-
29
- Job post:
30
- {job_text[:1000]}
31
- </s>
32
- <|assistant|>
33
- """
34
-
35
- payload = {
36
- "inputs": prompt,
37
- "parameters": {
38
- "max_new_tokens": 300,
39
- "temperature": 0.1,
40
- "return_full_text": False
41
- }
42
- }
43
-
44
- try:
45
- response = requests.post(API_URL, headers=headers, json=payload, timeout=60)
46
- result = response.json()
47
-
48
- if isinstance(result, list):
49
- text = result[0].get("generated_text", "")
50
- elif isinstance(result, dict):
51
- text = result.get("generated_text", str(result))
52
- else:
53
- text = str(result)
54
-
55
- # Extract JSON from response
56
- start = text.find("{")
57
- end = text.rfind("}") + 1
58
- if start != -1 and end > start:
59
- json_str = text[start:end]
60
- parsed = json.loads(json_str)
61
- if parsed.get("discrimination_found"):
62
- categories = ", ".join(parsed.get("categories", [])) or "None"
63
- keywords = ", ".join(parsed.get("keywords", [])) or "None"
64
- explanation = parsed.get("explanation", "")
65
- else:
66
- categories = "No discrimination detected"
67
- keywords = "None"
68
- explanation = parsed.get("explanation", "No discrimination detected")
69
- return categories, keywords, explanation
70
- else:
71
- return "Parse error", "None", text[:200]
72
-
73
- except Exception as e:
74
- return "Error", "None", str(e)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75
 
76
 
77
  def analyze_file(file):
78
  if file is None:
79
  return None, None
80
 
81
- try:
82
- df = pd.read_excel(file.name)
83
- except Exception as e:
84
- raise gr.Error(f"Failed to read Excel file: {str(e)}")
85
-
86
- col_names = df.columns.tolist()
87
 
88
- # Auto-detect the text column
89
  text_col = None
90
- for col in df.columns:
91
- if any(keyword in col.lower() for keyword in ["job", "post", "description", "text", "title"]):
92
- text_col = col
93
  break
94
-
95
  if text_col is None:
96
- text_col = df.columns[0]
97
 
98
- try:
99
- categories_list = []
100
- keywords_list = []
101
- explanations_list = []
 
 
102
 
103
- for text in df[text_col]:
104
- cat, key, exp = analyze_with_llama(str(text))
105
- categories_list.append(cat)
106
- keywords_list.append(key)
107
- explanations_list.append(exp)
108
 
109
- df["Discrimination_Type"] = categories_list
110
- df["Detected_Keywords"] = keywords_list
111
- df["Explanation"] = explanations_list
112
 
113
- output = "results.xlsx"
114
- df.to_excel(output, index=False)
115
 
116
- return df, output
 
117
 
118
- except Exception as e:
119
- raise gr.Error(f"Columns: {col_names}. Error: {str(e)}")
120
 
 
 
 
 
 
 
121
 
122
  demo = gr.Interface(
123
  fn=analyze_file,
124
- inputs=gr.File(label="Upload Job Posts Excel"),
125
- outputs=[gr.Dataframe(label="Results"), gr.File(label="Download File")],
126
- title="AI Job Post Discrimination Checker",
127
- description="Upload an Excel file with job posts. Llama AI will analyze each post for discriminatory elements."
 
 
 
 
 
 
 
 
 
128
  )
129
 
130
- demo.launch(server_name="0.0.0.0", server_port=7860)
 
1
+ import os
2
+ import re
3
+ import json
4
+ import time
5
  import gradio as gr
6
  import pandas as pd
7
  import requests
8
+ 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", "")
18
+
19
+ SYSTEM_PROMPT = """You are an expert HR compliance analyst.
20
+ Analyse the job posting and identify discriminatory elements from this list:
21
+ - Gender: gendered job titles or language implying a preferred gender
22
+ - Age: explicit age limits or age-suggestive language
23
+ - Origin: nationality, ethnicity, or geographic residency requirements
24
+ - Mobility: unjustified driving licence or own vehicle requirement
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(
42
+ "https://api.anthropic.com/v1/messages",
43
+ headers={
44
+ "x-api-key": ANTHROPIC_KEY,
45
+ "anthropic-version": "2023-06-01",
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}"}],
53
+ },
54
+ timeout=30,
55
+ )
56
+ resp.raise_for_status()
57
+ raw = resp.json()["content"][0]["text"].strip()
58
+ data = json.loads(raw)
59
+ elements = data.get("discriminatory_elements", ["None"])
60
+ explanation = data.get("explanation", "")
61
+ return ", ".join(elements), explanation
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
 
95
  def analyze_file(file):
96
  if file is None:
97
  return None, None
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
 
 
 
119
 
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",
152
  )
153
 
154
+ demo.launch(server_name="0.0.0.0", server_port=7860)