Python Forum
IBM Watson: Handshake status 403 Forbidden or No section: 'auth'
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
IBM Watson: Handshake status 403 Forbidden or No section: 'auth'
#1
Hi everyone,

I am trying to implement this code (live speech to text with IBM watson)
https://github.com/nicknochnack/RealTimeSpeechToText

I am using python 3.7 and Visual Studio code on Windows 10

-> i m getting : Handshake status 403 Forbidden

When i am using Pycharm python 3.5 and 3.7

->i am getting: configparser.NoSectionError: No section: 'auth'

Does anyone has an idea

below the snippet of the relevant files

transcribe.py:

#!/usr/bin/env python
#
# Copyright 2016 IBM
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import argparse
import base64
import configparser
import json
import threading
import time

import pyaudio
import websocket
from websocket._abnf import ABNF

CHUNK = 1024
FORMAT = pyaudio.paInt16
# Even if your default input is multi channel (like a webcam mic),
# it's really important to only record 1 channel, as the STT service
# does not do anything useful with stereo. You get a lot of "hmmm"
# back.
CHANNELS = 1
# Rate is important, nothing works without it. This is a pretty
# standard default. If you have an audio device that requires
# something different, change this.
RATE = 44100
RECORD_SECONDS = 5
FINALS = []
LAST = None

REGION_MAP = {
'us-east': 'gateway-wdc.watsonplatform.net',
'us-south': 'stream.watsonplatform.net',
'eu-gb': 'stream.watsonplatform.net',
'eu-de': 'stream-fra.watsonplatform.net',
'au-syd': 'gateway-syd.watsonplatform.net',
'jp-tok': 'gateway-syd.watsonplatform.net',
}


def read_audio(ws, timeout):
"""Read audio and sent it to the websocket port.

This uses pyaudio to read from a device in chunks and send these
over the websocket wire.

"""
global RATE
p = pyaudio.PyAudio()
# NOTE(sdague): if you don't seem to be getting anything off of
# this you might need to specify:
#
# input_device_index=N,
#
# Where N is an int. You'll need to do a dump of your input
# devices to figure out which one you want.
RATE = int(p.get_default_input_device_info()['defaultSampleRate'])
stream = p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK)

print("* recording")
rec = timeout or RECORD_SECONDS

for i in range(0, int(RATE / CHUNK * rec)):
data = stream.read(CHUNK)
# print("Sending packet... %d" % i)
# NOTE(sdague): we're sending raw binary in the stream, we
# need to indicate that otherwise the stream service
# interprets this as text control messages.
ws.send(data, ABNF.OPCODE_BINARY)

# Disconnect the audio stream
stream.stop_stream()
stream.close()
print("* done recording")

# In order to get a final response from STT we send a stop, this
# will force a final=True return message.
data = {"action": "stop"}
ws.send(json.dumps(data).encode('utf8'))
# ... which we need to wait for before we shutdown the websocket
time.sleep(1)
ws.close()

# ... and kill the audio device
p.terminate()


def on_message(self, msg):
"""Print whatever messages come in.

While we are processing any non trivial stream of speech Watson
will start chunking results into bits of transcripts that it
considers "final", and start on a new stretch. It's not always
clear why it does this. However, it means that as we are
processing text, any time we see a final chunk, we need to save it
off for later.
"""
global LAST
data = json.loads(msg)
if "results" in data:
if data["results"][0]["final"]:
FINALS.append(data)
LAST = None
else:
LAST = data
# This prints out the current fragment that we are working on
print(data['results'][0]['alternatives'][0]['transcript'])


def on_error(self, error):
"""Print any errors."""
print(error)


def on_close(ws):
"""Upon close, print the complete and final transcript."""
global LAST
if LAST:
FINALS.append(LAST)
transcript = "".join([x['results'][0]['alternatives'][0]['transcript']
for x in FINALS])
print(transcript)


def on_open(ws):
"""Triggered as soon a we have an active connection."""
args = ws.args
data = {
"action": "start",
# this means we get to send it straight raw sampling
"content-type": "audio/l16;rate=%d" % RATE,
"continuous": True,
"interim_results": True,
# "inactivity_timeout": 5, # in order to use this effectively
# you need other tests to handle what happens if the socket is
# closed by the server.
"word_confidence": True,
"timestamps": True,
"max_alternatives": 3
}

