Spaces:
Sleeping
Sleeping
NTThong0710 commited on
Commit ·
866fc77
1
Parent(s): 22da32d
ADD IMAGE DETECTION
Browse files- app/app_ui.py +29 -11
- app/safety_check.py +43 -2
- requirements.txt +1 -0
app/app_ui.py
CHANGED
|
@@ -1,8 +1,10 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from app.safety_check import is_prompt_safe
|
| 3 |
from app.gen_ai import generate_response
|
| 4 |
from app.mlops_logger import log_prompt
|
|
|
|
| 5 |
|
|
|
|
| 6 |
def handle_prompt(prompt):
|
| 7 |
safe, info = is_prompt_safe(prompt)
|
| 8 |
if not safe:
|
|
@@ -13,13 +15,29 @@ def handle_prompt(prompt):
|
|
| 13 |
log_prompt(prompt, "OK", True, response)
|
| 14 |
return "✅ Prompt an toàn", response
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from app.safety_check import is_prompt_safe, is_image_safe
|
| 3 |
from app.gen_ai import generate_response
|
| 4 |
from app.mlops_logger import log_prompt
|
| 5 |
+
from PIL import Image
|
| 6 |
|
| 7 |
+
# === Kiểm duyệt prompt ===
|
| 8 |
def handle_prompt(prompt):
|
| 9 |
safe, info = is_prompt_safe(prompt)
|
| 10 |
if not safe:
|
|
|
|
| 15 |
log_prompt(prompt, "OK", True, response)
|
| 16 |
return "✅ Prompt an toàn", response
|
| 17 |
|
| 18 |
+
# === Kiểm duyệt ảnh ===
|
| 19 |
+
def check_image_safety(image: Image.Image):
|
| 20 |
+
safe, reasons = is_image_safe(image)
|
| 21 |
+
if safe:
|
| 22 |
+
return f"✅ Ảnh an toàn: {', '.join(reasons)}"
|
| 23 |
+
else:
|
| 24 |
+
return f"🚨 Ảnh KHÔNG an toàn: {', '.join(reasons)}"
|
| 25 |
+
|
| 26 |
+
# === Giao diện ===
|
| 27 |
+
with gr.Blocks(title="SAIFGuard") as demo:
|
| 28 |
+
gr.Markdown("## 🛡️ SAIFGuard")
|
| 29 |
+
|
| 30 |
+
with gr.Tab("📝 Kiểm duyệt Prompt"):
|
| 31 |
+
prompt_input = gr.Textbox(label="Nhập Prompt")
|
| 32 |
+
prompt_status = gr.Textbox(label="Trạng thái kiểm duyệt")
|
| 33 |
+
prompt_output = gr.Textbox(label="Kết quả GenAI")
|
| 34 |
+
prompt_button = gr.Button("Kiểm tra Prompt")
|
| 35 |
+
prompt_button.click(handle_prompt, inputs=prompt_input, outputs=[prompt_status, prompt_output])
|
| 36 |
+
|
| 37 |
+
with gr.Tab("🖼️ Kiểm duyệt Hình ảnh"):
|
| 38 |
+
image_input = gr.Image(type="pil", label="Tải ảnh lên")
|
| 39 |
+
image_output = gr.Textbox(label="Trạng thái kiểm duyệt hình ảnh")
|
| 40 |
+
image_button = gr.Button("Kiểm tra Hình ảnh")
|
| 41 |
+
image_button.click(fn=check_image_safety, inputs=image_input, outputs=image_output)
|
| 42 |
+
|
| 43 |
+
demo.launch()
|
app/safety_check.py
CHANGED
|
@@ -1,9 +1,21 @@
|
|
| 1 |
from detoxify import Detoxify
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
# Load
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
detox_model = Detoxify('original')
|
| 5 |
|
| 6 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
def is_prompt_safe(prompt: str):
|
| 8 |
results = detox_model.predict(prompt)
|
| 9 |
|
|
@@ -14,3 +26,32 @@ def is_prompt_safe(prompt: str):
|
|
| 14 |
if flagged:
|
| 15 |
return False, list(flagged.keys())
|
| 16 |
return True, []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from detoxify import Detoxify
|
| 2 |
+
from transformers import AutoProcessor, AutoModelForImageClassification , ViTForImageClassification, ViTFeatureExtractor
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import torch
|
| 5 |
|
| 6 |
+
# ==== Load models ====
|
| 7 |
+
# Load mô hình kiểm duyệt ảnh bạo lực
|
| 8 |
+
violence_model = ViTForImageClassification.from_pretrained('jaranohaal/vit-base-violence-detection')
|
| 9 |
+
violence_processor = ViTFeatureExtractor.from_pretrained('jaranohaal/vit-base-violence-detection')
|
| 10 |
+
|
| 11 |
+
# Load mô hình kiểm duyệt văn bản
|
| 12 |
detox_model = Detoxify('original')
|
| 13 |
|
| 14 |
+
# Load mô hình kiểm duyệt ảnh
|
| 15 |
+
image_processor = AutoProcessor.from_pretrained("Falconsai/nsfw_image_detection")
|
| 16 |
+
image_model = AutoModelForImageClassification.from_pretrained("Falconsai/nsfw_image_detection")
|
| 17 |
+
|
| 18 |
+
# ==== Hàm kiểm duyệt prompt ====
|
| 19 |
def is_prompt_safe(prompt: str):
|
| 20 |
results = detox_model.predict(prompt)
|
| 21 |
|
|
|
|
| 26 |
if flagged:
|
| 27 |
return False, list(flagged.keys())
|
| 28 |
return True, []
|
| 29 |
+
|
| 30 |
+
# ==== Hàm kiểm duyệt Hình ảnh ====
|
| 31 |
+
def is_image_safe(image: Image.Image):
|
| 32 |
+
reasons = []
|
| 33 |
+
|
| 34 |
+
# --- Kiểm tra NSFW ---
|
| 35 |
+
nsfw_inputs = image_processor(images=image, return_tensors="pt")
|
| 36 |
+
with torch.no_grad():
|
| 37 |
+
nsfw_outputs = image_model(**nsfw_inputs)
|
| 38 |
+
nsfw_logits = nsfw_outputs.logits
|
| 39 |
+
nsfw_pred = nsfw_logits.argmax(-1).item()
|
| 40 |
+
nsfw_label = image_model.config.id2label[nsfw_pred]
|
| 41 |
+
if nsfw_label.lower() in ["porn", "hentai", "sexy"]:
|
| 42 |
+
reasons.append(f"Khiêu dâm ({nsfw_label})")
|
| 43 |
+
|
| 44 |
+
# --- Kiểm tra Bạo lực ---
|
| 45 |
+
violence_inputs = violence_processor(images=image, return_tensors="pt")
|
| 46 |
+
with torch.no_grad():
|
| 47 |
+
violence_outputs = violence_model(**violence_inputs)
|
| 48 |
+
violence_logits = violence_outputs.logits
|
| 49 |
+
violence_pred = violence_logits.argmax(-1).item()
|
| 50 |
+
violence_label = violence_model.config.id2label[violence_pred]
|
| 51 |
+
if violence_label.lower() in ["violence", "bloody", "weapon", "fight"]:
|
| 52 |
+
reasons.append(f"Bạo lực ({violence_label})")
|
| 53 |
+
|
| 54 |
+
# --- Kết quả ---
|
| 55 |
+
if reasons:
|
| 56 |
+
return False, reasons
|
| 57 |
+
return True, [f"An toàn ({nsfw_label}, {violence_label})"]
|
requirements.txt
CHANGED
|
@@ -4,3 +4,4 @@ torch
|
|
| 4 |
protobuf
|
| 5 |
presidio-analyzer
|
| 6 |
detoxify
|
|
|
|
|
|
| 4 |
protobuf
|
| 5 |
presidio-analyzer
|
| 6 |
detoxify
|
| 7 |
+
Pillow
|