Machlovi commited on
Commit
40fb589
·
verified ·
1 Parent(s): 56af1aa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -22
app.py CHANGED
@@ -19,33 +19,54 @@ class SafetyChecker:
19
  self.ENDPOINT_URL = ENDPOINT_URL
20
  self.HF_API_TOKEN = HF_API_TOKEN
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."
 
19
  self.ENDPOINT_URL = ENDPOINT_URL
20
  self.HF_API_TOKEN = HF_API_TOKEN
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 extract_and_parse_json(self, response: str):
50
+ if response.startswith("```"):
51
+ response = response.strip("`").strip()
52
+ if response.startswith("json"):
53
+ response = response[4:].strip()
54
+
55
+ # Now response should be clean JSON text
56
  try:
57
+ parsed = json.loads(response)
58
+ except Exception:
59
+ # If somehow still not JSON, return default empty
60
+ parsed = {
61
+ "Safety": "",
62
+ "Score": "",
63
+ "Unsafe Categories": [],
64
+ }
65
+
 
 
 
 
 
 
 
 
66
  return parsed
67
 
68
+
69
+
70
  def check_safety(self, input_text):
71
  if not input_text.strip():
72
  return "⚠️ Please enter some text to check."