Google speech long_running_recognizer does not work when fired through celery task - python-3.x

The code below works when fired as a script using python. But the same, when i encapsulate in a celery task and try to execute, it does not work. The celery task prints the line before the long_running_recognize, but does not print the one after the operation - seems like it gets stuck at the long_running_recognize call when executing as a celery task.
#!/usr/bin/env python3
import speech_recognition as sr
import json
import sqlalchemy
import io
import os
# Imports the Google Cloud client library
from google.cloud import speech
from google.cloud.speech import enums
from google.cloud.speech import types
from sqlalchemy import create_engine
# Instantiates a client
client = speech.SpeechClient()
audio=speech.types.RecognitionAudio(uri='gs://<bucket_name>/<audio_file>')
config = types.RecognitionConfig(
encoding=enums.RecognitionConfig.AudioEncoding.LINEAR16,
language_code='en-US')
print('FIRING GOOGLE SPEECH RECOGNITION')
# Detects speech in the audio file
operation = client.long_running_recognize(config, audio)
print('Waiting for operation to complete...')
response = operation.result(timeout=600)
outfile = open('test.txt', 'w')
for result in response.results:
for alternative in result.alternatives:
print('=' * 20)
outfile.write('Transcript: {}'.format(alternative.transcript))
outfile.write('=' * 20)
outfile.write("Confidence: {}".format(alternative.confidence))
print('Transcript: {}'.format(alternative.transcript))
print(alternative.confidence)
outfile.close()

I had the same issue today, but it worked recently. I rolled back to these requirements in pip and it solved the problem.
google-api-python-client==1.6.4
google-auth==1.1.1
google-cloud-core==0.27.1
google-cloud-speech==0.29.0
google-gax==0.15.15
googleapis-common-protos==1.5.2

Related

Is there a way to use the ffmpeg binary/unix executable in a py2app application to run ffmpeg on computers without it installed?

I wrote a small python script that is essentially just a text to speech script. It uses the pydub - audiosegment python library to convert the mp3 from gTTS to an ogg that can be played in pygame. A link to my github repository can be found here: https://github.com/AnupPlays/TTS
this is the main function:
def webscrape():
global x
global b
b.state(['disabled'])
src = "sound.mp3"
dst = "sound.ogg"
murl = str(url.get())
response = requests.get(murl)
response.raise_for_status()
parse = bs4.BeautifulSoup(response.text, 'html.parser')
x = str(parse.get_text())
print(x)
text = gTTS(x)
text.save("sound.mp3")
AudioSegment.from_mp3(src).export(dst, format='ogg')
b.state(['!disabled'])
this is a list of my imports:
#Imports
import os
import sys
import pygame
#google text to speech
from gtts import gTTS
#requests and BeautifulSoup
import requests
import bs4
#pygame audio player
from pygame import mixer
#tkinter ui
from tkinter import *
from tkinter import ttk
from tkinter import filedialog
from tkinter import messagebox
#mp3 -> wav
from os import path
from pydub import AudioSegment
For anyone wondering using homebrew you can get the dependencies for this and copy those dependencies into your packager.

Cloud Run connect to Cloud SQL module error Python

I keep getting an error trying to connect Cloud Run and I keep getting the following error. Any idea?
__import__("pg8000") ModuleNotFoundError: No module named 'pg8000'
import pandas as pd
import sqlalchemy
import datetime
import requests
from urllib.parse import urlencode
import warnings
from flask import Flask
import os
import google
db_user = os.environ.get("DB_USER")
db_pass = os.environ.get("DB_PASS")
db_name = os.environ.get("DB_NAME")
cloud_sql_connection_name = os.environ.get("CLOUD_SQL_CONNECTION_NAME")
db = sqlalchemy.create_engine(
# Equivalent URL:
# postgres+pg8000://<db_user>:<db_pass>#/<db_name>?unix_sock=/cloudsql/<cloud_sql_instance_name>/.s.PGSQL.5432
sqlalchemy.engine.url.URL(
drivername='postgres+psycopg2',
username=db_user,
password=db_pass,
database=db_name,
query={
'unix_sock': '/cloudsql/{}/.s.PGSQL.5432'.format(
cloud_sql_connection_name)
}
),
# ... Specify additional properties here.
# ...
)
You need to install one of the supported database drivers.
If you want to use postgres+pg8000, you need to install the pg8000 package, otherwise based on your example, you actually need to install psycopg2.

