msmaje commited on
Commit
9850f4b
Β·
verified Β·
1 Parent(s): 0c0c1de

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +207 -486
app.py CHANGED
@@ -1,435 +1,124 @@
1
  """
2
- Enhanced Gradio Space for Human-AI Text Attribution (HATA) Model
3
- With Comprehensive Bias Detection and Explainability (SHAP/LIME)
4
- Supports multiple African languages with fairness auditing
5
  """
6
 
7
- import os
8
- import sys
9
- import types
10
  import gradio as gr
11
  import torch
12
- import numpy as np
13
- import pandas as pd
14
  from transformers import AutoTokenizer, AutoModelForSequenceClassification
15
- from sklearn.metrics import confusion_matrix, classification_report
16
- import matplotlib.pyplot as plt
17
- import seaborn as sns
18
- from collections import defaultdict
19
- import math
20
-
21
- # Disable audio stack
22
- os.environ["GRADIO_DISABLE_PYDUB"] = "1"
23
- if "audioop" not in sys.modules:
24
- sys.modules["audioop"] = types.ModuleType("audioop")
25
- if "pyaudioop" not in sys.modules:
26
- sys.modules["pyaudioop"] = types.ModuleType("pyaudioop")
27
-
28
- # Import explainability libraries
29
- try:
30
- import shap
31
- SHAP_AVAILABLE = True
32
- except ImportError:
33
- SHAP_AVAILABLE = False
34
- print("⚠️ SHAP not available. Install with: pip install shap")
35
-
36
- try:
37
- from lime.lime_text import LimeTextExplainer
38
- LIME_AVAILABLE = True
39
- except ImportError:
40
- LIME_AVAILABLE = False
41
- print("⚠️ LIME not available. Install with: pip install lime")
42
 
43
- # -----------------------------------------------------------------------------
44
- # Configuration
45
- # -----------------------------------------------------------------------------
46
  MODEL_NAME = "msmaje/phdhatamodel"
47
- SUPPORTED_LANGUAGES = ["Hausa", "Yoruba", "Igbo", "Nigerian Pidgin"]
48
- LANGUAGE_CODES = {
49
- "Hausa": "ha",
50
- "Yoruba": "yo",
51
- "Igbo": "ig",
52
- "Nigerian Pidgin": "pcm"
53
- }
54
-
55
- # -----------------------------------------------------------------------------
56
- # Model Loading
57
- # -----------------------------------------------------------------------------
58
- print("πŸ“₯ Loading model and tokenizer...")
59
- try:
60
- tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
61
- model = AutoModelForSequenceClassification.from_pretrained(
62
- MODEL_NAME,
63
- output_attentions=True # Enable attention outputs for explainability
64
- )
65
- model.eval()
66
- print("βœ… Model loaded successfully!")
67
- print(f" Model: {MODEL_NAME}")
68
- print(f" Device: {'GPU' if torch.cuda.is_available() else 'CPU'}")
69
- except Exception as e:
70
- print(f"❌ Error loading model: {e}")
71
- raise
72
-
73
- # Initialize explainability tools
74
- if LIME_AVAILABLE:
75
- try:
76
- lime_explainer = LimeTextExplainer(class_names=["Human", "AI"])
77
- print("βœ… LIME explainer initialized")
78
- except Exception as e:
79
- print(f"⚠️ LIME initialization failed: {e}")
80
- LIME_AVAILABLE = False
81
-
82
- if SHAP_AVAILABLE:
83
- try:
84
- # Create a wrapper for SHAP
85
- def model_predict_proba(texts):
86
- if isinstance(texts, str):
87
- texts = [texts]
88
- inputs = tokenizer(texts, return_tensors="pt", truncation=True,
89
- max_length=128, padding=True)
90
- with torch.no_grad():
91
- outputs = model(**inputs)
92
- probs = torch.nn.functional.softmax(outputs.logits, dim=-1)
93
- return probs.numpy()
94
-
95
- shap_explainer = shap.Explainer(model_predict_proba, tokenizer)
96
- print("βœ… SHAP explainer initialized")
97
- except Exception as e:
98
- print(f"⚠️ SHAP initialization failed: {e}")
99
- print(" Will use attention-based explanations as fallback")
100
- SHAP_AVAILABLE = False
101
 
