Spaces:
Sleeping
Sleeping
Update irpr/main.py
Browse files- irpr/main.py +63 -14
irpr/main.py
CHANGED
|
@@ -1,22 +1,66 @@
|
|
| 1 |
-
from fastapi import FastAPI, HTTPException, Response
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
from irpr.models import IngestRequest, GenerateRequest
|
|
|
|
| 3 |
|
| 4 |
-
app = FastAPI(title="IR/PR Co-Pilot Pro", version="0.
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
return {"ok": True, "service": "IR/PR Co-Pilot Pro"}
|
| 9 |
|
|
|
|
| 10 |
@app.post("/ingest/edinet")
|
| 11 |
def ingest_edinet(req: IngestRequest):
|
| 12 |
-
# ← ここで初めて読み込む(起動時に重い依存を踏まない)
|
| 13 |
from rag.ingest import ingest_edinet_for_company
|
| 14 |
n = ingest_edinet_for_company(req.edinet_code, req.date)
|
| 15 |
return {"ingested_chunks": n}
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
@app.post("/generate/all")
|
| 18 |
def generate_all(req: GenerateRequest):
|
| 19 |
-
# 必要なものだけ都度 import(遅延)
|
| 20 |
from generators.summary import make_summary
|
| 21 |
from generators.qa import make_qa
|
| 22 |
from export.ppt import build_deck, save_pptx
|
|
@@ -36,21 +80,26 @@ def generate_all(req: GenerateRequest):
|
|
| 36 |
prs = build_deck(sections, links)
|
| 37 |
ppt_path = save_pptx(prs, "data/ir_summary.pptx")
|
| 38 |
csv_path = save_qa_csv(qa_list, links2, "data/qa_30.csv")
|
| 39 |
-
return {
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
m = re.search(pat, text, re.S)
|
| 45 |
-
return (m.group(1).strip() if m else "").strip()
|
| 46 |
|
|
|
|
| 47 |
@app.get("/proxy/edinet/{doc_id}")
|
| 48 |
def proxy_edinet(doc_id: str, type: str = "pdf"):
|
| 49 |
if type != "pdf":
|
| 50 |
raise HTTPException(400, "unsupported type")
|
| 51 |
-
# ここも遅延
|
| 52 |
from rag.ingest import download_edinet_pdf
|
| 53 |
pdf = download_edinet_pdf(doc_id)
|
| 54 |
if not pdf:
|
| 55 |
raise HTTPException(404, "file not found")
|
| 56 |
return Response(content=pdf, media_type="application/pdf")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException, Response, UploadFile, File, Request
|
| 2 |
+
from fastapi.responses import HTMLResponse
|
| 3 |
+
from fastapi.staticfiles import StaticFiles
|
| 4 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 5 |
+
from fastapi.templating import Jinja2Templates
|
| 6 |
from irpr.models import IngestRequest, GenerateRequest
|
| 7 |
+
import os
|
| 8 |
|
| 9 |
+
app = FastAPI(title="IR/PR Co-Pilot Pro", version="0.2.0")
|
| 10 |
|
| 11 |
+
# --- CORS(同一オリジン運用想定だが念のため全許可) ---
|
| 12 |
+
app.add_middleware(
|
| 13 |
+
CORSMiddleware,
|
| 14 |
+
allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"],
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
# --- 静的 & 生成物の公開マウント ---
|
| 18 |
+
os.makedirs("static", exist_ok=True)
|
| 19 |
+
os.makedirs("templates", exist_ok=True)
|
| 20 |
+
os.makedirs("data", exist_ok=True)
|
| 21 |
+
os.makedirs("data/uploads", exist_ok=True)
|
| 22 |
+
|
| 23 |
+
app.mount("/static", StaticFiles(directory="static"), name="static")
|
| 24 |
+
app.mount("/files", StaticFiles(directory="data"), name="files")
|
| 25 |
+
templates = Jinja2Templates(directory="templates")
|
| 26 |
+
|
| 27 |
+
# ---------------- UI ----------------
|
| 28 |
+
@app.get("/", response_class=HTMLResponse)
|
| 29 |
+
def ui(request: Request):
|
| 30 |
+
return templates.TemplateResponse("index.html", {"request": request})
|
| 31 |
+
|
| 32 |
+
@app.get("/api/health")
|
| 33 |
+
def health():
|
| 34 |
return {"ok": True, "service": "IR/PR Co-Pilot Pro"}
|
| 35 |
|
| 36 |
+
# ------------- API: 取込 -------------
|
| 37 |
@app.post("/ingest/edinet")
|
| 38 |
def ingest_edinet(req: IngestRequest):
|
|
|
|
| 39 |
from rag.ingest import ingest_edinet_for_company
|
| 40 |
n = ingest_edinet_for_company(req.edinet_code, req.date)
|
| 41 |
return {"ingested_chunks": n}
|
| 42 |
|
| 43 |
+
@app.post("/ingest/upload")
|
| 44 |
+
async def ingest_upload(files: list[UploadFile] = File(...)):
|
| 45 |
+
from rag.ingest import ingest_pdf_bytes
|
| 46 |
+
total = 0
|
| 47 |
+
saved = []
|
| 48 |
+
for f in files:
|
| 49 |
+
if not f.filename.lower().endswith(".pdf"):
|
| 50 |
+
continue
|
| 51 |
+
blob = await f.read()
|
| 52 |
+
# 保存してダウンロード可能に
|
| 53 |
+
save_path = os.path.join("data", "uploads", f.filename)
|
| 54 |
+
with open(save_path, "wb") as w:
|
| 55 |
+
w.write(blob)
|
| 56 |
+
source_url = f"/files/uploads/{f.filename}"
|
| 57 |
+
total += ingest_pdf_bytes(title=f.filename, source_url=source_url, pdf_bytes=blob)
|
| 58 |
+
saved.append(source_url)
|
| 59 |
+
return {"ingested_chunks": total, "saved": saved}
|
| 60 |
+
|
| 61 |
+
# ------------- API: 生成 -------------
|
| 62 |
@app.post("/generate/all")
|
| 63 |
def generate_all(req: GenerateRequest):
|
|
|
|
| 64 |
from generators.summary import make_summary
|
| 65 |
from generators.qa import make_qa
|
| 66 |
from export.ppt import build_deck, save_pptx
|
|
|
|
| 80 |
prs = build_deck(sections, links)
|
| 81 |
ppt_path = save_pptx(prs, "data/ir_summary.pptx")
|
| 82 |
csv_path = save_qa_csv(qa_list, links2, "data/qa_30.csv")
|
| 83 |
+
return {
|
| 84 |
+
"pptx": ppt_path.replace("data/", "/files/"),
|
| 85 |
+
"qa_csv": csv_path.replace("data/", "/files/"),
|
| 86 |
+
"links": links
|
| 87 |
+
}
|
|
|
|
|
|
|
| 88 |
|
| 89 |
+
# ------------- API: EDINET PDF 直配 -------------
|
| 90 |
@app.get("/proxy/edinet/{doc_id}")
|
| 91 |
def proxy_edinet(doc_id: str, type: str = "pdf"):
|
| 92 |
if type != "pdf":
|
| 93 |
raise HTTPException(400, "unsupported type")
|
|
|
|
| 94 |
from rag.ingest import download_edinet_pdf
|
| 95 |
pdf = download_edinet_pdf(doc_id)
|
| 96 |
if not pdf:
|
| 97 |
raise HTTPException(404, "file not found")
|
| 98 |
return Response(content=pdf, media_type="application/pdf")
|
| 99 |
+
|
| 100 |
+
# ------------- helper -------------
|
| 101 |
+
def _extract_section(text: str, head: str):
|
| 102 |
+
import re
|
| 103 |
+
pat = rf"{head}[::]\s*(.*?)(?:\n-\s*\S+[::]|\Z)"
|
| 104 |
+
m = re.search(pat, text, re.S)
|
| 105 |
+
return (m.group(1).strip() if m else "").strip()
|