In this tutorial we will setup three containers:
- api-service
- frontend-simple
- frontend-react
- Have Docker installed
- Have VSCode or editor of choice
In this tutorial we will setup containers to run python code for creating APIs and a container to run HTML web server.
- Clone or download from here
It is important to note that we do not want any secure information in Git. So we will manage these files outside of the git folder. At the same level as the mushroom-app-v1
folder create a folder called secrets
Your folder structure should look like this:
|-mushroom-app-v1
|-images
|-src
|---api-service
|---frontend-simple
|-secrets
|-persistent-folder
We will create a basic backend container to run our REST API. The FastAPI framework will be used for this.
The following tasks is what we will implement:
- Open a terminal and go to the location where
mushroom-app-v2/api-service
- Run
sh docker-shell.sh
ordocker-shell.bat
for windows
- Review
Dockerfile
forEXPOSE 9000
- Review
docker-shell.sh
for-p 9000:9000
option - Review
docker-shell.sh
for-e DEV=1
option - Review
docker-entrypoint.sh
for dev mode vs production mode
- To run development API service run
uvicorn_server
from the docker shell - What is the command
uvicorn_server
? - Test the API service by going to
http://localhost:9000/
We need to download model experiments data from GCS bucket
- Open
api/service.py
in your editor - Review the service.py file
- Uncomment the following line:
# Start the tracker service
asyncio.create_task(tracker_service.track())
- Save changes and wait for the experiments to be downloaded
- Do we need to restart the API service?
- Review
api/tracker.py
- Once experiments are downloaded,
tracker.py
generates a file calledleaderboard.csv
- Review
leaderboard.csv
opening the file using your host OS - If we shutdown the
api-service
container can we still accessleaderboard.csv
?
- Open
api/service.py
- Uncomment the following line:
@app.get("/experiments")
def experiments_fetch():
# Fetch experiments
df = pd.read_csv("/persistent/experiments/experiments.csv")
df["id"] = df.index
df = df.fillna("")
return df.to_dict("records")
-
Test the API by going to
http://localhost:9000/experiments
-
Also uncomment the apis
@app.get("/best_model")
and@app.post("/predict")
to expose other APIs
Fast API gives us an interactive API documentation and exploration tool for free.
- Go to
http://localhost:9000/docs
- You can test APIs from this tool
We will build a simple frontend app that uses basic HTML & Javascript. We will consume the REST APIs exposed by the api service container
The following tasks is what we will implement:
- Open a terminal and go to the location where
mushroom-app-v2/frontend-simple
- Run
sh docker-shell.sh
ordocker-shell.bat
for windows
- To run development web server run
http-server
from the docker shell - Go to
http://localhost:8080/experiments.html
- If your API service is running, the page should show data from all mushroom model experiments
- Open
experiments.html
- Review HTML & Javascript code on how APIs are called
Now we have completed the following:
- We read some data from a csv file in the persistent folder.
- Converted the data to list of dictionary objects in Python
- Exposed the results as a REST API using FastAPI
- Called the API to get the experiments data
- Displayed the data in a HTML table
Here we will implement model serving using two different techniques:
- Self hosting our model in FastAPI
- Utilizing a deployed model on Vertex AI
- Go to
http://localhost:8080/
- Upload an image of a mushroom and view results
- Open
index.html
- Review HTML & Javascript code on how model serving is called
- Review
service.py
and inspect the endpoint/predict
- Review
model.py
Here we will use the React frontend framework to build a robust mushroom app. The app will have multiple components and navigation.
- Open a terminal and go to the location where
mushroom-app-v2/frontend-react
- Run
sh docker-shell.sh
ordocker-shell.bat
for windows
- Run
yarn start
from the docker shell - Go to
http://localhost:3000
- Run
docker container ls
- Stop any container that is running
- Run
docker system prune
- Run
docker image ls