102
- # -----------------------------------------------------------------------------
103
- # Bias and Fairness Metrics
104
- # -----------------------------------------------------------------------------
105
- class BiasMetrics:
106
- """Calculate fairness and bias metrics"""
107
-
108
- @staticmethod
109
- def calculate_eod(y_true, y_pred, groups):
110
- """Equal Opportunity Difference"""
111
- unique_groups = np.unique(groups)
112
- recalls = []
113
-
114
- for group in unique_groups:
115
- mask = groups == group
116
- if np.sum(y_true[mask] == 1) > 0:
117
- tp = np.sum((y_true[mask] == 1) & (y_pred[mask] == 1))
118
- fn = np.sum((y_true[mask] == 1) & (y_pred[mask] == 0))
119
- recall = tp / (tp + fn) if (tp + fn) > 0 else 0
120
- recalls.append(recall)
121
-
122
- return max(recalls) - min(recalls) if len(recalls) > 1 else 0.0
123
-
124
- @staticmethod
125
- def calculate_aaod(y_true, y_pred, groups):
126
- """Average Absolute Odds Difference"""
127
- unique_groups = np.unique(groups)
128
- tpr_diffs = []
129
- fpr_diffs = []
130
-
131
- for i, g1 in enumerate(unique_groups):
132
- for g2 in unique_groups[i+1:]:
133
- m1 = groups == g1
134
- m2 = groups == g2
135
-
136
- # TPR differences
137
- if np.sum(y_true[m1] == 1) > 0 and np.sum(y_true[m2] == 1) > 0:
138
- tpr1 = np.sum((y_true[m1] == 1) & (y_pred[m1] == 1)) / np.sum(y_true[m1] == 1)
139
- tpr2 = np.sum((y_true[m2] == 1) & (y_pred[m2] == 1)) / np.sum(y_true[m2] == 1)
140
- tpr_diffs.append(abs(tpr1 - tpr2))
141
-
142
- # FPR differences
143
- tn1 = np.sum((y_true[m1] == 0) & (y_pred[m1] == 0))
144
- fp1 = np.sum((y_true[m1] == 0) & (y_pred[m1] == 1))
145
- tn2 = np.sum((y_true[m2] == 0) & (y_pred[m2] == 0))
146
- fp2 = np.sum((y_true[m2] == 0) & (y_pred[m2] == 1))
147
-
148
- fpr1 = fp1 / (fp1 + tn1) if (fp1 + tn1) > 0 else 0
149
- fpr2 = fp2 / (fp2 + tn2) if (fp2 + tn2) > 0 else 0
150
- fpr_diffs.append(abs(fpr1 - fpr2))
151
-
152
- return (np.mean(tpr_diffs) + np.mean(fpr_diffs)) / 2 if tpr_diffs else 0.0
153
-
154
- @staticmethod
155
- def demographic_parity(y_pred, groups):
156
- """Demographic Parity Difference"""
157
- unique_groups = np.unique(groups)
158
- positive_rates = []
159
-
160
- for group in unique_groups:
161
- mask = groups == group
162
- positive_rate = np.mean(y_pred[mask] == 1)
163
- positive_rates.append(positive_rate)
164
-
165
- return max(positive_rates) - min(positive_rates) if len(positive_rates) > 1 else 0.0
166
 
167
- # -----------------------------------------------------------------------------
168
- # Explainability Functions
169
- # -----------------------------------------------------------------------------
170
- def get_shap_explanation(text, language="English"):
171
- """Generate SHAP-based explanation"""
172
- if not SHAP_AVAILABLE:
173
- return "⚠️ SHAP is not installed. Install with: pip install shap", None
174
-
175
- try:
176
- # Simpler approach - use attention weights as proxy for SHAP
177
- inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=128)
178
-
179
- with torch.no_grad():
180
- outputs = model(**inputs, output_attentions=True)
181
- # Get mean attention across all layers and heads
182
- attentions = outputs.attentions
183
- mean_attention = torch.mean(torch.stack([att.mean(dim=1) for att in attentions]), dim=0)
184
- token_importance = mean_attention[0].sum(dim=0).numpy()
185
-
186
- # Get tokens
187
- tokens = tokenizer.convert_ids_to_tokens(inputs['input_ids'][0])
188
- tokens = tokens[1:-1] # Remove [CLS] and [SEP]
189
- token_importance = token_importance[1:-1] # Match tokens
190
-
191
- # Normalize
192
- token_importance = token_importance / (token_importance.max() + 1e-8)
193
-
194
- # Create simple bar plot
195
- fig, ax = plt.subplots(figsize=(12, 6))
196
- colors = ['red' if x < 0 else 'green' for x in token_importance]
197
- ax.barh(range(min(20, len(tokens))), token_importance[:20], color=colors[:20])
198
- ax.set_yticks(range(min(20, len(tokens))))
199
- ax.set_yticklabels(tokens[:20])
200
- ax.set_xlabel('Importance (Attention Weight)')
201
- ax.set_title(f'Token Importance - {language}')
202
- ax.invert_yaxis()
203
- plt.tight_layout()
204
-
205
- explanation = f"## Attention-Based Explanation for {language}\n\n"
206
- explanation += "Tokens with **higher values** are more important for classification.\n\n"
207
- explanation += f"Top 5 most important tokens:\n"
208
-
209
- top_indices = np.argsort(token_importance)[-5:][::-1]
210
- for idx in top_indices:
211
- if idx < len(tokens):
212
- token = tokens[idx]
213
- value = token_importance[idx]
214
- explanation += f"- **{token}**: {value:.4f}\n"
215
-
216
- return explanation, fig
217
-
218
- except Exception as e:
219
- return f"❌ Explanation failed: {str(e)}", None
220
 
