内容简介:Google Cloud Run became Generally-Available (GA) around November of 2019. It provides a fully managed serverless execution platform to abstract infrastructure for stateless code deployment with HTTP-driven containers. Cloud Run is a Knative service utilizi
Serving Intelligent APIs with Google Cloud Run
Mar 20 ·5min read
On Google Cloud Run
Google Cloud Run became Generally-Available (GA) around November of 2019. It provides a fully managed serverless execution platform to abstract infrastructure for stateless code deployment with HTTP-driven containers. Cloud Run is a Knative service utilizing the same APIs and runtime environments that make it possible to build container-based applications that can run anywhere, whether on Google cloud or Anthos deployed on-premises or on the Cloud. As a “serverless execution environment”, Cloud Run can scale in response to the computing needs of the running application. Instant execution of application code, scalability and portability are core features of Cloud Run.
On Google Cloud Text-to-Speech API
Google Cloud Text-to-Speech exposes an API to synthesize natural-sounding speech from text. The text and its corresponding speech output may be different languages, including English, French, Russian and Arabic, to mention just a few. This state-of-the-art APIs opens multiple opportunities for integrating lifelike high-fidelity text-to-speech interactions into end-user applications.
Service Account for Authentication
To use the Google Cloud Text-to-Speech API, we have to create a service account key for authentication. Service accounts authenticate an application or a virtual machine (VM) to make authorized API calls on Google Cloud Platform.
Use the following link to create a service account for the Cloud Text-to-Speech API. -> Service Account for Cloud Text-to-Speech .
Use the following parameters:
- Service account: New service account.
- Service account name:[Assign a name].
- Role:No role is required.
Click Create without role
and download the JSON file containing the key to your computer.
After creating and downloading your secure service account key, set the environment variable GOOGLE_APPLICATION_CREDENTIALS
to the path of the JSON file that contains your service account key (see Fig 3).
The Application Code
The code for this project is hosted on a Github repository with the link https://github.com/dvdbisong/text-to-speech-cloud-run
. The Python file app.py
contains the logic for accessing the containerized application via HTTP. The code is in the text-speech
directory and can be viewed on Google Code Editor (a component of Cloud Shell)
. The code is commented and easy to understand.
Upload the Service Account Key to the Application
Upload the Service Account Key to authenticate the application making a call to the Text-to-Speech API. After uploading, place the key within the folder text-speech
.
The Dockerfile
A Dockerfile outlines the recipe for creating a container image. This projects’ Dockerfile is based on the official Python image. The Dockerfile specifies the relevant packages that will be installed on the container when the image is built. The Flask application uses the gunicorn
package to expose the port for accessing the application within the container. On Google Code Editor,
navigate to text-speech
to view the Dockerfile.
In the interest of completeness, the table below summarizes the commands used in the Dockerfile.
+------------+-----------------------------------------------------+
| Command | Description |
+------------+-----------------------------------------------------+
| FROM | The base Docker image for the Dockerfile. |
| RUN | It executes commands on top of the current image as | | new layers. |
| COPY | Copies files from the local machine to the |
| container filesystem. |
| CMD | Specifies the command to execute when running the | | container. This command is overridden if another | | command is specified at runtime. |
| WORKDIR | Sets the working directory of the container. |
| ENV | Set Environment variable as a key-value pair that | | will be available in the container after building. |
+------------+-----------------------------------------------------+
Build the Image with Cloud Build and upload to Container Registry
Cloud Build is a GCP service for automating the deployment of code artifacts as part of a CI/CD pipeline. Here, Cloud Build is used to build the Docker image as a series of build steps, and the completed image is pushed to Google Container Registry.
The following code builds the image with Cloud Build and uploads the image to Container Register. Run this code from the directory text-speech
.
gcloud builds submit — tag gcr.io/ekabasandbox/ebisong-text-to-speech
where,
- ekabasandbox
is the GCP Project ID, and
- ebisong-text-to-speech
is the image name.
Be sure to modify these parameters as needed.
Deploy the Container with Cloud Run
To deploy the container stored on Google Container Registry to Cloud Run , execute the code:
gcloud run deploy --image gcr.io/ekabasandbox/ebisong-text-to-speech --platform managed
Select the region to deploy the managed Cloud Run compute (e.g. us-central1). When prompted for the service name, press Enter
to accept the default and also respond y
(yes) when asked to allow unauthenticated invocations.
When the container is deployed to Cloud Run, click the URL to view the running application.
https://ebisong-text-to-speech-4fa5lgaxpq-uw.a.run.app/
You may go ahead and explore the application.
Conclusion
The article provides an example for deploying applications on Cloud Run that makes API calls. It provides sample codes for using Google Cloud Text-to-Speech APIs and running a simple web application with Flask that is packaged as a container and deployed on Cloud Run. The entire application is open-source at https://github.com/dvdbisong/text-to-speech-cloud-run . Finally, be sure to delete the Cloud Run service, and images in Cloud Container Registry when they are no longer needed.
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。