user7519553
user7519553

Reputation:

Failing to open websocket connection to IBM Watson speech to text

The following code just returns the output: "Failed to open WebSocket" Please help me correct it. I suspect the ws_url might be faulty, but still unsure.

from ws4py.client.threadedclient import WebSocketClient
import base64, time

class SpeechToTextClient(WebSocketClient):
    def __init__(self):
    ws_url = "wss://stream.watsonplatform.net/speech-to-text/api/v1/recognize"

    username = "your username"
    password = "your password"
    auth_string = "%s:%s" % (username, password)
    base64string = base64.b64encode(auth_string.encode())

    try:
        WebSocketClient.__init__(self, ws_url,
            headers=[("Authorization", "Basic %s" % base64string)])
        self.connect()
    except: print("Failed to open WebSocket.")

    def opened(self):
        self.send('{"action": "start", "content-type": "audio/l16;rate=16000"}')

def received_message(self, message):
    print(message)

stt_client = SpeechToTextClient()
time.sleep(3)
stt_client.close()

Error message is provided below:¨

Here is the full error message:

Traceback (most recent call last): File "C:\Users\Vetle\Desktop\Python projects\problem.py", line 27, in stt_client.close() File "C:\Users\Vetle\AppData\Local\Programs\Python\Python35\lib\site-packages\ws4py\client__init__.py", line 205, in close self._write(self.stream.close(code=code, reason=reason).single(mask=True)) File "C:\Users\Vetle\AppData\Local\Programs\Python\Python35\lib\site-packages\ws4py\websocket.py", line 283, in _write raise RuntimeError("Cannot send on a terminated websocket") RuntimeError: Cannot send on a terminated websocket

Upvotes: 0

Views: 849

Answers (1)

chughts
chughts

Reputation: 4747

As per the API documentation you need to pass in an authentication token and not the basic service credentials. The Documentation has sample web socket code - https://www.ibm.com/watson/developercloud/speech-to-text/api/v1/#recognize_audio_websockets

Upvotes: 1

Related Questions