Corin1998 commited on
Commit
b66ca20
·
verified ·
1 Parent(s): 38a74c5

Update irpr/main.py

Browse files
Files changed (1) hide show
  1. irpr/main.py +10 -23
irpr/main.py CHANGED
@@ -1,3 +1,4 @@
 
1
  from fastapi import FastAPI, UploadFile, File, Request
2
  from fastapi.responses import HTMLResponse, JSONResponse
3
  from fastapi.staticfiles import StaticFiles
@@ -8,7 +9,7 @@ import os, traceback
8
  from irpr.models import IngestRequest, GenerateRequest
9
  from irpr.config import settings
10
 
11
- app = FastAPI(title="IR/PR Co-Pilot Pro", version="0.3.3")
12
 
13
  app.add_middleware(
14
  CORSMiddleware, allow_origins=["*"], allow_credentials=True,
@@ -16,43 +17,33 @@ app.add_middleware(
16
  )
17
 
18
  def _pick_writable_dir() -> str:
19
- """
20
- 書き込み可能なディレクトリを候補から選ぶ。
21
- 優先: DATA_DIR(env) -> /data -> ./var -> /tmp/irpr
22
- """
23
- candidates = []
24
- if settings.DATA_DIR: candidates.append(settings.DATA_DIR)
25
- candidates += ["/data", "./var", "/tmp/irpr"]
26
  for base in candidates:
27
  try:
 
28
  os.makedirs(base, exist_ok=True)
29
- testfile = os.path.join(base, ".write_test")
30
- with open(testfile, "w") as f:
31
- f.write("ok")
32
- os.remove(testfile)
33
  return base
34
  except Exception:
35
  continue
36
- # 万一すべて失敗したらカレントディレクトリにフォールバック
37
  return "."
38
 
39
  BASE_DIR = _pick_writable_dir()
40
  UPLOAD_DIR = os.path.join(BASE_DIR, "uploads")
41
  os.makedirs(UPLOAD_DIR, exist_ok=True)
42
 
43
- # 画面用の静的ディレクトリ
44
  os.makedirs("static", exist_ok=True)
45
  os.makedirs("templates", exist_ok=True)
46
-
47
- # /files は BASE_DIR を公開(ここにPPT/CSVやアップロードPDFが置かれる)
48
  app.mount("/static", StaticFiles(directory="static"), name="static")
49
- app.mount("/files", StaticFiles(directory=BASE_DIR), name="files")
50
 
51
  templates = Jinja2Templates(directory="templates")
52
 
53
  @app.get("/", response_class=HTMLResponse)
54
  def ui(request: Request):
55
- # データ保存場所をUIに渡しておくと調査に便利
56
  return templates.TemplateResponse("index.html", {"request": request, "base_dir": BASE_DIR})
57
 
58
  @app.get("/api/health")
@@ -82,11 +73,7 @@ async def ingest_upload(files: list[UploadFile] = File(...)):
82
  with open(save_path, "wb") as w:
83
  w.write(blob)
84
  source_url = f"/files/uploads/{f.filename}"
85
- total += ingest_pdf_bytes(
86
- title=f.filename,
87
- source_url=source_url,
88
- pdf_bytes=blob
89
- )
90
  saved.append(source_url)
91
  return {"ok": True, "ingested_chunks": total, "saved": saved}
92
  except Exception as e:
 
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
  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.0 (OpenAI)")
13
 
14
  app.add_middleware(
15
  CORSMiddleware, allow_origins=["*"], allow_credentials=True,
 
17
  )
18
 
19
  def _pick_writable_dir() -> str:
20
+ candidates = [settings.DATA_DIR, "/data", "./var", "/tmp/irpr", "."]
 
 
 
 
 
 
21
  for base in candidates:
22
  try:
23
+ if not base: continue
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
  os.makedirs(UPLOAD_DIR, exist_ok=True)
36
 
37
+ # 静的
38
  os.makedirs("static", exist_ok=True)
39
  os.makedirs("templates", exist_ok=True)
 
 
40
  app.mount("/static", StaticFiles(directory="static"), name="static")
41
+ app.mount("/files", StaticFiles(directory=BASE_DIR), name="files")
42
 
43
  templates = Jinja2Templates(directory="templates")
44
 
45
  @app.get("/", response_class=HTMLResponse)
46
  def ui(request: Request):
 
47
  return templates.TemplateResponse("index.html", {"request": request, "base_dir": BASE_DIR})
48
 
49
  @app.get("/api/health")
 
73
  with open(save_path, "wb") as w:
74
  w.write(blob)
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: