This repository contains complete code for running Machine Learning (ML) models purpose and getting document policy from a website.
Following Components are used for each purposes.
- Twitter API key: GET Twitter data using Twitter API
- Cloud Storage: Store ML models
- Cloud Functions:
- Run ML models
- GET document policy from a website
- API Gateway: Manage all the API calls to serverless backends (in this case Cloud Functions)
This is how the architecture look a like.
Architecture: Serverless API with API Gateway for Running Machine Learning Models
Architecture: Serverless API with API Gateway for Getting Policies On Website
These following steps guide you how to run ML models using Cloud Functions.
- Create a bucket for storing ML models
- Upload all the models and tokenizers to the bucket. Remember the filename, or you can rename it after the upload has done. It would be used by Cloud Functions to call the files.
In this case, we have 3 models:
- GET policy trending list
The model and tokenizer could be found here
- GET sentiment result of tweets based on positive and negative responses
The model and tokenizer could be found here
- GET sentiment result to detect buzzer
The model could be found here
- Go to Cloud Function on GCP
- Click CREATE FUNCTION
- Name the function on Function name
- Choose Region as asia-southeast2 or any region you wish
- Choose HTTP as Trigger type
- Copy the URL provided for later purpose
- For Authentication, select Require authentication, click SAVE
- Click RUNTIME, BUILD AND CONNECTIONS SETTINGS
- In RUNTIME tab, for Memory allocated you could select 1 GiB, for Timeout you could write 120
- Left the others as default until you reach Runtime environment variables
- For Name, use all variables using for using Twitter API: consumer_key, consumer_secret, access_token, access_secret and fill the Value met by the Twitter API key you got.
- Click Next
- Select Python 3.9 as the Runtime and fill the Entry point as policy_sentiment
- Use this python code for main.py and define the dependencies on Requirements.txt. The full code could be accessed here
- Click DEPLOY to deploy the function and wait for the deployment succeed
- Go to Cloud Function on GCP
- Click CREATE FUNCTION
- Name the function on Function name
- Choose Region as asia-southeast2 or any region you wish
- Choose HTTP as Trigger type
- Copy the URL provided for later purpose
- For Authentication, select Require authentication, click SAVE
- Click RUNTIME, BUILD AND CONNECTIONS SETTINGS
- In RUNTIME tab, for Memory allocated you could select 1 GiB, for Timeout you could write 120
- Left the others as default until you reach Runtime environment variables
- For Name, use all variables using for using Twitter API: consumer_key, consumer_secret, access_token, access_secret and fill the Value met by the Twitter API key you got.
- Click Next
- Select Python 3.9 as the Runtime and fill the Entry point as offensive_sentiment
- Use this python code for main.py and define the dependencies on Requirements.txt. The full code could be accessed here
- Click DEPLOY to deploy the function and wait for the deployment succeed
- Go to Cloud Function on GCP
- Click CREATE FUNCTION
- Name the function on Function name
- Choose Region as asia-southeast2 or any region you wish
- Choose HTTP as Trigger type
- Copy the URL provided for later purpose
- For Authentication, select Require authentication, click SAVE
- Click RUNTIME, BUILD AND CONNECTIONS SETTINGS
- In RUNTIME tab, for Memory allocated you could select 1 GiB, for Timeout you could write 120
- Left the others as default until you reach Runtime environment variables
- For Name, use all variables using for using Twitter API: consumer_key, consumer_secret, access_token, access_secret and fill the Value met by the Twitter API key you got.
- Click Next
- Select Python 3.9 as the Runtime and fill the Entry point as offensive_sentiment
- Use this python code for main.py and define the dependencies on Requirements.txt. The full code could be accessed here
- Click DEPLOY to deploy the function and wait for the deployment succeed
We did scraping from https://peraturan.go.id/ to get the document policies. The GET method then run on Cloud Function. There are 4 functions that should be deployed:
- GetAllPolicyCategory
- GetPolicyByCategory
- GetPolicyByType
- GetDocumentPolicy
Deploy this in 4 separate functions based on codes provided here The steps below applied to 4 functions.
- Go to Cloud Function on GCP
- Click CREATE FUNCTION
- Name the function on Function name
- Choose Region as asia-southeast2 or any region you wish
- Choose HTTP as Trigger type
- Copy the URL provided for later purpose
- For Authentication, select Require authentication, click SAVE
- Click Next
- Select Python 3.9 as the Runtime and fill the Entry point consecutively as getAllPolicyCategory, getPolicyByCategory, getPolicyByType, and getDocumentPolicy
- Fill the main.py and define the dependencies on Requirements.txt based on link above
- Click DEPLOY to deploy the function and wait for the deployment succeed
We use Swagger to define the rules with 2 different yaml files:
- For the sentiment analysis purpose. The file could be found on openapi-sentiment.yaml
- For the document policy purpose. The file could be found on openapi-definition.yaml
Use different filenames for both to differ them on the next steps.
Two APIs are created here: one for sentiment analysis purpose and the other one for the document policy purpose. Follow these steps below to create API using Cloud Shell.
- Enable API for Cloud Build, Cloud Function, and three required services.
gcloud services enable apigateway.googleapis.com
gcloud services enable servicemanagement.googleapis.com
gcloud services enable servicecontrol.googleapis.com
- Set environment for these three.
If you haven't had the service account email, create it on APIs & Services --> Credentials. Click CREATE CREDENTIALS --> Service account. Name your service account. In this case, I use backend-auth-service as the service account name. Click CREATE AND CONTINUE, select Owner as the role, and then DONE.
## specify the correct path for the yaml file, remove the {}
export API_DEFINITION="/{path}/openapi-sentiment.yaml"
export PROJECT_ID="YOUR_PROJECT_ID"
export SERVICE_ACCOUNT_EMAIL="backend-auth-service@YOUR_PROJECT_ID.iam.gserviceaccount.com"
- Create API
## replace your-sentiment-api-name with the name you wish
gcloud beta api-gateway apis create your-sentiment-api-name --project=$PROJECT_ID
- Create API config using the yaml file we've created before
## replace your-sentiment-config with the config name you want
gcloud beta api-gateway api-configs create your-sentiment-config \
--api=your-sentiment-api-name --openapi-spec=$API_DEFINITION \
--project=$PROJECT_ID --backend-auth-service-account=$SERVICE_ACCOUNT_EMAIL
- Enable API
gcloud beta api-gateway apis describe your-sentiment-api-name --project=$PROJECT_ID
- After enabled successfully, enable the API name from managedService field.
gcloud services enable your-sentiment-api-name-HASH.apigateway.PROJECT_ID.cloud.goog
- Set environment for these three.
## specify the correct path for the yaml file, remove the {}
export API_DEFINITION="/{path}/openapi-definition.yaml"
export PROJECT_ID="YOUR_PROJECT_ID"
export SERVICE_ACCOUNT_EMAIL="backend-auth-service@YOUR_PROJECT_ID.iam.gserviceaccount.com"
- Create API
## replace your-policy-api-name with the name you wish
gcloud beta api-gateway apis create your-policy-api-name --project=$PROJECT_ID
- Create API config using the yaml file we've created before
## replace your-policy-config with the config name you want
gcloud beta api-gateway api-configs create your-policy-config \
--api=your-policy-api-name --openapi-spec=$API_DEFINITION \
--project=$PROJECT_ID --backend-auth-service-account=$SERVICE_ACCOUNT_EMAIL
- Enable API
gcloud beta api-gateway apis describe your-policy-api-name --project=$PROJECT_ID
- After enabled successfully, enable the API name from managedService field.
gcloud services enable your-policy-api-name-HASH.apigateway.PROJECT_ID.cloud.goog
For the sentiment analysis purpose, we're gonna create 3 gateways to avoid crowded traffic and timeout.
- Gateway to GET Trending list
For the region, API Gateway supports only 11 GCP regions for deployment.
## you could change the trending-gateway as the gateway name you wish
gcloud beta api-gateway gateways create trending-gateway \
--api=your-sentiment-api-name --api-config=your-sentiment-config \
--location=asia-east1 --project=$PROJECT_ID
gcloud beta api-gateway gateways describe trending-gateway \
--location=us-central1 --project=$PROJECT_ID
- Gateway to GET Sentiment Result of Tweets
## you could change the offensive-gateway as the gateway name you wish
gcloud beta api-gateway gateways create offensive-gateway \
--api=your-sentiment-api-name --api-config=your-sentiment-config \
--location=us-central1 --project=$PROJECT_ID
gcloud beta api-gateway gateways describe offensive-gateway \
--location=us-central1 --project=$PROJECT_ID
- Gateway to GET Sentiment Result of Buzzer Detection
## you could change the buzzer-gateway as the gateway name you wish
gcloud beta api-gateway gateways create buzzer-gateway \
--api=your-sentiment-api-name --api-config=your-sentiment-config \
--location=us-central1 --project=$PROJECT_ID
gcloud beta api-gateway gateways describe buzzer-gateway \
--location=us-central1 --project=$PROJECT_ID
- Gateway to GET Policies
## you could change the policy-gateway as the gateway name you wish
gcloud beta api-gateway gateways create policy-gateway \
--api=your-policy-api-name --api-config=your-policy-config \
--location=asia-east1 --project=$PROJECT_ID
gcloud beta api-gateway gateways describe policy-gateway \
--location=asia-east1 --project=$PROJECT_ID
- Use the defaultHostname as the gateway link.
The gateway would look like this: trending-gateway-{HASH}.uc.gateway.dev/Trending
or if using query: buzzer-gateway-{HASH}.uc.gateway.dev/Buzzer?trending={input the trending here}
This is to give authorization to API gateway so that that it will be able to access resources in services. Do this for total 7 functions you've built before by differ them on the function name.
## Replace BuzzerDetection with each your functions deployed
gcloud functions add-iam-policy-binding BuzzerDetection \
--region asia-southeast2 \
--member "serviceAccount:$SERVICE_ACCOUNT_EMAIL" \
--role "roles/cloudfunctions.invoker" \
--project $PROJECT_ID
Ex: if you made change in openapi-sentiment.yaml, then you should redeploy the config and the gateway linked to the config.
gcloud beta api-gateway api-configs create your-new-sentiment-config \
--api=your-sentiment-api-name --openapi-spec="/{path}/openapi-sentiment.yaml" \
--project=$PROJECT_ID --backend-auth-service-account=$SERVICE_ACCOUNT_EMAIL
gcloud beta api-gateway gateways update buzzer-gateway \
--api=your-sentiment-api-name --api-config=your-new-sentiment-config \
--location=us-central1 --project=$PROJECT_ID