HARSHIT-hash-07 commited on
Commit
085d33c
·
1 Parent(s): 239ff4c

refactor(backend): bulletproof imports for cloud deployment environment

Browse files
backend/main.py CHANGED
@@ -1,8 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  from fastapi import FastAPI, HTTPException
2
  from fastapi.middleware.cors import CORSMiddleware
3
  from pydantic import BaseModel
4
  from typing import List, Optional
5
- from model_loader import SignModel
6
 
7
  app = FastAPI(title="SignBridge API", version="1.0.0")
8
 
 
1
+ import sys
2
+ import os
3
+
4
+ # Add backend directory to sys.path to resolve imports in both Docker and Local runs
5
+ BACKEND_DIR = os.path.dirname(os.path.abspath(__file__))
6
+ if BACKEND_DIR not in sys.path:
7
+ sys.path.append(BACKEND_DIR)
8
+
9
+ try:
10
+ from .model_loader import SignModel
11
+ except (ImportError, ValueError):
12
+ from model_loader import SignModel
13
+
14
  from fastapi import FastAPI, HTTPException
15
  from fastapi.middleware.cors import CORSMiddleware
16
  from pydantic import BaseModel
17
  from typing import List, Optional
 
18
 
19
  app = FastAPI(title="SignBridge API", version="1.0.0")
20
 
backend/model_loader.py CHANGED
@@ -8,7 +8,10 @@ CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))
8
  if CURRENT_DIR not in sys.path:
9
  sys.path.append(CURRENT_DIR)
10
 
11
- from sign_bridge_inference import SignBridgeInference
 
 
 
12
 
13
  # For Hugging Face Spaces / Docker, we'll store weights in a local weights directory
14
  MODEL_ROOT = os.path.join(os.path.dirname(CURRENT_DIR), "weights")
 
8
  if CURRENT_DIR not in sys.path:
9
  sys.path.append(CURRENT_DIR)
10
 
11
+ try:
12
+ from .sign_bridge_inference import SignBridgeInference
13
+ except (ImportError, ValueError):
14
+ from sign_bridge_inference import SignBridgeInference
15
 
16
  # For Hugging Face Spaces / Docker, we'll store weights in a local weights directory
17
  MODEL_ROOT = os.path.join(os.path.dirname(CURRENT_DIR), "weights")
backend/verify_api.py CHANGED
@@ -1,10 +1,16 @@
1
  import sys
2
  import os
3
- # Add current directory to path
4
- sys.path.append(os.getcwd())
 
 
 
 
 
 
 
5
 
6
  from fastapi.testclient import TestClient
7
- from main import app
8
 
9
  client = TestClient(app)
10
 
 
1
  import sys
2
  import os
3
+ # Add backend directory to sys.path to resolve imports in both Docker and Local runs
4
+ BACKEND_DIR = os.path.dirname(os.path.abspath(__file__))
5
+ if BACKEND_DIR not in sys.path:
6
+ sys.path.append(BACKEND_DIR)
7
+
8
+ try:
9
+ from .main import app
10
+ except (ImportError, ValueError):
11
+ from main import app
12
 
13
  from fastapi.testclient import TestClient
 
14
 
15
  client = TestClient(app)
16