221
- def get_lime_explanation(text, language="English"):
222
- """Generate LIME-based explanation"""
223
- if not LIME_AVAILABLE:
224
- return "⚠️ LIME is not installed. Install with: pip install lime", None
225
 
226
- try:
227
- def predict_fn(texts):
228
- """Prediction function for LIME"""
229
- if isinstance(texts, str):
230
- texts = [texts]
231
-
232
- results = []
233
- for txt in texts:
234
- inputs = tokenizer(txt, return_tensors="pt", truncation=True,
235
- max_length=128, padding=True)
236
- with torch.no_grad():
237
- outputs = model(**inputs)
238
- probs = torch.nn.functional.softmax(outputs.logits, dim=-1)
239
- results.append(probs[0].numpy())
240
-
241
- return np.array(results)
242
-
243
- # Generate explanation
244
- exp = lime_explainer.explain_instance(
245
- text,
246
- predict_fn,
247
- num_features=10,
248
- num_samples=50 # Reduced for speed
249
- )
250
-
251
- # Create visualization
252
- fig = exp.as_pyplot_figure()
253
- plt.tight_layout()
254
-
255
- # Extract feature weights
256
- weights = exp.as_list()
257
 
258
- explanation = f"## LIME Explanation for {language}\n\n"
259
- explanation += "Features with **positive weights** indicate AI-generated characteristics.\n"
260
- explanation += "Features with **negative weights** indicate Human-written characteristics.\n\n"
261
- explanation += "Top contributing features:\n\n"
262
-
263
- for feature, weight in weights[:5]:
264
- direction = "β†’ AI" if weight > 0 else "β†’ Human"
265
- explanation += f"- **{feature}**: {weight:.4f} {direction}\n"
266
-
267
- return explanation, fig
268
-
269
- except Exception as e:
270
- return f"❌ LIME explanation failed: {str(e)}\n\nTry using SHAP instead.", None
271
-
272
- # -----------------------------------------------------------------------------
273
- # Main Classification Function
274
- # -----------------------------------------------------------------------------
275
- def classify_with_explanation(text, language, explainer_type="SHAP"):
276
- """Classify text and provide explanation"""
277
-
278
  if not text or len(text.strip()) == 0:
279
- return "⚠️ Please enter text to classify", None, None, None
280
 
281
- # Get prediction
282
- inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=128)
 
 
 
 
 
 
283
 
 
284
  with torch.no_grad():
285
  outputs = model(**inputs)
286
  probabilities = torch.nn.functional.softmax(outputs.logits, dim=-1)
287
  predicted_class = torch.argmax(probabilities, dim=-1).item()
288
  confidence = probabilities[0][predicted_class].item()
289
 
290
- # Classification result
291
  labels = {0: "πŸ‘€ Human-written", 1: "πŸ€– AI-generated"}
292
- result = f"## Classification Result\n\n"
293
- result += f"**Prediction:** {labels[predicted_class]}\n"
294
- result += f"**Confidence:** {confidence:.2%}\n"
295
- result += f"**Language:** {language}\n\n"
296
 
297
- # Confidence interpretation
 
 
 
 
298
  if confidence > 0.9:
299
- result += "βœ… **High confidence** - Very certain about this prediction\n"
300
  elif confidence > 0.7:
301
- result += "⚠️ **Moderate confidence** - Fairly certain with some uncertainty\n"
302
  else:
303
- result += "❓ **Low confidence** - Uncertain, mixed characteristics detected\n"
304
 
305
- # Probability breakdown - Create DataFrame for BarPlot
306
- prob_data = pd.DataFrame({
307
- "Class": ["Human-written", "AI-generated"],
308
- "Probability": [float(probabilities[0][0]), float(probabilities[0][1])]
309
- })
310
 
