Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
3 |
+
import torch
|
4 |
+
|
5 |
+
# 加载模型
|
6 |
+
model_name = "LilithHu/mbert-manipulative-detector"
|
7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
8 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
9 |
+
|
10 |
+
# 二分类标签(非操纵性是0,操纵性是1)
|
11 |
+
labels = ["Non-manipulative / 非操纵性", "Manipulative / 操纵性"]
|
12 |
+
|
13 |
+
def classify(text):
|
14 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
|
15 |
+
with torch.no_grad():
|
16 |
+
outputs = model(**inputs)
|
17 |
+
probs = torch.softmax(outputs.logits, dim=1)
|
18 |
+
pred = torch.argmax(probs, dim=1).item()
|
19 |
+
confidence = probs[0][pred].item()
|
20 |
+
return f"🧠 预测 / Prediction: {labels[pred]}\n📊 置信度 / Confidence: {confidence:.2%}"
|
21 |
+
|
22 |
+
# Gradio 界面
|
23 |
+
interface = gr.Interface(
|
24 |
+
fn=classify,
|
25 |
+
inputs=gr.Textbox(lines=4, placeholder="Enter text in English or Chinese... / 输入中文或英文句子"),
|
26 |
+
outputs="text",
|
27 |
+
title="🔍 Manipulative Language Detector / 操纵性语言识别器",
|
28 |
+
description="🧪 输入英文或中文句子,系统将判断其是否包含操纵性语言。\nEnter a sentence in English or Chinese to detect if it's manipulative.",
|
29 |
+
examples=[
|
30 |
+
["If you really cared, you'd do what I say."],
|
31 |
+
["你不爱我就证明给我看!"],
|
32 |
+
["今天的天气真不错。"]
|
33 |
+
]
|
34 |
+
)
|
35 |
+
|
36 |
+
interface.launch()
|