Spaces:
Sleeping
Sleeping
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] | |
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 | |
#谁大选谁 | |
#pred = torch.argmax(probs).item() | |
#confidence = min(probs[pred].item(), 0.95) # 限制置信度最大为95% | |
# Gradio 界面 | |
interface = gr.Interface( | |
fn=classify, | |
inputs=gr.Textbox( | |
lines=4, | |
placeholder="Enter text in English or Chinese... ", | |
label="📝 Input Text" | |
), | |
outputs=gr.Markdown(label="📊 Prediction"), | |
title="🔍 Manipulative Language Detector", | |
description=""" | |
🧪 Enter a sentence in English or Chinese to detect if it's manipulative. | |
📌 **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 | |
⚠️ **About Examples** | |
The examples provided below are those **cited in the paper**, including implicit moral coercion, polite masking and false positives. | |
🌐 **Built with Gradio and hosted on HuggingFace Spaces** | |
""", | |
examples=[ | |
["A good kid would never act like this"], | |
["hi, your mother is tired, can you just do whatever she says"], | |
["I feel so sad when you don’t listen to me."], | |
["If you are willing to help, it doesn’t matter if you are not... let it go, i know my position"], | |
["If you take that job, don’t call me mom again."], | |
["I’ve sacrificed my whole life for you, how could you treat me this way "] | |
], | |
theme="default", | |
allow_flagging="never" | |
) | |
interface.launch() | |