22

Cloud Run: Google Cloud Text to Speech API

 4 years ago
source link: https://towardsdatascience.com/cloud-run-google-cloud-text-to-speech-api-dff308665c70?gi=b8084d6ede61
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Serving Intelligent APIs with Google Cloud Run

ryYjQzy.png!web

Mar 20 ·5min read

nMrUBfi.png!web

Google Text-to-Speech API served on Cloud Run .

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.

ZRZzA3r.png!web

Fig 1: Google Cloud Text-to-Speech API.

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 .

Q73Qbav.png!web

Fig 2: Create a Service Account for Text-to-Speech API.

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.

JNBbQnB.png!web

Fig 3: Application code in the Python file app.py.

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 .

jUjaauB.png!web

Fig 4: Upload Service Account key.

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.

FZZRV3n.png!web

Fig 5: Dockerfile to build the image for the application container.

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.

niaURnM.png!web

Fig 6: Building the image with Cloud Build.

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.

IFzEJf3.png!web

Fig 7: Text-to-Speech Application hosted on Cloud Run.

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.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK