| import gradio as gr | |
| from pathlib import Path | |
| import tempfile,datetime, json | |
| from core import generate_report | |
| import shutil | |
| TITLE = "IR/ESG Report Generator(Hugging Face Space)" | |
| DESC = "" | |
| CSV/YAMLをアップロードしてIR/ESGレポート(HTML/PDF/DOCX)を生成します。 | |
| "" | |
| def run(company_yaml,financials_csv,esg_csv,use_llm,lang): | |
| if company_yamal is None or financials_csv is None or esg_csv is None: | |
| return "すべてのファイルをアップロードしてください。"None,None,None,None | |
| with tempfile.TemporaryDirectory() as td: | |
| cpath = Path(td)/"company_yaml";cpath.write_bytes(company_yaml.read()) | |
| fpath = Path(td)/"financials_csv";fpath.write_bytes(financials_csv.read()) | |
| epath = Path(td)/"esg_csv";epath.write_bytes(esg_csv.read()) | |
| outdir = Path(td)/"out" | |
| outdir.mkdir(parents=True, exist_ok=True) | |
| llm=None | |
| if use_llm: | |
| try: | |
| from llm import OpenAILLM | |
| llm = OpenAILLM() | |
| except Exception as e: | |
| return f"LLM初期化エラー: {e}",None,None,None | |
| html,pdf,docx,meta_json = generate_report( | |
| company_yamal=str(cpath), | |
| financials_csv=str(fpath), | |
| esg_csv=str(epath), | |
| templates_dir="templates", | |
| template_name="report.html.j2", | |
| out_html=str(outdir/"report.html") | |
| out_pdf=str(outdir/"report.html") | |
| lang=lang, | |
| llm=llm | |
| ) | |
| repo_tmp=Path("./tmp") | |
| repo_tmp.mkdir(exit_ok=True) | |
| ts = detatime.datetime.now().strftime("%Ym%d-%H%M%S") | |
| html_out=repo_tmp/f"report-{ts}.html" | |
| pdf_out=repo_tmp/f"report-{ts}.pdf" | |
| docx_out=repo_tmp/f"report-{ts}.docx" | |
| meta_out=repo_tmp/f"report-{ts}.json" | |
| shutil.copy(html,html_out) | |
| shutil.copy(pdf,pdf_out) | |
| shutil.copy(docx,docx_out) | |
| Path(meta_out).write_text(json.dumps(meta_json,ensure_ascii=False,indent=2),encoding="utf-8") | |
| return"生成が完了しました。",str(html_out),str(pdf_out),str(docx_out),str(meta_out) | |
| with gr.Blocks(title=TITLE) as demo: | |
| gr.Markdown(f"#{TITLE}\n{DESC}") | |
| with gr.Row(): | |
| company_yaml=gr.File(label="company.yaml(会社情報・年度等)",file_types=[".yaml",".yml"]) | |
| financials_csv=gr.File(label="financials.csv(財務KPI)",file_types=[".csv"]) | |
| esg_csv=gr.File(label="esg_metrics.csv(ESG指標)",file_types=[".csv"]) | |
| with gr.Row(): | |
| use_llm=gr.Chheckbox(label="LLMで要約/翻訳を行う(OPENAI_API_KE2 必須)",value=True) | |
| lang = gr.Dropdown(choices=["ja","en","zh","ko","de","fr"],value="ja",label="出力言語") | |
| run_btn=gr.Button("レポート生成") | |
| status =gr.Textbox(label="ステータス",interactive=False) | |
| html_file=gr.File(label="HTMLダウンロード") | |
| pdf_file=gr.File(label="PDFダウンロード") | |
| docx_file=gr.File(label="DOCXダウンロード") | |
| meta_file=gr.File(label="メタ情報(JSON)") | |
| run_btn.click( | |
| fu=run | |
| inputs=[company_yamal,finanxials_csv,esg_csv,use_llm,lang], | |
| outputs=[status,html_file,pdf_file,docx_file,meta_file] | |
| ) | |
| if__name__=="__main__": | |
| demo.launch() |