File size: 3,221 Bytes
22db0eb
 
 
 
1e3779f
22db0eb
 
 
 
1e3779f
 
 
 
 
 
 
 
22db0eb
 
1e3779f
22db0eb
1e3779f
22db0eb
3662b93
2cb5c95
e25e3be
 
 
 
 
 
 
 
 
2cb5c95
ac74601
3662b93
fde1d98
e25e3be
 
 
22db0eb
 
 
 
3662b93
 
9c7d77b
3662b93
 
2c6452e
9c7d77b
6d27a75
 
 
3662b93
 
 
6d27a75
3662b93
3143491
d57172d
3662b93
 
ff91fbe
3662b93
8055a6c
d57172d
76e8a30
8055a6c
 
3662b93
6d27a75
fb2aa26
93db1dd
fb2aa26
f326936
93db1dd
f326936
6d27a75
3662b93
 
22db0eb
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
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  

🌐 **Built with Gradio and hosted on HuggingFace Spaces**

⚠️ **About Examples / 关于例子**:
The examples provided below are those cited in the paper, including implicit moral coercion, polite masking and false positives.

""",
    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()