alessandro trinca tornidor commited on
Commit
ce62bf6
1 Parent(s): 1e100ac

bug: add missing statements for gradio/fastapi app

Browse files
Files changed (1) hide show
  1. app.py +21 -12
app.py CHANGED
@@ -3,6 +3,7 @@ import os
3
  import pathlib
4
  import uuid
5
 
 
6
  import uvicorn
7
  from fastapi import FastAPI, HTTPException, Request, status
8
  from fastapi.exceptions import RequestValidationError
@@ -11,11 +12,10 @@ from fastapi.staticfiles import StaticFiles
11
  from fastapi.templating import Jinja2Templates
12
  from lisa_on_cuda.utils import app_helpers, frontend_builder, create_folders_and_variables_if_not_exists
13
  from pydantic import ValidationError
14
- from samgis_core.utilities.fastapi_logger import setup_logging
15
 
 
16
  from samgis_lisa_on_zero import PROJECT_ROOT_FOLDER, WORKDIR
17
- from samgis_lisa_on_zero.prediction_api.global_models import models_dict
18
- from samgis_lisa_on_zero.utilities.constants import LISA_INFERENCE_FN
19
  from samgis_lisa_on_zero.utilities.type_hints import ApiRequestBody, StringPromptApiRequestBody
20
 
21
 
@@ -98,8 +98,9 @@ def post_test_string(request_input: StringPromptApiRequestBody) -> JSONResponse:
98
 
99
  @app.post("/infer_lisa")
100
  def infer_lisa(request_input: StringPromptApiRequestBody) -> JSONResponse:
101
- from samgis_lisa_on_zero.prediction_api import lisa
102
  from samgis_lisa_on_zero.io.wrappers_helpers import get_parsed_bbox_points_with_string_prompt, get_source_name
 
 
103
 
104
  app_logger.info("starting lisa inference request...")
105
 
@@ -253,6 +254,10 @@ frontend_builder.build_frontend(
253
  )
254
  create_folders_and_variables_if_not_exists.folders_creation()
255
 
 
 
 
 
256
 
257
  # important: the index() function and the app.mount MUST be at the end
258
  # samgis.html
@@ -281,18 +286,22 @@ app.mount("/", StaticFiles(directory=static_dist_folder, html=True), name="index
281
  async def index() -> FileResponse:
282
  return FileResponse(path=static_dist_folder / "index.html", media_type="text/html")
283
 
 
 
 
284
 
285
- lisa.load_model_and_inference_fn(LISA_INFERENCE_FN)
286
- inference_fn = models_dict[LISA_INFERENCE_FN]["inference"]
287
-
288
-
289
  io = app_helpers.get_gradio_interface(inference_fn)
290
- app_logger.info("mounting gradio app within FastAPI...")
 
 
291
 
292
 
293
  if __name__ == '__main__':
294
  try:
295
  uvicorn.run(host="0.0.0.0", port=7860, app=app)
296
- except Exception as e:
297
- app_logger.error("e:", e)
298
- raise e
 
 
 
3
  import pathlib
4
  import uuid
5
 
6
+ import gradio as gr
7
  import uvicorn
8
  from fastapi import FastAPI, HTTPException, Request, status
9
  from fastapi.exceptions import RequestValidationError
 
12
  from fastapi.templating import Jinja2Templates
13
  from lisa_on_cuda.utils import app_helpers, frontend_builder, create_folders_and_variables_if_not_exists
14
  from pydantic import ValidationError
15
+ from spaces import GPU as SPACES_GPU
16
 
17
+ from samgis_core.utilities.fastapi_logger import setup_logging
18
  from samgis_lisa_on_zero import PROJECT_ROOT_FOLDER, WORKDIR
 
 
19
  from samgis_lisa_on_zero.utilities.type_hints import ApiRequestBody, StringPromptApiRequestBody
20
 
21
 
 
98
 
99
  @app.post("/infer_lisa")
100
  def infer_lisa(request_input: StringPromptApiRequestBody) -> JSONResponse:
 
101
  from samgis_lisa_on_zero.io.wrappers_helpers import get_parsed_bbox_points_with_string_prompt, get_source_name
102
+ from samgis_lisa_on_zero.prediction_api import lisa
103
+ from samgis_lisa_on_zero.utilities.constants import LISA_INFERENCE_FN
104
 
105
  app_logger.info("starting lisa inference request...")
106
 
 
254
  )
255
  create_folders_and_variables_if_not_exists.folders_creation()
256
 
257
+ logging.info("build_frontend ok!")
258
+
259
+ templates = Jinja2Templates(directory="templates")
260
+
261
 
262
  # important: the index() function and the app.mount MUST be at the end
263
  # samgis.html
 
286
  async def index() -> FileResponse:
287
  return FileResponse(path=static_dist_folder / "index.html", media_type="text/html")
288
 
289
+ args = app_helpers.parse_args([])
290
+ app_helpers.app_logger.info(f"prepared default arguments:{args}.")
291
+ inference_fn = app_helpers.get_inference_model_by_args(args, inference_decorator=SPACES_GPU)
292
 
293
+ app_helpers.app_logger.info(f"prepared inference_fn function:{inference_fn.__name__}, creating gradio interface...")
 
 
 
294
  io = app_helpers.get_gradio_interface(inference_fn)
295
+ app_helpers.app_logger.info("created gradio interface, mounting gradio app within FastAPI...")
296
+ app = gr.mount_gradio_app(app, io, path=CUSTOM_GRADIO_PATH)
297
+ app_helpers.app_logger.info("mounted gradio app within fastapi")
298
 
299
 
300
  if __name__ == '__main__':
301
  try:
302
  uvicorn.run(host="0.0.0.0", port=7860, app=app)
303
+ except Exception as ex:
304
+ import logging
305
+ logging.error(f"fastapi/gradio application {FASTAPI_TITLE}, exception:{ex}.")
306
+ print(f"fastapi/gradio application {FASTAPI_TITLE}, exception:{ex}.")
307
+ raise ex