Add Dash
Signed-off-by: András Schmelczer <andras@schmelczer.dev>
This commit is contained in:
parent
57cccac722
commit
a2458c7ed1
13 changed files with 249 additions and 50 deletions
|
|
@ -1,14 +1,13 @@
|
|||
import logging
|
||||
import os
|
||||
METRICS_PATH = "/metrics"
|
||||
|
||||
logger = logging.getLogger("good_ai")
|
||||
# logger = logging.getLogger("good_ai")
|
||||
|
||||
PRODUCTION_KEY = "production"
|
||||
# PRODUCTION_KEY = "production"
|
||||
|
||||
environment = os.environ.get("ENVIRONMENT", PRODUCTION_KEY).lower()
|
||||
is_production = environment == PRODUCTION_KEY
|
||||
# environment = os.environ.get("ENVIRONMENT", PRODUCTION_KEY).lower()
|
||||
# is_production = environment == PRODUCTION_KEY
|
||||
|
||||
if is_production:
|
||||
logger.info("Running in production mode ✅")
|
||||
else:
|
||||
logger.warn("Running in development mode ‼️")
|
||||
# if is_production:
|
||||
# logger.info("Running in production mode ✅")
|
||||
# else:
|
||||
# logger.warn("Running in development mode ‼️")
|
||||
|
|
|
|||
35
good_ai/src/good_ai/good_ai/deploy/create_fastapi_app.py
Normal file
35
good_ai/src/good_ai/good_ai/deploy/create_fastapi_app.py
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
from fastapi import FastAPI, status
|
||||
from fastapi.middleware.wsgi import WSGIMiddleware
|
||||
from fastapi.openapi.docs import get_swagger_ui_html
|
||||
from fastapi.responses import RedirectResponse
|
||||
from starlette.responses import HTMLResponse
|
||||
|
||||
from ..config import METRICS_PATH
|
||||
from ..helper import snake_case_to_text
|
||||
from ..metrics import create_dash_app
|
||||
from ..views import HealthCheckResponse
|
||||
|
||||
|
||||
def create_fastapi_app(function_name: str) -> FastAPI:
|
||||
app = FastAPI(
|
||||
title=snake_case_to_text(function_name),
|
||||
description=f"REST API wrapper for interacting with the '{function_name}' function.",
|
||||
docs_url=None,
|
||||
redoc_url=None,
|
||||
)
|
||||
|
||||
@app.get("/", include_in_schema=False)
|
||||
def redirect_to_entrypoint() -> RedirectResponse:
|
||||
return RedirectResponse("/metrics")
|
||||
|
||||
app.mount(METRICS_PATH, WSGIMiddleware(create_dash_app(function_name)))
|
||||
|
||||
@app.get("/docs", include_in_schema=False)
|
||||
def custom_swagger_ui_html() -> HTMLResponse:
|
||||
return get_swagger_ui_html(openapi_url="openapi.json", title=app.title)
|
||||
|
||||
@app.get("/health", status_code=status.HTTP_200_OK)
|
||||
def check_health() -> HealthCheckResponse:
|
||||
return HealthCheckResponse(is_healthy=True)
|
||||
|
||||
return app
|
||||
|
|
@ -1,15 +1,14 @@
|
|||
from typing import Any, Callable
|
||||
|
||||
import uvicorn
|
||||
from fastapi import FastAPI, status
|
||||
from fastapi.openapi.docs import get_swagger_ui_html
|
||||
from fastapi.responses import RedirectResponse
|
||||
from starlette.responses import HTMLResponse
|
||||
from fastapi import status
|
||||
from typing_extensions import Never
|
||||
|
||||
from good_ai.good_ai.deploy.create_fastapi_app import create_fastapi_app
|
||||
|
||||
from ..set_default_config import set_default_config_if_uninitialized
|
||||
from ..tracing import TracingContext
|
||||
from ..views import HealthCheckResponse, Trace
|
||||
from ..views import Trace
|
||||
|
||||
|
||||
def serve(
|
||||
|
|
@ -17,24 +16,7 @@ def serve(
|
|||
) -> Never:
|
||||
set_default_config_if_uninitialized()
|
||||
|
||||
app = FastAPI(
|
||||
title=function.__name__.capitalize().replace("_", " "),
|
||||
description=f"REST API wrapper for interacting with the {function.__name__} function.",
|
||||
docs_url=None,
|
||||
redoc_url=None,
|
||||
)
|
||||
|
||||
@app.get("/", include_in_schema=False)
|
||||
def redirect_to_docs() -> RedirectResponse:
|
||||
return RedirectResponse("/docs")
|
||||
|
||||
@app.get("/docs", include_in_schema=False)
|
||||
def custom_swagger_ui_html() -> HTMLResponse:
|
||||
return get_swagger_ui_html(openapi_url="openapi.json", title=app.title)
|
||||
|
||||
@app.get("/health", status_code=status.HTTP_200_OK)
|
||||
def check_health() -> HealthCheckResponse:
|
||||
return HealthCheckResponse(is_healthy=True)
|
||||
app = create_fastapi_app(function.__name__)
|
||||
|
||||
@app.post("/score", status_code=status.HTTP_200_OK, response_model=Trace)
|
||||
def process(input: Any) -> Trace:
|
||||
|
|
|
|||
1
good_ai/src/good_ai/good_ai/helper/__init__.py
Normal file
1
good_ai/src/good_ai/good_ai/helper/__init__.py
Normal file
|
|
@ -0,0 +1 @@
|
|||
from .snake_case_to_text import snake_case_to_text
|
||||
2
good_ai/src/good_ai/good_ai/helper/snake_case_to_text.py
Normal file
2
good_ai/src/good_ai/good_ai/helper/snake_case_to_text.py
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
def snake_case_to_text(snake_case: str) -> str:
|
||||
return snake_case.capitalize().replace("_", " ")
|
||||
1
good_ai/src/good_ai/good_ai/metrics/__init__.py
Normal file
1
good_ai/src/good_ai/good_ai/metrics/__init__.py
Normal file
|
|
@ -0,0 +1 @@
|
|||
from .create_dash_app import create_dash_app
|
||||
48
good_ai/src/good_ai/good_ai/metrics/create_dash_app.py
Normal file
48
good_ai/src/good_ai/good_ai/metrics/create_dash_app.py
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
import pandas as pd
|
||||
import plotly.express as px
|
||||
from dash import Dash, dcc, html
|
||||
from flask import Flask
|
||||
|
||||
from ..config import METRICS_PATH
|
||||
from ..helper import snake_case_to_text
|
||||
|
||||
|
||||
def create_dash_app(function_name: str) -> Flask:
|
||||
app = Dash(function_name, requests_pathname_prefix=METRICS_PATH + "/")
|
||||
|
||||
markdown_text = f"""
|
||||
# {snake_case_to_text(function_name)} - metrics
|
||||
> A human-friendly framework for robust end-to-end AI deployments
|
||||
|
||||
## Using the API
|
||||
|
||||
You can find the available endpoints at [/docs](/docs).
|
||||
|
||||
## Metrics
|
||||
|
||||
The recent traces and aggregated metrics are presented below.
|
||||
"""
|
||||
|
||||
df = pd.read_csv(
|
||||
"https://gist.githubusercontent.com/chriddyp/5d1ea79569ed194d432e56108a04d188/raw/a9f9e8076b837d541398e999dcbac2b2826a81f8/gdp-life-exp-2007.csv"
|
||||
)
|
||||
|
||||
fig = px.scatter(
|
||||
df,
|
||||
x="gdp per capita",
|
||||
y="life expectancy",
|
||||
size="population",
|
||||
color="continent",
|
||||
hover_name="country",
|
||||
log_x=True,
|
||||
size_max=60,
|
||||
)
|
||||
|
||||
app.layout = html.Div(
|
||||
[
|
||||
dcc.Markdown(children=markdown_text),
|
||||
dcc.Graph(id="life-exp-vs-gdp", figure=fig),
|
||||
]
|
||||
)
|
||||
|
||||
return app.server
|
||||
|
|
@ -1,4 +1,5 @@
|
|||
from typing import Any, List
|
||||
from uuid import uuid4
|
||||
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
|
@ -6,6 +7,7 @@ from .model import Model
|
|||
|
||||
|
||||
class Trace(BaseModel):
|
||||
id = str(uuid4())
|
||||
created: str
|
||||
execution_time_ms: float
|
||||
input: Any
|
||||
|
|
|
|||
|
|
@ -218,24 +218,26 @@ class LargeFile:
|
|||
|
||||
copy(str(path), str(Path(tmp) / self._local_name))
|
||||
|
||||
logger.info(f"Compressing {self._local_name}")
|
||||
shutil.make_archive(
|
||||
str(Path(tmp) / self._local_name),
|
||||
"gztar",
|
||||
tmp,
|
||||
)
|
||||
with tempfile.TemporaryDirectory() as tmp2:
|
||||
# A directory has to be zipped and it cannot contain the output of the zipping
|
||||
logger.info(f"Compressing {self._local_name}")
|
||||
shutil.make_archive(
|
||||
str(Path(tmp2) / self._local_name),
|
||||
"gztar",
|
||||
tmp,
|
||||
)
|
||||
|
||||
logger.info(f"Uploading {self._local_name} to S3 from {path}")
|
||||
logger.info(f"Uploading {self._local_name} to S3 from {path}")
|
||||
|
||||
file_to_be_uploaded = Path(tmp) / f"{self._local_name}.tar.gz"
|
||||
self._client.upload_file(
|
||||
Filename=str(file_to_be_uploaded),
|
||||
Bucket=self.bucket_name,
|
||||
Key=self._s3_name,
|
||||
Callback=None
|
||||
if hide_progress
|
||||
else UploadProgressBar(path=file_to_be_uploaded, logger=logger),
|
||||
)
|
||||
file_to_be_uploaded = Path(tmp2) / f"{self._local_name}.tar.gz"
|
||||
self._client.upload_file(
|
||||
Filename=str(file_to_be_uploaded),
|
||||
Bucket=self.bucket_name,
|
||||
Key=self._s3_name,
|
||||
Callback=None
|
||||
if hide_progress
|
||||
else UploadProgressBar(path=file_to_be_uploaded, logger=logger),
|
||||
)
|
||||
|
||||
self.clean_up()
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue