File size: 1,435 Bytes
2c18765
 
 
 
321048b
 
 
 
2c18765
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import gradio as gr
from summarize import summarize_text
from pdf2text import process_pdf
from aggregate import aggregate_summaries
import logging
import os
from pathlib import Path

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

examples_dir = Path("examples")
name_to_path = {f.name: f for f in examples_dir.glob("*.txt") if f.is_file()}
logger.info(f"Loaded {len(name_to_path)} examples")
default_example = next(iter(name_to_path), None)

with gr.Blocks() as demo:
    gr.Markdown("## 文件摘要工具 - 輸入純文字或上傳 PDF")
    with gr.Row():
        input_textbox = gr.Textbox(label="輸入文件內容", lines=20)
        output_textbox = gr.Textbox(label="摘要結果", lines=10)
    summarize_button = gr.Button("產生摘要")
    upload_pdf = gr.File(label="或上傳 PDF 檔案", type="file")
    examples = gr.Examples(
        examples=[[name] for name in name_to_path.keys()],
        label="範例資料",
        inputs=[input_textbox],
    )

    def run_summarization(text, pdf_file):
        if pdf_file is not None:
            text = process_pdf(pdf_file.name)
        if text.strip() == "":
            return "請輸入文字或上傳有效的 PDF。"
        summary = summarize_text(text)
        return summary

    summarize_button.click(
        run_summarization,
        inputs=[input_textbox, upload_pdf],
        outputs=[output_textbox],
    )

demo.launch()