AsyncCoder-10 commited on
Commit
9e3b93f
·
verified ·
1 Parent(s): d5fca1f

Update app/main.py

Browse files
Files changed (1) hide show
  1. app/main.py +17 -4
app/main.py CHANGED
@@ -101,12 +101,25 @@ async def moderate_text(request: TextRequest):
101
 
102
  profanity_result = profanity_filter.check_profanity(text)
103
  # tox_result = toxicity_pipe(text)[0]
104
- raw_result = toxicity_pipe(text)
105
- tox_result = raw_result[0][0] if isinstance(raw_result[0], list) else raw_result[0]
106
 
107
- is_toxic_by_model = (
108
- tox_result["label"].lower() in ["toxic", "label_1"] and tox_result["score"] > 0.5
 
 
 
 
 
 
 
 
 
 
 
 
109
  )
 
110
  is_threat_by_keyword = bool(threat_pattern.search(text))
111
 
112
  is_flagged = bool(
 
101
 
102
  profanity_result = profanity_filter.check_profanity(text)
103
  # tox_result = toxicity_pipe(text)[0]
104
+ # raw_result = toxicity_pipe(text)
105
+ # tox_result = raw_result[0][0] if isinstance(raw_result[0], list) else raw_result[0]
106
 
107
+ # is_toxic_by_model = (
108
+ # tox_result["label"].lower() in ["toxic", "label_1"] and tox_result["score"] > 0.5
109
+ # )
110
+ raw_result = toxicity_pipe(text)
111
+ # Handle both nested list [[{...}]] and flat list [{...}] outputs safely
112
+ if isinstance(raw_result, list) and len(raw_result) > 0:
113
+ tox_result = raw_result[0][0] if isinstance(raw_result[0], list) else raw_result[0]
114
+ else:
115
+ tox_result = {"label": "neutral", "score": 0.0}
116
+
117
+ is_toxic_by_model = (
118
+ isinstance(tox_result, dict)
119
+ and tox_result.get("label", "").lower() in ["toxic", "label_1"]
120
+ and tox_result.get("score", 0.0) > 0.5
121
  )
122
+
123
  is_threat_by_keyword = bool(threat_pattern.search(text))
124
 
125
  is_flagged = bool(