msmaje commited on
Commit
6489332
·
verified ·
1 Parent(s): 1f7a30c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -34
app.py CHANGED
@@ -2,9 +2,8 @@
2
  import os
3
  import math
4
  import requests
5
- from flask import Flask, request, jsonify
6
- from flask_cors import CORS
7
  from langdetect import detect
 
8
 
9
  # -----------------------------------------------------------------------------
10
  # Configuration
@@ -12,14 +11,14 @@ from langdetect import detect
12
  HF_API_URL = "https://api-inference.huggingface.co/models/YOUR_USERNAME/YOUR_MODEL"
13
  HF_TOKEN = os.getenv("HF_TOKEN")
14
 
 
 
 
15
  HEADERS = {
16
  "Authorization": f"Bearer {HF_TOKEN}",
17
  "Content-Type": "application/json"
18
  }
19
 
20
- app = Flask(__name__)
21
- CORS(app)
22
-
23
  # -----------------------------------------------------------------------------
24
  # Utility Functions
25
  # -----------------------------------------------------------------------------
@@ -39,7 +38,6 @@ def normalize_labels(hf_output):
39
  result = {item["label"].lower(): float(item["score"]) for item in hf_output}
40
  human_p = result.get("human", 0.0)
41
  ai_p = result.get("ai", 0.0)
42
-
43
  return human_p, ai_p
44
 
45
  def hf_inference(text):
@@ -49,17 +47,14 @@ def hf_inference(text):
49
  return r.json()
50
 
51
  # -----------------------------------------------------------------------------
52
- # Core Endpoint
53
  # -----------------------------------------------------------------------------
54
- @app.route("/analyze", methods=["POST"])
55
- def analyze():
56
- data = request.get_json()
57
- text = data.get("text", "").strip()
58
-
59
  if not text:
60
- return jsonify({"error": "Empty input"}), 400
61
 
62
- # 1. Language detection (supports linguistic auditing)
63
  try:
64
  language = detect(text)
65
  except Exception:
@@ -67,9 +62,8 @@ def analyze():
67
 
68
  # 2. Hugging Face inference
69
  hf_raw = hf_inference(text)
70
-
71
  if not isinstance(hf_raw, list):
72
- return jsonify({"error": "Unexpected model response", "raw": hf_raw}), 500
73
 
74
  human_p, ai_p = normalize_labels(hf_raw)
75
 
@@ -80,7 +74,7 @@ def analyze():
80
  # 4. Epistemic uncertainty
81
  H = entropy([human_p, ai_p])
82
 
83
- # 5. Explainability placeholder (XAI-ready schema)
84
  explainability_stub = {
85
  "method": "pending",
86
  "note": (
@@ -91,7 +85,7 @@ def analyze():
91
  "token_attributions": []
92
  }
93
 
94
- # 6. Fairness metadata (for downstream auditing)
95
  fairness_context = {
96
  "language": language,
97
  "human_probability": human_p,
@@ -122,25 +116,25 @@ def analyze():
122
  "fairness_audit_fields": fairness_context
123
  }
124
 
125
- return jsonify(response)
126
 
127
  # -----------------------------------------------------------------------------
128
- # Health Check
129
  # -----------------------------------------------------------------------------
130
- @app.route("/", methods=["GET"])
131
- def index():
132
- return jsonify({
133
- "system": "HATA API",
134
- "capabilities": [
135
- "Human vs AI classification",
136
- "Probability calibration",
137
- "Uncertainty estimation",
138
- "Language-aware auditing",
139
- "Explainability-ready schema",
140
- "Fairness instrumentation"
141
- ]
142
- })
143
 
 
 
144
  # -----------------------------------------------------------------------------
145
  if __name__ == "__main__":
146
- app.run(host="0.0.0.0", port=5000, debug=True)
 
2
  import os
3
  import math
4
  import requests
 
 
5
  from langdetect import detect
6
+ import gradio as gr
7
 
8
  # -----------------------------------------------------------------------------
9
  # Configuration
 
11
  HF_API_URL = "https://api-inference.huggingface.co/models/YOUR_USERNAME/YOUR_MODEL"
12
  HF_TOKEN = os.getenv("HF_TOKEN")
13
 
14
+ if HF_TOKEN is None:
15
+ raise ValueError("HF_TOKEN environment variable not set!")
16
+
17
  HEADERS = {
18
  "Authorization": f"Bearer {HF_TOKEN}",
19
  "Content-Type": "application/json"
20
  }
21
 
 
 
 
22
  # -----------------------------------------------------------------------------
23
  # Utility Functions
24
  # -----------------------------------------------------------------------------
 
38
  result = {item["label"].lower(): float(item["score"]) for item in hf_output}
39
  human_p = result.get("human", 0.0)
40
  ai_p = result.get("ai", 0.0)
 
41
  return human_p, ai_p
42
 
43
  def hf_inference(text):
 
47
  return r.json()
48
 
49
  # -----------------------------------------------------------------------------
50
+ # Gradio Prediction Function
51
  # -----------------------------------------------------------------------------
52
+ def analyze_text(text):
53
+ text = text.strip()
 
 
 
54
  if not text:
55
+ return {"error": "Empty input"}
56
 
57
+ # 1. Language detection
58
  try:
59
  language = detect(text)
60
  except Exception:
 
62
 
63
  # 2. Hugging Face inference
64
  hf_raw = hf_inference(text)
 
65
  if not isinstance(hf_raw, list):
66
+ return {"error": "Unexpected model response", "raw": hf_raw}
67
 
68
  human_p, ai_p = normalize_labels(hf_raw)
69
 
 
74
  # 4. Epistemic uncertainty
75
  H = entropy([human_p, ai_p])
76
 
77
+ # 5. Explainability placeholder
78
  explainability_stub = {
79
  "method": "pending",
80
  "note": (
 
85
  "token_attributions": []
86
  }
87
 
88
+ # 6. Fairness metadata
89
  fairness_context = {
90
  "language": language,
91
  "human_probability": human_p,
 
116
  "fairness_audit_fields": fairness_context
117
  }
118
 
119
+ return response
120
 
121
  # -----------------------------------------------------------------------------
122
+ # Gradio Interface
123
  # -----------------------------------------------------------------------------
124
+ iface = gr.Interface(
125
+ fn=analyze_text,
126
+ inputs=gr.Textbox(lines=5, placeholder="Enter text here..."),
127
+ outputs=gr.JSON(),
128
+ title="HATA: Human-AI Text Attribution",
129
+ description=(
130
+ "Detect whether text is human-written or AI-generated.\n"
131
+ "Supports uncertainty estimation, language-aware auditing, "
132
+ "and XAI-ready outputs."
133
+ )
134
+ )
 
 
135
 
136
+ # -----------------------------------------------------------------------------
137
+ # Launch Gradio App
138
  # -----------------------------------------------------------------------------
139
  if __name__ == "__main__":
140
+ iface.launch(server_name="0.0.0.0", server_port=7860)