Stop google speech to text streaming recognize client in python - python

I use google speech to text API to transcript voice to text.
I am trying to terminate this service when we hit the is_final=True
requests = (types.StreamingRecognizeRequest(audio_content=message.chunk)
for message in messages)
responses = client.streaming_recognize(streaming_config, requests)
I have tried: responses.cancel() but it is coming up with error
I found that in Java there is this method available to terminate streaming recognition service:
SpeechClient speech;
speech.close();
But I can not find the same method in Python client library. Can somebody guide me how to terminate streaming service properly in Python?

Like Leferis S already mentioned. If you want to stop a StreamingRecognizeRequest after receiving is_final=True you have to set single_utterance=True inside your StreamingRecognitionConfig.
An extended solution would be to parse the responses. Here (line 147-160) is an official example, where you can see how to exit the streaming process if a special word was finally recognized.

Related

How to read pub sub alerts using python in cloud functions and read the json

I am trying to read the alerts from pub/sub topics message which is integrated with the security center in the GCP console using the Cloud function with python code. Is there any way to read the alerts resource type and check its status and make changes to that resource using python?
Since I have tried searching all docs I didn't get the proper doc on this part. Can anyone guide me on this?
import base64
def hello_pubsub(event, context):
pubsub_message = base64.b64decode(event['data']).decode('utf-8')
print(pubsub_message)

Azure Speech SDK Speech to text from stream using python

I am trying to send the stream from UI to python API as stream. I need python Azure Speech logic to convert the speech to text. I am not sure about how to use pull/pusha audio input stream for speech to text
In my case I receive an audio stream from some other source. When the connection with my application is made (upon reception of the first package), a PushAudioInputStream is started. This stream pushes the data to SDK for each package that is received. The speech recognition with push stream is thus used in this case. See snippet of code below. This has worked for my case.
if newConnection:
stream = speechsdk.audio.PushAudioInputStream()
speech_recognition_with_push_stream(stream)
stream_data = base64.b64decode(data)
stream.write(stream_data)
There is a sample for using cognitive services speech sdk.
Specifically, for using it with pull stream, you may refer to: speech_recognition_with_pull_stream() , and for using it with push stream, you may refer to: speech_recognition_with_push_stream().
Hope it helps.

Google Cloud Pub/Sub Python SDK retrieve single message at a time

Problem: My use case is I want to receive messages from Google Cloud Pub/Sub - one message at a time using the Python Api. All the current examples mention using Async/callback option for pulling the messages from a Pub/Sub subscription. The problem with that approach is I need to keep the thread alive.
Is it possible to just receive 1 message and close the connection i.e. is there a feature where I can just set a parameter (something like a max_messages) to 1 so that once it receives 1 message the thread terminates?
The documentation here doesn't list anything for Python Synchronous pull which seem to have num_of_messages option for other languages like Java.
See the following example in this link:
from google.cloud import pubsub_v1
client = pubsub_v1.SubscriberClient()
subscription = client.subscription_path('[PROJECT]', '[SUBSCRIPTION]')
max_messages = 1
response = client.pull(subscription, max_messages)
print(response)
I've tried myself and using that you get one message at a time.
If you get some error try updating pubsub library to the last version:
pip install --upgrade google-cloud-pubsub
In docs here you have more info about the pull method used in the code snippet:
The Pull method relies on a request/response model:
The application sends a request for messages. The server replies with
zero or more messages and closes the connection.
As per the official documentation here:
...you can achieve exactly once processing of Pub/Sub message streams,
as PubsubIO de-duplicates messages based on custom message identifiers
or identifiers assigned by Pub/Sub.
So you should be able to use record IDs, i.e. identifiers for you messages, to allow for exactly-once processing across the boundary between Dataflow and other systems. To use record IDs, you invoke idLabel when constructing PubsubIO.Read or PubsubIO.Write transforms, passing a string value of your choice. In java this would be:
public PubsubIO.Read.Bound<T> idLabel(String idLabel)
This returns a transform that's like this one but that reads unique message IDs from the given message attribute.

What are Telegram Client Messaging limits?

From here I know that Telegram limits BOT messages like this:
> 1msg/second per chat
> 30msg/second different chats
Happens that I'am not using the python-telegram-bot API, I'am using a normal client through telethon's library. I'am getting Flood\Spam error from Telegram when trying to send messages in that cadence. BTW, I'am using delayQueue Class to control the messages limits like this:
dqueue = DelayQueue(burst_limit=29, time_limit_ms=1014)
dqueue(client.send_message,inputPeerUser,msg)
Already asked directly to telegram's support, but got no answer. Does someone know what are the limits to normal clients?
There is a limit of 30 msg/sec.
Also,a bot can use the API requests for a limited no. of times
I get this error when I usually test the bot.During testing,there would be a lot of scenarios to test.So,I would be sending a huge amount of requests.
I get an error like this, {error_code:429,description:Too many requests try after (some_time)}
Idk the exact limit.
To avoid this:
You should not flood the telegram server with requests

Server Sent Events(SSE) in Google App Engine

Does GAE support Server Sent Events (SSE)?
I tried using SSE but it did not work ,so I switched to Channel API. But still is it possible to implement SSE in GAE ?
I've been trying like crazy to pull this one off but the GAE response is being buffered and compressed.
I'll be very happy if someone has an idea how to write the code/headers so the php file is streamed.
FYI, these are the headers I'm using:
header("Content-Type: text/event-stream; charset=utf-8");
header("Accept-Encoding: identity");
header("Cache-Control: no-cache");
header("Access-Control-Allow-Origin: https://mail.google.com");
header("Access-Control-Allow-Credentials: true");
header('Access-Control-Allow-Methods "PUT, GET, POST, DELETE, OPTIONS"');
[UPDATE]
From: http://grokbase.com/t/gg/google-appengine/15623azjjf/server-sent-events-using-channels-api
What this means in practice is that your stream will not be
"keep-alive" and will close each time one response is sent. Or, if you
implement your server-sent event code server-side as most people do,
it will buffer up all of its responses and finally send them all only
when it terminates.
Please read: https://cloud.google.com/appengine/docs/php/requests#PHP_Responses
Resume: there is no way to do SSE using GAE.

Categories

Resources