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()