Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 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"
|
| 115 |
)
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 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()}")
|