I am following the Google Cloud API Text-to-Speech Python tutorial. I would like to know if there is a way to return the phonemes and their duration, an intermediate step in generating the interpreted speech. Is that possible? If so, can you please refer me to the documentation and hopefully some sample code that does this. I searched and could not find anything that already answered my question.
Thanks!
gma
Mentioning all the steps to get phonemes from Google cloud API Text-to-Speech. In Part-3, you can find the sample code.
Here are the steps you can follow:
[Part-1]
In the Google Cloud Console, on the project selector page, select or create a Google Cloud project.
Make sure that billing is enabled for your Cloud project
Enable the Cloud Text-to-Speech API.
Create a service account:
a. In the Cloud Console, go to the Create service account page.
b. Select a project.
c. In the Service account name field, enter a name. The Cloud Console fills in
the Service account ID field based on this name.
d. Click Done to finish creating the service account.
Do not close your browser window. You will use it in the next step.
Create a service account key:
a. In the Cloud Console, click the email address for the service account
that you created.
b. Click Keys.
c. Click Add key, then click Create new key.
d. Click Create. A JSON key file is downloaded to your computer.
e. Click Close.
Set the environment variable GOOGLE_APPLICATION_CREDENTIALS to the path of the JSON file that contains your service account key. This variable only applies to your current shell session, so if you open a new session, set the variable again.
Example 1. Linux or macOS
export GOOGLE_APPLICATION_CREDENTIALS="KEY_PATH"
Replace KEY_PATH with the path of the JSON file that contains your service account key.
For example:-
export GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/service-account-file.json"
Example 2. Windows
For powershell:
$env:GOOGLE_APPLICATION_CREDENTIALS="KEY_PATH"
Replace KEY_PATH with the path of the JSON file that contains your service
account key.
For example:
$env:GOOGLE_APPLICATION_CREDENTIALS="C:\Users\username\Downloads\service-account-file.json"
For command promt:
set GOOGLE_APPLICATION_CREDENTIALS=KEY_PATH
Replace KEY_PATH with the path of the JSON file that contains your service
account key.
Install and initialize the cloud SDK.
[Part-2]
Install the client library
pip install --upgrade google-cloud-texttospeech
[Part-3]
Create audio data
Now you can use Text-to-Speech to create an audio file of synthetic human speech. Use the following code to send a synthesize request to the Text-to-Speech API.
from google.cloud import texttospeech
client = texttospeech.TextToSpeechClient()
synthesis_input = texttospeech.SynthesisInput(text="Hello, World!")
voice = texttospeech.VoiceSelectionParams(
language_code="en-US", ssml_gender=texttospeech.SsmlVoiceGender.NEUTRAL
)
audio_config = texttospeech.AudioConfig(
audio_encoding=texttospeech.AudioEncoding.MP3
)
response = client.synthesize_speech(
input=synthesis_input, voice=voice, audio_config=audio_config
)
with open("output.mp3", "wb") as out:
out.write(response.audio_content)
print('Audio content written to file "output.mp3"')
If you face any issue, please refer to the link below:
https://cloud.google.com/text-to-speech/docs/quickstart-client-libraries#client-libraries-install-python
Thanks for your reply #Akshansha.
I know how to create an audio file of synthetic human speech. My question was more about how to get metadata like phoneme or viseme. For exemple, with the Amazon Polly API you can get this kind of data when using Text-to-Speech :
{"time":0,"type":"sentence","start":0,"end":23,"value":"Mary had a little lamb."}
{"time":6,"type":"word","start":0,"end":4,"value":"Mary"}
{"time":6,"type":"viseme","value":"p"}
{"time":73,"type":"viseme","value":"E"}
{"time":180,"type":"viseme","value":"r"}
{"time":292,"type":"viseme","value":"i"}
{"time":373,"type":"word","start":5,"end":8,"value":"had"}
{"time":373,"type":"viseme","value":"k"}
{"time":460,"type":"viseme","value":"a"}
{"time":521,"type":"viseme","value":"t"}
{"time":604,"type":"word","start":9,"end":10,"value":"a"}
{"time":604,"type":"viseme","value":"#"}
{"time":643,"type":"word","start":11,"end":17,"value":"little"}
{"time":643,"type":"viseme","value":"t"}
{"time":739,"type":"viseme","value":"i"}
{"time":769,"type":"viseme","value":"t"}
{"time":799,"type":"viseme","value":"t"}
{"time":882,"type":"word","start":18,"end":22,"value":"lamb"}
{"time":882,"type":"viseme","value":"t"}
{"time":964,"type":"viseme","value":"a"}
{"time":1082,"type":"viseme","value":"p"}
I was asking if we can have a similar result with the Google Cloud API TTS ?
Thanks,
gma
Related
I'm currently working on a private script that allows for data warehouse ETL work to run. I'm saving my env secrets on a cloud vault. The problem comes when a script runs and pulls information from a private google sheet, this requires a token generated from a project on G Cloud, however, it appears the token has a limited lifespan, is there a way to get a perma-token that does not expire? (Without publishing the project)
Thanks
The issue is that you haven't published the project, it will expire every 7 days.
To solve the issue you will need to publish the project (you can leave it as private) by following the steps:
1. Access to console.cloud.google.com
2. Make sure that you have selected the correct project.
3. Click the 3 lines for the menu.
4. Search for "Marketplace."
5. At the top, in the search bar, type "Google Workspace Marketplace SDK," and press enter.
6. On the Google Workspace Marketplace SDK page, click Enable.
7. Create the store listing
Basically I am looking for updating the google sheet from my linux/unix box using shell scripts. Since I couldnt find much with shell scripts , thought of using python to update the google sheet.
Most answers were redirecting to create google project for its api key / OAuth key. I could not create a project in google api / generate a api key as i dont have access to create one at organization level / no organization level. Its asking for location to choose and I dont find any.
Is there any other way to update a google sheet with wget or sudo or CURL or any other method in python ?
You actually don't need an organization to create an API Key, when you click on "New Project" in the Google Cloud Platform you can leave it blank.
https://i.stack.imgur.com/m7C2T.png
Then you have to use the api key in your python code to be able to access your sheets.
I have full edit access to a Google Sheet not owned by me. I want to be able to write to the spreadsheet using Python without Google API authorization. I checked several available packages (gdata, gspread etc.) and seems all of them ask for the credentials.
I was also able to read the content of a spreadsheet without authorization using requests or pd.read_csv() by pandas (I tweaked the URL by changing the last part saying ...edit#gid=... to ...export?format=csv&gid=...). Yet, when sending a POST request to the same URL I received 200 status code but the same old empty spreadsheet.
Any help is highly appreciated.
I believe your goal as follows.
You want to put the values to the publicly shared Google Spreadsheet using python.
In this case, you want to access to the Spreadsheet without authorization.
For this, how about this answer?
Issue and workaround:
In order to put the values to the publicly shared Google Spreadsheet, the POST method is used. In this case, it is required to use the access token. On the other hand, in the case of the GET method, when the Sheets API is used, an API key can be used. And the endpoints like exportLinks, you can retrieve the values without the API key. These are the specification of Google side.
Under this condition, in order to achieve your goal, I would like to propose the following 2 patterns.
Pattern 1:
In this pattern, I would like to propose to access to the Spreadsheet using the access token retrieved from the service account. In this case, the script can be simpler.
Sample script:
import gspread
from oauth2client.service_account import ServiceAccountCredentials
spreadsheetId = "###" # Please set the Spreadsheet ID.
scope = ['https://www.googleapis.com/auth/spreadsheets']
credentials = ServiceAccountCredentials.from_json_keyfile_name('credentials.json', scope)
client = gspread.authorize(credentials)
spreadsheet = client.open_by_key(spreadsheetId)
worksheet = spreadsheet.sheet1
worksheet.update_acell('A1', 'sample')
In this sample script, sample is put to the cell "A1" of the 1st tab in the publicly shared Spreadsheet using gspread.
Pattern 2:
In this pattern, I would like to propose to access to the Spreadsheet using the Web Apps created by Google Apps Script as the wrapper API. In this case, the python script is more simpler.
Usage:
Please do the following flow.
1. Create new project of Google Apps Script.
Sample script of Web Apps is a Google Apps Script. So please create a project of Google Apps Script.
If you want to directly create it, please access to https://script.new/. In this case, if you are not logged in Google, the log in screen is opened. So please log in to Google. By this, the script editor of Google Apps Script is opened.
2. Prepare script.
Please copy and paste the following script (Google Apps Script) to the script editor. And please enable Google Sheets API at Advanced Google services. This script is for the Web Apps.
function doPost(e) {
try {
const spreadsheetId = e.parameter.spreadsheetId;
const obj = JSON.parse(e.postData.contents);
const resource = obj.body;
const range = obj.arguments.range;
const valueInputOption = obj.arguments.valueInputOption;
Sheets.Spreadsheets.Values.update(resource, spreadsheetId, range, {valueInputOption: valueInputOption});
return ContentService.createTextOutput("ok");
} catch(e) {
return ContentService.createTextOutput(JSON.stringify(e));
}
}
In this case, the POST method is used.
In this sample script, as a test script, the values are put to the Spreadsheet using the method of spreadsheets.values.update in Sheets API.
3. Deploy Web Apps.
On the script editor, Open a dialog box by "Publish" -> "Deploy as web app".
Select "Me" for "Execute the app as:".
By this, the script is run as the owner.
Select "Anyone, even anonymous" for "Who has access to the app:".
In this case, no access token is required to be request. I think that I recommend this setting for your goal.
Of course, you can also use the access token. At that time, please set this to "Anyone".
Click "Deploy" button as new "Project version".
Automatically open a dialog box of "Authorization required".
Click "Review Permissions".
Select own account.
Click "Advanced" at "This app isn't verified".
Click "Go to ### project name ###(unsafe)"
Click "Allow" button.
Click "OK".
Copy the URL of Web Apps. It's like https://script.google.com/macros/s/###/exec.
When you modified the Google Apps Script, please redeploy as new version. By this, the modified script is reflected to Web Apps. Please be careful this.
4. Run the function using Web Apps.
This is a sample python script for requesting Web Apps. Please set your Web Apps URL, Spreadsheet ID and range.
import json
import requests
spreadsheet_id = '###' # Please set the Spreadsheet ID.
body = {
"arguments": {"range": "Sheet1!A1", "valueInputOption": "USER_ENTERED"},
"body": {"values": [["sample"]]}
}
url = 'https://script.google.com/macros/s/###/exec?spreadsheetId=' + spreadsheet_id
res = requests.post(url, json.dumps(body), headers={'Content-Type': 'application/json'})
print(res.text)
In this sample script, sample is put to the cell "A1" of the 1st tab in the publicly shared Spreadsheet.
In this case, no authorization is required in the python script, because it has already been done when Web Apps is deployed.
Note:
When you modified the script of Web Apps, please redeploy the Web Apps as new version. By this, the latest script is reflected to the Web Apps. Please be careful this.
References:
Web Apps
Taking advantage of Web Apps with Google Apps Script
Advanced Google services
publish a Google Spreadsheet through Google Apps Scripts
spreadsheets.values.update
I have a google sheet which I set so that anyone can edit.
I noticed that in the gspread package, you can get a spreadsheet from the link alone. Example:
sht1 = gc.open_by_key('1wj9L7Hn779GKP0s4MblkU1wHcqaVcG_E2YKAo1vdof0')
Since anyone can edit, providing an auth code for access is not necessary. However, it seems that you always need to initialize it using an auth code.
Is there a way to edit a google sheet without any auth code?
This may get confused as a duplicate of this post Using gspread to read from a Google Drive Spreadsheet without logging in however, the solutions are to either have a client provide their auth code, or download the file directly, but no option to edit the file.
You want to edit Google Spreadsheet without logging in the Google for retrieving the authorization code.
You want to achieve this using gspread with python.
You have already been able to get and put values for Google Spreadsheet with Sheets API.
If my understanding is correct, how about this workaround? Please think of this as just one of several workarounds.
Workaround:
In this workaround, the service account is used. When the service account is used, you can get and put values for Google Spreadsheet without logging in Google account using the gspread.
In order to access to Google Spreadsheet with the service account, please do the following flow.
Create the service account and download the JSON file for using the service account.
Share the email address of the service account to the Google Spreadsheet you want to use.
You can see the email address at the downloaded JSON file.
Sample script:
The sample script for using the script of sht1 = gc.open_by_key('1wj9L7Hn779GKP0s4MblkU1wHcqaVcG_E2YKAo1vdof0') with the service account is as follows.
import gspread
from oauth2client.service_account import ServiceAccountCredentials
scope = ['https://www.googleapis.com/auth/spreadsheets']
credentials = ServiceAccountCredentials.from_json_keyfile_name('### downloaded JSON file ###', scope)
gc = gspread.authorize(credentials)
sht1 = gc.open_by_key('1wj9L7Hn779GKP0s4MblkU1wHcqaVcG_E2YKAo1vdof0')
Please set ### downloaded JSON file ###.
References:
Using OAuth 2.0 for Server to Server Applications
Create a G Suite service account
Unfortunately you can't what you want without authentication. Is a mandatory thing.
In the end if you take a look at Gspread you could see that is actually using the Google Sheets API. And through the API you need to authenticate even when accessing (like in your case) public data.
There are other ways to authenticate with the API but sincerely I don't know if they are supported by Gspread (I believe that they are not).
So you could use some workaround proposed in the question that you mentioned.
I'd like to use the Dropbox API (with access only to my own account) to generate a link to SomeFile.xlsx that I can put in an email to multiple Dropbox account holders, all of whom are presumed to have access to the file. I'd like for the same link, when clicked on, to talk to Dropbox to figure out where SomeFile.xlsx is on their local filesystem and open that up directly.
In other words, I do NOT want to link to the cloud copy of the file. I want to link to the clicker's locally-synced version of the file.
Does Dropbox have that service and does the API let me consume it? I haven't been able to discover the answer from the documentation yet.
No, Dropbox doesn't have an API like this.