Source: Deep Learning on Medium
You are a hobbyist Machine Learning developer. You come across tons of exciting news related to artificial intelligence. You followed online tutorials and built something cool. Next, you want to show your creation to the world.
If you have been in this situation, you know there are very less number of options available to you. But this might change in future. Because Google recently announced changes to their cloud platform.
Cloud Run is one of the most exciting addition to their platform. In this article, we will deploy an open source pre-trained deep learning model on Cloud Run.
If you do not have an active Google Cloud account, you can sign up here. If you are a new user, you will get 300$ free credits for one year. Once ready you can start Cloud Shell.
Into The Code
If you want to deploy to Cloud Run, you can skip this section. Otherwise you can stick here and see the code in action.
index.html contains user interface code.
model.py, I have implemented the logic to extend text using GPT-2 model.
And finally create a flask server to serve requests (in
You can test it by first installing following requirements:
- Pytorch (CPU version will work just fine)
Once ready, you can start server by:
Next we need to build Docker image for our project. So that we can deploy it to Cloud Run. I have created following
Dockerfile which uses
python:3 as base image, installs dependencies and runs our server.
You can build docker image locally on your system and then push it to Google Cloud. But if you have slow internet, it can take quite some time. So for this step, we will use Cloud Shell.
You will need your project ID for following steps. To see your project ID in Cloud Shell, use the following command:
gcloud config list — format ‘value(core.project)’ 2>/dev/null
Follow these steps to setup docker image: (make sure to replace [PROJECT_ID] with your cloud project ID.)
- Clone Repository:
git clone https://github.com/NaxAlpha/gpt-2xy
- Go to Source Directory:
- Build Docker Image
docker build -t gcr.io/[PROJECT_ID]/gpt-2xy .
- Configure Docker for Google Cloud:
gcloud auth configure-docker
- Push image:
docker push gcr.io/[PROJECT_ID]/gpt-2xy
- And you are all set!
Deploying to Cloud Run
From top left icon, go to Cloud Run page
Then click on create service:
If you skipped building your own docker you can use
gcr.io/innate-harbor-198712/gpt-2xy in Container image URL field. Also Allow unauthenticated invocations. And then click on Show Optional Settings.
Next change Memory to 2GB and other settings as following:
Click Create and it will take some time, once done you are ready to go.
You can also use domain/subdomain mapping. I have deployed my own version here: https://gpt2.ai-demo.xyz/ and here is a sample:
I hope you enjoyed this tutorial. Let me know if I have any feedback related to my article. Also stay tuned for future tutorials.