311
- # Generate explanation
312
- explanation_text = ""
313
- explanation_viz = None
314
-
315
- if explainer_type == "SHAP" and SHAP_AVAILABLE:
316
- explanation_text, explanation_viz = get_shap_explanation(text, language)
317
- if explanation_viz and isinstance(explanation_viz, tuple):
318
- explanation_viz = explanation_viz[0] # Extract just the figure
319
- elif explainer_type == "LIME" and LIME_AVAILABLE:
320
- explanation_text, explanation_viz = get_lime_explanation(text, language)
321
- elif explainer_type == "Both":
322
- shap_text, shap_viz = get_shap_explanation(text, language)
323
- lime_text, lime_viz = get_lime_explanation(text, language)
324
- explanation_text = shap_text + "\n\n---\n\n" + lime_text
325
- # Use SHAP visualization by default for "Both"
326
- if shap_viz and isinstance(shap_viz, tuple):
327
- explanation_viz = shap_viz[0]
328
- elif isinstance(shap_viz, plt.Figure):
329
- explanation_viz = shap_viz
330
- else:
331
- explanation_viz = lime_viz
332
  else:
333
- explanation_text = "⚠️ Selected explainer not available. Please install SHAP and/or LIME."
334
-
335
- return result, prob_data, explanation_text, explanation_viz
336
 
337
- # -----------------------------------------------------------------------------
338
- # Bias Auditing Function
339
- # -----------------------------------------------------------------------------
340
- def audit_bias(uploaded_file):
341
- """Perform bias audit on uploaded dataset"""
342
-
343
- if uploaded_file is None:
344
- return "⚠️ Please upload a CSV file with columns: text, label, language"
345
 
 
346
  try:
347
- # Read CSV
348
- df = pd.read_csv(uploaded_file.name)
349
-
350
- required_cols = ['text', 'label', 'language']
351
- if not all(col in df.columns for col in required_cols):
352
- return f"❌ CSV must have columns: {required_cols}"
353
-
354
- # Get predictions
355
- predictions = []
356
- for text in df['text']:
357
- inputs = tokenizer(str(text), return_tensors="pt", truncation=True, max_length=128)
358
- with torch.no_grad():
359
- outputs = model(**inputs)
360
- pred = torch.argmax(outputs.logits, dim=-1).item()
361
- predictions.append(pred)
362
-
363
- df['prediction'] = predictions
364
-
365
- # Calculate metrics
366
- y_true = df['label'].values
367
- y_pred = df['prediction'].values
368
- groups = df['language'].values
369
-
370
- eod = BiasMetrics.calculate_eod(y_true, y_pred, groups)
371
- aaod = BiasMetrics.calculate_aaod(y_true, y_pred, groups)
372
- dpd = BiasMetrics.demographic_parity(y_pred, groups)
373
-
374
- # Per-language metrics
375
- lang_metrics = {}
376
- for lang in df['language'].unique():
377
- mask = df['language'] == lang
378
- lang_true = y_true[mask]
379
- lang_pred = y_pred[mask]
380
-
381
- accuracy = np.mean(lang_true == lang_pred)
382
- precision = np.sum((lang_true == 1) & (lang_pred == 1)) / np.sum(lang_pred == 1) if np.sum(lang_pred == 1) > 0 else 0
383
- recall = np.sum((lang_true == 1) & (lang_pred == 1)) / np.sum(lang_true == 1) if np.sum(lang_true == 1) > 0 else 0
384
- f1 = 2 * precision * recall / (precision + recall) if (precision + recall) > 0 else 0
385
 
386
- lang_metrics[lang] = {
387
- 'accuracy': accuracy,
388
- 'precision': precision,
389
- 'recall': recall,
390
- 'f1': f1,
391
- 'samples': int(np.sum(mask))
392
- }
393
-
394
- # Create report
395
- report = f"# Bias Audit Report\n\n"
396
- report += f"**Total Samples:** {len(df)}\n"
397
- report += f"**Languages:** {', '.join(df['language'].unique())}\n\n"
398
-
399
- report += f"## Fairness Metrics\n\n"
400
- report += f"| Metric | Value | Interpretation |\n"
401
- report += f"|--------|-------|----------------|\n"
402
- report += f"| EOD | {eod:.4f} | {'βœ… Fair' if eod < 0.1 else '⚠️ Bias detected'} |\n"
403
- report += f"| AAOD | {aaod:.4f} | {'βœ… Fair' if aaod < 0.1 else '⚠️ Bias detected'} |\n"
404
- report += f"| Demographic Parity | {dpd:.4f} | {'βœ… Fair' if dpd < 0.1 else '⚠️ Bias detected'} |\n\n"
405
-
406
- report += f"## Per-Language Performance\n\n"
407
- report += f"| Language | Accuracy | F1 Score | Precision | Recall | Samples |\n"
408
- report += f"|----------|----------|----------|-----------|--------|----------|\n"
409
-
410
- for lang, metrics in sorted(lang_metrics.items()):
411
- report += f"| {lang} | {metrics['accuracy']:.4f} | {metrics['f1']:.4f} | "
412
- report += f"{metrics['precision']:.4f} | {metrics['recall']:.4f} | {metrics['samples']} |\n"
413
-
414
- # Confusion matrix
415
- fig, ax = plt.subplots(figsize=(8, 6))
416
- cm = confusion_matrix(y_true, y_pred)
417
- sns.heatmap(cm, annot=True, fmt='d', cmap='Blues', ax=ax)
418
- ax.set_title('Overall Confusion Matrix')
419
- ax.set_xlabel('Predicted')
420
- ax.set_ylabel('Actual')
421
- ax.set_xticklabels(['Human', 'AI'])
422
- ax.set_yticklabels(['Human', 'AI'])
423
- plt.tight_layout()
424
 
