Unable to improve transcription accuracy with speech adaptation boost - python

I'm using SpeechRecognition Python library to perform Speech to Text operations. I'm using the recognize_google_cloud function to use Google Cloud Speech-to-Text API.
Here is my code:
import speech_recognition as sr;
import json;
j = '';
with open('key.json', 'r') as f:
j = f.read().replace('\n', '');
js = json.loads(j);
r = sr.Recognizer();
mic = sr.Microphone();
with candide as source:
audio = r.record(source);
print(r.recognize_google_cloud(audio, language='fr-FR', preferred_phrases=['pistoles', 'disait'], credentials_json=j));
The function recognize_google_cloud send the data captured by the microphone to Google API and selects the most probable transcription of the given speech among a set of alternatives.
The parameter preferered_phrases, as explained in this page of the documentation, is used to select an other alternative that contains the listed words.
It is possible to improve these results using speech adaptation boost. As the version of the SpeechRecognition library doesn't let us to specify a boost value, I updated the speech_recognition/__init__.py file with an hard-coded boost value:
if preferred_phrases is not None:
speech_config["speechContexts"] = {"phrases": preferred_phrases, "boost": 19}
Unfortunately, when I execute my code, I get the following error:
Traceback (most recent call last):
File "/home/pierre/.local/lib/python3.8/site-packages/speech_recognition/__init__.py", line 931, in recognize_google_cloud
response = request.execute()
File "/home/pierre/.local/lib/python3.8/site-packages/googleapiclient/_helpers.py", line 134, in positional_wrapper
return wrapped(*args, **kwargs)
File "/home/pierre/.local/lib/python3.8/site-packages/googleapiclient/http.py", line 915, in execute
raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 400 when requesting https://speech.googleapis.com/v1/speech:recognize?alt=json returned "Invalid JSON payload received. Unknown name "boost" at 'config.speech_contexts': Cannot find field.". Details: "[{'#type': 'type.googleapis.com/google.rpc.BadRequest', 'fieldViolations': [{'field': 'config.speech_contexts', 'description': 'Invalid JSON payload received. Unknown name "boost" at \'config.speech_contexts\': Cannot find field.'}]}]">
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "spech_reco.py", line 23, in <module>
print(r.recognize_google_cloud(audio, language='fr-FR', preferred_phrases=['pistoles', 'disait'], credentials_json=j));
File "/home/pierre/.local/lib/python3.8/site-packages/speech_recognition/__init__.py", line 933, in recognize_google_cloud
raise RequestError(e)
speech_recognition.RequestError: <HttpError 400 when requesting https://speech.googleapis.com/v1/speech:recognize?alt=json returned "Invalid JSON payload received. Unknown name "boost" at 'config.speech_contexts': Cannot find field.". Details: "[{'#type': 'type.googleapis.com/google.rpc.BadRequest', 'fieldViolations': [{'field': 'config.speech_contexts', 'description': 'Invalid JSON payload received. Unknown name "boost" at \'config.speech_contexts\': Cannot find field.'}]}]">
Is there an error in my request?

I understand that you are modifying the speech_recognition/__init__.py file of the SpeechRecognition library in order to include the "boost" parameter in your request.
When reviewing this file I noticed that it is using the 'v1' version of the API; however, the "boost" parameter is only supported in the ‘v1p1beta1’ version
Therefore, another of the adaptations that you could make in the code is the following:
`speech_service = build ("speech","v1p1beta1", credentials = api_credentials, cache_discovery = False)`
With this modification you should no longer see the BadRequest error.
At the same time, please consider that this library is a third-party library that uses the Google Speech-to-text API internally. Therefore, if this library does not cover all your current needs, another alternative could create your own implementation directly using the Speech-to-text API Python Client library.

Related

Cannot download Excel file through Drive API -

I am trying to download an Excel file using Drive API. Here is my code:
def downloadXlsx(vars, file, creds):
try:
service = build('drive', 'v3', credentials=creds)
fileId = file['id']
fileName = file['name']
# request = service.files().get_media(fileId=fileId)
request = service.files().get_media(fileId=fileId, acknowledgeAbuse=True)
# request = service.files().get(fileId=fileId, supportsTeamDrives=True, fields='*').execute()
fh = io.BytesIO()
downloader = MediaIoBaseDownload(fh, request)
done = False
while done is False:
status, done = downloader.next_chunk()
print("Download %d%%." % int(status.progress() * 100))
fh.seek(0)
print('%s%s' % (vars.download_directory, fileName))
with open('%s%s' % (vars.download_directory, fileName), 'wb') as f:
shutil.copyfileobj(fh, f, length=131072)
except HttpError as error:
print(f'An error occurred: {error}')
Every time I run it most files return this error
An error occurred: <HttpError 403 when requesting https://www.googleapis.com/drive/v3/files/xxxxxxxxxxxxxxxxxxxx?acknowledgeAbuse=true&alt=media returned "This file has been identified as malware or spam and cannot be downloaded.". Details: "[{'domain': 'global', 'reason': 'cannotDownloadAbusiveFile', 'message': 'This file has been identified as malware or spam and cannot be downloaded.'}]">
I tried adding the acknowledgeAbuse=True flag but it doesn't change anything. Previously it would give me this error:
An error occurred: <HttpError 403 when requesting
https://www.googleapis.com/drive/v3/files/1fr7NwhToKFgvbNgExl0QMgurLJlx8KmV?acknowledgeAbuse=true&alt=media
returned "Only the owner can download abusive files.". Details:
"[{'domain': 'global', 'reason': 'cannotDownloadAbusiveFile',
'message': 'Only the owner can download abusive files.',
'locationType': 'parameter', 'location': 'acknowledgeAbuse'}]">
But I no longer get this error and I'm not sure why as I haven't changed anything.
I tried using this line:
request = service.files().get(fileId=fileId, supportsTeamDrives=True, fields='*').execute()
Which would download the file but it would be corrupted and unable to be opened.
Anyway, does anyone have a clue how I can get around this? Maybe a different method I could try or a way to get the .get() to download the file properly? I don't know why it's saying I'm not the owner - if anyone has knowledge on how Drive API determines 'who' is executing the API that would be helpful.
Edit:
I'm looking at the files.get method documentation here and it reads the following:
By default, this responds with a Files resource in the response body.
If you provide the URL parameter alt=media, then the response includes
the file contents in the response body. Downloading content with
alt=media only works if the file is stored in Drive. To download
Google Docs, Sheets, and Slides use files.export instead. For further
information on downloading files, refer to Download files
Seems like I need to specify alt=media somehow but not sure if that is possible in my situation. Maybe that's referring to get_media?
Fixed! It was a bug with Google Drive API. https://issuetracker.google.com/issues/238551542
There is an optional parm that you can send with your file.get request
acknowledgeAbuse boolean Whether the user is acknowledging the risk of downloading known malware or other abusive files. This is only applicable when alt=media. (Default: false)
try
request = service.files().get(fileId=fileId, supportsTeamDrives=True, fields='*', acknowledgeAbuse='true').execute()

I am getting an error when using the facebook api

