Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,56 +3,60 @@ from transformers import pipeline
|
|
| 3 |
from PIL import ImageFilter
|
| 4 |
import spaces
|
| 5 |
|
| 6 |
-
#
|
| 7 |
classifier = pipeline("zero-shot-image-classification", model="openai/clip-vit-base-patch32")
|
| 8 |
|
| 9 |
-
# ال
|
| 10 |
-
|
| 11 |
-
"عري أو كشف
|
| 12 |
-
"
|
| 13 |
-
"أفخاذ م
|
| 14 |
-
"
|
| 15 |
-
"
|
| 16 |
-
"تقارب جسدي والتصاق حميمي": "a romantic close physical embrace or intimate touch between a man and a woman",
|
| 17 |
-
"وضعيات إغراء وحركات حميمية": "a suggestive, sensual, or provocative pose"
|
| 18 |
}
|
| 19 |
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
@spaces.GPU
|
| 23 |
-
def
|
| 24 |
if image is None:
|
| 25 |
return None, {"status": "لا توجد صورة"}
|
| 26 |
|
| 27 |
-
# قائمة الفحص ال
|
| 28 |
-
|
|
|
|
| 29 |
|
| 30 |
-
# فحص الصورة
|
| 31 |
-
predictions = classifier(image, candidate_labels=
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
|
|
|
| 35 |
|
| 36 |
-
# تحليل النتائج ومقارنتها بالمعايير الصارمة
|
| 37 |
for pred in predictions:
|
| 38 |
score = pred['score']
|
| 39 |
label = pred['label']
|
| 40 |
|
| 41 |
-
# ال
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
| 51 |
|
| 52 |
-
# إذا ك
|
| 53 |
-
is_violation =
|
| 54 |
|
| 55 |
-
# تطبيق التغبيش الصارم والفوري
|
| 56 |
if is_violation:
|
| 57 |
filtered_image = image.filter(ImageFilter.GaussianBlur(radius=65))
|
| 58 |
else:
|
|
@@ -60,22 +64,23 @@ def strict_censor(image, strictness=0.25):
|
|
| 60 |
|
| 61 |
return filtered_image, {
|
| 62 |
"is_censored": is_violation,
|
| 63 |
-
"status": "🚨 محج
|
| 64 |
-
"
|
| 65 |
-
"
|
|
|
|
| 66 |
}
|
| 67 |
|
| 68 |
demo = gr.Interface(
|
| 69 |
-
fn=
|
| 70 |
inputs=[
|
| 71 |
-
gr.Image(type="pil", label="الصورة
|
| 72 |
-
gr.Slider(minimum=0.
|
| 73 |
],
|
| 74 |
outputs=[
|
| 75 |
-
gr.Image(type="pil", label="النتيجة
|
| 76 |
-
gr.JSON(label="تقرير ال
|
| 77 |
],
|
| 78 |
-
title="Safe Watch AI -
|
| 79 |
)
|
| 80 |
|
| 81 |
-
demo.launch()
|
|
|
|
| 3 |
from PIL import ImageFilter
|
| 4 |
import spaces
|
| 5 |
|
| 6 |
+
# نموذج الفهم الدلالي من OpenAI
|
| 7 |
classifier = pipeline("zero-shot-image-classification", model="openai/clip-vit-base-patch32")
|
| 8 |
|
| 9 |
+
# الممنوعات المحددة بدقة (التركيز على عري وجلد المرأة والقبلات فقط)
|
| 10 |
+
UNSAFE_TAGS = {
|
| 11 |
+
"عري أو صدر امرأة مكشوف": "a photo of a woman exposing her bare breasts, cleavage, or nudity",
|
| 12 |
+
"بيكيني أو ملابس سباحة لامرأة": "a woman in a bikini, swimsuit, or lingerie",
|
| 13 |
+
"أفخاذ امرأة أو تنورة قصيرة جداً": "a woman with exposed bare thighs, legs, or miniskirt",
|
| 14 |
+
"قبلات حميمية بين رجل وامرأة": "a man and woman passionately kissing on the lips",
|
| 15 |
+
"علاقة جسدية حميمية": "intimate sexual intercourse"
|
|
|
|
|
|
|
| 16 |
}
|
| 17 |
|
| 18 |
+
# المسموحات الصريحة (غوكو، رجال الأكشن، الملابس العادية)
|
| 19 |
+
SAFE_TAGS = [
|
| 20 |
+
"a male anime character or man in clothing or martial arts uniform",
|
| 21 |
+
"a fully clothed man or male action hero",
|
| 22 |
+
"a normal family friendly cartoon, anime or movie scene"
|
| 23 |
+
]
|
| 24 |
|
| 25 |
@spaces.GPU
|
| 26 |
+
def smart_censor(image, sensitivity=0.35):
|
| 27 |
if image is None:
|
| 28 |
return None, {"status": "لا توجد صورة"}
|
| 29 |
|
| 30 |
+
# قائمة الفحص المقارنة
|
| 31 |
+
unsafe_labels = list(UNSAFE_TAGS.values())
|
| 32 |
+
all_labels = unsafe_labels + SAFE_TAGS
|
| 33 |
|
| 34 |
+
# فحص الصورة
|
| 35 |
+
predictions = classifier(image, candidate_labels=all_labels)
|
| 36 |
|
| 37 |
+
safe_score = 0.0
|
| 38 |
+
unsafe_score = 0.0
|
| 39 |
+
detected_reason = ""
|
| 40 |
|
|
|
|
| 41 |
for pred in predictions:
|
| 42 |
score = pred['score']
|
| 43 |
label = pred['label']
|
| 44 |
|
| 45 |
+
# إذا كانت النتيجة تدل على شخصية رجل / أنمي / ملابس عادية
|
| 46 |
+
if label in SAFE_TAGS:
|
| 47 |
+
safe_score += score
|
| 48 |
+
else:
|
| 49 |
+
# إذا كانت النتيجة لمحظور من محظورات المرأة أو القبلات
|
| 50 |
+
if score > unsafe_score:
|
| 51 |
+
unsafe_score = score
|
| 52 |
+
# معرفة السبب بالعربية
|
| 53 |
+
for ar_name, en_tag in UNSAFE_TAGS.items():
|
| 54 |
+
if label == en_tag:
|
| 55 |
+
detected_reason = f"{ar_name} ({round(score * 100)}%)"
|
| 56 |
|
| 57 |
+
# الشرط الصارم: لا يحجب إلا إذا كانت نسبة المحظور أعلى من نسبة الأمان وتتجاوز الحساسية
|
| 58 |
+
is_violation = (unsafe_score > safe_score) and (unsafe_score >= sensitivity)
|
| 59 |
|
|
|
|
| 60 |
if is_violation:
|
| 61 |
filtered_image = image.filter(ImageFilter.GaussianBlur(radius=65))
|
| 62 |
else:
|
|
|
|
| 64 |
|
| 65 |
return filtered_image, {
|
| 66 |
"is_censored": is_violation,
|
| 67 |
+
"status": "🚨 تم الحجب (مخالفة)" if is_violation else "✅ آمنة ومسموحة",
|
| 68 |
+
"reason": detected_reason if is_violation else "الصورة طبيعية أو لشخصية رجل/أنمي",
|
| 69 |
+
"unsafe_score": round(unsafe_score, 3),
|
| 70 |
+
"safe_score": round(safe_score, 3)
|
| 71 |
}
|
| 72 |
|
| 73 |
demo = gr.Interface(
|
| 74 |
+
fn=smart_censor,
|
| 75 |
inputs=[
|
| 76 |
+
gr.Image(type="pil", label="الصورة"),
|
| 77 |
+
gr.Slider(minimum=0.20, maximum=0.60, value=0.35, step=0.05, label="حساسية الفلتر")
|
| 78 |
],
|
| 79 |
outputs=[
|
| 80 |
+
gr.Image(type="pil", label="النتيجة"),
|
| 81 |
+
gr.JSON(label="تقرير الذكاء الاصطناعي")
|
| 82 |
],
|
| 83 |
+
title="Safe Watch AI - فلتر التمييز الذكي (حجب عري المرأة والقبلات فقط)"
|
| 84 |
)
|
| 85 |
|
| 86 |
+
demo.launch()
|