Why can't ipdb be imported when using multiprocessing

I am not trying to use ipdb with multiprocessing, I had it imported before I started adding multiprocessing features and I couldn't figure out why the code wouldn't run. Here is a minimal example;
from ipdb import set_trace as st
import multiprocessing
def worker(instructions):
return "good boi"
pool = multiprocessing.Pool(4)
results = [pool.apply(worker, args=("woof", )) for _ in range(3)]
pool.close()
If you comment out the first line it runs, otherwise it prints a cryptic error message about failing to pickle worker. I don't need ipdb, but why does this happen?

Using Prometheus with Connexion - ValueError: Duplicated timeseries in CollectorRegistry

I get the following error message when using prometheus with connexion using python3.6/3.7:
ValueError: Duplicated timeseries in CollectorRegistry: {'app_request_processing_seconds_sum', 'app_request_processing_seconds_count', 'app_request_processing_seconds_created', 'app_request_processing_seconds'}
#!/usr/bin/env python3
from gevent import monkey # noqa
# monkey.patch_all() # noqa
import json
import os
import connexion
import datetime
import logging
from connexion import NoContent
from prometheus_client import Summary, Counter
logger = logging.getLogger(__name__)
REQUEST_TIME = Summary('app_request_processing_seconds', 'time spent processing')
REQUEST_COUNTER = Counter('app_request_count', 'number of requests')
#REQUEST_TIME.time()
def get_health():
try:
'Hello'
except Exception:
return connexion.problem(503, "Service Unavailable", "Unhealthy")
else:
return "Healthy"
logging.basicConfig(level=logging.INFO)
app = connexion.App(__name__)
app.add_api("swagger.yaml")
if __name__ == "__main__":
# run our standalone gevent server
app.run(port=8080, server="gevent")
There is a swagger.yaml that is identical to:
https://github.com/hjacobs/connexion-example-redis-kubernetes/blob/master/swagger.yaml
Any help would be great
As a guess, you have named your file app.py. What happens it that when the swagger is loaded, the handling is specified as app.get_health:
paths:
/health:
get:
operationId: app.get_health
And it loads (a second time) app.py to import the get_health() function.
The reason it that the main file is loaded as __main__ module and thus get loaded a second time; see this other question for more information.
Therefore, you end-up with defining your Prometheus metrics twice which doesn't sit well with the collector.
The most simple solution is to rename your file and implement your application in another file named app.py.

JPype error: import jpype ModuleNotFoundError: No module named 'jpype'

I installed JPype in a correct way and anything is fine and my instalation was succeed but when i run my refactor.py from command prompt i have error that i pointed in title.
i hope you can help me for solving this problem.
also i have to point that i am beginner in python3.
here is my code:
import urllib.request
import os
import tempfile
import sys
import fileinput
import logging
import jpype
logging.basicConfig(filename="ERROR.txt", level= logging.ERROR)
try:
logging.debug('we are in the main try loop')
jpype.startJVM("C:/Users/user/AppData/Local/Programs/Python/Python36/ClassWithTest.java", "-ea")
test_class = jpype.JClass("ClassWithTest")
a = testAll()
file_java_class = open("OUTPUT.txt", "w")
file_java_class.write(a)
except Exception as e1:
logging.error(str(e1))
jpype.shutdownJVM()
startJVM() function takes the path to the JVM which is like this - C:\\Program Files\\Java\\jdk-10.0.2\\bin\\server\\jvm.dll. You could use the getDefaultJVMPath() function to get the JVM path on your PC. So you can just start the JVM this way:
startJVM(getDefaultJVMPath(), "-ea")
Hope that helps!

Resources