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) # 二分类标签(非操纵性是0,操纵性是1) labels = ["Non-manipulative / 非操纵性", "Manipulative / 操纵性"] def classify(text): inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True) with torch.no_grad(): outputs = model(**inputs) probs = torch.softmax(outputs.logits, dim=1) pred = torch.argmax(probs, dim=1).item() confidence = probs[0][pred].item() return f"🧠 预测 / Prediction: {labels[pred]}\n📊 置信度 / Confidence: {confidence:.2%}" # Gradio 界面 interface = gr.Interface( fn=classify, inputs=gr.Textbox(lines=4, placeholder="Enter text in English or Chinese... / 输入中文或英文句子"), outputs="text", 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()