File "SAMPLE_CODE.py", line 21, in <module>
from facebookads.adobjects.adaccount import AdAccount
File "/usr/local/lib/python3.7/site-packages/facebookads/adobjects/adaccount.py", line 1582
def get_insights(self, fields=None, params=None, async=False, batch=None, pending=False):
^
SyntaxError: invalid syntax
I am trying to use the Facebook API and I am getting this error. Not sure what to do about it. This is my first time setting up and trying to use this API. Any help would be appreciated
Here is the file I am trying to run
"""python
# Copyright 2014 Facebook, Inc.
# You are hereby granted a non-exclusive, worldwide, royalty-free license to
# use, copy, modify, and distribute this software in source code or binary
# form for use in connection with the web services and APIs provided by
# Facebook.
# As with any software that integrates with the Facebook platform, your use
# of this software is subject to the Facebook Developer Principles and
# Policies [http://developers.facebook.com/policy/]. This copyright notice
# shall be included in all copies or substantial portions of the software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
from facebookads.adobjects.adaccount import AdAccount
from facebookads.adobjects.adsinsights import AdsInsights
from facebookads.api import FacebookAdsApi
access_token = 'token'
ad_account_id = 'id'
app_secret = 'secret'
app_id = 'id'
FacebookAdsApi.init(access_token=access_token)
fields = [
'reach',
'delivery',
'cost_per_result',
'cpp',
'cpm',
'cost_per_action_type:page_engagement',
'purchase_roas:omni_purchase',
'website_purchase_roas:offsite_conversion_fb_pixel_purchase',
'mobile_app_purchase_roas:app_custom_event_fb_mobile_purchase',
'campaign_group_name',
]
params = {
'level': 'ad',
'filtering': [{'field':'delivery_info','operator':'IN','value':['active']}],
'breakdowns': ['days_1'],
'time_range': {'since':'2019-11-14','until':'2019-11-21'},
}
print(AdAccount(ad_account_id).get_insights(
fields=fields,
params=params,
))
"""
Here is another stack overflow question where async= False is the error too
Problem with connect facebookads library for extract data from Facebook with Marketing API using Python
Could someone explain what is going on here ?
Thanks
I downloaded version 3.6 of Python using pyenv and Homebrew and tried to run the file again but I got a new error code.
"""
Kiefer's Macbook Pro:facebook-python-business-sdk kiefergallant$ python3.6 SAMPLE_CODE.py
/Users/kiefergallant/.pyenv/versions/3.6.0/lib/python3.6/site-packages/facebookads/utils/api_utils.py:30: UserWarning: value of breakdowns might not be compatible. Expect list<breakdowns_enum>; got <class 'list'>
warnings.warn(message)
/Users/kiefergallant/.pyenv/versions/3.6.0/lib/python3.6/site-packages/facebookads/utils/api_utils.py:30: UserWarning: insights does not allow field delivery
warnings.warn(message)
/Users/kiefergallant/.pyenv/versions/3.6.0/lib/python3.6/site-packages/facebookads/utils/api_utils.py:30: UserWarning: insights does not allow field cost_per_result
warnings.warn(message)
/Users/kiefergallant/.pyenv/versions/3.6.0/lib/python3.6/site-packages/facebookads/utils/api_utils.py:30: UserWarning: insights does not allow field cost_per_action_type:page_engagement
warnings.warn(message)
/Users/kiefergallant/.pyenv/versions/3.6.0/lib/python3.6/site-packages/facebookads/utils/api_utils.py:30: UserWarning: insights does not allow field purchase_roas:omni_purchase
warnings.warn(message)
/Users/kiefergallant/.pyenv/versions/3.6.0/lib/python3.6/site-packages/facebookads/utils/api_utils.py:30: UserWarning: insights does not allow field website_purchase_roas:offsite_conversion_fb_pixel_purchase
warnings.warn(message)
/Users/kiefergallant/.pyenv/versions/3.6.0/lib/python3.6/site-packages/facebookads/utils/api_utils.py:30: UserWarning: insights does not allow field mobile_app_purchase_roas:app_custom_event_fb_mobile_purchase
warnings.warn(message)
/Users/kiefergallant/.pyenv/versions/3.6.0/lib/python3.6/site-packages/facebookads/utils/api_utils.py:30: UserWarning: insights does not allow field campaign_group_name
warnings.warn(message)
Traceback (most recent call last):
File "SAMPLE_CODE.py", line 51, in <module>
params=params
File "/Users/kiefergallant/.pyenv/versions/3.6.0/lib/python3.6/site-packages/facebookads/adobjects/adaccount.py", line 1639, in get_insights
return request.execute()
File "/Users/kiefergallant/.pyenv/versions/3.6.0/lib/python3.6/site-packages/facebookads/api.py", line 664, in execute
cursor.load_next_page()
File "/Users/kiefergallant/.pyenv/versions/3.6.0/lib/python3.6/site-packages/facebookads/api.py", line 851, in load_next_page
params=self.params,
File "/Users/kiefergallant/.pyenv/versions/3.6.0/lib/python3.6/site-packages/facebookads/api.py", line 332, in call
raise fb_response.error()
facebookads.exceptions.FacebookRequestError:
Message: Call was not successful
Method: GET
Path: https://graph.facebook.com/v2.11/act_423073558387349/insights
Params: {'level': 'ad', 'filtering': '[{"field":"delivery_info","operator":"IN","value":["active"]}]', 'breakdowns': '["days_1"]', 'time_range': '{"since":"2019-11-14","until":"2019-11-21"}', 'fields': 'reach,delivery,cost_per_result,cpp,cpm,cost_per_action_type:page_engagement,purchase_roas:omni_purchase,website_purchase_roas:offsite_conversion_fb_pixel_purchase,mobile_app_purchase_roas:app_custom_event_fb_mobile_purchase,campaign_group_name'}
Status: 400
Response:
{
"error": {
"message": "(#2635) You are calling a deprecated version of the Ads API. Please update to the latest version: v5.0.",
"type": "OAuthException",
"code": 2635,
"fbtrace_id": "AASs5AEHjZ0VOZrOfeNziJy"
}
}
"""
Taken from this Question:
The version of your API and Python are not compatible.
The error is caused by the parameter called async from get_insights(), which since Python 3.7 isn't allowed anymore since async is now a reserved keyword of python.
Solution: Use an older Python version (<3.7) or use a version of the API that is
compatible with Python 3.7.
Try facebook_business instead of facebookads. See this.

