Spaces:
Sleeping
Sleeping
Update irpr/main.py
Browse files- irpr/main.py +32 -8
irpr/main.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
# irpr/main.py
|
|
|
|
| 2 |
from fastapi import FastAPI, UploadFile, File, Request
|
| 3 |
from fastapi.responses import HTMLResponse, JSONResponse
|
| 4 |
from fastapi.staticfiles import StaticFiles
|
|
@@ -9,13 +10,15 @@ import os, traceback
|
|
| 9 |
from irpr.models import IngestRequest, GenerateRequest
|
| 10 |
from irpr.config import settings
|
| 11 |
|
| 12 |
-
app = FastAPI(title="IR/PR Co-Pilot Pro", version="0.4.
|
| 13 |
|
| 14 |
app.add_middleware(
|
| 15 |
CORSMiddleware, allow_origins=["*"], allow_credentials=True,
|
| 16 |
allow_methods=["*"], allow_headers=["*"],
|
| 17 |
)
|
| 18 |
|
|
|
|
|
|
|
| 19 |
def _ensure_dir_writable(path: str) -> bool:
|
| 20 |
try:
|
| 21 |
os.makedirs(path, exist_ok=True)
|
|
@@ -31,15 +34,22 @@ def _ensure_dir_writable(path: str) -> bool:
|
|
| 31 |
except Exception:
|
| 32 |
return False
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
def _pick_writable_dir() -> str:
|
| 35 |
candidates = []
|
| 36 |
if settings.DATA_DIR:
|
| 37 |
candidates.append(settings.DATA_DIR)
|
| 38 |
-
candidates += ["/
|
| 39 |
for base in candidates:
|
| 40 |
-
if
|
| 41 |
return base
|
| 42 |
-
|
|
|
|
|
|
|
| 43 |
|
| 44 |
BASE_DIR = _pick_writable_dir()
|
| 45 |
UPLOAD_DIR = os.path.join(BASE_DIR, "uploads")
|
|
@@ -47,22 +57,35 @@ INDEX_DIR = os.path.join(BASE_DIR, "simple_index")
|
|
| 47 |
_ensure_dir_writable(UPLOAD_DIR)
|
| 48 |
_ensure_dir_writable(INDEX_DIR)
|
| 49 |
|
|
|
|
| 50 |
os.makedirs("static", exist_ok=True)
|
| 51 |
os.makedirs("templates", exist_ok=True)
|
| 52 |
app.mount("/static", StaticFiles(directory="static"), name="static")
|
| 53 |
app.mount("/files", StaticFiles(directory=BASE_DIR), name="files")
|
| 54 |
-
|
| 55 |
templates = Jinja2Templates(directory="templates")
|
| 56 |
|
|
|
|
|
|
|
| 57 |
@app.get("/", response_class=HTMLResponse)
|
| 58 |
def ui(request: Request):
|
| 59 |
return templates.TemplateResponse("index.html", {"request": request, "base_dir": BASE_DIR})
|
| 60 |
|
| 61 |
@app.get("/api/health")
|
| 62 |
def health():
|
| 63 |
-
return {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
-
# ---- ingest ----
|
| 66 |
@app.post("/ingest/edinet")
|
| 67 |
def ingest_edinet(req: IngestRequest):
|
| 68 |
from rag.ingest import ingest_edinet_for_company
|
|
@@ -126,7 +149,8 @@ async def ingest_upload(files: list[UploadFile] = File(...)):
|
|
| 126 |
tb = "".join(traceback.format_exception(type(e), e, e.__traceback__))[-2000:]
|
| 127 |
return {"ok": False, "error": repr(e), "trace": tb, "ingested_chunks": total, "saved": saved, "base_dir": BASE_DIR}
|
| 128 |
|
| 129 |
-
#
|
|
|
|
| 130 |
@app.post("/generate/all")
|
| 131 |
def generate_all(req: GenerateRequest):
|
| 132 |
try:
|
|
|
|
| 1 |
# irpr/main.py
|
| 2 |
+
from __future__ import annotations
|
| 3 |
from fastapi import FastAPI, UploadFile, File, Request
|
| 4 |
from fastapi.responses import HTMLResponse, JSONResponse
|
| 5 |
from fastapi.staticfiles import StaticFiles
|
|
|
|
| 10 |
from irpr.models import IngestRequest, GenerateRequest
|
| 11 |
from irpr.config import settings
|
| 12 |
|
| 13 |
+
app = FastAPI(title="IR/PR Co-Pilot Pro", version="0.4.4 (OpenAI)")
|
| 14 |
|
| 15 |
app.add_middleware(
|
| 16 |
CORSMiddleware, allow_origins=["*"], allow_credentials=True,
|
| 17 |
allow_methods=["*"], allow_headers=["*"],
|
| 18 |
)
|
| 19 |
|
| 20 |
+
# ===== ディレクトリ選定(deps と同じ戦略) =====
|
| 21 |
+
|
| 22 |
def _ensure_dir_writable(path: str) -> bool:
|
| 23 |
try:
|
| 24 |
os.makedirs(path, exist_ok=True)
|
|
|
|
| 34 |
except Exception:
|
| 35 |
return False
|
| 36 |
|
| 37 |
+
def _ensure_dir_tree(base: str, sub: str = "simple_index") -> bool:
|
| 38 |
+
if not _ensure_dir_writable(base):
|
| 39 |
+
return False
|
| 40 |
+
return _ensure_dir_writable(os.path.join(base, sub))
|
| 41 |
+
|
| 42 |
def _pick_writable_dir() -> str:
|
| 43 |
candidates = []
|
| 44 |
if settings.DATA_DIR:
|
| 45 |
candidates.append(settings.DATA_DIR)
|
| 46 |
+
candidates += ["/tmp/irpr", "/mnt/data", os.path.join(os.getcwd(), "data")]
|
| 47 |
for base in candidates:
|
| 48 |
+
if _ensure_dir_tree(base, "simple_index"):
|
| 49 |
return base
|
| 50 |
+
fallback = "/tmp/irpr"
|
| 51 |
+
_ensure_dir_tree(fallback, "simple_index")
|
| 52 |
+
return fallback
|
| 53 |
|
| 54 |
BASE_DIR = _pick_writable_dir()
|
| 55 |
UPLOAD_DIR = os.path.join(BASE_DIR, "uploads")
|
|
|
|
| 57 |
_ensure_dir_writable(UPLOAD_DIR)
|
| 58 |
_ensure_dir_writable(INDEX_DIR)
|
| 59 |
|
| 60 |
+
# 静的配信
|
| 61 |
os.makedirs("static", exist_ok=True)
|
| 62 |
os.makedirs("templates", exist_ok=True)
|
| 63 |
app.mount("/static", StaticFiles(directory="static"), name="static")
|
| 64 |
app.mount("/files", StaticFiles(directory=BASE_DIR), name="files")
|
|
|
|
| 65 |
templates = Jinja2Templates(directory="templates")
|
| 66 |
|
| 67 |
+
# ===== UI / Health =====
|
| 68 |
+
|
| 69 |
@app.get("/", response_class=HTMLResponse)
|
| 70 |
def ui(request: Request):
|
| 71 |
return templates.TemplateResponse("index.html", {"request": request, "base_dir": BASE_DIR})
|
| 72 |
|
| 73 |
@app.get("/api/health")
|
| 74 |
def health():
|
| 75 |
+
return {
|
| 76 |
+
"ok": True,
|
| 77 |
+
"service": "IR/PR Co-Pilot Pro",
|
| 78 |
+
"base_dir": BASE_DIR,
|
| 79 |
+
"upload_dir": UPLOAD_DIR,
|
| 80 |
+
"index_dir": INDEX_DIR,
|
| 81 |
+
"data_dir_env": settings.DATA_DIR,
|
| 82 |
+
"index_dir_env": settings.INDEX_DIR,
|
| 83 |
+
"upload_writable": _ensure_dir_writable(UPLOAD_DIR),
|
| 84 |
+
"index_writable": _ensure_dir_writable(INDEX_DIR),
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
# ===== ingest =====
|
| 88 |
|
|
|
|
| 89 |
@app.post("/ingest/edinet")
|
| 90 |
def ingest_edinet(req: IngestRequest):
|
| 91 |
from rag.ingest import ingest_edinet_for_company
|
|
|
|
| 149 |
tb = "".join(traceback.format_exception(type(e), e, e.__traceback__))[-2000:]
|
| 150 |
return {"ok": False, "error": repr(e), "trace": tb, "ingested_chunks": total, "saved": saved, "base_dir": BASE_DIR}
|
| 151 |
|
| 152 |
+
# ===== generate =====
|
| 153 |
+
|
| 154 |
@app.post("/generate/all")
|
| 155 |
def generate_all(req: GenerateRequest):
|
| 156 |
try:
|