Spaces:
Sleeping
Sleeping
File size: 763 Bytes
50a43ab |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
from transformers import pipeline
# 使用 Hugging Face 支援中文的 Pegasus 模型
summarizer = pipeline(
"summarization",
model="IDEA-CCNL/Randeng-Pegasus-523M-Summary-Chinese"
)
def 摘要(text, max_length=128, min_length=20):
if not text.strip():
return "⚠️ 請輸入要摘要的內容"
# 依據模型最大長度做裁剪
if len(text) > 1500:
text = text[:1500]
result = summarizer(text, max_length=max_length, min_length=min_length, do_sample=False)
# 處理不同模型回傳格式
if isinstance(result, list) and "summary_text" in result[0]:
return result[0]["summary_text"]
elif isinstance(result, str):
return result
else:
return str(result)
|