Spaces:
Sleeping
Sleeping
Commit ·
fcb4a95
1
Parent(s): f6246c8
Fix API URL
Browse files
app.py
CHANGED
|
@@ -4,28 +4,29 @@ import os
|
|
| 4 |
import time
|
| 5 |
|
| 6 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 7 |
-
API_URL = "https://
|
| 8 |
|
| 9 |
def query(text):
|
| 10 |
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
def predict(comment):
|
| 18 |
if not comment.strip():
|
| 19 |
return "⚠️ Please enter some text.", ""
|
| 20 |
|
| 21 |
try:
|
| 22 |
-
|
| 23 |
|
| 24 |
-
if
|
| 25 |
-
return f"
|
| 26 |
-
|
| 27 |
-
import json
|
| 28 |
-
result = json.loads(text)
|
| 29 |
|
| 30 |
if isinstance(result, list):
|
| 31 |
result = result[0][0]
|
|
@@ -41,7 +42,7 @@ def predict(comment):
|
|
| 41 |
|
| 42 |
return prediction, f"{probability}%"
|
| 43 |
|
| 44 |
-
return f"
|
| 45 |
|
| 46 |
except Exception as e:
|
| 47 |
return f"Exception: {str(e)}", ""
|
|
|
|
| 4 |
import time
|
| 5 |
|
| 6 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 7 |
+
API_URL = "https://router.huggingface.co/hf-inference/models/Hate-speech-CNERG/dehatebert-mono-english"
|
| 8 |
|
| 9 |
def query(text):
|
| 10 |
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 11 |
+
for i in range(3):
|
| 12 |
+
response = requests.post(API_URL, headers=headers, json={
|
| 13 |
+
"inputs": text,
|
| 14 |
+
"options": {"wait_for_model": True}
|
| 15 |
+
})
|
| 16 |
+
if response.status_code == 200:
|
| 17 |
+
return response.json()
|
| 18 |
+
time.sleep(3)
|
| 19 |
+
return {"error": f"Status: {response.status_code}, Response: {response.text}"}
|
| 20 |
|
| 21 |
def predict(comment):
|
| 22 |
if not comment.strip():
|
| 23 |
return "⚠️ Please enter some text.", ""
|
| 24 |
|
| 25 |
try:
|
| 26 |
+
result = query(comment)
|
| 27 |
|
| 28 |
+
if isinstance(result, dict) and "error" in result:
|
| 29 |
+
return f"API Error: {result['error']}", ""
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
if isinstance(result, list):
|
| 32 |
result = result[0][0]
|
|
|
|
| 42 |
|
| 43 |
return prediction, f"{probability}%"
|
| 44 |
|
| 45 |
+
return f"Unexpected: {result}", ""
|
| 46 |
|
| 47 |
except Exception as e:
|
| 48 |
return f"Exception: {str(e)}", ""
|