bakisae commited on
Commit
bf5243f
·
verified ·
1 Parent(s): 4b82865

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -42
app.py CHANGED
@@ -3,56 +3,60 @@ from transformers import pipeline
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_CRITERIA = {
11
- "عري أو كشف جسد": "a photo of nudity, bare skin, or exposed body",
12
- "ملابس سباحة أو بيكيني أو ملابس داخلية": "a woman in a bikini, swimsuit, bra, or lingerie",
13
- "أفخاذ مكشوفة أو سروال قصير جداً": "a photo with exposed bare thighs, legs, or short shorts",
14
- "كشف جلد المرأة أو الصدر والكتفين": "a photo of exposed female skin, bare shoulders, or cleavage",
15
- "قبلات بين رجل وامرأة": "a man and a woman kissing or intimate lips touch",
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
- SAFE_PROMPT = "a normal, fully clothed, family-friendly scene or landscape"
 
 
 
 
 
21
 
22
  @spaces.GPU
23
- def strict_censor(image, strictness=0.25):
24
  if image is None:
25
  return None, {"status": "لا توجد صورة"}
26
 
27
- # قائمة الفحص الكاملة
28
- labels_to_check = list(UNSAFE_CRITERIA.values()) + [SAFE_PROMPT]
 
29
 
30
- # فحص الصورة بأعلى دقة
31
- predictions = classifier(image, candidate_labels=labels_to_check)
32
 
33
- detected_violations = []
34
- max_unsafe_score = 0.0
 
35
 
36
- # تحليل النتائج ومقارنتها بالمعايير الصارمة
37
  for pred in predictions:
38
  score = pred['score']
39
  label = pred['label']
40
 
41
- # البحث في الممنوعات
42
- for ar_name, en_desc in UNSAFE_CRITERIA.items():
43
- if label == en_desc:
44
- if score >= strictness:
45
- detected_violations.append({
46
- "السبب": ar_name,
47
- "نسبة التأكد": f"{round(score * 100, 1)}%"
48
- })
49
- if score > max_unsafe_score:
50
- max_unsafe_score = score
 
51
 
52
- # إذا كشف أي مخالفة من القائمة
53
- is_violation = len(detected_violations) > 0
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": "🚨 محجوبة بصرامة (تم كشف مخالفة)" if is_violation else "✅ آمنة ومحتشمة",
64
- "violations": detected_violations if is_violation else "لا توجد أي لقطة حساسة",
65
- "highest_score": round(max_unsafe_score, 3)
 
66
  }
67
 
68
  demo = gr.Interface(
69
- fn=strict_censor,
70
  inputs=[
71
- gr.Image(type="pil", label="الصورة للمعاينة"),
72
- gr.Slider(minimum=0.10, maximum=0.50, value=0.25, step=0.05, label="مستوى الصرامة (0.25 هو الأنسب والأشد صرامة)")
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()