Saturday 17 June 2017

Convert speech to text using watson api's in Rails

The Watson api's Speech to Text service of IBM® provides an API that lets you add speech transcription capabilities to your applications. To transcribe the human voice accurately, the service leverages machine intelligence to combine information about grammar and language structure with knowledge of the composition of the audio signal.

To get service credentials, follow these steps:

    1. Log in to Bluemix at https://bluemix.net. If you don't have an account, sign   up here for a 30-day free trial.

    2. Create an instance of the service:

        a) In the Bluemix Catalog, scroll down to Services and select the Speech To Text service from Watson.

        b) Under Add Service, type a unique name for the service instance in the Service name field. For example, type my-service-name. Leave the default values for the other options.

        c) Click Create.

    3. Copy your credentials:

        a) On the left side of the page, click Service Credentials to view your service credentials.

        b) Copy username and password from these service credentials and paste them into the watson.yml file

Make a watson.yml file in config

In watson.yml

default: &default
   auth_url: https://stream.watsonplatform.net/authorization/api/v1/token?url=https://stream.watsonplatform.net/speech-to-text/api
   watson_url: wss://stream.watsonplatform.net/speech-to-text/api/v1/recognize?watson-token=
   user_name: username
   password: password
   params:
      action: start
      content-type: audio/wav
      continuous: true
      inactivity_timeout: -1
      interim_results: true

 development:
   <<: *default

 test:
   <<: *default

 staging:
   <<: *default

 production:
   <<: *default 


In Gemfile:

 gem 'activesupport'
 gem 'eventmachine'
 gem 'websocket-client-simple'

Run bundle.

Make a File.rb file in config/intializer. In which we will define a instance method for an audio file.

 class File
   def read_chunk(chunk_size=2000)
     yield read(chunk_size) until eof?
   end
 end


Now, we will create a method for speech to text conversion in which will take an audio file as an argument.

   WATSON = YAML.load_file(File.join(Rails.root, 'config', 'watson.yml'))[Rails.env]

   def speech_to_text(wav_file)
     params = WATSON['params']

     token = ''
     uri = URI.parse(WATSON['auth_url'])
     Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
       request = Net::HTTP::Get.new(uri)
       request.basic_auth WATSON['user_name'], WATSON['password']
       token = http.request(request)
     end

     watson_url = WATSON['watson_url']+"#{token.body}"
     file_url = wav_file #replace this with the file you want to read
     init_message = params.to_json
     ws = ''
     # run websocket in an EventMachine
     EM.run {
       ws = WebSocket::Client::Simple.connect watson_url
       file_sent = false

       ws.on :message do |event|
  data = JSON.parse(event.data)
  if data['state'] && data['state'] == 'listening'
    if !!file_sent
      EM::stop_event_loop
    else
      file_sent = true
      open(file_url, 'rb') do |file|
        file.read_chunk { |chunk| ws.send(chunk, type: :binary) }
      end
      ws.send("", type: :binary)
    end
  elsif data['results'] && data['results'].first['final']
    puts data['results'].first['alternatives'].first['transcript'] if data['results'].first['alternatives']
  end

       end

       ws.on :open do
  puts "-- websocket open"
  puts init_message
  ws.send(init_message)
       end

       ws.on :close do |e|
  puts "-- websocket close #{
  if e!=nil then
    (e)
  end}"
  exit 1
       end

       ws.on :error do |e|
  puts "-- error (#{e.inspect})"
       end
     }
     ws.close
   end

4 comments:

  1. It is nice blog Thank you porovide importent information and i am searching for same information to save my time thank you for sharing on Ruby on Rails Online Training India

    ReplyDelete
  2. Thank you.Well it was nice post and very helpful information on
    Ruby on Rails Online Training

    ReplyDelete
  3. Nice blog very useful information I will visit again to read more your post.
    Speech To Text


    ReplyDelete
  4. Wow this is really amazing post. Thanks for sharing the useful informative data. I appreciate your difficulty work. Keep blogging ruby on rails course

    ReplyDelete