Module: SpeechToText::MozillaDeepspeechS2T

Includes:
Util
Defined in:
lib/speech_to_text/deepspeech.rb

Overview

rubocop:disable Style/Documentation

Class Method Summary collapse

Methods included from Util

captions_json, recording_json, seconds_to_timestamp, video_to_audio, write_to_webvtt

Class Method Details

.checkstatus(job_id, server_url, api_key) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# 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) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# 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) ⇒ Object

rubocop:disable Metrics/AbcSize



109
110
111
112
113
114
115
116
117
118
119
120
# 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) ⇒ Object

rubocop:disable Metrics/MethodLength



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# 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) ⇒ Object

used by deepspeech server only



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# 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) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# 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