File size: 4,716 Bytes
851f5e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
'''

    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": "[email protected]",
    },
    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
@app.get('/')
def index():

    return {
        "message": "Landing page:  prototype for fastApi"
    }

@app.get('/api')
def rte_api():

    return {
        "message": "Landing page:  prototype for fastApi"
    }

@app.get('/gradio')
def rte_gradio():

    #return {
    #    "message": "Landing page:  this is where gradio would start from if we could mount with fastapi"
    #} 
    return RedirectResponse("/gradio_svc")


@app.get('/streamlit')
def rte_streamlit():

    return {
        "message": "Landing page:  this is where streamlit would start from if we could mount with fastapi"
    }

@app.get('/shiny')
def rte_shiny():

    return {
        "message": "Landing page:  this is where shiny would start mounted with fastapi"
    }

@app.get('/syscheck/{check_id}')
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}


@app.get('/sanity')
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"]