Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,17 +1,16 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
import re
|
| 4 |
-
import numpy as np
|
| 5 |
from PIL import Image, ImageEnhance, ImageOps
|
| 6 |
from transformers import AutoModel, AutoTokenizer, AutoModelForSequenceClassification
|
| 7 |
import torchvision.transforms as T
|
| 8 |
|
| 9 |
-
# ====
|
| 10 |
phobert_path = "DuyKien016/phobert-scam-detector"
|
| 11 |
phobert_tokenizer = AutoTokenizer.from_pretrained(phobert_path, use_fast=False)
|
| 12 |
phobert_model = AutoModelForSequenceClassification.from_pretrained(phobert_path).eval().to("cuda" if torch.cuda.is_available() else "cpu")
|
| 13 |
|
| 14 |
-
# ====
|
| 15 |
vintern_model = AutoModel.from_pretrained(
|
| 16 |
"5CD-AI/Vintern-1B-v3_5",
|
| 17 |
trust_remote_code=True,
|
|
@@ -21,18 +20,16 @@ vintern_model = AutoModel.from_pretrained(
|
|
| 21 |
).eval()
|
| 22 |
vintern_tokenizer = AutoTokenizer.from_pretrained("5CD-AI/Vintern-1B-v3_5", trust_remote_code=True)
|
| 23 |
|
| 24 |
-
# ====
|
| 25 |
def predict_phobert(texts):
|
| 26 |
results = []
|
| 27 |
for text in texts:
|
| 28 |
encoded = phobert_tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=256)
|
| 29 |
encoded = {k: v.to(phobert_model.device) for k, v in encoded.items()}
|
| 30 |
-
|
| 31 |
with torch.no_grad():
|
| 32 |
logits = phobert_model(**encoded).logits
|
| 33 |
probs = torch.softmax(logits, dim=1).squeeze()
|
| 34 |
label = torch.argmax(probs).item()
|
| 35 |
-
|
| 36 |
results.append({
|
| 37 |
"text": text,
|
| 38 |
"prediction": "🛑 LỪA ĐẢO" if label == 1 else "✅ BÌNH THƯỜNG",
|
|
@@ -40,7 +37,7 @@ def predict_phobert(texts):
|
|
| 40 |
})
|
| 41 |
return results
|
| 42 |
|
| 43 |
-
# ====
|
| 44 |
def enhance_image_for_ocr(image):
|
| 45 |
enhancer = ImageEnhance.Contrast(image)
|
| 46 |
image = enhancer.enhance(1.8)
|
|
@@ -53,7 +50,6 @@ def ocr_with_vintern(img: Image.Image):
|
|
| 53 |
img = enhance_image_for_ocr(img)
|
| 54 |
img.thumbnail((448, 448), Image.Resampling.LANCZOS)
|
| 55 |
img = ImageOps.pad(img, (448, 448), color=(245, 245, 245))
|
| 56 |
-
|
| 57 |
transform = T.Compose([
|
| 58 |
T.ToTensor(),
|
| 59 |
T.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
|
|
@@ -88,7 +84,7 @@ Bắt đầu:"""
|
|
| 88 |
cleaned = [re.sub(r'^\d+[\.\)\-\s]+', '', re.sub(r"\s+", " ", m.strip())) for m in messages if m.strip()]
|
| 89 |
return cleaned or ["Không tìm thấy tin nhắn trong ảnh."]
|
| 90 |
|
| 91 |
-
# ====
|
| 92 |
def handle_input(image, text, history):
|
| 93 |
if image and text:
|
| 94 |
return history + [[text, "❗ Chỉ chọn một: ảnh hoặc văn bản!"]]
|
|
@@ -105,21 +101,16 @@ def handle_input(image, text, history):
|
|
| 105 |
else:
|
| 106 |
return history + [["", "❗ Bạn chưa gửi gì cả."]]
|
| 107 |
|
| 108 |
-
# ====
|
| 109 |
-
|
| 110 |
-
#
|
| 111 |
-
|
| 112 |
-
"""
|
| 113 |
-
|
| 114 |
-
with gr.Blocks(css=CUSTOM_CSS, theme=gr.themes.Base(primary_hue="blue")) as demo:
|
| 115 |
-
gr.Markdown("## 🔍 Phân loại tin nhắn lừa đảo bằng ảnh hoặc văn bản", elem_id="title")
|
| 116 |
-
chatbot = gr.Chatbot([], elem_id="chatbox").style(height=400)
|
| 117 |
|
| 118 |
with gr.Row():
|
| 119 |
with gr.Column(scale=1):
|
| 120 |
-
image = gr.Image(type="pil", label="📷 Ảnh (1 ảnh
|
| 121 |
with gr.Column(scale=4):
|
| 122 |
-
textbox = gr.Textbox(lines=2, placeholder="💬 Nhập
|
| 123 |
|
| 124 |
send_btn = gr.Button("Gửi")
|
| 125 |
|
|
@@ -127,6 +118,6 @@ with gr.Blocks(css=CUSTOM_CSS, theme=gr.themes.Base(primary_hue="blue")) as demo
|
|
| 127 |
return None, ""
|
| 128 |
|
| 129 |
send_btn.click(fn=handle_input, inputs=[image, textbox, chatbot], outputs=chatbot)
|
| 130 |
-
send_btn.click(fn=clear_inputs,
|
| 131 |
|
| 132 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
import re
|
|
|
|
| 4 |
from PIL import Image, ImageEnhance, ImageOps
|
| 5 |
from transformers import AutoModel, AutoTokenizer, AutoModelForSequenceClassification
|
| 6 |
import torchvision.transforms as T
|
| 7 |
|
| 8 |
+
# ==== PhoBERT ====
|
| 9 |
phobert_path = "DuyKien016/phobert-scam-detector"
|
| 10 |
phobert_tokenizer = AutoTokenizer.from_pretrained(phobert_path, use_fast=False)
|
| 11 |
phobert_model = AutoModelForSequenceClassification.from_pretrained(phobert_path).eval().to("cuda" if torch.cuda.is_available() else "cpu")
|
| 12 |
|
| 13 |
+
# ==== Vintern ====
|
| 14 |
vintern_model = AutoModel.from_pretrained(
|
| 15 |
"5CD-AI/Vintern-1B-v3_5",
|
| 16 |
trust_remote_code=True,
|
|
|
|
| 20 |
).eval()
|
| 21 |
vintern_tokenizer = AutoTokenizer.from_pretrained("5CD-AI/Vintern-1B-v3_5", trust_remote_code=True)
|
| 22 |
|
| 23 |
+
# ==== PhoBERT Predict ====
|
| 24 |
def predict_phobert(texts):
|
| 25 |
results = []
|
| 26 |
for text in texts:
|
| 27 |
encoded = phobert_tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=256)
|
| 28 |
encoded = {k: v.to(phobert_model.device) for k, v in encoded.items()}
|
|
|
|
| 29 |
with torch.no_grad():
|
| 30 |
logits = phobert_model(**encoded).logits
|
| 31 |
probs = torch.softmax(logits, dim=1).squeeze()
|
| 32 |
label = torch.argmax(probs).item()
|
|
|
|
| 33 |
results.append({
|
| 34 |
"text": text,
|
| 35 |
"prediction": "🛑 LỪA ĐẢO" if label == 1 else "✅ BÌNH THƯỜNG",
|
|
|
|
| 37 |
})
|
| 38 |
return results
|
| 39 |
|
| 40 |
+
# ==== Xử lý ảnh cho Vintern ====
|
| 41 |
def enhance_image_for_ocr(image):
|
| 42 |
enhancer = ImageEnhance.Contrast(image)
|
| 43 |
image = enhancer.enhance(1.8)
|
|
|
|
| 50 |
img = enhance_image_for_ocr(img)
|
| 51 |
img.thumbnail((448, 448), Image.Resampling.LANCZOS)
|
| 52 |
img = ImageOps.pad(img, (448, 448), color=(245, 245, 245))
|
|
|
|
| 53 |
transform = T.Compose([
|
| 54 |
T.ToTensor(),
|
| 55 |
T.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
|
|
|
|
| 84 |
cleaned = [re.sub(r'^\d+[\.\)\-\s]+', '', re.sub(r"\s+", " ", m.strip())) for m in messages if m.strip()]
|
| 85 |
return cleaned or ["Không tìm thấy tin nhắn trong ảnh."]
|
| 86 |
|
| 87 |
+
# ==== Giao tiếp chính ====
|
| 88 |
def handle_input(image, text, history):
|
| 89 |
if image and text:
|
| 90 |
return history + [[text, "❗ Chỉ chọn một: ảnh hoặc văn bản!"]]
|
|
|
|
| 101 |
else:
|
| 102 |
return history + [["", "❗ Bạn chưa gửi gì cả."]]
|
| 103 |
|
| 104 |
+
# ==== Giao diện ====
|
| 105 |
+
with gr.Blocks(theme=gr.themes.Base(primary_hue="blue")) as demo:
|
| 106 |
+
gr.Markdown("## 🔍 Phân loại Lừa đảo từ Ảnh hoặc Văn bản", elem_id="title")
|
| 107 |
+
chatbot = gr.Chatbot([], label="Chat kết quả", show_label=False, height=450)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
with gr.Row():
|
| 110 |
with gr.Column(scale=1):
|
| 111 |
+
image = gr.Image(type="pil", label="📷 Ảnh (chọn 1 ảnh)")
|
| 112 |
with gr.Column(scale=4):
|
| 113 |
+
textbox = gr.Textbox(lines=2, placeholder="💬 Nhập văn bản...", label="")
|
| 114 |
|
| 115 |
send_btn = gr.Button("Gửi")
|
| 116 |
|
|
|
|
| 118 |
return None, ""
|
| 119 |
|
| 120 |
send_btn.click(fn=handle_input, inputs=[image, textbox, chatbot], outputs=chatbot)
|
| 121 |
+
send_btn.click(fn=clear_inputs, outputs=[image, textbox])
|
| 122 |
|
| 123 |
demo.launch()
|