57 lines
1.6 KiB
Python
57 lines
1.6 KiB
Python
#!/usr/bin/env python
|
|
|
|
# WS server example
|
|
|
|
import asyncio
|
|
import websockets
|
|
|
|
# Imports the Google Cloud client library
|
|
from google.cloud import speech
|
|
|
|
# Instantiates a client
|
|
client = speech.SpeechClient()
|
|
|
|
|
|
config = speech.RecognitionConfig(
|
|
encoding=speech.RecognitionConfig.AudioEncoding.LINEAR16,
|
|
sample_rate_hertz=16000,
|
|
language_code="en-US",
|
|
)
|
|
|
|
streaming_config = speech.StreamingRecognitionConfig(config=config)
|
|
def test(stream):
|
|
requests = (
|
|
speech.StreamingRecognizeRequest(audio_content=chunk) for chunk in stream
|
|
)
|
|
|
|
# Detects speech in the audio file
|
|
responses = client.streaming_recognize(
|
|
config=config,
|
|
requests=requests
|
|
)
|
|
|
|
for response in responses:
|
|
# Once the transcription has settled, the first result will contain the
|
|
# is_final result. The other results will be for subsequent portions of
|
|
# the audio.
|
|
for result in response.results:
|
|
print("Finished: {}".format(result.is_final))
|
|
print("Stability: {}".format(result.stability))
|
|
alternatives = result.alternatives
|
|
# The alternatives are ordered from most likely to least.
|
|
for alternative in alternatives:
|
|
print("Confidence: {}".format(alternative.confidence))
|
|
print(u"Transcript: {}".format(alternative.transcript))
|
|
|
|
|
|
stream = []
|
|
async def hello(websocket, path):
|
|
chunk = await websocket.recv()
|
|
print(chunk)
|
|
|
|
|
|
start_server = websockets.serve(hello, "localhost", 8080)
|
|
|
|
asyncio.get_event_loop().run_until_complete(start_server)
|
|
asyncio.get_event_loop().run_forever()
|
|
|