I want to use google.cloud library on my Google App Engine python application. In my local all my tests work since I installed this library on my local. I was expecting it t
Try this: sudo pip install --upgrade google-cloud-speech
Or:
I'm using another library,
Does this method help you up?
import speech_recognition as sp
import time
print("Say something!")
while True:
rec = sp.Recognizer()
with sp.Microphone() as mic:
audio = rec.listen(mic)
try:
print(rec.recognize_google(audio))
except sp.UnknownValueError:
print("I cannot understand what you said")
time.sleep(0.5)
print("Say again")
except sp.RequestError as e:
print("Error".format(e))
word = rec.recognize_google(audio)
if word == 'goodbye':
break
Installation:
sudo pip install SpeechRecognition
sudo pip install pyaudio
If you found an error:
sudo apt-get install python-pyaudio
sudo apt-get install libjack-jackd2-dev portaudio19-dev
Then again:
sudo pip install pyaudio
If you found error try this:
sudo pip install --upgrade pyaudio
As already answered in this other Stack Overflow post about using Client Libraries in App Engine Standard, GAE Standard does not support Google Client Libraries, so you can either go with App Engine Flexible, a Compute Engine instance or work with the REST API (which also has a Python library that works in App Engine Standard).
If you specifically need to use the google.cloud
library, you will have to use App Engine Flexible and not Standard, but in case you would rather work with the Standard environment, below I share the code for a sample App Engine Standard application using Cloud Speech API through the Google API Python Client Library. This library is not built-in for GAE Standard, so you will have to vendor it as if it was a third-party library. To do so, you will have to create the lib
folder in your local application's directory, and the requirements.txt
file I share below, and then install this library with the command pip install -t lib/ -r requirements.txt
.
Files needed to run this sample GAE Application:
requirements.txt
google-api-python-client
appengine_config.py
from google.appengine.ext import vendor
# Add any libraries install in the "lib" folder.
vendor.add('lib')
app.yaml
runtime: python27
api_version: 1
threadsafe: True
handlers:
- url: /.*
script: main.app
main.py
import webapp2
from apiclient.discovery import build
from oauth2client.client import GoogleCredentials
class MainPage(webapp2.RequestHandler):
# Presentation page
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
self.response.write('This is a sample App Engine Standard application working with Cloud Speech API! :)\n\nGo to /speechAPI to transcribe your audio file (you will need to upload it to one of your Cloud Storage buckets)!')
class Recognize(webapp2.RequestHandler):
# Working with Python API Client Library (NOT NEW CLIENT LIBRARIES)
def get(self):
# Add credentials
credentials = GoogleCredentials.get_application_default()
service = build('speech', 'v1', credentials=credentials)
# Methods available in: https://developers.google.com/resources/api-libraries/documentation/speech/v1/python/latest/index.html
collection = service.speech()
# Build the data structure JSON-like
data = {}
data['audio'] = {}
data['audio']['uri'] = 'gs://<BUCKET>/<FOLDER>/<FILE>'
data['config'] = {}
data['config']['encoding'] = '<ENCODING>'
data['config']['languageCode'] = '<LANGUAGE_CODE>'
data['config']['sampleRateHertz'] = <SAMPLE_RATE>
# Build the request and execute it
request = collection.recognize(body=data)
res = request.execute()
# Webapp2 Response
self.response.headers['Content-Type'] = 'text/plain'
self.response.write(res)
app = webapp2.WSGIApplication([
('/', MainPage),
('/speechAPI', Recognize),
], debug=True)
This application also works in the Local Development Server, so you can test it with the command dev_appserver.py app.yaml
before deploying it to App Engine.