Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Updated Speech ruby app1.

  • Loading branch information...
commit d131eaa32fcb3de5c1927c8b867037e650f39ca0 1 parent 5ea403f
attdevsupport authored
Showing with 52 additions and 42 deletions.
  1. +39 −27 Speech/Ruby/app1/speech.rb
  2. +13 −15 Speech/Ruby/app1/views/speech.erb
View
66 Speech/Ruby/app1/speech.rb
@@ -32,34 +32,44 @@
end
post '/SpeechToText' do
-if params[:f1] != nil
- speech_to_text
-else
-speech_default_file
+ if params[:f1] != nil
+ @type = (params[:f1][:filename]).to_s.split(".")[1]
+ #Basic file extension check to ensure proper file types are uploaded
+ #Some times browser's may recognize mime types are application/octet-stream if the system does not know about the files mime type
+ if @type.to_s.eql?"wav"
+ @type = "audio/wav"
+ speech_to_text
+ elsif @type.to_s.eql?"amr"
+ @type = "audio/amr"
+ speech_to_text
+ else
+ @error = "Invalid file type, use audio/wav,audio/x-wav or audio/amr formats..."
+ return erb :speech
+ end
+ else
+ speech_default_file
+ end
end
-end
def speech_to_text
+ temp_file = params[:f1][:tempfile]
- @type = params[:f1][:type]
- temp_file = params[:f1][:tempfile]
-
- @file_contents = File.read(temp_file.path)
+ @file_contents = File.read(temp_file.path)
- if @type == "application/octet-stream"
- @type = "audio/amr"
- end
-
- url = "#{settings.FQDN}/rest/1/SpeechToText"
+ 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'
+ 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
+ @result = JSON.parse response
rescue => e
- @error = e.message
+ if e.response.nil?
+ @error = e.message
+ else
+ @error = e.response
+ end
ensure
return erb :speech
end
@@ -67,24 +77,26 @@ def speech_to_text
def speech_default_file
-@filename = 'bostonSeltics.wav'
-
-@type = ' audio/wav'
+ @filename = 'bostonSeltics.wav'
+ @type = 'audio/wav'
-fullname = File.expand_path(File.dirname(File.dirname(__FILE__)))
-final = fullname + '/' + @filename
-@file_contents = File.read(final)
+ fullname = File.expand_path(File.dirname(File.dirname(__FILE__)))
+ final = fullname + '/' + @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'
+ 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
+ @result = JSON.parse response
rescue => e
- @error = e.message
+ if e.response.nil?
+ @error = e.message
+ else
+ @error = e.response
+ end
ensure
return erb:speech
end
-
View
28 Speech/Ruby/app1/views/speech.erb
@@ -1,7 +1,7 @@
-<!-- Licensed by AT&T under 'Software Development Kit Tools Agreement.' 2012
+<!-- 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
+Copyright 2012 AT&T Intellectual Property. All rights reserved. http://developer.att.com
For more information contact developer.support@att.com -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
@@ -17,8 +17,7 @@ For more information contact developer.support@att.com -->
<div id="header">
<div>
<div class="hcRight">
- <%= Time.now.utc.strftime("%a, %B %d, %Y %H:%M:%S UTC") %>
- </script>
+ <%= Time.now.utc.strftime("%a, %B %d, %Y %H:%M:%S UTC") %>
</div>
<div class="hcLeft">
Server Time:</div>
@@ -106,38 +105,38 @@ For more information contact developer.support@att.com -->
</tr>
</thead>
<tbody>
- <%
+ <%
nbest = @result['Recognition']['NBest'];
nbest.each do |key, val| %>
- <tr>
+ <tr>
<td class="cell" align="center"><em>ResponseId</em></td>
<td class="cell" align="center"><em><%= @result['Recognition']['ResponseId'] %></em></td>
</tr>
- <tr>
+ <tr>
<td class="cell" align="center"><em>Hypothesis</em></td>
<td class="cell" align="center"><em><%= key['Hypothesis'] %></em></td>
</tr>
- <tr>
+ <tr>
<td class="cell" align="center"><em>LanguageId</em></td>
<td class="cell" align="center"><em><%= key['LanguageId'] %></em></td>
</tr>
- <tr>
+ <tr>
<td class="cell" align="center"><em>Confidence</em></td>
<td class="cell" align="center"><em><%= key['Confidence'] %></em></td>
</tr>
- <tr>
+ <tr>
<td class="cell" align="center"><em>Grade</em></td>
<td class="cell" align="center"><em><%= key['Grade'] %></em></td>
</tr>
- <tr>
+ <tr>
<td class="cell" align="center"><em>ResultText</em></td>
<td class="cell" align="center"><em><%= key['ResultText'] %></em></td>
</tr>
- <tr>
+ <tr>
<td class="cell" align="center"><em>Words</em></td>
<td class="cell" align="center"><em><%= key['Words'].join(", ") %></em></td>
</tr>
- <tr>
+ <tr>
<td class="cell" align="center"><em>WordScores</em></td>
<td class="cell" align="center"><em><%= key['WordScores'].join(", ") %></em></td>
</tr>
@@ -150,7 +149,7 @@ For more information contact developer.support@att.com -->
<div class="errorWide">
<strong>ERROR:</strong>
<br />
- Invalid file specified. Valid file formats are .wav and .amr
+ <%=@error %>
</div>
<% end %>
<br clear="all" />
@@ -176,4 +175,3 @@ For more information contact developer.support@att.com -->
&nbsp;</p>
</body>
</html>
-
Please sign in to comment.
Something went wrong with that request. Please try again.