425
- return report, fig
 
 
 
 
426
 
427
- except Exception as e:
428
- return f"❌ Error during bias audit: {str(e)}", None
 
 
429
 
430
- # -----------------------------------------------------------------------------
431
- # Gradio Interface
432
- # -----------------------------------------------------------------------------
433
  custom_css = """
434
  #title {
435
  text-align: center;
@@ -438,155 +127,187 @@ custom_css = """
438
  -webkit-text-fill-color: transparent;
439
  font-size: 2.5em;
440
  font-weight: bold;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
441
  }
442
  """
443
 
 
444
  with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
445
 
446
- gr.Markdown("<h1 id='title'>πŸ” HATA: Human vs AI Text Detector</h1>")
447
- gr.Markdown("""
448
- <div style='text-align: center; margin-bottom: 20px;'>
449
- Detect AI-generated text in African languages with **explainable AI** and **fairness auditing**
450
- </div>
451
- """)
452
 
 
453
  with gr.Tabs():
454
- # Tab 1: Classification with Explanation
455
- with gr.Tab("πŸ“ Text Classification"):
456
  with gr.Row():
457
- with gr.Column():
458
  text_input = gr.Textbox(
459
- label="Enter Text",
460
- placeholder="Paste text here to classify...",
461
- lines=8
462
- )
463
- language_select = gr.Dropdown(
464
- choices=SUPPORTED_LANGUAGES,
465
- value="Hausa",
466
- label="Select Language"
467
  )
468
- explainer_select = gr.Radio(
469
- choices=["SHAP", "LIME", "Both"],
470
- value="SHAP",
471
- label="Explainability Method"
472
  )
473
- classify_btn = gr.Button("πŸ” Classify & Explain", variant="primary")
 
 
 
474
 
475
- with gr.Column():
476
- result_output = gr.Markdown(label="Classification Result")
477
- prob_chart = gr.BarPlot(
478
- x="Class",
479
- y="Probability",
480
- title="Prediction Probabilities",
481
  y_lim=[0, 1],
482
  height=300,
483
- width=400
484
  )
485
 
486
- with gr.Row():
487
- with gr.Column():
488
- explanation_output = gr.Markdown(label="Explanation")
489
- with gr.Column():
490
- explanation_viz = gr.Plot(label="Visual Explanation")
491
-
492
- # Examples to help users
493
  gr.Examples(
494
- examples=[
495
- ["Ka rubuta labari game da kasuwa a Kano", "Hausa", "SHAP"],
496
- ["ÌwΓ© yìí jẹ́ Γ¬wΓ© tΓ³ dΓ‘ra pΓΊpọ̀ fΓΊn Γ wọn akẹ́kọ̀ọ́", "Yoruba", "LIME"],
497
- ["Akwα»₯kwọ a dα»‹ mma maka α»₯mα»₯ akwα»₯kwọ", "Igbo", "SHAP"],
498
- ["Dis book dey very good for students wey wan learn", "Nigerian Pidgin", "Both"]
499
- ],
500
- inputs=[text_input, language_select, explainer_select],
501
- label="Try these examples in different languages"
502
  )
503
 
 
504
  classify_btn.click(
505
- fn=classify_with_explanation,
506
- inputs=[text_input, language_select, explainer_select],
507
- outputs=[result_output, prob_chart, explanation_output, explanation_viz]
508
  )
509
 
510
- # Tab 2: Bias Auditing
511
- with gr.Tab("βš–οΈ Bias Audit"):
512
  gr.Markdown("""
513
- ### Fairness and Bias Auditing
514
-
515
- Upload a CSV file with columns: `text`, `label` (0=Human, 1=AI), `language`
516
 