# Send the initial control message which sets expectations for the
# binary stream that follows:
ws.send(json.dumps(data).encode('utf8'))
# Spin off a dedicated thread where we are going to read and
# stream out audio.
threading.Thread(target=read_audio,
args=(ws, args.timeout)).start()

def get_url():
config = configparser.RawConfigParser()
config.read('speech.cfg')
# See
# https://console.bluemix.net/docs/service...websockets
# for details on which endpoints are for each region.
region = config.get('auth', 'region')
host = REGION_MAP[region]
return ("wss://{}/speech-to-text/api/v1/recognize"
"?model=en-US_BroadbandModel").format(host)

def get_auth():
config = configparser.RawConfigParser()
config.read('speech.cfg')
apikey = config.get('auth', 'apikey')
return ("apikey", apikey)


def parse_args():
parser = argparse.ArgumentParser(
description='Transcribe Watson text in real time')
parser.add_argument('-t', '--timeout', type=int, default=5)
# parser.add_argument('-d', '--device')
# parser.add_argument('-v', '--verbose', action='store_true')
args = parser.parse_args()
return args


def main():
# Connect to websocket interfaces
headers = {}
userpass = ":".join(get_auth())
headers["Authorization"] = "Basic " + base64.b64encode(
userpass.encode()).decode()
url = get_url()

# If you really want to see everything going across the wire,
# uncomment this. However realize the trace is going to also do
# things like dump the binary sound packets in text in the
# console.
#
# websocket.enableTrace(True)
ws = websocket.WebSocketApp(url,
header=headers,
on_message=on_message,
on_error=on_error,
on_close=on_close)
ws.on_open = on_open
ws.args = parse_args()
# This gives control over the WebSocketApp. This is a blocking
# call, so it won't return until the ws.close() gets called (after
# 6 seconds in the dedicated thread).
ws.run_forever()


if __name__ == "__main__":
main()



***********************************************************************
setup.py:



# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# THIS FILE IS MANAGED BY THE GLOBAL REQUIREMENTS REPO - DO NOT EDIT
import setuptools

# In python < 2.7.4, a lazy loading of package pbr will break
# setuptools if some other modules registered functions in atexit.
# solution from: http://bugs.python.org/issue15881#msg170215
try:
import multiprocessing # noqa
except ImportError:
pass

setuptools.setup(
setup_requires=['pbr>=1.8'],
pbr=True)


************************************************************************************
speech.cfg:

[auth]
apikey = Qg_1RJg3EP6igjSU9hDgl6pu1S4xjN_L9IqM-4qP6pr6
# Modify region based on where you provisioned your stt instance
region = eu-gb


***********************************************************************************************
Reply
#2
Please post code using the Python tags, and complete error message. Wild guess - your auth in speech.cfg is wrong.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to add multi-line comment section? Winfried 1 184 Mar-24-2024, 04:34 PM
Last Post: deanhystad
  auth - URL SUN 2 1,502 May-20-2022, 07:50 AM
Last Post: SUN
  Append data to Yaml section tbaror 0 6,971 Feb-09-2022, 06:56 PM
Last Post: tbaror
  Access Forbidden Niv_Neos 2 1,846 Jul-12-2020, 07:02 PM
Last Post: Niv_Neos
  ImportError: No module named pydrive.auth lknights1987 5 5,919 Jun-23-2020, 06:40 PM
Last Post: snippsat
  Create an isolated section in bash? yxk 1 1,940 Feb-27-2020, 11:16 AM
Last Post: Larz60+
  Best section to do a while loop in my code mrapple2020 0 1,645 Apr-15-2019, 01:14 AM
Last Post: mrapple2020
  PUT with Basic Auth and urllib2 rootVIII 0 2,933 Feb-22-2019, 11:25 PM
Last Post: rootVIII
  Passing basic auth data between views nikos 0 1,885 Feb-13-2019, 09:32 PM
Last Post: nikos
  google-auth requirements.txt strange behavior randalpinto 3 3,724 Dec-21-2018, 02:03 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020