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.


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

查看所有标签

猜你喜欢:

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

互联网金融手册

互联网金融手册

谢平、邹传伟、刘海二 / 中国人民大学出版社 / 2014-4-1 / 68.00元

本书作者在2012年4月7日“金融四十人年会”上首次公开提出了“互联网金融”概念。在短短两年中,互联网金融已经成为中国金融界和IT界最热门的词汇之一,相关创业活动也非常活跃。本书是作者两年来深入研究、思考的结晶,畅想了金融与IT结合的未来图景,将理论与实践高度融合,与读者分享了许多深具洞察力的观点。本书力图规范互联网金融的定义,完善互联网金融的理论体系,分析互联网金融目前的六种主要类型——金融互联......一起来看看 《互联网金融手册》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具

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

HEX CMYK 互转工具