🚀 DeBERTa 情感预测器
本项目提供了一个基于 DeBERTa 的模型,用于预测日语文本中的情感。借助预训练的 DeBERTa 模型,该 Python 包能够对日语文本进行情感推断。它提供了针对 8 种情感(喜悦、悲伤、期待、惊讶、愤怒、恐惧、厌恶、信任)的模型,可轻松获取每个文本的情感预测标签和肯定类别的置信度。
🚀 快速开始
安装
使用 pip 进行安装:
pip install deberta-emotion-predictor
使用示例
基础用法
from deberta_emotion_predictor import DeBERTaEmotionPredictor
predictor = DeBERTaEmotionPredictor()
result = predictor.predict_emotions("今日はとても嬉しい!")
predictor.show_emotions(result)
注:由于需要从 Hugging-face 下载 8 种 DeBERTa 模型,首次启动会比较耗时,后续运行速度会加快。
高级用法
还可以输入数据框进行预测:
import pandas as pd
from deberta_emotion_predictor import DeBERTaEmotionPredictor
predictor = DeBERTaEmotionPredictor()
sample_texts = [
"そうだ 京都、行こう。",
"がんばるひとの、がんばらない時間。",
"わたしらしくをあたらしく",
"ピースはここにある。",
"結婚しなくても幸せになれるこの時代に、私は、あなたと結婚したいのです。",
"これからの地球のために一肌、脱ぎました。",
"自分は、きっと想像以上だ。",
"ハローしあわせ。",
"日本を、1枚で。"
]
res_df = predictor.predict_emotions(sample_texts)
predictor.show_emotions(res_df)
运行该包需要安装 torch
、transformers
和 pandas
:
pip install torch
pip install transformers
pip install pandas
另外,如果要使用 GPU,需要安装 NVIDIA GPU 驱动等,具体请参考其他资料。
✨ 主要特性
- 8 种情感推断:使用针对每种情感微调的模型,对文本进行情感推断。
- 灵活的输入形式:支持单一文本、文本列表或 pandas Series 作为输入,并以 DataFrame 形式返回结果。
- 高效的推理:为了减少 GPU 内存使用,设计为仅在需要时将模型加载到 GPU 上。
📚 详细文档
文本传递方式(列表)
sample_texts = [
"そうだ 京都、行こう。",
"がんばるひとの、がんばらない時間。"
]
result_df = predictor.predict_emotions(sample_texts)
predictor.show_emotions(result_df)
单一文本情况
result_single = predictor.predict_emotions("新しい朝が来た。")
print(result_single)
输出的数据框
输出的数据框包含表示每种情感是否存在的 8 列,以及每种情感的概率值。
print(result_df)
🔧 技术细节
目录结构
deberta_emotion_predictor/
├── README.md # 本说明文件
├── deberta_emotion_predictor.py # DeBERTaEmotionPredictor 类的实现
│ └── tokenizer_DeBERTa_v3_large/ # 分词器
├── setup.py
├── pyproject.toml
├── README.md
├── LICENSE
└── usage.py
必要环境
- Python 3.6 及以上
- PyTorch
- transformers
- pandas
📄 许可证
本项目采用知识共享署名 - 相同方式共享 4.0 国际许可协议(CC BY - SA 4.0)。
版权所有 (c) 2025 Yoichi Takenaka
本作品根据知识共享署名 - 相同方式共享 4.0 国际许可协议进行许可。要查看此许可协议的副本,请访问 https://creativecommons.org/licenses/by-sa/4.0/
本项目基于以下内容:
- DeBERTa (https://huggingface.co/microsoft/deberta-v3-large),采用 MIT 许可协议。
- DeBERTa 日语模型 (https://huggingface.co/globis-university/deberta-v3-japanese-large),采用 CC BY - SA 4.0 许可协议。
任何修改或派生作品也必须根据相同的 CC BY - SA 4.0 许可协议进行分发。