Spaces:
Sleeping
Sleeping
Update app/app_ui.py
Browse files- app/app_ui.py +47 -9
app/app_ui.py
CHANGED
|
@@ -1,8 +1,45 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
from app.gen_ai import generate_response
|
| 4 |
from app.mlops_logger import log_prompt
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# === Kiểm duyệt Prompt ===
|
| 8 |
def handle_prompt(prompt):
|
|
@@ -10,11 +47,11 @@ def handle_prompt(prompt):
|
|
| 10 |
if not safe:
|
| 11 |
log_prompt(prompt, info, False, "")
|
| 12 |
return f"🚨 Prompt không an toàn! Phát hiện: {', '.join(info)}", ""
|
| 13 |
-
|
| 14 |
response = generate_response(prompt)
|
| 15 |
log_prompt(prompt, "OK", True, response)
|
| 16 |
return "✅ Prompt an toàn", response
|
| 17 |
-
|
| 18 |
# === Giao diện ===
|
| 19 |
with gr.Blocks(title="SAIFGuard - HỆ THỐNG KIỂM DUYỆT THÔNG MINH", css="""
|
| 20 |
.yellow-btn {
|
|
@@ -23,7 +60,7 @@ with gr.Blocks(title="SAIFGuard - HỆ THỐNG KIỂM DUYỆT THÔNG MINH", css=
|
|
| 23 |
}
|
| 24 |
""") as demo:
|
| 25 |
gr.Markdown("## 🛡️ SAIFGuard: HỆ THỐNG KIỂM DUYỆT THÔNG MINH")
|
| 26 |
-
|
| 27 |
with gr.Tab("📝 Kiểm duyệt Prompt"):
|
| 28 |
with gr.Row():
|
| 29 |
with gr.Column(scale=1):
|
|
@@ -33,7 +70,7 @@ with gr.Blocks(title="SAIFGuard - HỆ THỐNG KIỂM DUYỆT THÔNG MINH", css=
|
|
| 33 |
prompt_output = gr.Textbox(label="Kết quả GenAI")
|
| 34 |
prompt_button = gr.Button("Kiểm tra Prompt", elem_classes="yellow-btn")
|
| 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 |
gr.Markdown("### 📷 Tải ảnh và kiểm tra từng tiêu chí")
|
| 39 |
|
|
@@ -43,7 +80,7 @@ with gr.Blocks(title="SAIFGuard - HỆ THỐNG KIỂM DUYỆT THÔNG MINH", css=
|
|
| 43 |
with gr.Row():
|
| 44 |
with gr.Column(scale=1):
|
| 45 |
nsfw_output = gr.Textbox(label="🔞 Kết quả kiểm duyệt ảnh nhạy cảm")
|
| 46 |
-
|
| 47 |
with gr.Column(scale=1):
|
| 48 |
violence_output = gr.Textbox(label="🧨 Kết quả kiểm duyệt ảnh bạo lực")
|
| 49 |
violence_button = gr.Button("Kiểm tra Ảnh Bạo Lực", elem_classes="yellow-btn")
|
|
@@ -59,8 +96,9 @@ with gr.Blocks(title="SAIFGuard - HỆ THỐNG KIỂM DUYỆT THÔNG MINH", css=
|
|
| 59 |
url_output = gr.Textbox(label="Kết quả kiểm duyệt URL")
|
| 60 |
url_button = gr.Button("Kiểm tra URL", elem_classes="yellow-btn")
|
| 61 |
url_button.click(fn=check_url, inputs=url_input, outputs=url_output)
|
| 62 |
-
|
| 63 |
-
|
|
|
|
| 64 |
btn2 = gr.Button("Chuyển đổi & Kiểm tra")
|
| 65 |
stt, trans, gen2 = (
|
| 66 |
gr.Textbox(label="Trạng thái kiểm duyệt"),
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
import whisper
|
| 4 |
+
import torch
|
| 5 |
+
import scipy.io.wavfile
|
| 6 |
+
import cv2
|
| 7 |
+
from app.safety_check import is_prompt_safe, check_nsfw_image, check_violence_image, check_url
|
| 8 |
from app.gen_ai import generate_response
|
| 9 |
from app.mlops_logger import log_prompt
|
| 10 |
+
|
| 11 |
+
# Load Whisper model
|
| 12 |
+
asr_model = whisper.load_model("tiny", device="cuda" if torch.cuda.is_available() else "cpu")
|
| 13 |
+
|
| 14 |
+
# Hàm chuyển giọng nói thành văn bản
|
| 15 |
+
def transcribe_and_check(audio):
|
| 16 |
+
if audio is None:
|
| 17 |
+
return "❌ Không có dữ liệu âm thanh", "", ""
|
| 18 |
+
|
| 19 |
+
sr, data = audio
|
| 20 |
+
|
| 21 |
+
# Chuyển dữ liệu âm thanh sang dạng float32
|
| 22 |
+
audio_fp32 = whisper.pad_or_trim(data.astype(np.float32) / 32768.0)
|
| 23 |
+
|
| 24 |
+
# Tạo spectrogram
|
| 25 |
+
mel = whisper.log_mel_spectrogram(audio_fp32).to(asr_model.device)
|
| 26 |
+
|
| 27 |
+
# Giải mã
|
| 28 |
+
result = asr_model.decode(mel)
|
| 29 |
+
text = result.text.strip()
|
| 30 |
+
|
| 31 |
+
if not text:
|
| 32 |
+
return "⚠️ Không nhận diện được giọng nói.", "", ""
|
| 33 |
+
|
| 34 |
+
# Kiểm tra prompt an toàn
|
| 35 |
+
is_safe, categories = is_prompt_safe(text)
|
| 36 |
+
|
| 37 |
+
if is_safe:
|
| 38 |
+
return "✅ Nội dung an toàn", text, ""
|
| 39 |
+
else:
|
| 40 |
+
cat_text = ", ".join(categories)
|
| 41 |
+
return f"🚨 Phát hiện nội dung không an toàn", text, f"❗ Các danh mục phát hiện: {cat_text}"
|
| 42 |
+
|
| 43 |
|
| 44 |
# === Kiểm duyệt Prompt ===
|
| 45 |
def handle_prompt(prompt):
|
|
|
|
| 47 |
if not safe:
|
| 48 |
log_prompt(prompt, info, False, "")
|
| 49 |
return f"🚨 Prompt không an toàn! Phát hiện: {', '.join(info)}", ""
|
| 50 |
+
|
| 51 |
response = generate_response(prompt)
|
| 52 |
log_prompt(prompt, "OK", True, response)
|
| 53 |
return "✅ Prompt an toàn", response
|
| 54 |
+
|
| 55 |
# === Giao diện ===
|
| 56 |
with gr.Blocks(title="SAIFGuard - HỆ THỐNG KIỂM DUYỆT THÔNG MINH", css="""
|
| 57 |
.yellow-btn {
|
|
|
|
| 60 |
}
|
| 61 |
""") as demo:
|
| 62 |
gr.Markdown("## 🛡️ SAIFGuard: HỆ THỐNG KIỂM DUYỆT THÔNG MINH")
|
| 63 |
+
|
| 64 |
with gr.Tab("📝 Kiểm duyệt Prompt"):
|
| 65 |
with gr.Row():
|
| 66 |
with gr.Column(scale=1):
|
|
|
|
| 70 |
prompt_output = gr.Textbox(label="Kết quả GenAI")
|
| 71 |
prompt_button = gr.Button("Kiểm tra Prompt", elem_classes="yellow-btn")
|
| 72 |
prompt_button.click(handle_prompt, inputs=prompt_input, outputs=[prompt_status, prompt_output])
|
| 73 |
+
|
| 74 |
with gr.Tab("🖼️ Kiểm duyệt Hình ảnh"):
|
| 75 |
gr.Markdown("### 📷 Tải ảnh và kiểm tra từng tiêu chí")
|
| 76 |
|
|
|
|
| 80 |
with gr.Row():
|
| 81 |
with gr.Column(scale=1):
|
| 82 |
nsfw_output = gr.Textbox(label="🔞 Kết quả kiểm duyệt ảnh nhạy cảm")
|
| 83 |
+
nsfw_button = gr.Button("Kiểm tra Ảnh Nhạy Cảm", elem_classes="yellow-btn")
|
| 84 |
with gr.Column(scale=1):
|
| 85 |
violence_output = gr.Textbox(label="🧨 Kết quả kiểm duyệt ảnh bạo lực")
|
| 86 |
violence_button = gr.Button("Kiểm tra Ảnh Bạo Lực", elem_classes="yellow-btn")
|
|
|
|
| 96 |
url_output = gr.Textbox(label="Kết quả kiểm duyệt URL")
|
| 97 |
url_button = gr.Button("Kiểm tra URL", elem_classes="yellow-btn")
|
| 98 |
url_button.click(fn=check_url, inputs=url_input, outputs=url_output)
|
| 99 |
+
|
| 100 |
+
with gr.Tab("🎙️Giọng Nói"):
|
| 101 |
+
audio_input = gr.Audio(type="numpy", label="Thu âm giọng nói")
|
| 102 |
btn2 = gr.Button("Chuyển đổi & Kiểm tra")
|
| 103 |
stt, trans, gen2 = (
|
| 104 |
gr.Textbox(label="Trạng thái kiểm duyệt"),
|