Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -8,78 +8,95 @@ ENDPOINT_URL = os.environ.get("ENDPOINT_URL", "https://dz0eq6vxq3nm0uh7.us-east-
|
|
8 |
HF_API_TOKEN = os.environ.get("HF_API_TOKEN", "").strip() # Use strip() to remove extra whitespaces and newlines
|
9 |
|
10 |
|
11 |
-
# Check if the API token is configured
|
12 |
-
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 |
import requests
|
17 |
-
|
18 |
import json
|
19 |
-
import requests
|
20 |
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
"
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
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 |
-
|
62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
|
74 |
with gr.Blocks(title="Safety Content Classifier", css="footer {display: none !important}") as demo:
|
75 |
-
gr.Markdown(
|
76 |
-
gr.Markdown(
|
77 |
|
78 |
token_status = gr.Markdown(is_token_configured())
|
79 |
|
80 |
with gr.Accordion("About this demo", open=False):
|
81 |
gr.Markdown("""
|
82 |
-
This demo uses an external API endpoint to classify text
|
83 |
It checks content against the following categories:
|
84 |
- Harassment
|
85 |
- Dangerous Content
|
@@ -111,12 +128,13 @@ with gr.Blocks(title="Safety Content Classifier", css="footer {display: none !im
|
|
111 |
|
112 |
gr.Examples(
|
113 |
[
|
114 |
-
["Hello, how are you today?"
|
115 |
-
["I want to learn how to make a bomb."
|
116 |
-
["
|
117 |
-
["
|
118 |
],
|
119 |
inputs=[input_text]
|
120 |
)
|
121 |
|
122 |
demo.launch()
|
|
|
|
8 |
HF_API_TOKEN = os.environ.get("HF_API_TOKEN", "").strip() # Use strip() to remove extra whitespaces and newlines
|
9 |
|
10 |
|
|
|
|
|
|
|
|
|
|
|
11 |
import requests
|
|
|
12 |
import json
|
|
|
13 |
|
14 |
+
# Assuming your class with extract_and_parse_json exists
|
15 |
+
# Or you can define it separately if needed
|
16 |
+
|
17 |
+
class SafetyChecker:
|
18 |
+
def __init__(self):
|
19 |
+
self.ENDPOINT_URL = os.environ.get("ENDPOINT_URL", "https://your-endpoint")
|
20 |
+
self.HF_API_TOKEN = os.environ.get("HF_API_TOKEN", "").strip()
|
21 |
+
|
22 |
+
def extract_and_parse_json(self, response: str):
|
23 |
+
match = re.search(r'```(?:json)?\s*(.*?)\s*```', response, re.DOTALL)
|
24 |
+
content = match.group(1).strip() if match else response.strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
+
if not content.startswith("{") and ":" in content:
|
27 |
+
content = "{" + content + "}"
|
28 |
+
|
29 |
+
try:
|
30 |
+
parsed = json.loads(content)
|
31 |
+
except json.JSONDecodeError:
|
32 |
+
cleaned = content.replace(""", "\"").replace(""", "\"").replace("'", "\"")
|
33 |
+
cleaned = re.sub(r',\s*}', '}', cleaned)
|
34 |
+
cleaned = re.sub(r',\s*]', ']', cleaned)
|
35 |
+
try:
|
36 |
+
parsed = json.loads(cleaned)
|
37 |
+
except Exception:
|
38 |
+
pairs = re.findall(r'"([^"]+)":\s*"?([^",\{\}\[\]]+)"?', content)
|
39 |
+
if pairs:
|
40 |
+
parsed = {k.strip(): v.strip() for k, v in pairs}
|
41 |
+
else:
|
42 |
+
parsed = {
|
43 |
+
"Safety": "",
|
44 |
+
"Score": "",
|
45 |
+
"Unsafe Categories": "",
|
46 |
+
}
|
47 |
+
return parsed
|
48 |
|
49 |
+
def check_safety(self, input_text):
|
50 |
+
if not input_text.strip():
|
51 |
+
return "β οΈ Please enter some text to check."
|
52 |
+
|
53 |
+
payload = {"inputs": input_text}
|
54 |
+
headers = {
|
55 |
+
"Content-Type": "application/json",
|
56 |
+
"Authorization": f"Bearer {self.HF_API_TOKEN}"
|
57 |
+
}
|
58 |
+
|
59 |
+
try:
|
60 |
+
response = requests.post(self.ENDPOINT_URL, json=payload, headers=headers, timeout=30)
|
61 |
+
|
62 |
+
if response.status_code == 200:
|
63 |
+
result_raw = response.json() # still a string inside triple backticks
|
64 |
+
|
65 |
+
if isinstance(result_raw, str):
|
66 |
+
parsed_result = self.extract_and_parse_json(result_raw)
|
67 |
+
else:
|
68 |
+
parsed_result = result_raw
|
69 |
|
70 |
+
# Now parsed_result is a dictionary
|
71 |
+
safety = parsed_result.get("Safety", "Unknown")
|
72 |
+
score = parsed_result.get("Score", "")
|
73 |
+
categories = parsed_result.get("Unsafe Categories", "")
|
74 |
+
|
75 |
+
is_safe = (safety.lower() == "safe")
|
76 |
+
|
77 |
+
if is_safe:
|
78 |
+
return f"β
Safe\n\nSafety: {safety}\nScore: {score}\nUnsafe Categories: {categories}"
|
79 |
+
else:
|
80 |
+
return f"β Unsafe\n\nSafety: {safety}\nScore: {score}\nUnsafe Categories: {categories}"
|
81 |
+
else:
|
82 |
+
return f"β Error: Request failed with status code {response.status_code}.\nDetails: {response.text}"
|
83 |
+
|
84 |
+
except requests.exceptions.Timeout:
|
85 |
+
return "β Error: Request timed out."
|
86 |
+
except requests.exceptions.ConnectionError:
|
87 |
+
return "β Error: Failed to connect to the endpoint."
|
88 |
+
except Exception as e:
|
89 |
+
return f"β Error: {str(e)}"
|
90 |
|
91 |
with gr.Blocks(title="Safety Content Classifier", css="footer {display: none !important}") as demo:
|
92 |
+
gr.Markdown("# Safety Content Classifier")
|
93 |
+
gr.Markdown("## Connected to external safety model endpoint")
|
94 |
|
95 |
token_status = gr.Markdown(is_token_configured())
|
96 |
|
97 |
with gr.Accordion("About this demo", open=False):
|
98 |
gr.Markdown("""
|
99 |
+
This demo uses an external API endpoint to classify text based on safety policies.
|
100 |
It checks content against the following categories:
|
101 |
- Harassment
|
102 |
- Dangerous Content
|
|
|
128 |
|
129 |
gr.Examples(
|
130 |
[
|
131 |
+
["Hello, how are you today?"],
|
132 |
+
["I want to learn how to make a bomb."],
|
133 |
+
["Let's meet for coffee tomorrow."],
|
134 |
+
["COVID vaccines are a secret plot by the government."],
|
135 |
],
|
136 |
inputs=[input_text]
|
137 |
)
|
138 |
|
139 |
demo.launch()
|
140 |
+
|