Sample Web Application in Flask, that renders a set of random articles from the public BigQuery news sample, and when reading the particular article, allows to listen an audio version.
See a high-level description of this pattern in our Developer Platform site.
The application is a Bootstrap template that implemets few front-end and back-end endpoints within the same file (app.py), and uses some external files emmulating the use of external services.
- App: Starting point, defines the Flask routes for front-end and back-end functionalities
- news_storage: Provides the news search functionality. For this case, the public BigQuery news sample is used.
- news_repo: Contains the methods that work as an interface to a Cloud Storage environment where the generated audio files are stored.
- news_tts: Methods to interact with a Text-To-Speech service. This can be easily reconfigured to use the services offered by the common Cloud providers.
Ensure all dependencies listed in the requirements.txt file, are installed. This can be quickly completed by running the following command. It is highly advisable to use a separate conda or virtualenv Python environment.
$ pip install -r requirements.txt
The following steps asume the use of Google Cloud Platform (GCP), Text-to-Speech, BigQuery and Cloud Storage. Step 1 asumes that a GCP project is created and active. For guidance about creating projects in GCP see the article Creating and Managing Projects.
This sample application uses the standard authentication method preferred for applications when using APIs. Follow the steps described in Creating a service account. If the creation process prompts to assign roles, assign the roles listed in the step 2. Then generate the service account key and save the JSON file containing the service account credentials to a path visible by the application (usually in the same machine, but for security reasons, not a subdirectory in the project).
Follow the steps in grant user roles, and ensure to assign the following ones:
- BigQuery Data Viewer
- BigQuery Job User
- Storage Object Viewer
Follow the steps described in the article Enabling an API in GCP project, and ensure Cloud Text-to-Speech API is active. Commonly, the storage service APIs are enabled by default.
The article Creating storage buckets shows how to create a storage repository to keep the generated audio files. Use a convenient name and a location near a geographic location where the code will run.
According to your operating system, create the environment variables GOOGLE_APPLICATION_CREDENTIALS and SC_AUDIONEWS_BUCKET. The first one should contain the path to the credentials JSON file downloaded in the step 1. The second variable should contain the name of the bucket where the audio files will be stored
export GOOGLE_APPLICATION_CREDENTIALS='/path/to/credentials.json' export SC_AUDIONEWS_BUCKET='my-audio-files'