Spaces:
Sleeping
Sleeping
| import os | |
| import io | |
| import time | |
| import gradio as gr | |
| from modules.text_processing import process_text | |
| from modules.pptx_builder import build_presentation | |
| from modules.utils import safe_hex_to_rgb, ensure_tmpdir | |
| APP_NAME = "Auto-PPT Generator" | |
| def generate_pptx(long_text: str, | |
| title: str, | |
| theme_hex: str, | |
| add_summary: bool, | |
| add_charts: bool, | |
| use_inference_api: bool, | |
| summarize_model: str, | |
| generator_model: str, | |
| max_summary_words: int,): | |
| if not long_text or not long_text.strip(): | |
| raise gr.Error("入力テキストが空です。長文を貼り付けてください。") | |
| theme_rab = safe_hex_to_rgb(theme_hex or "#3B82F6") | |
| # Read logo (optional) | |
| logo_bytes = None | |
| if logo_file is not None: | |
| logo_bytes = logo_file.read() | |
| # Step 1-3: NLP pipeline(summary, sections, bullets, tables , chart data) | |
| results = process_text( | |
| text=long_text, | |
| use_inference_api=use_inference_api, | |
| summarize_model=summarize_model, | |
| generator_model=generator_model, | |
| want_summary=add_summary, | |
| want_tables=add_tables, | |
| want_charts=add_charts, | |
| max_summary_words=max_summary_words, | |
| ) | |
| # Step 4: Build PPTX | |
| ensure_tmpdir() | |
| timestamp = time.strftime("%Y%m%d-%H%M%S") | |
| out_path = f"/tmp/auto_ppt_{timestamp}.pptx" | |
| build_presentation( | |
| output_path=out_path, | |
| title=title or "Auto-PPT" | |
| theme_rgb=theme_rab, | |
| logo_bytes=logo_bytes, | |
| executive_summary=result.get("summary"), | |
| sections=result.get("sections", []), | |
| bullets_by_section=result.get("bullets", {}), | |
| tables=result.get("tables", []), | |
| charts=result.get("charts", []), | |
| ) | |
| # Return file path for download | |
| return out_path | |
| def ui(): | |
| with gr.Blocks(title=APP_NAME) as demo: | |
| gr.Markdown(f"# {APP_NAME}\n長文→要約→セクション分割→箇条書き/表/図→**PPTX出力**まで自動化") | |
| with gr.Row(): | |
| with gr.Column(scale=2): | |
| long_text = gr.Textbox(label="長文テキスト(貼り付け)", lines=20, placeholder="ここに長文テキストを貼り付けてください...") | |
| title = gr.Textbox(label="タイトル", value="自動生成スライド") | |
| theme_hex = gr.ColorPicker(label="テーマカラー", value="#3B82F6") | |
| logo = gr.File(label="ロゴ画像(任意)") | |
| with gr.Row(): | |
| add_summary = gr.Checkbox(label="要約スライドを追加", value=True) | |
| add_tables = gr.Checkbox(label="表を検出して追加", value=True) | |
| add_charts = gr.Checkbox(label="チャートを生成して追加", value=True) | |
| with gr.Column(scale=1): | |
| use_inference_api = gr.Checkbox(label="Hugging Face Inference APIを使用", value=False) | |
| summarize_api = gr.Textbox(label="要約モデル名(local or API)", value="sshleifer/distilbart-cnn-12-6") | |
| generator_model = gr.Textbox(label="生成モデル(API推奨,任意)", value="") | |
| max_summary_words = gr.Slider(label="要約の最大単語数", 50, 600, value=200, step=10) | |
| generate = gr.Button("PPTXを生成", variant="primary") | |
| output_file = gr.File(label="ダウンロード") | |
| generate.click( | |
| fn=generate_pptx, | |
| inputs=[ | |
| long_text, title, theme_hex, add_summary, add_charts,add_tables, | |
| use_inference_api, summarize_api, generator_model, max_summary_words, summarizer_model, | |
| ], | |
| outputs=output_file | |
| ) | |
| gr.Markdown(""" | |
| **Tips** | |
| - 日本語要約には`sonoisa/t5-base-japanese`を推奨('text2text-generation'). | |
| - Inference API を使う場合は、 Spaceの Secret に `HF_TOKEN` を設定してください。 | |
| - チャートは'Label: 123' 形式の行を自動検出して棒グラフを作成します。 | |
| """) | |
| return demo | |
| if __name__ == "__main__": | |
| app = ui() | |
| app.launch() |