module SpeechToText::MozillaDeepspeechS2T

Public Class Methods

checkstatus(job_id, server_url, api_key) click to toggle source
# File lib/speech_to_text/deepspeech.rb, line 42
def self.checkstatus(job_id, server_url, api_key)
  uri = URI.parse("#{server_url}/deepspeech/checkstatus/#{job_id}/#{api_key}")
  request = Net::HTTP::Post.new(uri)

  req_options = {
    use_ssl: uri.scheme == "https",
  }

  response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
    http.request(request)
  end
  data = JSON.load response.body
  data['status']
end
create_job(audio, server_url, jobdetails_json, api_key) click to toggle source
# File lib/speech_to_text/deepspeech.rb, line 21
def self.create_job(audio, server_url, jobdetails_json, api_key)
  request = "curl -F \"file=@#{audio}\" \"#{server_url}/deepspeech/createjob/#{api_key}\" > #{jobdetails_json}"
  
  Open3.popen2e(request) do |stdin, stdout_err, wait_thr|
    while line = stdout_err.gets
      puts "#{line}"
    end

    exit_status = wait_thr.value
    unless exit_status.success?
      puts '---------------------'
      puts "FAILED to execute --> #{request}"
      puts '---------------------'
    end
  end

  file = File.open(jobdetails_json, 'r')
  data = JSON.load file
  data['job_id']
end
create_mozilla_array(data) click to toggle source
# File lib/speech_to_text/deepspeech.rb, line 109
def self.create_mozilla_array(data) # rubocop:disable Metrics/AbcSize
  i = 0
  myarray = []
  while i < data['words'].length
    myarray.push(data['words'][i]['start_time '].to_f)
    endtime = data['words'][i]['start_time '].to_f + data['words'][i]['duration'].to_f
    myarray.push(endtime)
    myarray.push(data['words'][i]['word'])
    i += 1
  end
  myarray
end
create_mozilla_array_old(data) click to toggle source

rubocop:disable Metrics/MethodLength

# File lib/speech_to_text/deepspeech.rb, line 91
def self.create_mozilla_array_old(data) # rubocop:disable Metrics/AbcSize
  i = 0
  myarray = []
  while i < data['words'].length
    myarray.push(data['words'][i]['time'].to_f)
    endtime = if i == data['words'].length - 1
                data['file']['duration'].to_f
              else
                data['words'][i + 1]['time'].to_f
              end
    myarray.push(endtime)
    myarray.push(data['words'][i]['word'])
    i += 1
  end
  myarray
end
generate_transcript(audio, json_file, model_path) click to toggle source

used by deepspeech server only

# File lib/speech_to_text/deepspeech.rb, line 73
def self.generate_transcript(audio, json_file, model_path)
  #deepspeech_command = "#{model_path}/deepspeech --model #{model_path}/models/output_graph.pbmm --alphabet #{model_path}/models/alphabet.txt --lm #{model_path}/models/lm.binary --trie #{model_path}/models/trie -e --audio #{audio} > #{json_file}"
  deepspeech_command = "#{model_path}/deepspeech --json --model #{model_path}/deepspeech-0.6.1-models/output_graph.pbmm --lm #{model_path}/deepspeech-0.6.1-models/lm.binary --trie #{model_path}/deepspeech-0.6.1-models/trie --audio #{audio} > #{json_file}"
  Open3.popen2e(deepspeech_command) do |stdin, stdout_err, wait_thr|
    while line = stdout_err.gets
      puts "#{line}"
    end

    exit_status = wait_thr.value
    unless exit_status.success?
      puts '---------------------'
      puts "FAILED to execute --> #{deepspeech_command}"
      puts '---------------------'
    end
  end
end
order_transcript(job_id, server_url, api_key) click to toggle source
# File lib/speech_to_text/deepspeech.rb, line 57
def self.order_transcript(job_id, server_url, api_key)
  uri = URI.parse("#{server_url}/deepspeech/transcript/#{job_id}/#{api_key}")
  request = Net::HTTP::Post.new(uri)

  req_options = {
    use_ssl: uri.scheme == "https",
  }

  response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
    http.request(request)
  end
  data = JSON.load response.body
  data
end