File size: 2,117 Bytes
326115c
a84a5a1
 
b0660fb
719ecfd
b0660fb
719ecfd
c13c67e
a84a5a1
 
b0660fb
e5c9ee0
b0660fb
 
c13c67e
 
326115c
b0660fb
 
326115c
c5fe4a2
c13c67e
 
 
8959fb9
3bd20e4
b0660fb
c13c67e
 
 
 
 
 
 
 
ae85d7f
c13c67e
 
 
 
 
 
 
 
a84a5a1
 
 
2640499
8959fb9
2640499
b0660fb
2640499
8959fb9
2640499
f623930
2640499
b0660fb
 
 
 
 
 
c13c67e
b0660fb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import logging
import os
import sys

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 spaces import GPU as SPACES_GPU

from lisa_on_cuda import routes
from lisa_on_cuda.utils import app_helpers, session_logger, utils
from lisa_on_cuda.utils import frontend_builder


LOGLEVEL = os.getenv('LOGLEVEL', 'INFO').upper()
session_logger.change_logging(LOGLEVEL)

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=frontend_builder.env_project_root_folder,
    input_css_path=frontend_builder.env_input_css_path,
    output_dist_folder=utils.FASTAPI_STATIC / "dist"
)

logging.info("build_frontend ok!")

os.makedirs(utils.FASTAPI_STATIC, exist_ok=True)
app.mount(CUSTOM_STATIC_PATH, StaticFiles(directory=utils.FASTAPI_STATIC / "dist"), name="static")


@app.get(CUSTOM_STATIC_PATH)
async def static() -> FileResponse:
    return FileResponse(path=str(utils.FASTAPI_STATIC / "dist" / "index.html"), media_type="text/html")


templates = Jinja2Templates(directory="templates")


app_helpers.app_logger.info(f"sys.argv:{sys.argv}.")
args = app_helpers.parse_args([])
app_helpers.app_logger.info(f"prepared default arguments:{args}.")
inference_fn = app_helpers.get_inference_model_by_args(args, inference_decorator=SPACES_GPU)
app_helpers.app_logger.info(f"prepared inference_fn function:{inference_fn.__name__}, creating gradio interface...")
io = app_helpers.get_gradio_interface(inference_fn)
app_helpers.app_logger.info("created gradio interface")
app = gr.mount_gradio_app(app, io, path=CUSTOM_GRADIO_PATH)
app_helpers.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