mime-chat/ws.py
2021-02-05 16:41:55 -06:00

59 lines
1.3 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()
speechtotext_client = speech.SpeechClient()
config = speech.RecognitionConfig(
audio_channel_count=1,
encoding=speech.RecognitionConfig.AudioEncoding.LINEAR16,
sample_rate_hertz=16000,
language_code="en-US",
)
# Transcribe an audio file.
def transcribe(content):
print(len(content))
audio = speech.RecognitionAudio(content=content)
# Detects speech in the audio file
response = speechtotext_client.recognize(config=config, audio=audio)
input(f"Response: {response}")
for result in response.results:
return result.alternatives[0].transcript
async def session(websocket, path):
buf = []
chunk = await websocket.recv()
input(type(chunk))
transcribe(chunk)
# while True:
# chunk = await websocket.recv_frame()
# if chunk == "stop":
# break
# buf.append(chunk)
# input(f"Buffer sample: {buf[0]}")
# audio = b"".join(buf)
# transcribe(audio)
start_server = websockets.serve(session, "localhost", 8080)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()