Spaces:
Sleeping
Sleeping
Create main.py
Browse files- irpr/main.py +52 -0
irpr/main.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException, Response
|
| 2 |
+
from irpr.models import IngestRequest, GenerateRequest
|
| 3 |
+
from rag.ingest import ingest_edinet_for_company, download_edinet_pdf
|
| 4 |
+
from generators.summary import make_summary
|
| 5 |
+
from generators.qa import make_qa
|
| 6 |
+
from export.ppt import build_deck, save_pptx
|
| 7 |
+
from export.qa_csv import save_qa_csv
|
| 8 |
+
|
| 9 |
+
app = FastAPI(title="IR/PR Co-Pilot Pro", version="0.1.0")
|
| 10 |
+
|
| 11 |
+
@app.get("/")
|
| 12 |
+
def root():
|
| 13 |
+
return {"ok": True, "service": "IR/PR Co-Pilot Pro"}
|
| 14 |
+
|
| 15 |
+
@app.post("/ingest/edinet")
|
| 16 |
+
def ingest_edinet(req: IngestRequest):
|
| 17 |
+
n = ingest_edinet_for_company(req.edinet_code, req.date)
|
| 18 |
+
return {"ingested_chunks": n}
|
| 19 |
+
|
| 20 |
+
@app.post("/generate/all")
|
| 21 |
+
def generate_all(req: GenerateRequest):
|
| 22 |
+
summary_text, links = make_summary(req.query)
|
| 23 |
+
sections = {
|
| 24 |
+
"highlights": _extract_section(summary_text, "業績ハイライト"),
|
| 25 |
+
"outlook": _extract_section(summary_text, "見通し"),
|
| 26 |
+
"segments": _extract_section(summary_text, "セグメント"),
|
| 27 |
+
"finance": _extract_section(summary_text, "財務"),
|
| 28 |
+
"shareholder":_extract_section(summary_text, "株主還元"),
|
| 29 |
+
"esg": _extract_section(summary_text, "ESG"),
|
| 30 |
+
"risks": _extract_section(summary_text, "リスク"),
|
| 31 |
+
}
|
| 32 |
+
qa_list, links2 = make_qa(req.query, 30)
|
| 33 |
+
prs = build_deck(sections, links)
|
| 34 |
+
ppt_path = save_pptx(prs, "data/ir_summary.pptx")
|
| 35 |
+
csv_path = save_qa_csv(qa_list, links2, "data/qa_30.csv")
|
| 36 |
+
return {"pptx": ppt_path, "qa_csv": csv_path, "links": links}
|
| 37 |
+
|
| 38 |
+
def _extract_section(text: str, head: str):
|
| 39 |
+
import re
|
| 40 |
+
pat = rf"{head}[::]\s*(.*?)(?:\n-\s*\S+[::]|\Z)"
|
| 41 |
+
m = re.search(pat, text, re.S)
|
| 42 |
+
return (m.group(1).strip() if m else "").strip()
|
| 43 |
+
|
| 44 |
+
@app.get("/proxy/edinet/{doc_id}")
|
| 45 |
+
def proxy_edinet(doc_id: str, type: str = "pdf"):
|
| 46 |
+
if type != "pdf":
|
| 47 |
+
raise HTTPException(400, "unsupported type")
|
| 48 |
+
from rag.ingest import download_edinet_pdf
|
| 49 |
+
pdf = download_edinet_pdf(doc_id)
|
| 50 |
+
if not pdf:
|
| 51 |
+
raise HTTPException(404, "file not found")
|
| 52 |
+
return Response(content=pdf, media_type="application/pdf")
|