LilithHu's picture
Update app.py
3662b93 verified
raw
history blame
2.42 kB
import gradio as gr
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch
# 加载模型和 tokenizer
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] # 取第一个样本的概率向量
probs = torch.clamp(probs, max=0.95) # 限制最大置信度为 95%
result = "🧠 预测 / Prediction:\n"
for i, label in enumerate(labels):
percent = round(probs[i].item() * 100, 2)
result += f"{label}: {percent}%\n"
return result
# Gradio 界面
interface = gr.Interface(
fn=classify,
inputs=gr.Textbox(
lines=4,
placeholder="Enter a sentence in English or Chinese... / 输入英文或中文句子",
label="📝 Input Text / 输入文本"
),
outputs=gr.Textbox(label="📊 Prediction / 预测结果"),
title="🧠 Manipulative Language Detector / 操纵性语言识别器",
description="""
🔍 This tool detects **emotionally manipulative language** in English or Chinese digital communication.
🧪 The model was fine-tuned on a manually annotated dataset of 10,000 Chinese messages, categorized into four manipulation types.
---
📌 **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 labeled Chinese data
🌐 Built with Gradio and hosted on HuggingFace Spaces.
""",
theme="default",
allow_flagging="never"
)
interface.launch()