Cloud Run: Google Cloud Text to Speech API

栏目: IT技术 · 发布时间: 4年前

内容简介: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

Cloud Run: Google Cloud Text to Speech API

Mar 20 ·5min read

Cloud Run: Google Cloud Text to Speech API

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.

Cloud Run: Google Cloud Text to Speech API

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 .

Cloud Run: Google Cloud Text to Speech API

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.

Cloud Run: Google Cloud Text to Speech API

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 .

Cloud Run: Google Cloud Text to Speech API

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.

Cloud Run: Google Cloud Text to Speech API

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.

Cloud Run: Google Cloud Text to Speech API

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.

Cloud Run: Google Cloud Text to Speech API

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.


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

研究之美

研究之美

[美] Donald E. Knuth / 高博 / 电子工业出版社 / 2012-1-1 / 49.00元

《研究之美》是计算机科学大师、“算法分析之父”高德纳(Donald E.Knuth)在20世纪70年代旅居挪威时撰写的适用于计算机科学的一种全新基础数学结构的情景小品。全书以一对追求自由精神生活的青年男女为主人公,展开了一段对于该种全新结构的发现和构造的对白。在此过程中,本书充分展示了计算机科学的从业人员进行全新领域探索时所必备的怀疑、立论、构造、证明、归纳、演绎等逻辑推理和深入反思的能力。《研究......一起来看看 《研究之美》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具