kidcoconut commited on
Commit
43ffce1
·
0 Parent(s):

orphan: clean tree ready for push to hugspace

Browse files
Dockerfile ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #--- PREREQS:
2
+ # - to prep wkg dir: ./_env_config/local_dev/utl_prep_localUnitTest.sh
3
+ # - to backup wkg dir: ./_env_config/local_dev/utl_prep_localUnitTest.sh --backup
4
+
5
+ # - to build dkr image: make sure you stop and remove the container if you are replacing/upgrading;
6
+ # or change the version tag# from 0.0.1
7
+
8
+
9
+ #--- DOCKER:
10
+ # docker build -t img_fastapi_templ:0.0.1 .
11
+ # docker create -it -p 7860:7860 --name ctr_fastapi_templ img_fastapi_templ:0.0.1
12
+ # docker start -it ctr_fastapi_templ
13
+ # docker run -it -p 7860:7860 --name ctr_fastapi_templ img_fastapi_templ:0.0.1
14
+
15
+ # docker push kidcoconut73/<img:tag>
16
+
17
+
18
+ #--- use a base image of python
19
+ FROM python:3.8-slim-buster
20
+
21
+
22
+
23
+ # Set up a new user named "user" with user ID 1000
24
+ USER root
25
+ RUN useradd -m -u 1000 user
26
+
27
+ USER user
28
+ ENV HOME=/home/user \
29
+ PATH=/home/user/.local/bin:$PATH
30
+
31
+ #--- set docker image working directory to /app
32
+ RUN mkdir $HOME/app $HOME/app/scripts
33
+ WORKDIR $HOME/app
34
+
35
+ #--- install all lib dependencies into the image
36
+ COPY --chown=user ./requirements.txt ./requirements.txt
37
+ RUN pip install --no-cache-dir -r ./requirements.txt
38
+
39
+ #--- copy files from the local pwd to the docker image /app folder
40
+ #--- .dockerignore: ensure no local data folders or files (images) are copied into the docker image/container
41
+ COPY --chown=user ./_env_config/local_dev/utl_dkr_preRun.sh ./scripts/docker/
42
+ COPY --chown=user ./fastapi ./fastapi
43
+ COPY --chown=user ./streamlit ./streamlit
44
+
45
+ #--- for huggingface; assume 1:1 mapping between internal and external ports; and only one port can truly be exposed
46
+ #--- for fastapi; external 7860; internal 7860
47
+ #--- for streamlit; external xxxxx; internal 39131
48
+ EXPOSE 7860
49
+
50
+
51
+ #--- establish environment prereqs
52
+ ENTRYPOINT [ "./scripts/docker/utl_dkr_preRun.sh" ]
53
+
54
+
55
+ #--- WORKAROUND: you may have to stop the docker container through docker desktop, or cmd line eg docker kill <ctr_name>
README.md ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: spcdkr fastapi templ
3
+ emoji: 📉
4
+ colorFrom: purple
5
+ colorTo: pink
6
+ sdk: docker
7
+ app_port: 7860
8
+ pinned: false
9
+ license: mit
10
+ ---
11
+
12
+ This app is publically available and run at https://kidcoconut-dkr-fastapi-templ.hf.space/
13
+ example endpoint: &emsp;https://kidcoconut-dkr-fastapi-templ.hf.space/sanity
14
+ example endpoint: &emsp;https://kidcoconut-dkr-fastapi-templ.hf.space/syscheck/sanity
15
+
16
+ streamlit endpoint: &ensp;cannot be mounted to fastapi
17
+ fastapi endpoint: &emsp;&nbsp;https://kidcoconut-dkr-fastapi-templ.hf.space/api
18
+ gradio: &emsp;&emsp;&emsp;&emsp;&emsp;&ensp;https://kidcoconut-dkr-fastapi-templ.hf.space/gradio
19
+ docs: &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&ensp;https://kidcoconut-dkr-fastapi-templ.hf.space/docs
20
+
21
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
_env_config/local_dev/utl_dkr_preRun.sh ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/bash
2
+
3
+ #--- Note: this file is designed to run locally as well as within docker to prep the environment
4
+ #--- Entry: this script is assumed to run from the /app root folder
5
+ #--- Usage: ./_env_config/local_dev/utl_dkr_preRun.sh
6
+
7
+ #--- for volume initialization; ensure folders are in place; assume: we are in the /app folder
8
+
9
+
10
+ <<blockComment
11
+ -
12
+ blockComment
13
+ echo -e "INFO(utl_dkr_preRun):\t Initializing ..."
14
+
15
+ strpth_pwd=$(pwd)
16
+ strpth_scriptLoc=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
17
+ strpth_scrHome="${strpth_scriptLoc}/../"
18
+ strpth_appHome="${strpth_scrHome}../"
19
+ strpth_scrModels="${strpth_scrHome}models/"
20
+
21
+ #echo "strpth_appHome = ${strpth_appHome}"
22
+
23
+
24
+ #--- for fastapi; external 7860; internal 7860
25
+ echo "INFO: starting fastapi ..."
26
+ uvicorn --app-dir=./fastapi entry_fastapi:app --reload --workers 1 --host 0.0.0.0 --port 7860 & #--- specify a non-root app dir
27
+
28
+ #--- for streamlit; external 49131; internal 39131
29
+ #--- note: streamlit cannot be mounted into fastapi; you need nginx to run in parallel
30
+ echo "INFO: starting streamlit ..."
31
+ streamlit run ./streamlit/entry_streamlit.py --server.port=39131 --server.maxUploadSize=2000 #--- & run in the background
32
+
33
+
34
+
35
+ <<blockErrorLog
36
+ blockErrorLog
fastapi/ABOUTME.md ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ # ABOUTME: fastapi
2
+ ## Purpose: this environment is used to API endpoints
3
+ ## Prereq:
4
+ ## -
5
+
6
+
fastapi/entry_fastapi.py ADDED
@@ -0,0 +1,159 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ '''
2
+ purpose: fastAPI routing
3
+
4
+ refs:
5
+ fast api behind proxy/root_path: https://fastapi.tiangolo.com/advanced/behind-a-proxy/
6
+ '''
7
+
8
+ from fastapi import FastAPI
9
+ from fastapi.responses import HTMLResponse, RedirectResponse
10
+ from fastapi import APIRouter, Request, Response
11
+ from fastapi.templating import Jinja2Templates
12
+ import uvicorn
13
+ import gradio as gr
14
+
15
+ #--- import custom libraries
16
+ #import lib.utils as libUtils
17
+
18
+
19
+ #--- imported route handlers
20
+ #from routes.api.rte_api import rteApi
21
+
22
+
23
+ #--- fastAPI self doc descriptors
24
+ description = """
25
+ Prototype: FastApi in Docker on Huggingface
26
+
27
+ <insert purpose>
28
+
29
+ ## key business benefit #1
30
+ ## key business benefit #2
31
+ ## key business benefit #3
32
+
33
+ You will be able to:
34
+ * key feature #1
35
+ * key feature #2
36
+ * key feature #3
37
+ """
38
+
39
+ #--- main fast api app
40
+ app = FastAPI(
41
+ title="Prototype: Fastapi in docker",
42
+ description=description,
43
+ version="0.0.1",
44
+ terms_of_service="http://example.com/terms/",
45
+ contact={
46
+ "name": "Iain McKone",
47
+ "email": "iain.mckone@gmail.com",
48
+ },
49
+ license_info={
50
+ "name": "MIT",
51
+ "url": "http://opensource.org/licenses/MIT",
52
+ },
53
+ openapi_url="/api/v1/openapi.json"
54
+ )
55
+
56
+ io = gr.Interface(lambda x: "Hello, " + x + "!", "textbox", "textbox")
57
+ app = gr.mount_gradio_app(app, io, path="/gradio_svc")
58
+
59
+
60
+ #--- configure route handlers
61
+ #app.include_router(rteApi, prefix="/api")
62
+ #app.include_router(rteQa, prefix="/qa")
63
+
64
+
65
+ #m_kstrPath_templ = libUtils.pth_templ
66
+ #m_templRef = Jinja2Templates(directory=str(m_kstrPath_templ))
67
+
68
+
69
+ """ def get_jinja2Templ(request: Request, pdfResults, strParamTitle, lngNumRecords, blnIsTrain=False, blnIsSample=False):
70
+ lngNumRecords = min(lngNumRecords, libUtils.m_klngMaxRecords)
71
+ if (blnIsTrain): strParamTitle = strParamTitle + " - Training Data"
72
+ if (not blnIsTrain): strParamTitle = strParamTitle + " - Test Data"
73
+ if (blnIsSample): lngNumRecords = libUtils.m_klngSampleSize
74
+ strParamTitle = strParamTitle + " - max " + str(lngNumRecords) + " rows"
75
+
76
+ kstrTempl = 'templ_showDataframe.html'
77
+ jsonContext = {'request': request,
78
+ 'paramTitle': strParamTitle,
79
+ 'paramDataframe': pdfResults.sample(lngNumRecords).to_html(classes='table table-striped')
80
+ }
81
+ result = m_templRef.TemplateResponse(kstrTempl, jsonContext)
82
+ return result """
83
+
84
+
85
+ #--- get main ui/ux entry point
86
+ @app.get('/')
87
+ def index():
88
+
89
+ return {
90
+ "message": "Landing page: prototype for fastApi"
91
+ }
92
+
93
+ @app.get('/api')
94
+ def rte_api():
95
+
96
+ return {
97
+ "message": "Landing page: prototype for fastApi"
98
+ }
99
+
100
+ @app.get('/gradio')
101
+ def rte_gradio():
102
+
103
+ #return {
104
+ # "message": "Landing page: this is where gradio would start from if we could mount with fastapi"
105
+ #}
106
+ return RedirectResponse("/gradio_svc")
107
+
108
+
109
+ @app.get('/streamlit')
110
+ def rte_streamlit():
111
+
112
+ return {
113
+ "message": "Landing page: this is where streamlit would start from if we could mount with fastapi"
114
+ }
115
+
116
+ @app.get('/shiny')
117
+ def rte_shiny():
118
+
119
+ return {
120
+ "message": "Landing page: this is where shiny would start mounted with fastapi"
121
+ }
122
+
123
+ @app.get('/syscheck/{check_id}')
124
+ async def syscheck(check_id: str):
125
+ from os import system
126
+ #import commands
127
+
128
+ print("TRACE: fastapi.syscheck ... {check_id} ", check_id)
129
+
130
+ if (check_id=='apt_list'):
131
+ print("TRACE: fastapi.syscheck ... {apt_list} ")
132
+ m_sysMsg = system("apt list --installed")
133
+ return {"message: syscheck apt list complete " + str(m_sysMsg)}
134
+
135
+ elif (check_id=='sanity'):
136
+ print("TRACE: fastapi.syscheck ... {}".format(check_id))
137
+ return {"message: syscheck sanity ... ok "}
138
+
139
+ elif (check_id=='pip_list'):
140
+ print("TRACE: fastapi.syscheck ... {pip_list} ")
141
+ m_sysMsg = system("pip list")
142
+ return {"message: syscheck pip list complete " + str(m_sysMsg)}
143
+
144
+ else:
145
+ return {"message": "fastApi: syscheck {check_id} " + check_id}
146
+
147
+
148
+ @app.get('/sanity')
149
+ async def sanity():
150
+ print("TRACE: fastapi.sanity ... {}")
151
+ return {"message: syscheck sanity ... ok "}
152
+
153
+
154
+ if __name__ == '__main__':
155
+ #uvicorn.run("main:app", host="0.0.0.0", port=49132, reload=True)
156
+ #uvicorn --app-dir=./fastapi entry_fastapi:app --reload --workers 1 --host 0.0.0.0 --port 7860 & #--- specify a non-root app dir
157
+ uvicorn.run("fastapi.entry_fastapi:app", reload=True, workers=1, host="0.0.0.0", port=49132)
158
+ #CMD ["uvicorn", "main:app", "--host=0.0.0.0", "--reload"]
159
+
gradio/ABOUTME.md ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ # ABOUTME: gradio
2
+ ## Purpose: this environment is used to host a gradio UI
3
+ ## Prereq:
4
+ ## -
5
+
6
+
requirements.txt ADDED
@@ -0,0 +1,111 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ fastapi==0.111.0
3
+ gradio==4.37.2
4
+ shiny==0.10.2
5
+ streamlit==1.36.0
6
+ uvicorn==0.30.1
7
+
8
+ #--- 20240716: pip list on docker
9
+ #Package Version
10
+ #------------------------- -----------
11
+ #aiofiles 23.2.1
12
+ #altair 5.3.0
13
+ #annotated-types 0.7.0
14
+ #anyio 4.4.0
15
+ #appdirs 1.4.4
16
+ # asgiref 3.8.1
17
+ # attrs 23.2.0
18
+ # blinker 1.8.2
19
+ # cachetools 5.3.3
20
+ # certifi 2024.7.4
21
+ # charset-normalizer 3.3.2
22
+ # click 8.1.7
23
+ # contourpy 1.1.1
24
+ # cycler 0.12.1
25
+ # dnspython 2.6.1
26
+ # email_validator 2.2.0
27
+ # exceptiongroup 1.2.1
28
+ # fastapi 0.111.0
29
+ # fastapi-cli 0.0.4
30
+ # ffmpy 0.3.2
31
+ # filelock 3.15.4
32
+ # fonttools 4.53.1
33
+ # fsspec 2024.6.1
34
+ # gitdb 4.0.11
35
+ # GitPython 3.1.43
36
+ # gradio 4.37.2
37
+ # gradio_client 1.0.2
38
+ # h11 0.14.0
39
+ # htmltools 0.5.2
40
+ # httpcore 1.0.5
41
+ # httptools 0.6.1
42
+ # httpx 0.27.0
43
+ # huggingface-hub 0.23.4
44
+ # idna 3.7
45
+ # importlib_resources 6.4.0
46
+ # Jinja2 3.1.4
47
+ # jsonschema 4.23.0
48
+ # jsonschema-specifications 2023.12.1
49
+ # kiwisolver 1.4.5
50
+ # linkify-it-py 2.0.3
51
+ # markdown-it-py 3.0.0
52
+ # MarkupSafe 2.1.5
53
+ # matplotlib 3.7.5
54
+ # mdit-py-plugins 0.4.1
55
+ # mdurl 0.1.2
56
+ # numpy 1.24.4
57
+ # orjson 3.10.6
58
+ # packaging 24.1
59
+ # pandas 2.0.3
60
+ # pillow 10.4.0
61
+ # pip 23.0.1
62
+ # pkgutil_resolve_name 1.3.10
63
+ # prompt-toolkit 3.0.36
64
+ # protobuf 5.27.2
65
+ # pyarrow 16.1.0
66
+ # pydantic 2.8.2
67
+ # pydantic_core 2.20.1
68
+ # pydeck 0.9.1
69
+ # pydub 0.25.1
70
+ # Pygments 2.18.0
71
+ # pyparsing 3.1.2
72
+ # python-dateutil 2.9.0.post0
73
+ # python-dotenv 1.0.1
74
+ # python-multipart 0.0.9
75
+ # pytz 2024.1
76
+ # PyYAML 6.0.1
77
+ # questionary 2.0.1
78
+ # referencing 0.35.1
79
+ # requests 2.32.3
80
+ # rich 13.7.1
81
+ # rpds-py 0.19.0
82
+ # ruff 0.5.1
83
+ # semantic-version 2.10.0
84
+ # setuptools 57.5.0
85
+ # shellingham 1.5.4
86
+ # shiny 0.10.2
87
+ # six 1.16.0
88
+ # smmap 5.0.1
89
+ # sniffio 1.3.1
90
+ # starlette 0.37.2
91
+ # streamlit 1.36.0
92
+ # tenacity 8.5.0
93
+ # toml 0.10.2
94
+ # tomlkit 0.12.0
95
+ # toolz 0.12.1
96
+ # tornado 6.4.1
97
+ # tqdm 4.66.4
98
+ # typer 0.12.3
99
+ # typing_extensions 4.12.2
100
+ # tzdata 2024.1
101
+ # uc-micro-py 1.0.3
102
+ # ujson 5.10.0
103
+ # urllib3 2.2.2
104
+ # uvicorn 0.30.1
105
+ # uvloop 0.19.0
106
+ # watchdog 4.0.1
107
+ # watchfiles 0.22.0
108
+ # wcwidth 0.2.13
109
+ # websockets 11.0.3
110
+ # wheel 0.40.0
111
+ # zipp 3.19.2
streamlit/ABOUTME.md ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ # ABOUTME: streamlit
2
+ ## Purpose: this environment is used to host a streamlit UI
3
+ ## Prereq:
4
+ ## -
5
+
6
+
streamlit/entry_streamlit.py ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ '''
2
+ toExecute: (from root app folder) ... streamlit run entry_streamlit.py
3
+ '''
4
+ import streamlit as st
5
+ #import uix.lit_sidebar as litSideBar
6
+
7
+
8
+ #--- streamlit: specify title and logo
9
+ st.set_page_config(
10
+ page_title='Protoype - Fastapi with Streamlit Landing Page',
11
+ #page_icon='https://cdn.freebiesupply.com/logos/thumbs/1x/nvidia-logo.png',
12
+ layout="wide")
13
+ st.header('\
14
+ Protoyping Fastapi and Streamlit \
15
+ on Docker \
16
+ within Huggingface space \
17
+ ')
18
+ #st.markdown("##### Iain McKone (Deployment Lead) [LinkedIn](%s)" % "https://linkedin.com/in/iainmckone")
19
+ st.markdown('---')
20
+
21
+
22
+ #--- streamlit: add a sidebar
23
+ #litSideBar.init()