Shahid commited on
Commit
365d8a2
·
1 Parent(s): 82e5025

changed app

Browse files
Files changed (2) hide show
  1. Dockerfile +3 -0
  2. backend/app/main.py +16 -4
Dockerfile CHANGED
@@ -43,6 +43,9 @@ ENV PORT=7860 \
43
  DATA_DIR=/data \
44
  FRONTEND_DIST=/app/frontend_dist
45
 
 
 
 
46
  EXPOSE 7860
47
 
48
  # Run FastAPI (serve API + static frontend)
 
43
  DATA_DIR=/data \
44
  FRONTEND_DIST=/app/frontend_dist
45
 
46
+ # Ensure data dir exists with permissive permissions (in case volume isn't mounted yet)
47
+ RUN mkdir -p /data && chmod -R 777 /data
48
+
49
  EXPOSE 7860
50
 
51
  # Run FastAPI (serve API + static frontend)
backend/app/main.py CHANGED
@@ -3,8 +3,11 @@ from fastapi.middleware.cors import CORSMiddleware
3
  from fastapi.staticfiles import StaticFiles
4
  import os
5
 
6
- from .api.routers.ocr import router as ocr_router
7
- from .api.routers.synthetic import router as synthetic_router
 
 
 
8
 
9
 
10
  app = FastAPI(title="Unified Backend: OCR + Synthetic")
@@ -22,13 +25,22 @@ app.add_middleware(
22
  BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
23
  DEFAULT_DATA_DIR = os.path.join(BASE_DIR, "data")
24
  DATA_DIR = os.getenv("DATA_DIR", DEFAULT_DATA_DIR)
 
 
 
 
 
 
 
 
25
  UPLOAD_DIR = os.path.join(DATA_DIR, "uploaded_images")
26
  SYN_OUT_DIR = os.path.join(DATA_DIR, "synth_outputs")
27
-
28
  os.makedirs(UPLOAD_DIR, exist_ok=True)
29
  os.makedirs(SYN_OUT_DIR, exist_ok=True)
30
 
31
- # Routers
 
 
32
  app.include_router(ocr_router)
33
  app.include_router(synthetic_router)
34
 
 
3
  from fastapi.staticfiles import StaticFiles
4
  import os
5
 
6
+ """Main FastAPI application (serves API and static frontend).
7
+
8
+ Note: We delay importing routers until after DATA_DIR is finalized to avoid
9
+ permission issues when creating directories on certain platforms (e.g., Spaces).
10
+ """
11
 
12
 
13
  app = FastAPI(title="Unified Backend: OCR + Synthetic")
 
25
  BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
26
  DEFAULT_DATA_DIR = os.path.join(BASE_DIR, "data")
27
  DATA_DIR = os.getenv("DATA_DIR", DEFAULT_DATA_DIR)
28
+ try:
29
+ os.makedirs(DATA_DIR, exist_ok=True)
30
+ except Exception:
31
+ # Fallback to non-persistent tmp if permission denied
32
+ DATA_DIR = "/tmp/data"
33
+ os.environ["DATA_DIR"] = DATA_DIR
34
+ os.makedirs(DATA_DIR, exist_ok=True)
35
+
36
  UPLOAD_DIR = os.path.join(DATA_DIR, "uploaded_images")
37
  SYN_OUT_DIR = os.path.join(DATA_DIR, "synth_outputs")
 
38
  os.makedirs(UPLOAD_DIR, exist_ok=True)
39
  os.makedirs(SYN_OUT_DIR, exist_ok=True)
40
 
41
+ # Routers (import after DATA_DIR is finalized so they read correct env)
42
+ from .api.routers.ocr import router as ocr_router
43
+ from .api.routers.synthetic import router as synthetic_router
44
  app.include_router(ocr_router)
45
  app.include_router(synthetic_router)
46