-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathspeech.rb
92 lines (66 loc) · 2.3 KB
/
speech.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# Licensed by AT&T under 'Software Development Kit Tools Agreement.' 2012
# TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION: http://developer.att.com/sdk_agreement/
# Copyright 2012 AT&T Intellectual Property. All rights reserved. http://developer.att.com
# For more information contact [email protected]
#!/usr/bin/ruby
require 'rubygems'
require 'json'
require 'rest_client'
require 'sinatra'
require 'open-uri'
require 'sinatra/config_file'
require File.join(File.dirname(__FILE__), 'common.rb')
enable :sessions
config_file 'config.yml'
set :port, settings.port
SCOPE = 'SPEECH'
error do
@error = e.message
end
['/SpeechToText'].each do |path|
if settings.api_key.nil? || settings.secret_key.nil?
raise RuntimeError, "The API and Secret keys must be set the config.yml file"
end
before path do
obtain_tokens(settings.FQDN, settings.api_key, settings.secret_key, SCOPE, settings.tokens_file)
end
end
get '/' do
erb :speech
end
post '/SpeechToText' do
if params[:f1] != nil
speech_to_text
else
speech_default_file
end
end
def speech_to_text
@type = params[:f1][:type]
temp_file = params[:f1][:tempfile]
@file_contents = File.read(temp_file.path)
if @type == "application/octet-stream"
@type = "audio/amr"
end
url = "#{settings.FQDN}/rest/1/SpeechToText"
response = RestClient.post "#{settings.FQDN}/rest/1/SpeechToText", "#{@file_contents}", :Authorization => "Bearer #{@access_token}", :Content_Transfer_Encoding => 'chunked', :X_SpeechContext => 'Generic', :Content_Type => "#{@type}" , :Accept => 'application/json'
@result = JSON.parse response
rescue => e
@error = e.message
ensure
return erb :speech
end
def speech_default_file
@filename = 'bostonSeltics.wav'
@type = 'audio/wav'
fullname = File.expand_path(File.dirname(File.dirname(__FILE__)))
final = fullname + '/app1/' + @filename
@file_contents = File.read(final)
url = "#{settings.FQDN}/rest/1/SpeechToText"
response = RestClient.post "#{settings.FQDN}/rest/1/SpeechToText", "#{@file_contents}", :Authorization => "Bearer #{@access_token}", :Content_Transfer_Encoding => 'chunked', :X_SpeechContext => 'Generic', :Content_Type => "#{@type}" , :Accept => 'application/json'
@result = JSON.parse response
rescue => e
@error = e.message
ensure
return erb:speech
end