|
import gradio as gr |
|
from transformers import AutoTokenizer, AutoModelForSequenceClassification |
|
import torch |
|
|
|
|
|
model_name = "LilithHu/mbert-manipulative-detector" |
|
tokenizer = AutoTokenizer.from_pretrained(model_name) |
|
model = AutoModelForSequenceClassification.from_pretrained(model_name) |
|
|
|
|
|
model.eval() |
|
|
|
|
|
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") |
|
model.to(device) |
|
|
|
|
|
labels = ["Non-manipulative / 非操纵性", "Manipulative / 操纵性"] |
|
|
|
|
|
def classify(text): |
|
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True).to(device) |
|
with torch.no_grad(): |
|
outputs = model(**inputs) |
|
probs = torch.softmax(outputs.logits, dim=1)[0] |
|
|
|
threshold = 0.7 |
|
if probs[1].item() > threshold: |
|
pred = 1 |
|
else: |
|
pred = 0 |
|
|
|
confidence = min(probs[pred].item(), 0.95) |
|
|
|
percent = round(confidence * 100, 2) |
|
result = f"Prediction / 预测:\n{labels[pred]}\n" |
|
return result |
|
|
|
|
|
|
|
|
|
|
|
|
|
interface = gr.Interface( |
|
fn=classify, |
|
inputs=gr.Textbox( |
|
lines=4, |
|
placeholder="Enter text in English or Chinese... / 输入中文或英文句子", |
|
label="📝 Input Text / 输入文本" |
|
), |
|
outputs=gr.Textbox(label="📊 Prediction / 预测结果"), |
|
title="🔍 Manipulative Language Detector / 操纵性语言识别器", |
|
description="🧪 输入英文或中文句子,系统将判断其是否包含操纵性语言。\nEnter a sentence in English or Chinese to detect if it's manipulative.", |
|
examples=[ |
|
["If you really cared, you'd do what I say."], |
|
["你不爱我就证明给我看!"], |
|
["今天的天气真不错。"] |
|
] |
|
) |
|
|
|
interface.launch() |
|
--- |
|
|
|
📌 **Disclaimer / 免责声明:** |
|
This system is for **research and educational purposes only**. |
|
It **does not guarantee accuracy** and **should not be used as legal or clinical evidence**. |
|
本工具仅用于**学术研究与教学演示**,不构成法律、医疗或其他正式用途的依据。 |
|
|
|
|
|
--- |
|
|
|
🤖 **Model Info**: |
|
- Model: `LilithHu/mbert-manipulative-detector` |
|
- Base: `mDeBERTa-v3` multilingual pre-trained model |
|
- Fine-tuned using HuggingFace Transformers on 10,000 labeled Chinese data |
|
|
|
🌐 Built with Gradio and hosted on HuggingFace Spaces. |
|
""", |
|
theme="default", |
|
allow_flagging="never" |
|
) |
|
|
|
|
|
interface.launch() |
|
|