An example of how a machine learning model can be served from a REST API using Azure App Service, a PaaS for hosting web applications.
scripts/
contains the Python scripts used in themain.py
filestatic/
contains the Jupyter notebook, the data used for our model, and the model itselfmain.py
contains the logic for our API
local deployment
- Create a Python environment for this project (e.g.
python3 -m venv azure-app-service
). - Activate your new environment and install the dependencies listed in
requirements.txt
. If using PIP, runpip install
from this project's root directory. - Run
uvicorn main:app
from your projects root directory. - Navigate to
http://localhost:8000
from a browser.
from the Azure Portal
- Fork the repo to your own github account.
- From the Azure portal, look up "App Services" on the search bar and navigate to the service.
- Click "create"
- Fill out the first page.
- "Runtime stack" should be "Python 3.10"
- "Pricing plan" must be "Basic" or better for Github deployment (this is not a free pricing plan. if you don't want to incur charges, remember to delete resources).
- On the "Deployment" page, connect to the forked repository on your Github account.
- Create the app service.
- Once created, go to the resource and navigate to the "Configuration" setting.
- Under the "General Settings" tab, copy and paste
gunicorn main:app --workers 4 --worker-class uvicorn.workers.UvicornWorker --bind 0.0.0.0:8000
into startup command. - Go back to the "Overview" section and restart the App Service.
- Go to the "Overview" tab and click on the link under
Default domain
.
Making a GET request to your Default domain
or navigtaing to it from the browser should return the page seen in the image above.
A POST request to {your_default_domain}/predict?color={some_color}&shape={some_shape}
makes use of our ML model, and returns a response with our target variable happiness_score
.
- If you get a 429 error, try recreating the App Service in a different region.
- You should not have to specify a port. It takes a while for the app to deploy so give it 5-10 mins.