517
- The system will calculate:
518
- - **EOD (Equal Opportunity Difference)**: Fairness in recall across languages
519
- - **AAOD (Average Absolute Odds Difference)**: Disparity in TPR and FPR
520
- - **Demographic Parity**: Difference in positive prediction rates
521
  """)
522
 
523
  with gr.Row():
524
  with gr.Column():
525
- audit_file = gr.File(label="Upload CSV Dataset", file_types=[".csv"])
526
- audit_btn = gr.Button("πŸ” Run Bias Audit", variant="primary")
 
 
 
527
 
528
  with gr.Column():
529
- audit_report = gr.Markdown(label="Audit Report")
530
- audit_viz = gr.Plot(label="Confusion Matrix")
 
 
 
531
 
532
- audit_btn.click(
533
- fn=audit_bias,
534
- inputs=audit_file,
535
- outputs=[audit_report, audit_viz]
536
  )
537
 
538
  # Tab 3: About
539
  with gr.Tab("ℹ️ About"):
540
  gr.Markdown("""
541
- # About HATA System
542
 
543
- ## 🎯 Features
 
 
 
544
 
545
- ### Explainable AI
546
- - **SHAP**: Game-theory based feature attribution
547
- - **LIME**: Local interpretable model-agnostic explanations
548
- - Visual token-level attributions
 
 
 
 
549
 
550
- ### Fairness Auditing
551
- - Equal Opportunity Difference (EOD)
552
- - Average Absolute Odds Difference (AAOD)
553
- - Demographic Parity
554
- - Per-language performance metrics
555
 
556
- ## 🌍 Supported Languages
557
- Hausa, Yoruba, Igbo, Nigerian Pidgin
 
 
 
558
 
559
- ## πŸ“Š Model Performance
560
- - Accuracy: 100%
561
- - F1 Score: 100%
562
- - EOD: 0.0 (Perfect fairness)
563
- - AAOD: 0.0 (No bias)
564
 
565
- ## πŸ”¬ Technical Details
566
- - Base Model: AfroXLMR-base (davlan/afro-xlmr-base)
567
- - Parameters: ~270M
568
- - Max Sequence Length: 128 tokens
569
- - Training Dataset: PhD HATA African Dataset
570
- - Languages: 4 West African languages
571
 
572
  ## πŸ“š Citation
573
  ```bibtex
574
  @misc{msmaje2025hata,
575
  author = {Maje, M.S.},
576
- title = {HATA: Human-AI Text Attribution for African Languages},
577
  year = {2025},
578
  publisher = {HuggingFace},
579
  url = {https://huggingface.co/msmaje/phdhatamodel}
580
  }
581
  ```
 
 
 
 
 
 
 
 
582
  """)
583
 
 
584
  gr.Markdown("""
585
  ---
586
- <div style='text-align: center; color: #666;'>
587
- Built with πŸ’œ for African Language NLP | Powered by AfroXLMR & Explainable AI
 
588
  </div>
589
  """)
590
 
 
591
  if __name__ == "__main__":
592
  demo.launch()
 
1
  """
2
+ Gradio Space for Human-AI Text Attribution (HATA) Model
3
+ Detects whether text is human-written or AI-generated
4
+ Supports multiple African languages
5
  """
6
 
 
 
 
7
  import gradio as gr
8
  import torch
 
 
9
  from transformers import AutoTokenizer, AutoModelForSequenceClassification
10
+ import numpy as np
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
+ # Load model and tokenizer
 
 
13
  MODEL_NAME = "msmaje/phdhatamodel"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
+ print("Loading model...")
16
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
17
+ model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
18
+ model.eval()
19
+ print("Model loaded successfully!")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
+ # Language examples
22
+ EXAMPLES = [
23
+
24
+ ["ÌwΓ© yìí jẹ́ Γ¬wΓ© tΓ³ dΓ‘ra pΓΊpọ̀ fΓΊn Γ wọn akẹ́kọ̀ọ́.", "Yoruba"],
25
+ ["Wannan littafi mai kyau ne ga Ι—alibai.", "Hausa"],
26
+ ["Akwα»₯kwọ a dα»‹ mma maka α»₯mα»₯ akwα»₯kwọ.", "Igbo"],
27
+ ["Dis book dey very good for students wey wan learn.", "Nigerian Pidgin"],
28
+
29
+ ]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
 
31
+ def classify_text(text, show_probabilities=True):
32
+ """
33
+ Classify text as human-written or AI-generated
 
34
 
35
+ Args:
36
+ text: Input text to classify
37
+ show_probabilities: Whether to show probability scores
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
 
