Machlovi commited on
Commit
5feef48
Β·
verified Β·
1 Parent(s): e9a074f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -18
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}" # 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)}"
 
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)}"