Hugging Face GUI演示:https://huggingface.co/spaces/WJL110/Sentiment_Demo
Github:https://github.com/wjl110/Sentiment_Demo
Hugging Face模型卡:https://huggingface.co/spaces/WJL110/emotion-classifier
gradio
transformers
torchapp.py
https://drive.google.com/file/d/17JIWMDXvVC-aBmpXIA5g0QkjG3afyl9y/view?usp=sharing
这是一个基于 BERT 的中文情感分类模型,可以将文本分类为三种情感:快乐、愤怒、悲伤。
Emo.py: 最终版本情感分类模型,包含完整的训练和评估功能test_emotion.py: 模型测试脚本emotion_data.csv: 情感数据集requirements.txt: 项目依赖config.json: 配置文件.env: 环境变量文件(包含敏感信息,不提交到仓库)
- 克隆仓库:
git clone https://github.com/WJL110/Sentiment_Demo.git
cd Sentiment_Demo- 安装依赖:
pip install -r requirements.txt- 训练模型:
python Emo.py- 测试模型:
python test_emotion.py- 上传模型:
python upload_model.pyfrom transformers import pipeline
classifier = pipeline("text-classification", model="WJL110/chinese-emotion-classifier")
result = classifier("今天真是太开心了!")
print(result)- 基础模型:chinese-bert-wwm-ext
- 训练数据:约100条中文情感数据
- 情感类别:
- 快乐
- 愤怒
- 悲伤
- 特点:
- 支持数据增强
- 包含评估指标(准确率、F1分数等)
- 自动保存最佳模型
- Accuracy: 模型准确率
- F1 Score: F1分数
- Precision: 精确率
- Recall: 召回率
- Python 3.12
- PyTorch
- Transformers
- Hugging Face
from transformers import pipeline
# 创建分类器
classifier = pipeline("text-classification", model="WJL110/emotion-classifier")
# 标签映射
label_map = {
"LABEL_0": "快乐",
"LABEL_1": "愤怒",
"LABEL_2": "悲伤"
}
# 测试文本
test_texts = [
"今天真是太开心了!",
"这件事让我很生气。",
"听到这个消息很难过。"
]
print("=== 情感分析测试 ===")
for text in test_texts:
result = classifier(text)[0] # 获取第一个(也是唯一的)结果
emotion = label_map[result['label']]
confidence = result['score']
print(f"\n输入文本: {text}")
print(f"预测情感: {emotion}")
print(f"置信度: {confidence:.2f}")本作品采用 知识共享 署名-非商业性使用-禁止演绎 4.0 国际许可协议 授权。
- 您必须署名:明确标注原作者姓名及作品来源链接
- 禁止商用:不得用于任何商业目的(包括广告、付费服务等)
- 禁止修改:不得以任何形式改编、转换或二次创作
- 2024-01: 初始版本发布
- 2024-01: 添加数据增强功能
- 2024-01: 优化模型性能
- 2024-01: 集成 Hugging Face Hub
Copyright © [2025] [王健霖]。All rights reserved. This code is intended solely for the purpose of patent application and is not licensed for general use or distribution.




