Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speech to text not working with websockets #88

Closed
pandrews11 opened this issue Feb 6, 2020 · 2 comments
Closed

Speech to text not working with websockets #88

pandrews11 opened this issue Feb 6, 2020 · 2 comments
Assignees
Labels
bug Something isn't working

Comments

@pandrews11
Copy link

Describe the bug
Speech to text is not working with websockets. Works fine with synchronous recognize.

To Reproduce
Here is the code:

#!/usr/bin/env ruby

require "ibm_watson/speech_to_text_v1"
require "ibm_watson/websocket/recognize_callback"
require "ibm_watson/authenticators"
require "json"

authenticator = IBMWatson::Authenticators::IamAuthenticator.new(
  apikey: ENV['API_KEY']
)

speech_to_text = IBMWatson::SpeechToTextV1.new(
  authenticator: authenticator
)

speech_to_text.service_url = ENV['API_URL']

File.open(Dir.getwd + "/resources/speech.wav") do |audio_file|
  recognition = speech_to_text.recognize(
    audio: audio_file,
    content_type: "audio/wav",
    timestamps: true,
    word_confidence: true
  ).result
  puts JSON.pretty_generate(recognition)
end

# Example using websockets
class MyRecognizeCallback < IBMWatson::RecognizeCallback
  def initialize
    super
  end

  def on_transcription(transcript:)
    puts JSON.pretty_generate(transcript)
  end

  def on_connected
    puts "Connection was successful"
  end

  def on_error(error:)
    puts "Error received: #{error}"
    puts "Error message: #{error.message}"
  end

  def on_inactivity_timeout(error:)
    puts "Inactivity timeout: #{error}"
  end

  def on_listening
    puts "Service is listening"
  end

  def on_transcription_complete
    puts "Transcription completed"
  end

  def on_hypothesis(hypothesis:)
    puts hypothesis.to_s
  end

  def on_data(data:)
    puts data.to_s
  end
end

mycallback = MyRecognizeCallback.new
File.open(Dir.getwd + "/resources/speech.wav") do |audio_file|
  speech_to_text.recognize_using_websocket(
    audio: audio_file,
    recognize_callback: mycallback,
    content_type: "audio/wav"
  ).start
end

Expected behavior
I am expecting to see a successful connected message from the recognize callback.

Actual behavior

Error received: #<Faye::WebSocket::API::ErrorEvent:0x00000000029eaa28>
Error message: Error during WebSocket handshake: Unexpected response code: 401

Desktop (please complete the following information):

  • Docker
  • ruby-2.4.3
  • ibm_cloud_sdk_core-1.1.1
  • ibm_watson-1.2.0

Additional context
By adding the authentication step before sending websocket request I was able to make it work:

diff --git a/lib/ibm_watson/speech_to_text_v1.rb b/lib/ibm_watson/speech_to_text_v1.rb
index 7451924..ba3c20a 100644
--- a/lib/ibm_watson/speech_to_text_v1.rb
+++ b/lib/ibm_watson/speech_to_text_v1.rb
@@ -567,6 +567,7 @@ module IBMWatson

       require_relative("./websocket/speech_to_text_websocket_listener.rb")
       headers = {}
+      @authenticator.authenticate(headers)
       headers = conn.default_options.headers.to_hash unless conn.default_options.headers.to_hash.empty?
       service_url = @service_url.gsub("https:", "wss:")
       params = {

Thanks!

@pandrews11 pandrews11 added the bug Something isn't working label Feb 6, 2020
@mamoonraja
Copy link
Member

Hi @pandrews11, thanks a lot for opening the issue, we will have the fix out soon.

@mamoonraja
Copy link
Member

Fixed in #94

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Development

No branches or pull requests

3 participants