|
1 | 1 | from fastapi import FastAPI, Request |
2 | | -from fastapi.responses import HTMLResponse |
| 2 | +from fastapi.responses import HTMLResponse, RedirectResponse |
| 3 | +from fastapi.staticfiles import StaticFiles |
| 4 | +from fastapi.openapi.utils import get_openapi |
| 5 | + |
3 | 6 | import uvicorn |
4 | | -from typing import Callable, Optional, Union, List |
5 | 7 | import requests |
6 | 8 | import pandas as pd |
| 9 | +from typing import Callable, Optional, Union, List |
7 | 10 |
|
8 | 11 | from .vetiver_model import VetiverModel |
9 | 12 | from .utils import _jupyter_nb |
@@ -47,35 +50,43 @@ def __init__( |
47 | 50 |
|
48 | 51 | def _init_app(self): |
49 | 52 | app = self.app_factory() |
| 53 | + app.openapi = self._custom_openapi |
50 | 54 |
|
51 | | - @app.get("/") |
52 | | - async def main_app(): |
53 | | - return {"msg": "root path"} |
54 | | - |
55 | | - # redirect to docs? |
56 | | - # def docs_redirect(): |
57 | | - # return RedirectResponse("/rapidoc") |
| 55 | + @app.get("/", include_in_schema=False) |
| 56 | + def docs_redirect(): |
| 57 | + return RedirectResponse("/__docs__") |
58 | 58 |
|
59 | | - @app.get("/ping", include_in_schema=False) |
| 59 | + @app.get("/ping", include_in_schema=True) |
60 | 60 | async def ping(): |
61 | 61 | return {"ping": "pong"} |
62 | 62 |
|
63 | | - @app.get("/rapidoc", response_class=HTMLResponse) |
64 | | - async def rapidoc(): |
| 63 | + @app.get("/__docs__", response_class=HTMLResponse, include_in_schema=False) |
| 64 | + async def rapidoc_pg(): |
65 | 65 | return f""" |
66 | | - <!doctype html> |
67 | | - <html> |
68 | | - <head> |
69 | | - <meta charset="utf-8"> |
70 | | - <script |
71 | | - type="module" |
72 | | - src="https://unpkg.com/rapidoc@9.1.4/dist/rapidoc-min.js" |
73 | | - ></script> |
74 | | - </head> |
75 | | - <body> |
76 | | - <rapi-doc spec-url="{app.openapi_url}"></rapi-doc> |
77 | | - </body> |
78 | | - </html> |
| 66 | + <!doctype html> |
| 67 | + <html> |
| 68 | + <head> |
| 69 | + <meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1,user-scalable=yes"> |
| 70 | + <title>RapiDoc</title> |
| 71 | + <script type="module" src="https://unpkg.com/rapidoc@9.1.3/dist/rapidoc-min.js"></script> |
| 72 | + </script></head> |
| 73 | + <body> |
| 74 | + <rapi-doc spec-url="{app.openapi_url}" |
| 75 | + id="thedoc" render-style="read" schema-style="tree" |
| 76 | + show-components="true" show-info="true" show-header="true" |
| 77 | + allow-search="true" |
| 78 | + show-side-nav="false" |
| 79 | + allow-authentication="false" update-route="false" match-type="regex" |
| 80 | + theme="light" |
| 81 | + header-color="#F2C6AC" |
| 82 | + primary-color = "#8C2D2D"> |
| 83 | + <img |
| 84 | + slot="logo" |
| 85 | + width="55" |
| 86 | + src="https://raw.githubusercontent.com/rstudio/hex-stickers/master/SVG/vetiver.svg" |
| 87 | + </rapi-doc> |
| 88 | + </body> |
| 89 | + </html> |
79 | 90 | """ |
80 | 91 |
|
81 | 92 | if self.check_ptype == True: |
@@ -146,6 +157,18 @@ def run(self): |
146 | 157 | _jupyter_nb() |
147 | 158 | uvicorn.run(self.app, port=self.port, host=self.host) |
148 | 159 |
|
| 160 | + def _custom_openapi(self): |
| 161 | + if self.app.openapi_schema: |
| 162 | + return self.app.openapi_schema |
| 163 | + openapi_schema = get_openapi( |
| 164 | + title=self.model.model_name + " model API", |
| 165 | + version="0.1.3", |
| 166 | + description=self.model.description, |
| 167 | + routes=self.app.routes, |
| 168 | + ) |
| 169 | + openapi_schema["info"]["x-logo"] = {"url": "../docs/figures/logo.svg"} |
| 170 | + self.app.openapi_schema = openapi_schema |
| 171 | + return self.app.openapi_schema |
149 | 172 |
|
150 | 173 | def predict(endpoint, data: dict, **kw): |
151 | 174 | """Make a prediction from model endpoint |
@@ -188,7 +211,7 @@ def _batch_data(pred_data): |
188 | 211 |
|
189 | 212 |
|
190 | 213 | def vetiver_endpoint(url="http://127.0.0.1:8000/predict"): |
191 | | - """Wrap url |
| 214 | + """Wrap url where VetiverModel will be deployed |
192 | 215 |
|
193 | 216 | Parameters |
194 | 217 | ---------- |
|
0 commit comments