Python: how to login on Youtube

So, I'm a bit confused on how I get past authentication on Youtube using Python and successfully login. I always get error 403 when I try to PragmaticLogin():
yt_service = gdata.youtube.service.YouTubeService()
service.developer_key = 'MY Key'
service.client_id='My ID'
service.email = 'myemail#yahoo.gr'
service.password = 'mypassword'
service.source = 'my_program'
service.ProgrammaticLogin()
What do I have to do?
Update:
I think that it has to do with authentication. Do I need both developer_key and client_id? Where do I get each? I want to have rights to add comments to my videos etc.
Full error:
Traceback (most recent call last):
File "/home/bodhi32/Documents/bot.py", line 9, in <module>
client.ClientLogin(USERNAME, PASSWORD)
File "/usr/lib/pymodules/python2.7/gdata/service.py", line 833, in ClientLogin
self.ProgrammaticLogin(captcha_token, captcha_response)
File "/usr/lib/pymodules/python2.7/gdata/service.py", line 796, in ProgrammaticLogin
raise Error, 'Server responded with a 403 code'
gdata.service.Error: Server responded with a 403 code
ClientLogin is deprecated and has all sorts of errors. Don't use it.
Use OAuth2.
This sample should get you started:
https://github.com/youtube/api-samples/blob/master/python/my_uploads.py
Use your code but make sure you fill the developer_key and client_id fields ( check below how to get them).
yt_service = gdata.youtube.service.YouTubeService()
service.developer_key = 'MY Key'
service.client_id='My ID'
service.email = 'myemail#yahoo.gr'
service.password = 'mypassword'
service.source = 'my_program'
service.ProgrammaticLogin()
To obtain a youtube api go to
https://cloud.google.com/console/project and create a new project, then enable youtube.
Check this video for more info Obtaining a simple API key for use with the YouTube API

Python Youtube API: Attempt to access Watch Later results in invalid URI