39
+ Returns:
40
+ Classification result with confidence scores
41
+ """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
  if not text or len(text.strip()) == 0:
43
+ return "⚠️ Please enter some text to classify.", None
44
 
45
+ # Tokenize
46
+ inputs = tokenizer(
47
+ text,
48
+ return_tensors="pt",
49
+ truncation=True,
50
+ max_length=128,
51
+ padding=True
52
+ )
53
 
54
+ # Get prediction
55
  with torch.no_grad():
56
  outputs = model(**inputs)
57
  probabilities = torch.nn.functional.softmax(outputs.logits, dim=-1)
58
  predicted_class = torch.argmax(probabilities, dim=-1).item()
59
  confidence = probabilities[0][predicted_class].item()
60
 
61
+ # Labels
62
  labels = {0: "πŸ‘€ Human-written", 1: "πŸ€– AI-generated"}
 
 
 
 
63
 
64
+ # Create result text
65
+ result = f"## Prediction: {labels[predicted_class]}\n"
66
+ result += f"**Confidence:** {confidence:.2%}\n\n"
67
+
68
+ # Add interpretation
69
  if confidence > 0.9:
70
+ result += "βœ… **High confidence** - The model is very certain about this prediction."
71
  elif confidence > 0.7:
72
+ result += "⚠️ **Moderate confidence** - The model is fairly certain, but there's some uncertainty."
73
  else:
74
+ result += "❓ **Low confidence** - The model is uncertain. The text may have mixed characteristics."
75
 
76
+ # Probability chart data
77
+ prob_data = {
78
+ "Human-written": float(probabilities[0][0].item()),
79
+ "AI-generated": float(probabilities[0][1].item())
80
+ }
81
 
82
+ if show_probabilities:
83
+ return result, prob_data
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84
  else:
85
+ return result, None
 
 
86
 
87
+ def batch_classify(file):
88
+ """
89
+ Classify multiple texts from uploaded file
90
+ """
91
+ if file is None:
92
+ return "⚠️ Please upload a text file."
 
 
93
 
94
+ # Read file
95
  try:
96
+ with open(file.name, 'r', encoding='utf-8') as f:
97
+ texts = f.readlines()
98
+ except Exception as e:
99
+ return f"❌ Error reading file: {e}"
100
+
101
+ # Process each text
102
+ results = []
103
+ for i, text in enumerate(texts, 1):
104
+ text = text.strip()
105
+ if not text:
106
+ continue
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
107
 
108
+ inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=128)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
109
 
110
+ with torch.no_grad():
111
+ outputs = model(**inputs)
112
+ probabilities = torch.nn.functional.softmax(outputs.logits, dim=-1)
113
+ predicted_class = torch.argmax(probabilities, dim=-1).item()
114
+ confidence = probabilities[0][predicted_class].item()
115
 
116
+ label = "Human" if predicted_class == 0 else "AI"
117
+ results.append(f"{i}. [{label} - {confidence:.2%}] {text[:100]}...")
118
+
119
+ return "\n".join(results)
120
 
121
+ # Custom CSS
 
 
122
  custom_css = """
123
  #title {
124
  text-align: center;
 
127
  -webkit-text-fill-color: transparent;
128
  font-size: 2.5em;
129
  font-weight: bold;
130
+ margin-bottom: 0.5em;
131
+ }
132
+
133
+ #subtitle {
134
+ text-align: center;
135
+ color: #666;
136
+ font-size: 1.2em;
137
+ margin-bottom: 1em;
138
+ }
139
+
140
+ .output-box {
141
+ border: 2px solid #667eea;
142
+ border-radius: 10px;
143
+ padding: 15px;
144
+ }
145
+
146
+ .gradio-container {
147
+ max-width: 900px;
148
+ margin: auto;
149
  }
150
  """
151
 
152
+ # Create Gradio interface
153
  with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
154
 
155
+ # Header
156
+ gr.Markdown("<h1 id='title'>πŸ” Human vs AI Text Detector</h1>")
157
+ gr.Markdown(
158
+ "<p id='subtitle'>Detect whether text is human-written or AI-generated | "
159
+ "Supports African Languages 🌍</p>"
160
+ )
161
 
162
+ # Main interface
163
  with gr.Tabs():
164
+ # Tab 1: Single text classification
165
+ with gr.Tab("πŸ“ Single Text"):
166
  with gr.Row():
167
+ with gr.Column(scale=2):
168
  text_input = gr.Textbox(
169
+ label="Enter text to classify",
170
+ placeholder="Type or paste your text here...",
171
+ lines=6,
172
+ max_lines=10
 
 
 
 
173
  )
174
+
175
+ show_probs = gr.Checkbox(
176
+ label="Show probability distribution",
177
+ value=True
178
  )
179
+
180
+ with gr.Row():
181
+ classify_btn = gr.Button("πŸ” Classify Text", variant="primary")
182
+ clear_btn = gr.ClearButton([text_input])
183
 
184
+ with gr.Column(scale=2):
185
+ result_output = gr.Markdown(label="Result")
186
+ prob_plot = gr.BarPlot(
187
+ x="label",
188
+ y="probability",
189
+ title="Probability Distribution",
190
  y_lim=[0, 1],
191
  height=300,
192
+ visible=True
193
  )
194
 
195
+ # Examples
196
+ gr.Markdown("### πŸ“š Try these examples:")
 
 
 
 
 
197
  gr.Examples(
198
+ examples=EXAMPLES,
199
+ inputs=[text_input],
200
+ label="Example texts in different languages"
 
 
 
 
 
201
  )
202
 
203
+ # Connect classification function
204
  classify_btn.click(
205
+ fn=classify_text,
206
+ inputs=[text_input, show_probs],
207
+ outputs=[result_output, prob_plot]
208
  )
209
 
210
+ # Tab 2: Batch classification
211
+ with gr.Tab("πŸ“„ Batch Processing"):
212
  gr.Markdown("""
