File size: 669 Bytes
cd3e0b8
50a43ab
 
cd3e0b8
 
50a43ab
cd3e0b8
3f09f55
cd3e0b8
3f09f55
cd3e0b8
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# textsumm.py
from transformers import pipeline

# 使用 pegasus 中文摘要模型
summarizer = pipeline("summarization", model="IDEA-CCNL/Randeng-Pegasus-523M-Summary-Chinese")

def 摘要(text):
    """

    傳入中文長文本,回傳中文摘要

    """
    # Pegasus 最佳 max_length < 256,如需長摘要可微調
    try:
        result = summarizer(text, max_length=128, min_length=30, do_sample=False)
        if isinstance(result, list) and len(result) > 0:
            return result[0]['summary_text']
        else:
            return "❌ 無法產生摘要"
    except Exception as e:
        return f"❌ 摘要失敗: {str(e)}"