Harness AI advanced capabilities in a Pythonic way. The DotNet version of this repository is here.
A valid subscription is needed for your own endpoints. The project uses virtual environments, continuous integration pipelines, and containerization as optional. This repository is automatically deployed to container registries (Docker Hub and ACR) through GitHub Actions workflows.
Cognitive services used (so far):
- Neural Machine Translation (NMT): Translator.
- Convolutional Neural Networks (CNN): Computer Vision, Face.
Technologies used: Python, Flask, Pytest, unittest, Selenium, Swagger, GSAP, Docker, Minikube.
- Installation
- Azure Portal
- Deployment
- Pushing the images to container registries
- Container registries and GitHub Actions workflow
- Resources
Clone (or fork) this git repository:
git clone https://github.com/antazo/ai-python.git
cd ai-pythonIf we want to use a virtual environment, we need to invoke the venv module, and activate it. In my case, I'm calling it my_env:
On Linux/macOS:
# sudo apt-get install -y python3-pip python3-venv # Libraries needed
python3 -m venv my_venv
source ./my_venv/bin/activateOn Windows:
py -3 -m venv my_venv
.\.my_venv\Scripts\activateThis project is using:
pip install flask flask-swagger-ui python-dotenv requests azure-mgmt-compute[!Web testing] Selenium is not included in the workflow, run it manually.
Optionally, we can automate this process by adding the list of modules to requirements.txt. To create one with the current modules installed in our environment:
pip freeze >> requirements.txtCreate this file for later use in our Dockerfile or CI pipeline. It should look something like this:
flask
flask-swagger-ui
python-dotenv
requests
azure-mgmt-compute
To install them:
pip install -r requirements.txtThis application uses a valid subscription to Azure AI Services and Cognitive APIs to be able to use Language, Vision (Computer Vision, Custom Vision, Face), Decision, Speech, Metrics Advisor, and Document Intelligence.
Create a single resource for all of them, your Keys and Endpoints information must be stored in a .env file.
Note that the Translator service uses its own endpoint. Save your key-values like this:
LOCATION=
AI_SERVICES_KEY=
AI_SERVICES_ENDPOINT=
TRANSLATOR_ENDPOINT=https://api.cognitive.microsofttranslator.com/
Set the environment variables.
On Linux/macOS:
export FLASK_ENV=developmentOn Windows:
set FLASK_ENV=developmentRun app.py:
py app.pyor:
flask run -p 80This should run the web application on localhost:
To containerize this application, build the Docker image by using the Dockerfile included:
docker build -t ai-python-app .Run the image:
docker run -it -p 80:80 ai-python-app
# -it lets you stop it with Ctrl+CStart K8s and login your Docker:
minikube start --driver=docker
docker loginPoint the shell to Minikube's docker-daemon before building the Docker image:
eval $(minikube -p minikube docker-env)If "eval" isn't working, you must be using CMD on Windows. In that case, do it manually:.
& minikube -p minikube docker-env --shell powershell | Invoke-ExpressionBuild the Docker image inside Minikube. Don't forget to login your Docker first:
docker build -t ai-python-app .Apply the Deployment and Service YAML files. Inside deployment.yaml you should point to your Docker image ("image: [DOCKER_USERNAME]/ai-python-app:latest"):
kubectl apply -f static/deployment.yaml # Edit this file before applying!
kubectl apply -f static/service.yamlAccess the Flask application:
minikube service ai-flask-app-serviceTroubleshoot:
kubectl get pods # Get the list of pods
kubectl describe pod ai-flask-app -n default
kubectl get services # Get the list of services
kubectl describe service ai-flask-app-serviceTag the Docker image (don't forget to replace [DOCKER_USERNAME]!):
docker tag ai-python-app:latest [DOCKER_USERNAME]/ai-python-app:latestLog in to your Docker Hub:
docker loginPush the Docker image:
docker push [DOCKER_USERNAME]/ai-python-app:latestYou can use the image from my Docker Hub for integration tests (the keys are not valid for E2E tests): https://hub.docker.com/repository/docker/antazo/ai-python-app/general
This repository integrates ACR in the GitHub Actions workflow. All the following steps are already automated in the ci-acr.yml file:
Log in to your Azure CLI:
az loginCreate your ACR in your own resource group (replace [AZURE_RESOURCE_GROUP]). My registry will be called aipython:
az acr create --resource-group [AZURE_RESOURCE_GROUP] --name aipython --sku BasicLogin to the ACR we just created:
az acr login --name aipythonShow the ACR login server, and then use it to create a tag:
az acr show --name aipython --query loginServer --output tableIn my case, it's aipython.azurecr.io:
docker tag ai-python-app aipython.azurecr.io/ai-python-app:v1Push the image to the ACR (this step is not needed in the YAML file):
docker push aipython.azurecr.io/ai-python-app:v1List the repository, and the tags:
az acr repository list --name aipython --output table
# is it "ai-python-app"?
az acr repository show-tags --name aipython --repository ai-python-app --output table
# is it "v1"?Create the container. You will also need to check your Access Keys and privileges to be able to replace [ACR_PASSWORD]. The DNS label will be aidemo:
az container create \
--resource-group [AZURE_RESOURCE_GROUP] \
--name ai-python-app \
--image aipython.azurecr.io/ai-python-app:v1 \
--cpu 1 \
--memory 1 \
--registry-login-server aipython.azurecr.io \
--registry-username aipython \
--registry-password [ACR_PASSWORD] \
--ip-address Public \
--dns-name-label aidemo \
--ports 80Try it:
http://aidemo.northeurope.azurecontainer.io/
All you need to do is add your Docker credentials to secrets:
- DOCKER_USERNAME
- DOCKER_PASSWORD
Now, enable the workflow.
First, add these information to your secrets:
- AZURE_RESOURCE_GROUP: Name of your Azure resource group.
- ACR_USERNAME: The registry admin username.
- ACR_PASSWORD: The registry password.
Create a role assignment (RBAC) to be able to pull and push as a Contributor (in other cases we would use acrpull or acrpush for least privileges):
az ad sp create-for-rbac \
--name "http://ai-python-sp" \
--role Contributor \
--scopes /subscriptions/[AZURE_SUBSCRIPTION_ID]/resourceGroups/[AZURE_RESOURCE_GROUP]/providers/Microsoft.ContainerRegistry/registries/aipython \
--sdk-auth > acr-credentials.jsonThis will output a JSON to the file acr-credentials.json with the rest of information needed for your secrets. Add them to make the workflow work:
- AZURE_CLIENT_ID: The client ID of your Azure service principal.
- AZURE_CLIENT_SECRET: The client secret of your Azure service principal.
- AZURE_CREDENTIALS: Azure service principal credentials in JSON format (acr-credentials.json).
Getting started with Azure Functions
Create an Azure storage account
Create a single database - Azure SQL Database
Use the Azure portal to create a virtual network
Create a Windows virtual machine in the Azure portal
Host a web application with Azure App Service
Deploy and run a containerized web app with Azure App Service
Create a container image for deployment to Azure Container Instances
Deploy a container instance in Azure using the Azure portal
Azure Portal
Azure AI Studio
Vision Studio
Language Studio
Document Intelligence Studio
Sample documents
OneDrive
Lab Environment Setup
Azure AI Vision documentation
Create computer vision solutions with Azure AI Vision
Create Translator service
Lab files for Azure AI Vision modules
Lab 01 - Machine Learning: Explore Automated Machine Learning in Azure Machine Learning
Lab 02 - Content Safety: Explore Azure AI Services
Lab 11 - Explore an Azure AI Search index (UI)
Lab 12 - Explore Microsoft Copilot in Microsoft Edge
Explore the components and tools of the Azure AI Studio
Explore space with Python and Visual Studio Code; inspired by Netflix's Over the Moon