Spaces:
Sleeping
Sleeping
NTThong0710 commited on
Commit ·
ddda57a
1
Parent(s): e06abba
Last fix
Browse files- app/app_ui.py +41 -2
- app/safety_check.py +154 -91
- requirements.txt +3 -1
app/app_ui.py
CHANGED
|
@@ -1,8 +1,40 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
from app.gen_ai import generate_response
|
| 3 |
from app.mlops_logger import log_prompt
|
| 4 |
from app.safety_check import check_nsfw_image, check_violence_image, is_prompt_safe, check_url
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
# === Kiểm duyệt Prompt ===
|
| 7 |
def handle_prompt(prompt):
|
| 8 |
safe, info = is_prompt_safe(prompt)
|
|
@@ -58,5 +90,12 @@ with gr.Blocks(title="SAIFGuard - HỆ THỐNG KIỂM DUYỆT THÔNG MINH", css=
|
|
| 58 |
url_output = gr.Textbox(label="Kết quả kiểm duyệt URL")
|
| 59 |
url_button = gr.Button("Kiểm tra URL", elem_classes="yellow-btn")
|
| 60 |
url_button.click(fn=check_url, inputs=url_input, outputs=url_output)
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import whisper
|
| 3 |
+
import scipy.io.wavfile
|
| 4 |
+
|
| 5 |
+
from app.safety_check import is_prompt_safe
|
| 6 |
from app.gen_ai import generate_response
|
| 7 |
from app.mlops_logger import log_prompt
|
| 8 |
from app.safety_check import check_nsfw_image, check_violence_image, is_prompt_safe, check_url
|
| 9 |
|
| 10 |
+
# Load Whisper model
|
| 11 |
+
asr_model = whisper.load_model("base")
|
| 12 |
+
|
| 13 |
+
# Hàm chuyển giọng nói thành văn bản
|
| 14 |
+
def transcribe_and_check(audio):
|
| 15 |
+
if audio is None:
|
| 16 |
+
return "❌ Không có dữ liệu âm thanh", "", ""
|
| 17 |
+
|
| 18 |
+
# Nếu audio là tuple (sr, data), cần lưu lại file tạm
|
| 19 |
+
import tempfile
|
| 20 |
+
import numpy as np
|
| 21 |
+
import scipy.io.wavfile
|
| 22 |
+
|
| 23 |
+
sr, data = audio
|
| 24 |
+
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp:
|
| 25 |
+
scipy.io.wavfile.write(tmp.name, sr, data)
|
| 26 |
+
result = asr_model.transcribe(tmp.name)
|
| 27 |
+
|
| 28 |
+
text = result["text"]
|
| 29 |
+
safe, info = is_prompt_safe(text)
|
| 30 |
+
if not safe:
|
| 31 |
+
log_prompt(text, info, False, "")
|
| 32 |
+
return f"🚨 Prompt không an toàn! Phát hiện: {', '.join(info)}", text, ""
|
| 33 |
+
|
| 34 |
+
response = generate_response(text)
|
| 35 |
+
log_prompt(text, "OK", True, response)
|
| 36 |
+
return "✅ Prompt an toàn", text, response
|
| 37 |
+
|
| 38 |
# === Kiểm duyệt Prompt ===
|
| 39 |
def handle_prompt(prompt):
|
| 40 |
safe, info = is_prompt_safe(prompt)
|
|
|
|
| 90 |
url_output = gr.Textbox(label="Kết quả kiểm duyệt URL")
|
| 91 |
url_button = gr.Button("Kiểm tra URL", elem_classes="yellow-btn")
|
| 92 |
url_button.click(fn=check_url, inputs=url_input, outputs=url_output)
|
| 93 |
+
with gr.Tab("🤖 Giọng Nói"):
|
| 94 |
+
audio_input = gr.Audio( type="numpy", label="Thu âm giọng nói")
|
| 95 |
+
btn2 = gr.Button("Chuyển đổi & Kiểm tra")
|
| 96 |
+
stt, trans, gen2 = (
|
| 97 |
+
gr.Textbox(label="Trạng thái kiểm duyệt"),
|
| 98 |
+
gr.Textbox(label="Văn bản chuyển đổi"),
|
| 99 |
+
gr.Textbox(label="Kết quả GenAI"),
|
| 100 |
+
)
|
| 101 |
+
btn2.click(fn=transcribe_and_check, inputs=audio_input, outputs=[stt, trans, gen2])
|
app/safety_check.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
from detoxify import Detoxify
|
| 2 |
from transformers import (
|
| 3 |
AutoProcessor, AutoModelForImageClassification,
|
|
@@ -6,10 +7,55 @@ from transformers import (
|
|
| 6 |
)
|
| 7 |
from PIL import Image
|
| 8 |
import torch
|
|
|
|
| 9 |
import re
|
| 10 |
from urllib.parse import urlparse, unquote
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
# Load models
|
| 15 |
detox_model = Detoxify('original')
|
|
@@ -22,15 +68,11 @@ violence_model_id = "jaranohaal/vit-base-violence-detection"
|
|
| 22 |
violence_model = ViTForImageClassification.from_pretrained(violence_model_id)
|
| 23 |
violence_processor = ViTFeatureExtractor.from_pretrained(violence_model_id)
|
| 24 |
|
|
|
|
| 25 |
blip_processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 26 |
blip_model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 27 |
|
| 28 |
-
# Load mô hình zero-shot nhẹ
|
| 29 |
-
classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
|
| 30 |
|
| 31 |
-
|
| 32 |
-
# ========================== TEXT (Prompt) ==========================
|
| 33 |
-
@lru_cache(maxsize=128)
|
| 34 |
def is_prompt_safe(prompt: str):
|
| 35 |
results = detox_model.predict(prompt)
|
| 36 |
threshold = 0.5
|
|
@@ -39,8 +81,6 @@ def is_prompt_safe(prompt: str):
|
|
| 39 |
return False, list(flagged.keys())
|
| 40 |
return True, []
|
| 41 |
|
| 42 |
-
|
| 43 |
-
# ========================== IMAGE (NSFW + Violence) ==========================
|
| 44 |
def generate_caption(image: Image.Image):
|
| 45 |
inputs = blip_processor(images=image, return_tensors="pt")
|
| 46 |
with torch.no_grad():
|
|
@@ -48,92 +88,117 @@ def generate_caption(image: Image.Image):
|
|
| 48 |
caption = blip_processor.decode(out[0], skip_special_tokens=True)
|
| 49 |
return caption
|
| 50 |
|
| 51 |
-
def
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
|
| 103 |
-
# ===
|
| 104 |
-
@lru_cache(maxsize=128)
|
| 105 |
def check_url(url: str):
|
| 106 |
try:
|
|
|
|
| 107 |
decoded_url = unquote(url)
|
| 108 |
parsed = urlparse(decoded_url)
|
|
|
|
|
|
|
| 109 |
warnings = []
|
| 110 |
-
|
|
|
|
| 111 |
if re.match(r'^https?://\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}', decoded_url):
|
| 112 |
-
warnings.append("
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
|
|
|
|
|
|
| 117 |
if 'redirect' in parsed.path.lower() or 'url=' in parsed.query.lower():
|
| 118 |
-
warnings.append("
|
| 119 |
-
|
|
|
|
| 120 |
if re.search(r'%[0-9a-f]{2}|[\x00-\x1f\x7f]', url):
|
| 121 |
-
warnings.append("
|
| 122 |
-
|
|
|
|
| 123 |
if '@' in parsed.netloc:
|
| 124 |
-
warnings.append("
|
| 125 |
-
|
|
|
|
| 126 |
deceptive_domains = ['login', 'secure', 'account', 'verify', 'update']
|
| 127 |
if any(keyword in parsed.netloc.lower() for keyword in deceptive_domains):
|
| 128 |
-
warnings.append("
|
| 129 |
-
|
|
|
|
| 130 |
if parsed.scheme == 'http':
|
| 131 |
-
warnings.append("
|
| 132 |
-
|
|
|
|
| 133 |
ai_result = classifier(url, candidate_labels=["malicious", "safe"])
|
| 134 |
ai_label = ai_result["labels"][0]
|
| 135 |
ai_score = ai_result["scores"][0] * 100
|
| 136 |
-
|
|
|
|
| 137 |
report = {
|
| 138 |
"url": url,
|
| 139 |
"decoded_url": decoded_url,
|
|
@@ -145,39 +210,37 @@ def check_url(url: str):
|
|
| 145 |
"confidence": ai_score
|
| 146 |
}
|
| 147 |
}
|
| 148 |
-
|
|
|
|
| 149 |
if warnings or ai_label == "malicious":
|
| 150 |
return format_report(report, is_safe=False)
|
| 151 |
else:
|
| 152 |
return format_report(report, is_safe=True)
|
| 153 |
-
|
| 154 |
except Exception as e:
|
| 155 |
-
return f"
|
| 156 |
|
| 157 |
def format_report(report: dict, is_safe: bool):
|
|
|
|
| 158 |
warning_text = "\n".join(f"- {w}" for w in report["warnings"]) if report["warnings"] else "- Không phát hiện cảnh báo"
|
| 159 |
-
|
| 160 |
if not is_safe:
|
| 161 |
-
return f"""
|
| 162 |
-
|
| 163 |
• URL gốc: {report['url']}
|
| 164 |
• Domain: {report['domain']}
|
| 165 |
• Đường dẫn: {report['path']}
|
| 166 |
-
|
| 167 |
-
\ud83d\udce3 CẢNH BÁO:
|
| 168 |
{warning_text}
|
| 169 |
-
|
| 170 |
🤖 Phân tích AI:
|
| 171 |
- Kết quả: {report['ai_analysis']['label']}
|
| 172 |
- Độ tin cậy: {report['ai_analysis']['confidence']:.2f}%
|
| 173 |
-
|
| 174 |
🛡️ Khuyến nghị: KHÔNG TRUY CẬP!"""
|
| 175 |
else:
|
| 176 |
return f"""✅ URL AN TOÀN
|
| 177 |
-
|
| 178 |
• URL gốc: {report['url']}
|
| 179 |
• Domain: {report['domain']}
|
| 180 |
-
|
| 181 |
🤖 Phân tích AI:
|
| 182 |
- Kết quả: {report['ai_analysis']['label']}
|
| 183 |
- Độ tin cậy: {report['ai_analysis']['confidence']:.2f}%"""
|
|
|
|
| 1 |
+
import speech_recognition as sr
|
| 2 |
from detoxify import Detoxify
|
| 3 |
from transformers import (
|
| 4 |
AutoProcessor, AutoModelForImageClassification,
|
|
|
|
| 7 |
)
|
| 8 |
from PIL import Image
|
| 9 |
import torch
|
| 10 |
+
|
| 11 |
import re
|
| 12 |
from urllib.parse import urlparse, unquote
|
| 13 |
+
# Khởi tạo Detoxify model
|
| 14 |
+
detox_model = Detoxify('original')
|
| 15 |
+
|
| 16 |
+
# Hàm giảm kích thước ảnh
|
| 17 |
+
def resize_image(image: Image.Image, target_size=(224, 224)):
|
| 18 |
+
"""Giảm kích thước hình ảnh để phù hợp với mô hình"""
|
| 19 |
+
return image.resize(target_size, Image.ANTIALIAS)
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
# Hàm chuyển đổi giọng nói thành văn bản
|
| 23 |
+
def speech_to_text():
|
| 24 |
+
recognizer = sr.Recognizer()
|
| 25 |
+
|
| 26 |
+
with sr.Microphone() as source:
|
| 27 |
+
print("Đang nghe... Hãy nói điều gì đó")
|
| 28 |
+
recognizer.adjust_for_ambient_noise(source)
|
| 29 |
+
audio = recognizer.listen(source)
|
| 30 |
+
|
| 31 |
+
try:
|
| 32 |
+
print("Đang xử lý...")
|
| 33 |
+
text = recognizer.recognize_google(audio, language="vi-VN")
|
| 34 |
+
print(f"Bạn đã nói: {text}")
|
| 35 |
+
return text
|
| 36 |
+
except sr.UnknownValueError:
|
| 37 |
+
print("Không nhận dạng được giọng nói")
|
| 38 |
+
return ""
|
| 39 |
+
except sr.RequestError as e:
|
| 40 |
+
print(f"Lỗi kết nối đến dịch vụ nhận diện giọng nói: {e}")
|
| 41 |
+
return ""
|
| 42 |
+
|
| 43 |
+
# Hàm chính thực hiện quy trình: speech -> text -> toxic detection
|
| 44 |
+
def detect_toxic_speech():
|
| 45 |
+
text = speech_to_text()
|
| 46 |
+
|
| 47 |
+
if not text:
|
| 48 |
+
return "Không có văn bản để phân tích"
|
| 49 |
+
|
| 50 |
+
is_safe, toxic_categories = is_prompt_safe(text)
|
| 51 |
+
|
| 52 |
+
if is_safe:
|
| 53 |
+
return f"Văn bản an toàn: '{text}'"
|
| 54 |
+
else:
|
| 55 |
+
return f"Phát hiện nội dung không an toàn trong: '{text}'\nCác danh mục: {toxic_categories}"
|
| 56 |
+
|
| 57 |
+
# Load model phát hiện URL độc hại
|
| 58 |
+
classifier = pipeline("zero-shot-classification")
|
| 59 |
|
| 60 |
# Load models
|
| 61 |
detox_model = Detoxify('original')
|
|
|
|
| 68 |
violence_model = ViTForImageClassification.from_pretrained(violence_model_id)
|
| 69 |
violence_processor = ViTFeatureExtractor.from_pretrained(violence_model_id)
|
| 70 |
|
| 71 |
+
# Load BLIP cho caption của ảnh
|
| 72 |
blip_processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 73 |
blip_model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 74 |
|
|
|
|
|
|
|
| 75 |
|
|
|
|
|
|
|
|
|
|
| 76 |
def is_prompt_safe(prompt: str):
|
| 77 |
results = detox_model.predict(prompt)
|
| 78 |
threshold = 0.5
|
|
|
|
| 81 |
return False, list(flagged.keys())
|
| 82 |
return True, []
|
| 83 |
|
|
|
|
|
|
|
| 84 |
def generate_caption(image: Image.Image):
|
| 85 |
inputs = blip_processor(images=image, return_tensors="pt")
|
| 86 |
with torch.no_grad():
|
|
|
|
| 88 |
caption = blip_processor.decode(out[0], skip_special_tokens=True)
|
| 89 |
return caption
|
| 90 |
|
| 91 |
+
def check_nsfw_image(image: Image.Image) -> str:
|
| 92 |
+
# Giảm kích thước ảnh trước khi xử lý
|
| 93 |
+
image = resize_image(image)
|
| 94 |
+
"""Kiểm tra và trả về kết quả NSFW của ảnh"""
|
| 95 |
+
# Xử lý NSFW
|
| 96 |
+
nsfw_inputs = nsfw_processor(images=image, return_tensors="pt")
|
| 97 |
+
with torch.no_grad():
|
| 98 |
+
nsfw_outputs = nsfw_model(**nsfw_inputs)
|
| 99 |
+
nsfw_probs = torch.nn.functional.softmax(nsfw_outputs.logits, dim=1)[0]
|
| 100 |
+
|
| 101 |
+
nsfw_labels = list(nsfw_model.config.id2label.values())
|
| 102 |
+
nsfw_pred = nsfw_probs.argmax().item()
|
| 103 |
+
nsfw_label = nsfw_labels[nsfw_pred]
|
| 104 |
+
nsfw_score = nsfw_probs[nsfw_pred].item() * 100
|
| 105 |
+
|
| 106 |
+
# Tạo caption
|
| 107 |
+
caption = generate_caption(image)
|
| 108 |
+
|
| 109 |
+
# Đánh giá kết quả
|
| 110 |
+
if nsfw_label.lower() in ["porn", "hentai", "sex", "nsfw"]:
|
| 111 |
+
return f"""🚨 Ảnh KHÔNG an toàn (NSFW):
|
| 112 |
+
- Loại: {nsfw_label}
|
| 113 |
+
- Độ chính xác: {nsfw_score:.2f}%
|
| 114 |
+
- Mô tả: {caption}"""
|
| 115 |
+
else:
|
| 116 |
+
return f"""✅ Ảnh an toàn (NSFW):
|
| 117 |
+
- Loại: {nsfw_label}
|
| 118 |
+
- Độ chính xác: {nsfw_score:.2f}%
|
| 119 |
+
- Mô tả: {caption}"""
|
| 120 |
+
|
| 121 |
+
def check_violence_image(image: Image.Image) -> str:
|
| 122 |
+
# Giảm kích thước ảnh trước khi xử lý
|
| 123 |
+
image = resize_image(image)
|
| 124 |
+
"""Kiểm tra và trả về kết quả bạo lực của ảnh"""
|
| 125 |
+
# Xử lý bạo lực
|
| 126 |
+
violence_inputs = violence_processor(images=image, return_tensors="pt")
|
| 127 |
+
with torch.no_grad():
|
| 128 |
+
violence_outputs = violence_model(**violence_inputs)
|
| 129 |
+
violence_probs = torch.nn.functional.softmax(violence_outputs.logits, dim=1)[0]
|
| 130 |
+
|
| 131 |
+
violence_labels = ["Non-Violent", "Violent"]
|
| 132 |
+
violence_pred = violence_probs.argmax().item()
|
| 133 |
+
violence_label = violence_labels[violence_pred]
|
| 134 |
+
violence_score = violence_probs[violence_pred].item() * 100
|
| 135 |
+
|
| 136 |
+
# Tạo caption
|
| 137 |
+
caption = generate_caption(image)
|
| 138 |
+
|
| 139 |
+
# Đánh giá kết quả
|
| 140 |
+
is_violent = False
|
| 141 |
+
if violence_label.lower() == "non-violent" and violence_score > 50:
|
| 142 |
+
is_violent = True
|
| 143 |
+
elif violence_label.lower() == "violent" and violence_score > 80:
|
| 144 |
+
is_violent = True
|
| 145 |
+
|
| 146 |
+
if is_violent:
|
| 147 |
+
return f"""🚨 Ảnh KHÔNG an toàn (Bạo lực):
|
| 148 |
+
- Loại: {violence_label}
|
| 149 |
+
- Độ chính xác: {violence_score:.2f}%
|
| 150 |
+
- Mô tả: {caption}"""
|
| 151 |
+
else:
|
| 152 |
+
return f"""✅ Ảnh an toàn (Bạo lực):
|
| 153 |
+
- Loại: {violence_label}
|
| 154 |
+
- Độ chính xác: {violence_score:.2f}%
|
| 155 |
+
- Mô tả: {caption}"""
|
| 156 |
|
| 157 |
+
# ===Hàm check url===
|
|
|
|
| 158 |
def check_url(url: str):
|
| 159 |
try:
|
| 160 |
+
# Chuẩn hóa URL (decode các ký tự đặc biệt)
|
| 161 |
decoded_url = unquote(url)
|
| 162 |
parsed = urlparse(decoded_url)
|
| 163 |
+
|
| 164 |
+
# Danh sách cảnh báo
|
| 165 |
warnings = []
|
| 166 |
+
|
| 167 |
+
# 1. Phát hiện IP thay vì domain (http://203.0.113.45/...)
|
| 168 |
if re.match(r'^https?://\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}', decoded_url):
|
| 169 |
+
warnings.append("🚨 Nguy hiểm: Truy cập trực tiếp bằng IP (thường dùng cho tấn công)")
|
| 170 |
+
|
| 171 |
+
# 2. Phát hiện file thực thi (gift-card.exe)
|
| 172 |
+
if re.search(r'\.(exe|msi|bat|js|jar|apk|dmg)(\?|$)', parsed.path.lower()):
|
| 173 |
+
warnings.append("🚨 Nguy hiểm: URL chứa file thực thi có thể độc hại")
|
| 174 |
+
|
| 175 |
+
# 3. Phát hiện redirect độc hại (redirect?target=...)
|
| 176 |
if 'redirect' in parsed.path.lower() or 'url=' in parsed.query.lower():
|
| 177 |
+
warnings.append("⚠️ Cảnh báo: URL chứa chức năng redirect (có thể lừa đảo)")
|
| 178 |
+
|
| 179 |
+
# 4. Phát hiện ký tự đặc biệt (/login%20%2F%00%3F%2F%2E%2E)
|
| 180 |
if re.search(r'%[0-9a-f]{2}|[\x00-\x1f\x7f]', url):
|
| 181 |
+
warnings.append("🚨 Nguy hiểm: URL chứa ký tự mã hóa đáng ngờ (có thể tấn công)")
|
| 182 |
+
|
| 183 |
+
# 5. Phát hiện domain giả mạo (example.com@malicious-site.com)
|
| 184 |
if '@' in parsed.netloc:
|
| 185 |
+
warnings.append("🚨 Lừa đảo: URL chứa kỹ thuật giả mạo domain (user@fake-domain)")
|
| 186 |
+
|
| 187 |
+
# 6. Phát hiện domain giả danh (secure.example-login.com)
|
| 188 |
deceptive_domains = ['login', 'secure', 'account', 'verify', 'update']
|
| 189 |
if any(keyword in parsed.netloc.lower() for keyword in deceptive_domains):
|
| 190 |
+
warnings.append("⚠️ Cảnh báo: Domain có dấu hiệu giả mạo dịch vụ đăng nhập")
|
| 191 |
+
|
| 192 |
+
# 7. Kiểm tra giao thức không mã hóa
|
| 193 |
if parsed.scheme == 'http':
|
| 194 |
+
warnings.append("⚠️ Cảnh báo: Kết nối không mã hóa (HTTP)")
|
| 195 |
+
|
| 196 |
+
# Kết hợp với AI classifier
|
| 197 |
ai_result = classifier(url, candidate_labels=["malicious", "safe"])
|
| 198 |
ai_label = ai_result["labels"][0]
|
| 199 |
ai_score = ai_result["scores"][0] * 100
|
| 200 |
+
|
| 201 |
+
# Tạo báo cáo
|
| 202 |
report = {
|
| 203 |
"url": url,
|
| 204 |
"decoded_url": decoded_url,
|
|
|
|
| 210 |
"confidence": ai_score
|
| 211 |
}
|
| 212 |
}
|
| 213 |
+
|
| 214 |
+
# Quyết định cuối cùng
|
| 215 |
if warnings or ai_label == "malicious":
|
| 216 |
return format_report(report, is_safe=False)
|
| 217 |
else:
|
| 218 |
return format_report(report, is_safe=True)
|
| 219 |
+
|
| 220 |
except Exception as e:
|
| 221 |
+
return f"⚠️ Lỗi khi phân tích URL: {str(e)}"
|
| 222 |
|
| 223 |
def format_report(report: dict, is_safe: bool):
|
| 224 |
+
"""Định dạng báo cáo dễ đọc"""
|
| 225 |
warning_text = "\n".join(f"- {w}" for w in report["warnings"]) if report["warnings"] else "- Không phát hiện cảnh báo"
|
| 226 |
+
|
| 227 |
if not is_safe:
|
| 228 |
+
return f"""🚨 URL KHÔNG AN TOÀN
|
| 229 |
+
🔍 Phân tích chi tiết:
|
| 230 |
• URL gốc: {report['url']}
|
| 231 |
• Domain: {report['domain']}
|
| 232 |
• Đường dẫn: {report['path']}
|
| 233 |
+
📢 CẢNH BÁO:
|
|
|
|
| 234 |
{warning_text}
|
|
|
|
| 235 |
🤖 Phân tích AI:
|
| 236 |
- Kết quả: {report['ai_analysis']['label']}
|
| 237 |
- Độ tin cậy: {report['ai_analysis']['confidence']:.2f}%
|
|
|
|
| 238 |
🛡️ Khuyến nghị: KHÔNG TRUY CẬP!"""
|
| 239 |
else:
|
| 240 |
return f"""✅ URL AN TOÀN
|
| 241 |
+
🔍 Phân tích chi tiết:
|
| 242 |
• URL gốc: {report['url']}
|
| 243 |
• Domain: {report['domain']}
|
|
|
|
| 244 |
🤖 Phân tích AI:
|
| 245 |
- Kết quả: {report['ai_analysis']['label']}
|
| 246 |
- Độ tin cậy: {report['ai_analysis']['confidence']:.2f}%"""
|
requirements.txt
CHANGED
|
@@ -6,4 +6,6 @@ presidio-analyzer
|
|
| 6 |
detoxify
|
| 7 |
Pillow
|
| 8 |
google-genai
|
| 9 |
-
|
|
|
|
|
|
|
|
|
| 6 |
detoxify
|
| 7 |
Pillow
|
| 8 |
google-genai
|
| 9 |
+
openai-whisper
|
| 10 |
+
scipy
|
| 11 |
+
SpeechRecognition
|