Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -13,6 +13,52 @@ def is_token_configured():
|
|
13 |
if not HF_API_TOKEN:
|
14 |
return "⚠️ Warning: HF_API_TOKEN is not configured. The app won't work until you add this secret in your Space settings."
|
15 |
return "✅ API token is configured"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
|
18 |
with gr.Blocks(title="Safety Content Classifier", css="footer {display: none !important}") as demo:
|
|
|
13 |
if not HF_API_TOKEN:
|
14 |
return "⚠️ Warning: HF_API_TOKEN is not configured. The app won't work until you add this secret in your Space settings."
|
15 |
return "✅ API token is configured"
|
16 |
+
import requests
|
17 |
+
|
18 |
+
def check_safety(input_text):
|
19 |
+
if not input_text.strip():
|
20 |
+
return "⚠️ Please enter some text to check."
|
21 |
+
|
22 |
+
payload = {
|
23 |
+
"inputs": input_text
|
24 |
+
}
|
25 |
+
|
26 |
+
headers = {
|
27 |
+
"Content-Type": "application/json",
|
28 |
+
"Authorization": f"Bearer {HF_API_TOKEN}" # Assuming you have token ready
|
29 |
+
}
|
30 |
+
|
31 |
+
try:
|
32 |
+
response = requests.post(ENDPOINT_URL, json=payload, headers=headers, timeout=30)
|
33 |
+
|
34 |
+
if response.status_code == 200:
|
35 |
+
result = response.json()
|
36 |
+
|
37 |
+
is_safe = result.get("is_safe", False)
|
38 |
+
safety_result = result.get("safety_result", {})
|
39 |
+
|
40 |
+
# Extract the fields
|
41 |
+
safety = safety_result.get("Safety", "Unknown")
|
42 |
+
score = safety_result.get("Score", "")
|
43 |
+
categories = safety_result.get("Unsafe Categories", "")
|
44 |
+
|
45 |
+
# Format nicely
|
46 |
+
if is_safe:
|
47 |
+
return f"✅ Safe\n\nSafety: {safety}\nScore: {score}\nUnsafe Categories: {categories}"
|
48 |
+
else:
|
49 |
+
return f"❌ Unsafe\n\nSafety: {safety}\nScore: {score}\nUnsafe Categories: {categories}"
|
50 |
+
|
51 |
+
else:
|
52 |
+
return f"❗ Error: Request failed with status code {response.status_code}.\nDetails: {response.text}"
|
53 |
+
|
54 |
+
except requests.exceptions.Timeout:
|
55 |
+
return "❗ Error: Request timed out. The endpoint may be overloaded or unavailable."
|
56 |
+
|
57 |
+
except requests.exceptions.ConnectionError:
|
58 |
+
return "❗ Error: Failed to connect to the endpoint. Please check the endpoint URL."
|
59 |
+
|
60 |
+
except Exception as e:
|
61 |
+
return f"❗ Error: {str(e)}"
|
62 |
|
63 |
|
64 |
with gr.Blocks(title="Safety Content Classifier", css="footer {display: none !important}") as demo:
|