Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -42,7 +42,6 @@ def predict_safety_api(text):
|
|
| 42 |
full_encodings = tokenizer(cleaned_text, add_special_tokens=False, return_attention_mask=False)
|
| 43 |
input_ids = full_encodings['input_ids']
|
| 44 |
|
| 45 |
-
# 🎯 1. حساب عدد التوكنز الكلي الفعلي للنص المدخل
|
| 46 |
total_tokens_count = len(input_ids)
|
| 47 |
|
| 48 |
window_size = 60
|
|
@@ -58,12 +57,10 @@ def predict_safety_api(text):
|
|
| 58 |
if len(window) > 0: windows.append(window)
|
| 59 |
if i + window_size >= len(input_ids): break
|
| 60 |
|
| 61 |
-
# 🎯 2. حساب عدد النوافذ الناتجة رياضياً عن هذا النص
|
| 62 |
total_windows_count = len(windows)
|
| 63 |
|
| 64 |
is_blocked = False
|
| 65 |
highest_unsafe_prob = 0.0
|
| 66 |
-
# 🎯 3. مصفوفة مخصصة لاصطياد وحفظ النوافذ النصية التي تسببت في إطلاق الخطر
|
| 67 |
triggered_sentences = []
|
| 68 |
|
| 69 |
for win_ids in windows:
|
|
@@ -87,19 +84,17 @@ def predict_safety_api(text):
|
|
| 87 |
if unsafe_p > 0.50:
|
| 88 |
is_blocked = True
|
| 89 |
highest_unsafe_prob = max(highest_unsafe_prob, unsafe_p)
|
| 90 |
-
# اصطياد النافذة الخبيثة المسببة للحظر وحفظها بدون تكرار
|
| 91 |
if window_text not in triggered_sentences:
|
| 92 |
triggered_sentences.append(window_text)
|
| 93 |
|
| 94 |
-
# 🎯 حقن وحفظ الحقول الحسابية الجديدة والتفسيرية مباشرة داخل كائن الرد للـ JSON
|
| 95 |
if is_blocked:
|
| 96 |
return {
|
| 97 |
"verdict": "UNSAFE",
|
| 98 |
"block": True,
|
| 99 |
"confidence": f"{highest_unsafe_prob * 100:.2f}%",
|
| 100 |
-
"total_tokens": total_tokens_count,
|
| 101 |
-
"total_windows": total_windows_count,
|
| 102 |
-
"triggered_phrases": triggered_sentences
|
| 103 |
}
|
| 104 |
|
| 105 |
safe_p = 1.0 - highest_unsafe_prob
|
|
@@ -107,9 +102,9 @@ def predict_safety_api(text):
|
|
| 107 |
"verdict": "SAFE",
|
| 108 |
"block": False,
|
| 109 |
"confidence": f"{safe_p * 100:.2f}%",
|
| 110 |
-
"total_tokens": total_tokens_count,
|
| 111 |
-
"total_windows": total_windows_count,
|
| 112 |
-
"triggered_phrases": []
|
| 113 |
}
|
| 114 |
|
| 115 |
interface = gr.Interface(
|
|
|
|
| 42 |
full_encodings = tokenizer(cleaned_text, add_special_tokens=False, return_attention_mask=False)
|
| 43 |
input_ids = full_encodings['input_ids']
|
| 44 |
|
|
|
|
| 45 |
total_tokens_count = len(input_ids)
|
| 46 |
|
| 47 |
window_size = 60
|
|
|
|
| 57 |
if len(window) > 0: windows.append(window)
|
| 58 |
if i + window_size >= len(input_ids): break
|
| 59 |
|
|
|
|
| 60 |
total_windows_count = len(windows)
|
| 61 |
|
| 62 |
is_blocked = False
|
| 63 |
highest_unsafe_prob = 0.0
|
|
|
|
| 64 |
triggered_sentences = []
|
| 65 |
|
| 66 |
for win_ids in windows:
|
|
|
|
| 84 |
if unsafe_p > 0.50:
|
| 85 |
is_blocked = True
|
| 86 |
highest_unsafe_prob = max(highest_unsafe_prob, unsafe_p)
|
|
|
|
| 87 |
if window_text not in triggered_sentences:
|
| 88 |
triggered_sentences.append(window_text)
|
| 89 |
|
|
|
|
| 90 |
if is_blocked:
|
| 91 |
return {
|
| 92 |
"verdict": "UNSAFE",
|
| 93 |
"block": True,
|
| 94 |
"confidence": f"{highest_unsafe_prob * 100:.2f}%",
|
| 95 |
+
"total_tokens": total_tokens_count,
|
| 96 |
+
"total_windows": total_windows_count,
|
| 97 |
+
"triggered_phrases": triggered_sentences
|
| 98 |
}
|
| 99 |
|
| 100 |
safe_p = 1.0 - highest_unsafe_prob
|
|
|
|
| 102 |
"verdict": "SAFE",
|
| 103 |
"block": False,
|
| 104 |
"confidence": f"{safe_p * 100:.2f}%",
|
| 105 |
+
"total_tokens": total_tokens_count,
|
| 106 |
+
"total_windows": total_windows_count,
|
| 107 |
+
"triggered_phrases": []
|
| 108 |
}
|
| 109 |
|
| 110 |
interface = gr.Interface(
|