File size: 3,627 Bytes
b190df6
eb36871
ffe88ec
6f39ab6
eb36871
2cdac3e
ffe88ec
 
 
 
b190df6
6f39ab6
 
 
 
eb36871
 
 
 
 
 
 
2cdac3e
eb36871
2cdac3e
ffe88ec
6f39ab6
2cdac3e
ffe88ec
 
 
eb36871
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ffe88ec
 
 
 
 
 
 
eb36871
ffe88ec
eb36871
 
 
 
 
 
 
 
 
ffe88ec
 
 
 
 
 
 
 
6f39ab6
 
ffe88ec
 
 
 
6f39ab6
 
 
 
ffe88ec
 
 
 
 
 
 
 
 
 
eb36871
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
87
88
89
90
91
92
93
94
95
96
97
import streamlit as st
from transformers import pipeline, AutoTokenizer
import json
from onnxruntime import InferenceSession
import numpy as np

# 设置页面配置
st.set_page_config(page_title="中文垃圾信息分类器", page_icon="🚫", layout="wide")

# 加载中文垃圾信息分类器
@st.cache_resource
def load_classifiers():
    hf_classifier = pipeline("text-classification", model="app-x/chinese_spam_classifier")
    onnx_session = InferenceSession("app-x/chinese_spam_classifier_onnx/model_optimized.onnx")
    tokenizer = AutoTokenizer.from_pretrained("app-x/chinese_spam_classifier_onnx")
    
    # 加载配置文件
    with open("app-x/chinese_spam_classifier_onnx/config.json", "r") as f:
        config = json.load(f)
    
    id2label = config["id2label"]
    return hf_classifier, onnx_session, tokenizer, id2label

hf_classifier, onnx_session, tokenizer, id2label = load_classifiers()

st.title("🚫 中文垃圾信息分类器")
st.write("使用两个模型进行中文文本的垃圾信息分类。")

# 创建两列布局
col1, col2 = st.columns([2, 1])

def classify_text(text):
    # HuggingFace模型分类
    hf_result = hf_classifier(text)[0]
    hf_label = id2label[str(int(hf_result["label"].split("_")[-1]))]
    hf_confidence = hf_result["score"]
    
    # ONNX模型分类
    inputs = tokenizer(text, return_tensors="np", padding=True, truncation=True)
    onnx_result = onnx_session.run(None, dict(inputs))
    onnx_probs = onnx_result[0][0]
    onnx_label = id2label[str(np.argmax(onnx_probs))]
    onnx_confidence = np.max(onnx_probs)
    
    return {
        "hf": {"label": hf_label, "confidence": hf_confidence},
        "onnx": {"label": onnx_label, "confidence": float(onnx_confidence)}
    }

with col1:
    # 创建文本输入框
    text_input = st.text_area("请输入中文文本:", height=200)
    
    if st.button("分类", key="classify_button"):
        if text_input:
            with st.spinner("正在分析..."):
                results = classify_text(text_input)
                
                for model, result in results.items():
                    st.subheader(f"{model.upper()} 模型分类结果:")
                    label = "垃圾信息" if result["label"] == "spam" else "正常信息"
                    if label == "垃圾信息":
                        st.error(f"⚠️ {label}")
                    else:
                        st.success(f"✅ {label}")
                    st.write(f"概率: {result['confidence']:.2f}")
                    st.progress(result['confidence'])
        else:
            st.warning("请输入文本后再进行分类。")

with col2:
    st.subheader("使用说明")
    st.write("""
    1. 在左侧文本框中输入您想要分类的中文文本。
    2. 点击"分类"按钮。
    3. 系统将使用两个模型分析文本并显示结果。
    4. 结果包括两个模型的分类(垃圾信息或正常信息)、概率和JSON格式的详细输出。
    """)
    
    st.subheader("关于模型")
    st.write("""
    本分类器使用了两个模型:
    1. app-x/chinese_spam_classifier (HuggingFace模型)
    2. app-x/chinese_spam_classifier_onnx (ONNX模型)
    这两个模型都基于大规模中文数据集训练,能够有效识别各种类型的垃圾信息。
    """)
    
    st.subheader("免责声明")
    st.info("""
    此分类器仅作为辅助工具,不应完全依赖其结果。
    请始终保持警惕,谨慎处理可疑信息。
    """)

# 添加页脚
st.markdown("---")
st.markdown("由 Streamlit 和 Hugging Face 提供支持 | 作者:[app-x]")