🚀 高棉语mT5文本摘要模型
本项目是一个针对高棉语文本摘要任务微调的mT5模型。它基于Google的mT5-small模型,并在高棉语文本及其对应摘要的数据集上进行了微调。通过使用Hugging Face的Trainer
API进行微调,该模型能够生成简洁且有意义的高棉语文本摘要。
🚀 快速开始
✨ 主要特性
- 基础模型:基于
google/mt5-small
。
- 微调任务:专注于高棉语文本摘要。
- 训练数据集:使用
kimleang123/khmer-text-dataset
进行训练。
- 框架:采用Hugging Face的
transformers
。
- 任务类型:序列到序列(Seq2Seq)任务。
- 输入:高棉语文本(文章、段落或文档)。
- 输出:高棉语摘要文本。
- 训练硬件:使用GPU(Tesla T4)进行训练。
- 评估指标:使用ROUGE分数进行评估。
📦 安装指南
1️⃣ 安装依赖
确保你已经安装了transformers
、torch
和datasets
:
pip install transformers torch datasets
2️⃣ 加载模型
加载并使用微调后的模型:
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
model_name = "songhieng/khmer-mt5-summarization"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
💻 使用示例
基础用法
def summarize_khmer(text, max_length=150):
input_text = f"summarize: {text}"
inputs = tokenizer(input_text, return_tensors="pt", truncation=True, max_length=512)
summary_ids = model.generate(**inputs, max_length=max_length, num_beams=5, length_penalty=2.0, early_stopping=True)
summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
return summary
khmer_text = "កម្ពុជាមានប្រជាជនប្រមាណ ១៦ លាននាក់ ហើយវាគឺជាប្រទេសនៅតំបន់អាស៊ីអាគ្នេយ៍។"
summary = summarize_khmer(khmer_text)
print("🔹 高棉语摘要:", summary)
高级用法
使用Hugging Face Pipeline进行更简单的操作:
from transformers import pipeline
summarizer = pipeline("summarization", model="songhieng/khmer-mt5-summarization")
khmer_text = "កម្ពុជាមានប្រជាជនប្រមាណ ១៦ លាននាក់ ហើយវាគឺជាប្រទេសនៅតំបន់អាស៊ីអាគ្នេយ៍។"
summary = summarizer(khmer_text, max_length=150, min_length=30, do_sample=False)
print("🔹 高棉语摘要:", summary[0]['summary_text'])
部署为API
使用FastAPI将模型部署为API:
from fastapi import FastAPI
app = FastAPI()
@app.post("/summarize/")
def summarize(text: str):
inputs = tokenizer(f"summarize: {text}", return_tensors="pt", truncation=True, max_length=512)
summary_ids = model.generate(**inputs, max_length=150, num_beams=5, length_penalty=2.0, early_stopping=True)
summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
return {"summary": summary}
🔧 技术细节
模型评估使用ROUGE分数,以衡量生成的摘要与真实摘要之间的相似度:
from datasets import load_metric
rouge = load_metric("rouge")
def compute_metrics(pred):
labels_ids = pred.label_ids
pred_ids = pred.predictions
decoded_preds = tokenizer.batch_decode(pred_ids, skip_special_tokens=True)
decoded_labels = tokenizer.batch_decode(labels_ids, skip_special_tokens=True)
return rouge.compute(predictions=decoded_preds, references=decoded_labels)
trainer.evaluate()
💾 模型保存与上传
微调完成后,可将模型上传到Hugging Face Hub:
model.push_to_hub("songhieng/khmer-mt5-summarization")
tokenizer.push_to_hub("songhieng/khmer-mt5-summarization")
后续下载模型:
model = AutoModelForSeq2SeqLM.from_pretrained("songhieng/khmer-mt5-summarization")
tokenizer = AutoTokenizer.from_pretrained("songhieng/khmer-mt5-summarization")
📚 详细文档
属性 |
详情 |
基础模型 |
google/mt5-small |
任务类型 |
文本摘要 |
语言 |
高棉语(ខ្មែរ) |
训练数据 |
kimleang123/khmer-text-dataset |
框架 |
Hugging Face Transformers |
评估指标 |
ROUGE分数 |
部署方式 |
Hugging Face模型中心、API(FastAPI)、Python代码 |
🤝 贡献说明
欢迎大家参与贡献!如果你发现可以改进的地方,随时提出问题或提交拉取请求。
📬 联系我们
如果你有任何疑问,可以通过Hugging Face讨论区联系我们,也可以在仓库中创建问题。
📌 为高棉语自然语言处理社区而建 🇰🇭 🚀
📄 许可证
本项目采用MIT许可证。