lisa-on-cuda / app.py
alessandro trinca tornidor
feat: improve use of variables from WORKDIR, FASTAPI_STATIC
97b0909
import json
import logging
import os
import sys
from pathlib import Path
import gradio as gr
import uvicorn
from fastapi import FastAPI
from fastapi.responses import FileResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from samgis_core.utilities import create_folders_if_not_exists
from samgis_core.utilities import frontend_builder
# for some reason this do
from spaces import GPU as SPACES_GPU
from lisa_on_cuda import routes, app_logger, project_root_folder, static_dist_folder, workdir
from lisa_on_cuda.utils import app_helpers
app_logger.info(f"PROJECT_ROOT_FOLDER:{project_root_folder}, WORKDIR:{workdir}.")
folders_map = os.getenv("FOLDERS_MAP", "{}")
markdown_text = os.getenv("MARKDOWN_TEXT", "")
examples_text_list = os.getenv("EXAMPLES_TEXT_LIST", "").split("\n")
example_body = json.loads(os.getenv("EXAMPLE_BODY", "{}"))
mount_gradio_app = bool(os.getenv("MOUNT_GRADIO_APP", ""))
input_css_path = os.getenv("INPUT_CSS_PATH", "src/input.css")
create_folders_if_not_exists.folders_creation(folders_map)
CUSTOM_GRADIO_PATH = "/"
CUSTOM_STATIC_PATH = "/static"
FASTAPI_TITLE = "lisa_app"
app = FastAPI(title=FASTAPI_TITLE, version="1.0")
app.include_router(routes.router)
frontend_builder.build_frontend(
project_root_folder=workdir,
input_css_path=input_css_path,
output_dist_folder=static_dist_folder
)
app_logger.info("build_frontend ok!")
app.mount(CUSTOM_STATIC_PATH, StaticFiles(directory=str(static_dist_folder)), name="static")
@app.get(CUSTOM_STATIC_PATH)
async def static() -> FileResponse:
return FileResponse(path=str(static_dist_folder / "index.html"), media_type="text/html")
templates = Jinja2Templates(directory="templates")
app_logger.info(f"sys.argv:{sys.argv}.")
args = app_helpers.parse_args([])
app_logger.info(f"prepared default arguments:{args}.")
inference_fn = app_helpers.get_inference_model_by_args(args, inference_decorator=SPACES_GPU)
app_logger.info(f"prepared inference_fn function:{inference_fn.__name__}, creating gradio interface...")
io = app_helpers.get_gradio_interface(inference_fn, args=args)
app_logger.info("created gradio interface")
app = gr.mount_gradio_app(app, io, path=CUSTOM_GRADIO_PATH)
app_logger.info("mounted gradio app within fastapi")
if __name__ == '__main__':
try:
uvicorn.run(app, host="0.0.0.0", port=7860)
except Exception as ex:
logging.error(f"fastapi/gradio application {FASTAPI_TITLE} exception:{ex}.")
raise ex