Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -15,6 +15,9 @@ def is_token_configured():
|
|
| 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."
|
|
@@ -25,37 +28,44 @@ def check_safety(input_text):
|
|
| 25 |
|
| 26 |
headers = {
|
| 27 |
"Content-Type": "application/json",
|
| 28 |
-
"Authorization": f"Bearer {HF_API_TOKEN}"
|
| 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 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
-
# Format nicely
|
| 46 |
if is_safe:
|
| 47 |
-
return f"β
Safe\n\nSafety:
|
| 48 |
else:
|
| 49 |
-
return f"β Unsafe\n\nSafety:
|
| 50 |
|
| 51 |
else:
|
| 52 |
-
return f"β Error:
|
| 53 |
-
|
| 54 |
except requests.exceptions.Timeout:
|
| 55 |
-
return "β Error: Request timed out.
|
| 56 |
|
| 57 |
except requests.exceptions.ConnectionError:
|
| 58 |
-
return "β Error: Failed to connect to the endpoint.
|
| 59 |
|
| 60 |
except Exception as e:
|
| 61 |
return f"β Error: {str(e)}"
|
|
|
|
| 15 |
return "β
API token is configured"
|
| 16 |
import requests
|
| 17 |
|
| 18 |
+
import json
|
| 19 |
+
import requests
|
| 20 |
+
|
| 21 |
def check_safety(input_text):
|
| 22 |
if not input_text.strip():
|
| 23 |
return "β οΈ Please enter some text to check."
|
|
|
|
| 28 |
|
| 29 |
headers = {
|
| 30 |
"Content-Type": "application/json",
|
| 31 |
+
"Authorization": f"Bearer {HF_API_TOKEN}"
|
| 32 |
}
|
| 33 |
|
| 34 |
try:
|
| 35 |
response = requests.post(ENDPOINT_URL, json=payload, headers=headers, timeout=30)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
+
if response.headers.get("content-type", "").startswith("application/json"):
|
| 38 |
+
result = response.json() # result is a string containing triple backticks
|
| 39 |
+
|
| 40 |
+
if isinstance(result, str):
|
| 41 |
+
# Remove triple backticks if present
|
| 42 |
+
cleaned = result.strip()
|
| 43 |
+
if cleaned.startswith("```"):
|
| 44 |
+
cleaned = cleaned.strip("```").strip()
|
| 45 |
+
if cleaned.startswith("json"):
|
| 46 |
+
cleaned = cleaned[4:].strip() # remove 'json' label if there
|
| 47 |
+
|
| 48 |
+
# Now parse cleaned string
|
| 49 |
+
result = json.loads(cleaned)
|
| 50 |
+
|
| 51 |
+
# Now safely access fields
|
| 52 |
+
is_safe = result.get("Safety", "").lower() == "safe"
|
| 53 |
+
score = result.get("Score", "")
|
| 54 |
+
categories = result.get("Unsafe Categories", "")
|
| 55 |
|
|
|
|
| 56 |
if is_safe:
|
| 57 |
+
return f"β
Safe\n\nSafety: safe\nScore: {score}\nUnsafe Categories: {categories}"
|
| 58 |
else:
|
| 59 |
+
return f"β Unsafe\n\nSafety: unsafe\nScore: {score}\nUnsafe Categories: {categories}"
|
| 60 |
|
| 61 |
else:
|
| 62 |
+
return f"β Error: Server returned non-JSON response:\n\n{response.text}"
|
| 63 |
+
|
| 64 |
except requests.exceptions.Timeout:
|
| 65 |
+
return "β Error: Request timed out."
|
| 66 |
|
| 67 |
except requests.exceptions.ConnectionError:
|
| 68 |
+
return "β Error: Failed to connect to the endpoint."
|
| 69 |
|
| 70 |
except Exception as e:
|
| 71 |
return f"β Error: {str(e)}"
|