BELIEVED SOLVED: Python API only supports v1, while watch later was added in v2. SOURCE
SOLUTION: Use "Experimental" API v3
I am attempting to use the Youtube API to access my Watch Later playlist. Below is the code I am using.
import gdata.youtube
import gdata.youtube.service
yt_service = gdata.youtube.service.YouTubeService()
yt_service.ssl = True
yt_service.developer_key = 'REDACTED'
yt_service.email = 'REDACTED'
yt_service.password = 'REDACTED'
yt_service.ProgrammaticLogin()
playlist_uri = 'https://gdata.youtube.com/feeds/api/users/default/watch_later?v=2'
playlist_video_feed = yt_service.GetYouTubePlaylistVideoFeed(uri=playlist_uri)
for playlist_video_entry in playlist_video_feed.entry:
print playlist_video_entry.title.text
I am receiving the following error.
Traceback (most recent call last):
File "Youtube.py", line 21, in <module>
playlist_video_feed = yt_service.GetYouTubePlaylistVideoFeed(uri=playlist_uri)
File "/Library/Python/2.6/site-packages/gdata/youtube/service.py", line 393, in GetYouTubePlaylistVideoFeed
uri, converter=gdata.youtube.YouTubePlaylistVideoFeedFromString)
File "/Library/Python/2.6/site-packages/gdata/service.py", line 1108, in Get
'reason': server_response.reason, 'body': result_body}
gdata.service.RequestError: {'status': 400, 'body': 'Invalid request URI', 'reason': 'Bad Request'}
It would seem the URI https://gdata.youtube.com/feeds/api/users/default/watch_later?v=2 is invalid. However this is the one stated to be used in the google documents. Am I using it wrong, or is there another issue here?
In addition if I change the URI to http://gdata.youtube.com/feeds/api/playlists/63F0C78739B09958 it works as expected.
You should check your authentication. According to Retrieving and updating a user's 'Watch Later' playlist:
Again, the link will only be present in a profile entry if either of
the following conditions is true:
You submit an authenticated request to retrieve the logged-in user's
own profile.
The watch_later playlist is publicly available for the user whose
profile you are retrieving.
The API server will return a 40x HTTP response code if you try to
retrieve a watch_later playlist and neither of the above conditions is
true.
The second link would work most likely due to the second publicly available condition being met. One thing I do notice missing from your example is the client id/source:
# A complete client login request
yt_service.email = 'jo#gmail.com'
yt_service.password = 'mypassword'
yt_service.source = 'my-example-application'
yt_service.developer_key = 'ABC123...'
yt_service.client_id = 'my-example-application'
yt_service.ProgrammaticLogin()
You should look into that and ensure that your authentication is happening properly.

ValidationError while running Google adwords client library examples

I get the following error when I try to run sample example of Google adwords
[root#some v200909]# python get_related_keywords.py Traceback (most recent call last): File "get_related_keywords.py", line 53, in
page = targeting_idea_service.Get(selector)[0] File "../../aw_api/TargetingIdeaService.py", line 105, in Get
'TargetingIdea', self.__loc, request) File "../../aw_api/WebService.py", line 350, in CallMethod
raise ValidationError(error['data']) aw_api.Errors.ValidationError: Invalid headers for 'https://adwords-sandbox.google.com', see http://code.google.com/apis/adwords/docs/developer/adwords_api_sandbox.html#requestheaders. [root#some v200909]#
This sounds like an issue with the headers you're providing. The headers must be especially formatted for the sandbox, so make sure that:
a) You're formatting the headers as specified in http://code.google.com/apis/adwords/docs/developer/adwords_api_sandbox.html#requestheaders , as Goose Bumper mentioned. This applies to both v2009 and v13, as you still need to format the developer token and client email according to the instructions (the application token is now obsolete).
b) You're choosing the right endpoint, namely adwords-sandbox.google.com for v2009 and sandbox.google.com for v13
If this still doesn't work for you, the SOAP logs for your request might be useful.

Categories