alessandro trinca tornidor commited on
Commit
101b762
1 Parent(s): bfaaa1e

[ci] handle missing static folder under PROJECT_ROOT_FOLDER using env WORKDIR with default on ROOT repository path

Browse files
Files changed (2) hide show
  1. samgis/__init__.py +1 -0
  2. wrappers/fastapi_wrapper.py +11 -13
samgis/__init__.py CHANGED
@@ -8,6 +8,7 @@ from samgis.utilities.constants import SERVICE_NAME
8
 
9
  ROOT = Path(globals().get("__file__", "./_")).absolute().parent.parent
10
  PROJECT_ROOT_FOLDER = Path(os.getenv("PROJECT_ROOT_FOLDER", ROOT))
 
11
  MODEL_FOLDER = Path(PROJECT_ROOT_FOLDER / "machine_learning_models")
12
 
13
  IS_AWS_LAMBDA = bool(os.getenv("IS_AWS_LAMBDA", ""))
 
8
 
9
  ROOT = Path(globals().get("__file__", "./_")).absolute().parent.parent
10
  PROJECT_ROOT_FOLDER = Path(os.getenv("PROJECT_ROOT_FOLDER", ROOT))
11
+ WORKDIR = Path(os.getenv("WORKDIR", ROOT))
12
  MODEL_FOLDER = Path(PROJECT_ROOT_FOLDER / "machine_learning_models")
13
 
14
  IS_AWS_LAMBDA = bool(os.getenv("IS_AWS_LAMBDA", ""))
wrappers/fastapi_wrapper.py CHANGED
@@ -8,7 +8,7 @@ from fastapi.responses import FileResponse, JSONResponse
8
  from fastapi.staticfiles import StaticFiles
9
  from pydantic import ValidationError
10
 
11
- from samgis import PROJECT_ROOT_FOLDER
12
  from samgis.utilities.type_hints import ApiRequestBody, StringPromptApiRequestBody
13
  from samgis_core.utilities.fastapi_logger import setup_logging
14
 
@@ -17,12 +17,6 @@ app_logger = setup_logging(debug=True)
17
  app = FastAPI()
18
 
19
 
20
- try:
21
- app_logger.info(f"PROJECT_ROOT_FOLDER:{PROJECT_ROOT_FOLDER}.")
22
- except Exception as e:
23
- app_logger.error(f"e:{e}.")
24
-
25
-
26
  @app.middleware("http")
27
  async def request_middleware(request, call_next):
28
  request_id = str(uuid.uuid4())
@@ -107,10 +101,14 @@ def infer_lisa(request_input: StringPromptApiRequestBody) -> JSONResponse:
107
  return JSONResponse(status_code=200, content={"body": json.dumps(body)})
108
  except Exception as inference_exception:
109
  import subprocess
110
- home_content = subprocess.run(
111
  f"ls -l {PROJECT_ROOT_FOLDER}/", shell=True, universal_newlines=True, stdout=subprocess.PIPE
112
  )
113
- app_logger.error(f"'ls -l' command output: {home_content.stdout}.")
 
 
 
 
114
  app_logger.error(f"inference error:{inference_exception}.")
115
  raise HTTPException(
116
  status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="Internal server error on inference")
@@ -186,20 +184,20 @@ async def http_exception_handler(request: Request, exc: HTTPException) -> JSONRe
186
 
187
 
188
  # important: the index() function and the app.mount MUST be at the end
189
- app.mount("/lisa", StaticFiles(directory=PROJECT_ROOT_FOLDER / "static" / "dist", html=True), name="lisa")
190
 
191
 
192
  @app.get("/lisa")
193
  async def lisa() -> FileResponse:
194
- return FileResponse(path=PROJECT_ROOT_FOLDER / "static" / "dist" / "lisa.html", media_type="text/html")
195
 
196
 
197
- app.mount("/", StaticFiles(directory=PROJECT_ROOT_FOLDER / "static" / "dist", html=True), name="static")
198
 
199
 
200
  @app.get("/")
201
  async def index() -> FileResponse:
202
- return FileResponse(path=PROJECT_ROOT_FOLDER / "static" / "dist" / "index.html", media_type="text/html")
203
 
204
 
205
  if __name__ == '__main__':
 
8
  from fastapi.staticfiles import StaticFiles
9
  from pydantic import ValidationError
10
 
11
+ from samgis import PROJECT_ROOT_FOLDER, WORKDIR
12
  from samgis.utilities.type_hints import ApiRequestBody, StringPromptApiRequestBody
13
  from samgis_core.utilities.fastapi_logger import setup_logging
14
 
 
17
  app = FastAPI()
18
 
19
 
 
 
 
 
 
 
20
  @app.middleware("http")
21
  async def request_middleware(request, call_next):
22
  request_id = str(uuid.uuid4())
 
101
  return JSONResponse(status_code=200, content={"body": json.dumps(body)})
102
  except Exception as inference_exception:
103
  import subprocess
104
+ project_root_folder_content = subprocess.run(
105
  f"ls -l {PROJECT_ROOT_FOLDER}/", shell=True, universal_newlines=True, stdout=subprocess.PIPE
106
  )
107
+ app_logger.error(f"project_root folder 'ls -l' command output: {project_root_folder_content.stdout}.")
108
+ workdir_folder_content = subprocess.run(
109
+ f"ls -l {WORKDIR}/", shell=True, universal_newlines=True, stdout=subprocess.PIPE
110
+ )
111
+ app_logger.error(f"workdir folder 'ls -l' command output: {workdir_folder_content.stdout}.")
112
  app_logger.error(f"inference error:{inference_exception}.")
113
  raise HTTPException(
114
  status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="Internal server error on inference")
 
184
 
185
 
186
  # important: the index() function and the app.mount MUST be at the end
187
+ app.mount("/lisa", StaticFiles(directory=WORKDIR / "static" / "dist", html=True), name="lisa")
188
 
189
 
190
  @app.get("/lisa")
191
  async def lisa() -> FileResponse:
192
+ return FileResponse(path=WORKDIR / "static" / "dist" / "lisa.html", media_type="text/html")
193
 
194
 
195
+ app.mount("/", StaticFiles(directory=WORKDIR / "static" / "dist", html=True), name="static")
196
 
197
 
198
  @app.get("/")
199
  async def index() -> FileResponse:
200
+ return FileResponse(path=WORKDIR / "static" / "dist" / "index.html", media_type="text/html")
201
 
202
 
203
  if __name__ == '__main__':