from __future__ import annotations import sys import tempfile from pathlib import Path import gradio as gr # Hugging Face Spaces installs requirements but doesn't install this repository # as a wheel. Keep the src-layout importable in both Spaces and local execution. sys.path.insert(0, str(Path(__file__).resolve().parent / "src")) from finreport_agent.pipeline import convert_report from finreport_agent.models import FundPortfolioReport def convert_for_ui(file_path: str, progress=gr.Progress()): if not file_path: raise gr.Error("Lütfen bir PDF veya rapor görüntüsü yükleyin.") progress(0.08, desc="Rapor inceleniyor") output_dir = Path(tempfile.mkdtemp(prefix="finreport_")) output_path = output_dir / f"{Path(file_path).stem}_validated.xlsx" try: progress(0.28, desc="Tablolar çıkarılıyor") result = convert_report(file_path, output_path) except Exception as exc: raise gr.Error(str(exc)) from exc progress(0.85, desc="Excel hazırlanıyor") validation = result.validation report = result.report status_icon = ( "❌" if validation.error_count else "⚠️" if validation.warning_count else "✅" ) if isinstance(report, FundPortfolioReport): report_details = f""" **{report.metadata.fund_name}** · {report.metadata.period} - {len(report.equities)} hisse pozisyonu - {len(report.repos)} repo pozisyonu - {len(report.derivatives)} türev pozisyonu - {len(report.cash_collaterals)} nakit teminat kalemi """ else: report_details = f""" **{report.document_title}** - {len(report.tables)} tablo · {sum(len(table.rows) for table in report.tables)} satır """ summary = f""" ### {status_icon} {validation.status} {report_details} - {validation.error_count} hata · {validation.warning_count} uyarı · {validation.info_count} bilgi Kontrol ve kaynak bilgileri Excel dosyasına eklendi. """ progress(1.0, desc="Tamamlandı") return summary, result.output_path THEME = gr.themes.Soft(primary_hue="blue") with gr.Blocks(title="KAP Finansal Rapor → Excel") as demo: gr.Markdown( """ # KAP Finansal Rapor → Excel KAP raporunuzu yükleyin, tabloları düzenli bir Excel dosyası olarak indirin. """ ) with gr.Row(): with gr.Column(scale=1): source = gr.File( label="KAP raporu", file_types=[".pdf", ".png", ".jpg", ".jpeg", ".tif", ".tiff"], type="filepath", ) convert_button = gr.Button("Excel Oluştur", variant="primary") gr.Markdown( """ PDF veya rapor görüntüsü yükleyebilirsiniz. Tablo ve sekmeler otomatik belirlenir. """ ) with gr.Column(scale=1): result_markdown = gr.Markdown("Rapor bekleniyor.") output_file = gr.File(label="Excel çıktısı", interactive=False) convert_button.click( fn=convert_for_ui, inputs=source, outputs=[result_markdown, output_file], api_name="convert_financial_report", ) if __name__ == "__main__": demo.queue(default_concurrency_limit=2).launch( theme=THEME, ssr_mode=False, max_file_size="50mb", )