🚀 高棉語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許可證。