Spaces:
Sleeping
Sleeping
Update irpr/main.py
Browse files- irpr/main.py +44 -15
irpr/main.py
CHANGED
|
@@ -9,30 +9,52 @@ 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
|
| 20 |
-
|
| 21 |
-
|
|
|
|
| 22 |
try:
|
| 23 |
-
|
| 24 |
-
os.makedirs(base, exist_ok=True)
|
| 25 |
-
p = os.path.join(base, ".write_test")
|
| 26 |
-
with open(p, "w") as w: w.write("ok")
|
| 27 |
-
os.remove(p)
|
| 28 |
-
return base
|
| 29 |
except Exception:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
continue
|
|
|
|
|
|
|
| 31 |
return "."
|
| 32 |
|
| 33 |
BASE_DIR = _pick_writable_dir()
|
| 34 |
UPLOAD_DIR = os.path.join(BASE_DIR, "uploads")
|
| 35 |
-
|
| 36 |
|
| 37 |
# 静的
|
| 38 |
os.makedirs("static", exist_ok=True)
|
|
@@ -65,19 +87,26 @@ async def ingest_upload(files: list[UploadFile] = File(...)):
|
|
| 65 |
from rag.ingest import ingest_pdf_bytes
|
| 66 |
total = 0; saved = []
|
| 67 |
try:
|
|
|
|
|
|
|
|
|
|
| 68 |
for f in files:
|
| 69 |
if not f.filename.lower().endswith(".pdf"):
|
| 70 |
continue
|
| 71 |
blob = await f.read()
|
| 72 |
save_path = os.path.join(UPLOAD_DIR, f.filename)
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
source_url = f"/files/uploads/{f.filename}"
|
| 76 |
total += ingest_pdf_bytes(title=f.filename, source_url=source_url, pdf_bytes=blob)
|
| 77 |
saved.append(source_url)
|
| 78 |
-
return {"ok": True, "ingested_chunks": total, "saved": saved}
|
| 79 |
except Exception as e:
|
| 80 |
-
return {"ok": False, "error": repr(e), "ingested_chunks": total, "saved": saved}
|
| 81 |
|
| 82 |
# ---- generate ----
|
| 83 |
@app.post("/generate/all")
|
|
|
|
| 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.1 (OpenAI)")
|
| 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)
|
| 22 |
+
# 可能なら権限を広めに
|
| 23 |
try:
|
| 24 |
+
os.chmod(path, 0o777)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
except Exception:
|
| 26 |
+
pass
|
| 27 |
+
testfile = os.path.join(path, ".write_test")
|
| 28 |
+
with open(testfile, "wb") as f:
|
| 29 |
+
f.write(b"ok")
|
| 30 |
+
os.remove(testfile)
|
| 31 |
+
return True
|
| 32 |
+
except Exception:
|
| 33 |
+
return False
|
| 34 |
+
|
| 35 |
+
def _pick_writable_dir() -> str:
|
| 36 |
+
"""
|
| 37 |
+
優先順位:
|
| 38 |
+
1) DATA_DIR (env で指示)
|
| 39 |
+
2) /mnt/data (この環境では最も安全)
|
| 40 |
+
3) /data
|
| 41 |
+
4) ./var
|
| 42 |
+
5) /tmp/irpr
|
| 43 |
+
6) カレント
|
| 44 |
+
"""
|
| 45 |
+
candidates = []
|
| 46 |
+
if settings.DATA_DIR: candidates.append(settings.DATA_DIR)
|
| 47 |
+
candidates += ["/mnt/data", "/data", "./var", "/tmp/irpr", "."]
|
| 48 |
+
for base in candidates:
|
| 49 |
+
if not base:
|
| 50 |
continue
|
| 51 |
+
if _ensure_dir_writable(base):
|
| 52 |
+
return base
|
| 53 |
return "."
|
| 54 |
|
| 55 |
BASE_DIR = _pick_writable_dir()
|
| 56 |
UPLOAD_DIR = os.path.join(BASE_DIR, "uploads")
|
| 57 |
+
_ensure_dir_writable(UPLOAD_DIR)
|
| 58 |
|
| 59 |
# 静的
|
| 60 |
os.makedirs("static", exist_ok=True)
|
|
|
|
| 87 |
from rag.ingest import ingest_pdf_bytes
|
| 88 |
total = 0; saved = []
|
| 89 |
try:
|
| 90 |
+
# 念のため再確認
|
| 91 |
+
if not _ensure_dir_writable(UPLOAD_DIR):
|
| 92 |
+
raise PermissionError(f"UPLOAD_DIR not writable: {UPLOAD_DIR}")
|
| 93 |
for f in files:
|
| 94 |
if not f.filename.lower().endswith(".pdf"):
|
| 95 |
continue
|
| 96 |
blob = await f.read()
|
| 97 |
save_path = os.path.join(UPLOAD_DIR, f.filename)
|
| 98 |
+
try:
|
| 99 |
+
with open(save_path, "wb") as w:
|
| 100 |
+
w.write(blob)
|
| 101 |
+
except Exception as e:
|
| 102 |
+
# 失敗したパスも返す
|
| 103 |
+
return {"ok": False, "error": f"{repr(e)} at path={save_path}", "base_dir": BASE_DIR}
|
| 104 |
source_url = f"/files/uploads/{f.filename}"
|
| 105 |
total += ingest_pdf_bytes(title=f.filename, source_url=source_url, pdf_bytes=blob)
|
| 106 |
saved.append(source_url)
|
| 107 |
+
return {"ok": True, "ingested_chunks": total, "saved": saved, "base_dir": BASE_DIR}
|
| 108 |
except Exception as e:
|
| 109 |
+
return {"ok": False, "error": repr(e), "ingested_chunks": total, "saved": saved, "base_dir": BASE_DIR}
|
| 110 |
|
| 111 |
# ---- generate ----
|
| 112 |
@app.post("/generate/all")
|