devserenerocks commited on
Commit
bfc1409
·
verified ·
1 Parent(s): 46e3b1d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -27
app.py CHANGED
@@ -98,32 +98,37 @@ def run_shield_check(image: Image.Image) -> dict:
98
 
99
  @app.post("/moderate-image")
100
  def moderate_image(request: ImageRequest):
101
- image = load_image(str(request.image_url))
102
-
103
- nsfw_score = run_nsfw_check(image)
104
- shield_scores = run_shield_check(image)
105
-
106
- reasons = []
107
- if nsfw_score >= THRESHOLD:
108
- reasons.append(
109
- f"NSFW classifier flagged the image as explicit ({nsfw_score:.0%} confidence)"
110
- )
111
- for policy, score in shield_scores.items():
112
- if score >= THRESHOLD:
113
  reasons.append(
114
- f"ShieldGemma 2 flagged {POLICIES[policy]} ({score:.0%} confidence)"
115
  )
116
-
117
- is_safe = not reasons
118
- return {
119
- "rating": "safe" if is_safe else "not safe",
120
- "reason": (
121
- "No policy violations detected by either model."
122
- if is_safe
123
- else "; ".join(reasons)
124
- ),
125
- "scores": {
126
- "nsfw": round(nsfw_score, 4),
127
- **{k: round(v, 4) for k, v in shield_scores.items()},
128
- },
129
- }
 
 
 
 
 
 
 
 
 
 
98
 
99
  @app.post("/moderate-image")
100
  def moderate_image(request: ImageRequest):
101
+ try:
102
+ image = load_image(str(request.image_url))
103
+
104
+ nsfw_score = run_nsfw_check(image)
105
+ shield_scores = run_shield_check(image)
106
+
107
+ reasons = []
108
+ if nsfw_score >= THRESHOLD:
 
 
 
 
109
  reasons.append(
110
+ f"NSFW classifier flagged the image as explicit ({nsfw_score:.0%} confidence)"
111
  )
112
+ for policy, score in shield_scores.items():
113
+ if score >= THRESHOLD:
114
+ reasons.append(
115
+ f"ShieldGemma 2 flagged {POLICIES[policy]} ({score:.0%} confidence)"
116
+ )
117
+
118
+ is_safe = not reasons
119
+ return {
120
+ "rating": "safe" if is_safe else "not safe",
121
+ "reason": (
122
+ "No policy violations detected by either model."
123
+ if is_safe
124
+ else "; ".join(reasons)
125
+ ),
126
+ "scores": {
127
+ "nsfw": round(nsfw_score, 4),
128
+ **{k: round(v, 4) for k, v in shield_scores.items()},
129
+ },
130
+ }
131
+ except HTTPException:
132
+ raise
133
+ except Exception as e:
134
+ raise HTTPException(status_code=500, detail=f"{type(e).__name__}: {e}\n{traceback.format_exc()}")