Downloads · 30 days
17
13% of all-time downloads
titicacine/chinese-text-rating-model
chinese-text-rating-model is a text classification model from titicacine. Use it when you need a label for a piece of text. The card lists the license as mit.
这是一个基于BERT的文本评分预测模型,可以根据输入的文本内容预测1-10分的评分。
Downloads · 30 days
17
13% of all-time downloads
All-time downloads
132
Public
Repo size
819 MB
Likes
0
Public
Click a slice to open those files.
.bin410 MB · 100%
From the Hugging Face model README
这是一个基于BERT的文本评分预测模型,可以根据输入的文本内容预测1-10分的评分。
merged_models/
├── config.json # BERT模型配置文件
├── merge_models.py # 模型合并脚本
├── predict.py # 预测功能实现
├── pytorch_model.bin # 模型权重文件
├── tokenizer_config.json # 分词器配置
└── vocab.txt # 词表文件
pip install torch transformers numpy scikit-learn
首先确保您已经下载了完整的模型文件夹,包括所有必要的配置文件和模型权重。
在您的Python代码中导入必要的库并加载模型:
import torch
from transformers import BertTokenizer
from merge_models import SimpleRatingPredictor
# 设置设备
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
# 加载模型和分词器
model_path = './merged_models' # 指向模型文件夹的路径
tokenizer = BertTokenizer.from_pretrained(model_path)
model = SimpleRatingPredictor(model_path)
# 加载模型权重
state_dict = torch.load(f'{model_path}/pytorch_model.bin', map_location=device)
model.load_state_dict(state_dict)
model.to(device)
from predict import predict_rating
# 示例文本
text = "这是一个需要评分的文本"
# 进行预测
rating = predict_rating(text, model, tokenizer, device)
print(f"预测评分: {rating:.1f}")
模型使用以下指标进行评估:
如果需要对多个文本进行批量预测,可以使用以下代码:
from predict import load_test_data, evaluate_model
# 加载测试数据
test_file = 'test.jsonl'
texts, true_points = load_test_data(test_file)
# 进行预测
predictions = []
for i, text in enumerate(texts, 1):
rating = predict_rating(text, model, tokenizer, device)
predictions.append(rating)
if i % 10 == 0:
print(f"已完成 {i}/{len(texts)} 条预测")
# 评估模型性能
metrics = evaluate_model(predictions, true_points)
print("\n模型评估结果:")
for metric_name, value in metrics.items():
print(f"{metric_name}: {value:.4f}")
该模型基于BERT-base-Chinese预训练模型,添加了一个回归头用于评分预测。模型结构包括: