Spaces:
Paused
Paused
| ''' | |
| purpose: fastAPI routing | |
| refs: | |
| fast api behind proxy/root_path: https://fastapi.tiangolo.com/advanced/behind-a-proxy/ | |
| ''' | |
| from fastapi import FastAPI | |
| from fastapi.responses import HTMLResponse, RedirectResponse | |
| from fastapi import APIRouter, Request, Response | |
| from fastapi.templating import Jinja2Templates | |
| import uvicorn | |
| import gradio as gr | |
| #--- import custom libraries | |
| #import lib.utils as libUtils | |
| #--- imported route handlers | |
| #from routes.api.rte_api import rteApi | |
| #--- fastAPI self doc descriptors | |
| description = """ | |
| Prototype: FastApi in Docker on Huggingface | |
| <insert purpose> | |
| ## key business benefit #1 | |
| ## key business benefit #2 | |
| ## key business benefit #3 | |
| You will be able to: | |
| * key feature #1 | |
| * key feature #2 | |
| * key feature #3 | |
| """ | |
| #--- main fast api app | |
| app = FastAPI( | |
| title="Prototype: Fastapi in docker", | |
| description=description, | |
| version="0.0.1", | |
| terms_of_service="http://example.com/terms/", | |
| contact={ | |
| "name": "Iain McKone", | |
| "email": "iain.mckone@gmail.com", | |
| }, | |
| license_info={ | |
| "name": "MIT", | |
| "url": "http://opensource.org/licenses/MIT", | |
| }, | |
| openapi_url="/api/v1/openapi.json" | |
| ) | |
| io = gr.Interface(lambda x: "Hello, " + x + "!", "textbox", "textbox") | |
| app = gr.mount_gradio_app(app, io, path="/gradio_svc") | |
| #--- configure route handlers | |
| #app.include_router(rteApi, prefix="/api") | |
| #app.include_router(rteQa, prefix="/qa") | |
| #m_kstrPath_templ = libUtils.pth_templ | |
| #m_templRef = Jinja2Templates(directory=str(m_kstrPath_templ)) | |
| """ def get_jinja2Templ(request: Request, pdfResults, strParamTitle, lngNumRecords, blnIsTrain=False, blnIsSample=False): | |
| lngNumRecords = min(lngNumRecords, libUtils.m_klngMaxRecords) | |
| if (blnIsTrain): strParamTitle = strParamTitle + " - Training Data" | |
| if (not blnIsTrain): strParamTitle = strParamTitle + " - Test Data" | |
| if (blnIsSample): lngNumRecords = libUtils.m_klngSampleSize | |
| strParamTitle = strParamTitle + " - max " + str(lngNumRecords) + " rows" | |
| kstrTempl = 'templ_showDataframe.html' | |
| jsonContext = {'request': request, | |
| 'paramTitle': strParamTitle, | |
| 'paramDataframe': pdfResults.sample(lngNumRecords).to_html(classes='table table-striped') | |
| } | |
| result = m_templRef.TemplateResponse(kstrTempl, jsonContext) | |
| return result """ | |
| #--- get main ui/ux entry point | |
| def index(): | |
| return { | |
| "message": "Landing page: prototype for fastApi" | |
| } | |
| def rte_api(): | |
| return { | |
| "message": "Landing page: prototype for fastApi" | |
| } | |
| def rte_gradio(): | |
| #return { | |
| # "message": "Landing page: this is where gradio would start from if we could mount with fastapi" | |
| #} | |
| return RedirectResponse("/gradio_svc") | |
| def rte_streamlit(): | |
| return { | |
| "message": "Landing page: this is where streamlit would start from if we could mount with fastapi" | |
| } | |
| def rte_shiny(): | |
| return { | |
| "message": "Landing page: this is where shiny would start mounted with fastapi" | |
| } | |
| async def syscheck(check_id: str): | |
| from os import system | |
| #import commands | |
| print("TRACE: fastapi.syscheck ... {check_id} ", check_id) | |
| if (check_id=='apt_list'): | |
| print("TRACE: fastapi.syscheck ... {apt_list} ") | |
| m_sysMsg = system("apt list --installed") | |
| return {"message: syscheck apt list complete " + str(m_sysMsg)} | |
| elif (check_id=='sanity'): | |
| print("TRACE: fastapi.syscheck ... {}".format(check_id)) | |
| return {"message: syscheck sanity ... ok "} | |
| elif (check_id=='pip_list'): | |
| print("TRACE: fastapi.syscheck ... {pip_list} ") | |
| m_sysMsg = system("pip list") | |
| return {"message: syscheck pip list complete " + str(m_sysMsg)} | |
| else: | |
| return {"message": "fastApi: syscheck {check_id} " + check_id} | |
| async def sanity(): | |
| print("TRACE: fastapi.sanity ... {}") | |
| return {"message: syscheck sanity ... ok "} | |
| if __name__ == '__main__': | |
| #uvicorn.run("main:app", host="0.0.0.0", port=49132, reload=True) | |
| #uvicorn --app-dir=./fastapi entry_fastapi:app --reload --workers 1 --host 0.0.0.0 --port 7860 & #--- specify a non-root app dir | |
| uvicorn.run("fastapi.entry_fastapi:app", reload=True, workers=1, host="0.0.0.0", port=49132) | |
| #CMD ["uvicorn", "main:app", "--host=0.0.0.0", "--reload"] | |