Create services/pipeline.py
Browse files- app/services/pipeline.py +60 -0
app/services/pipeline.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app/services/pipeline.py
|
| 2 |
+
from __future__ import annotations
|
| 3 |
+
from typing import Dict, Any, Optional
|
| 4 |
+
import pandas as pd
|
| 5 |
+
from app.parsers.pdf import extract_pdf_chunks
|
| 6 |
+
from app.parsers.finstat import parse_financials
|
| 7 |
+
from app.derive.financials import build_ratios_table, build_kpis_table
|
| 8 |
+
from app.scoring import (
|
| 9 |
+
get_external_template_df, score_external_from_df,
|
| 10 |
+
ai_score_company, score_credit, score_lending, score_investment,
|
| 11 |
+
hybrid_score,
|
| 12 |
+
)
|
| 13 |
+
from app.llm.client import extract_structured_financials
|
| 14 |
+
|
| 15 |
+
def run_full_pipeline(
|
| 16 |
+
pdf_path: str,
|
| 17 |
+
unit_hint: Optional[str] = None,
|
| 18 |
+
company_name: str = "",
|
| 19 |
+
industry: str = "",
|
| 20 |
+
products: str = "",
|
| 21 |
+
) -> Dict[str, Any]:
|
| 22 |
+
# 1) 抽出
|
| 23 |
+
chunks = extract_pdf_chunks(pdf_path)
|
| 24 |
+
|
| 25 |
+
# 2) 財務
|
| 26 |
+
fin = parse_financials(chunks, unit_hint=unit_hint)
|
| 27 |
+
# LLM補完(主要科目がほとんど欠落時)
|
| 28 |
+
if fin.empty or fin.shape[0] < 4:
|
| 29 |
+
extra = extract_structured_financials(chunks.get("text",""))
|
| 30 |
+
if extra:
|
| 31 |
+
df = pd.DataFrame({"科目": list(extra.keys()), "値": list(extra.values())}).set_index("科目")
|
| 32 |
+
df.attrs["unit"] = "円"
|
| 33 |
+
fin = df
|
| 34 |
+
|
| 35 |
+
ratios = build_ratios_table(fin)
|
| 36 |
+
kpis = build_kpis_table(fin)
|
| 37 |
+
|
| 38 |
+
# 3) スコア
|
| 39 |
+
ext_tmpl = get_external_template_df()
|
| 40 |
+
external = score_external_from_df(ext_tmpl) # 今はテンプレに基づく(将来マッピング可)
|
| 41 |
+
|
| 42 |
+
ai = ai_score_company({
|
| 43 |
+
"industry": industry, "products": products,
|
| 44 |
+
"size_hint": float(fin.loc["売上高","値"]) if "売上高" in fin.index else 0.0,
|
| 45 |
+
"employees": float(fin.loc["従業員数","値"]) if "従業員数" in fin.index else 0.0,
|
| 46 |
+
"profitability": float(ratios.loc["売上高営業利益率","値"]) if "売上高営業利益率" in ratios.index and pd.notna(ratios.loc["売上高営業利益率","値"]) else None,
|
| 47 |
+
})
|
| 48 |
+
|
| 49 |
+
credit = score_credit(fin, ratios, kpis)
|
| 50 |
+
lending = score_lending(fin, ratios, kpis, credit)
|
| 51 |
+
invest = score_investment(fin, ratios, kpis)
|
| 52 |
+
hybrid = hybrid_score(external, ai, w_pdf=0.45, w_ai=0.55)
|
| 53 |
+
|
| 54 |
+
return {
|
| 55 |
+
"financials": fin, "ratios": ratios, "kpis": kpis,
|
| 56 |
+
"scores": {
|
| 57 |
+
"external": external, "ai": ai, "credit": credit,
|
| 58 |
+
"lending": lending, "investment": invest, "hybrid": hybrid
|
| 59 |
+
}
|
| 60 |
+
}
|