213
+ ### Upload a text file for batch classification
 
 
214
 
215
+ Upload a `.txt` file with one text sample per line.
216
+ The app will classify each line and show the results.
 
 
217
  """)
218
 
219
  with gr.Row():
220
  with gr.Column():
221
+ file_input = gr.File(
222
+ label="Upload text file (.txt)",
223
+ file_types=[".txt"]
224
+ )
225
+ batch_btn = gr.Button("πŸ” Classify All", variant="primary")
226
 
227
  with gr.Column():
228
+ batch_output = gr.Textbox(
229
+ label="Batch Results",
230
+ lines=15,
231
+ max_lines=20
232
+ )
233
 
234
+ batch_btn.click(
235
+ fn=batch_classify,
236
+ inputs=file_input,
237
+ outputs=batch_output
238
  )
239
 
240
  # Tab 3: About
241
  with gr.Tab("ℹ️ About"):
242
  gr.Markdown("""
243
+ # About This Model
244
 
245
+ ## 🎯 Purpose
246
+ This model detects whether text is **human-written** or **AI-generated**.
247
+ It has been specifically trained on African languages to ensure fair and
248
+ accurate detection across diverse linguistic contexts.
249
 
250
+ ## 🌍 Supported Languages
251
+ - **English**
252
+ - **Yoruba** (yo)
253
+ - **Hausa** (ha)
254
+ - **Igbo** (ig)
255
+ - **Swahili** (sw)
256
+ - **Amharic** (am)
257
+ - **Nigerian Pidgin** (pcm)
258
 
259
+ ## πŸ“Š Performance
260
+ - **Accuracy:** 100%
261
+ - **F1 Score:** 100%
262
+ - **Fairness Metrics:** EOD = 0.0, AAOD = 0.0 (Perfect fairness)
 
263
 
264
+ ## πŸ”¬ Model Details
265
+ - **Base Model:** [AfroXLMR-base](https://huggingface.co/davlan/afro-xlmr-base)
266
+ - **Parameters:** ~270M (0.3B)
267
+ - **Max Sequence Length:** 128 tokens
268
+ - **Training Dataset:** PhD HATA African Dataset
269
 
270
+ ## βš–οΈ Fairness & Ethics
271
+ This model has been trained with explicit fairness constraints to ensure:
272
+ - Equal performance across all supported languages
273
+ - No bias toward high-resource languages
274
+ - Fair treatment of diverse linguistic communities
275
 
276
+ ## ⚠️ Limitations
277
+ - Performance may vary on languages outside the training distribution
278
+ - AI detection capabilities are tied to the AI systems present in training data
279
+ - Should be used as one component in content verification, not sole determinant
280
+ - Text length and domain may affect accuracy
 
281
 
282
  ## πŸ“š Citation
283
  ```bibtex
284
  @misc{msmaje2025hata,
285
  author = {Maje, M.S.},
286
+ title = {AfroXLMR for Human-AI Text Attribution},
287
  year = {2025},
288
  publisher = {HuggingFace},
289
  url = {https://huggingface.co/msmaje/phdhatamodel}
290
  }
291
  ```
292
+
293
+ ## πŸ”— Links
294
+ - [Model on HuggingFace](https://huggingface.co/msmaje/phdhatamodel)
295
+ - [Training Visualizations](https://huggingface.co/msmaje/phdhatamodel/tree/main/visualizations)
296
+ - [Dataset](https://huggingface.co/datasets/msmaje/phd-hata-african-dataset)
297
+
298
+ ## πŸ‘€ Contact
299
+ For questions or feedback, please open an issue on the model repository.
300
  """)
301
 
302
+ # Footer
303
  gr.Markdown("""
304
  ---
305
+ <div style='text-align: center; color: #666; padding: 20px;'>
306
+ <p>Built with πŸ’œ for African Language NLP | Powered by AfroXLMR</p>
307
+ <p>Model: <a href='https://huggingface.co/msmaje/phdhatamodel'>msmaje/phdhatamodel</a></p>
308
  </div>
309
  """)
310
 
311
+ # Launch
312
  if __name__ == "__main__":
313
  demo.launch()