5
5
import pytest
6
6
import threading
7
7
8
- @pytest .mark .skipif (
9
- os .getenv ('VCAP_SERVICES' ) is None , reason = 'requires VCAP_SERVICES' )
8
+
9
+ @pytest .mark .skipif (os .getenv ('VCAP_SERVICES' ) is None ,
10
+ reason = 'requires VCAP_SERVICES' )
10
11
class TestSpeechToTextV1 (TestCase ):
11
12
text_to_speech = None
12
13
custom_models = None
@@ -17,12 +18,11 @@ class TestSpeechToTextV1(TestCase):
17
18
def setup_class (cls ):
18
19
cls .speech_to_text = ibm_watson .SpeechToTextV1 ()
19
20
cls .speech_to_text .set_default_headers ({
20
- 'X-Watson-Learning-Opt-Out' :
21
- '1' ,
22
- 'X-Watson-Test' :
23
- '1'
21
+ 'X-Watson-Learning-Opt-Out' : '1' ,
22
+ 'X-Watson-Test' : '1'
24
23
})
25
- cls .custom_models = cls .speech_to_text .list_language_models ().get_result ()
24
+ cls .custom_models = cls .speech_to_text .list_language_models (
25
+ ).get_result ()
26
26
cls .create_custom_model = cls .speech_to_text .create_language_model (
27
27
name = "integration_test_model" ,
28
28
base_model_name = "en-US_BroadbandModel" ).get_result ()
@@ -36,22 +36,27 @@ def teardown_class(cls):
36
36
def test_models (self ):
37
37
output = self .speech_to_text .list_models ().get_result ()
38
38
assert output is not None
39
- model = self .speech_to_text .get_model ('ko-KR_BroadbandModel' ).get_result ()
39
+ model = self .speech_to_text .get_model (
40
+ 'ko-KR_BroadbandModel' ).get_result ()
40
41
assert model is not None
41
42
try :
42
43
self .speech_to_text .get_model ('bogus' )
43
44
except Exception as e :
44
45
assert 'X-global-transaction-id:' in str (e )
45
46
46
47
def test_create_custom_model (self ):
47
- current_custom_models = self .speech_to_text .list_language_models ().get_result ()
48
+ current_custom_models = self .speech_to_text .list_language_models (
49
+ ).get_result ()
48
50
assert len (current_custom_models ['customizations' ]) - len (
49
51
self .custom_models .get ('customizations' )) >= 1
50
52
51
53
def test_recognize (self ):
52
- with open (os .path .join (os .path .dirname (__file__ ), '../../resources/speech.wav' ), 'rb' ) as audio_file :
54
+ with open (
55
+ os .path .join (os .path .dirname (__file__ ),
56
+ '../../resources/speech.wav' ), 'rb' ) as audio_file :
53
57
output = self .speech_to_text .recognize (
54
- audio = audio_file , content_type = 'audio/l16; rate=44100' ).get_result ()
58
+ audio = audio_file ,
59
+ content_type = 'audio/l16; rate=44100' ).get_result ()
55
60
assert output ['results' ][0 ]['alternatives' ][0 ][
56
61
'transcript' ] == 'thunderstorms could produce large hail isolated tornadoes and heavy rain '
57
62
@@ -60,7 +65,8 @@ def test_recognitions(self):
60
65
assert output is not None
61
66
62
67
def test_custom_corpora (self ):
63
- output = self .speech_to_text .list_corpora (self .customization_id ).get_result ()
68
+ output = self .speech_to_text .list_corpora (
69
+ self .customization_id ).get_result ()
64
70
assert not output ['corpora' ]
65
71
66
72
def test_acoustic_model (self ):
@@ -83,7 +89,9 @@ def test_acoustic_model(self):
83
89
get_acoustic_model ['customization_id' ]).get_result ()
84
90
85
91
def test_recognize_using_websocket (self ):
92
+
86
93
class MyRecognizeCallback (RecognizeCallback ):
94
+
87
95
def __init__ (self ):
88
96
RecognizeCallback .__init__ (self )
89
97
self .error = None
@@ -96,14 +104,19 @@ def on_transcription(self, transcript):
96
104
self .transcript = transcript
97
105
98
106
test_callback = MyRecognizeCallback ()
99
- with open (os .path .join (os .path .dirname (__file__ ), '../../resources/speech.wav' ), 'rb' ) as audio_file :
107
+ with open (
108
+ os .path .join (os .path .dirname (__file__ ),
109
+ '../../resources/speech.wav' ), 'rb' ) as audio_file :
100
110
audio_source = AudioSource (audio_file , False )
101
- t = threading .Thread (target = self .speech_to_text .recognize_using_websocket , args = (audio_source , "audio/l16; rate=44100" , test_callback ))
111
+ t = threading .Thread (
112
+ target = self .speech_to_text .recognize_using_websocket ,
113
+ args = (audio_source , "audio/l16; rate=44100" , test_callback ))
102
114
t .start ()
103
115
t .join ()
104
116
assert test_callback .error is None
105
117
assert test_callback .transcript is not None
106
- assert test_callback .transcript [0 ]['transcript' ] == 'thunderstorms could produce large hail isolated tornadoes and heavy rain '
118
+ assert test_callback .transcript [0 ][
119
+ 'transcript' ] == 'thunderstorms could produce large hail isolated tornadoes and heavy rain '
107
120
108
121
def test_custom_grammars (self ):
109
122
customization_id = None
@@ -116,37 +129,37 @@ def test_custom_grammars(self):
116
129
print ('Creating a new custom model' )
117
130
create_custom_model_for_grammar = self .speech_to_text .create_language_model (
118
131
name = "integration_test_model_for_grammar" ,
119
- base_model_name = "en-US_BroadbandModel"
120
- ). get_result ()
121
- customization_id = create_custom_model_for_grammar [ 'customization_id' ]
132
+ base_model_name = "en-US_BroadbandModel" ). get_result ()
133
+ customization_id = create_custom_model_for_grammar [
134
+ 'customization_id' ]
122
135
123
136
grammars = self .speech_to_text .list_grammars (
124
- customization_id
125
- ).get_result ()['grammars' ]
137
+ customization_id ).get_result ()['grammars' ]
126
138
127
139
if not grammars :
128
- with open (os .path .join (os .path .dirname (__file__ ), '../../resources/confirm-grammar.xml' ), 'rb' ) as grammar_file :
140
+ with open (
141
+ os .path .join (os .path .dirname (__file__ ),
142
+ '../../resources/confirm-grammar.xml' ),
143
+ 'rb' ) as grammar_file :
129
144
add_grammar_result = self .speech_to_text .add_grammar (
130
145
customization_id ,
131
146
grammar_name = 'test-add-grammar-python' ,
132
147
grammar_file = grammar_file ,
133
148
content_type = 'application/srgs+xml' ,
134
- allow_overwrite = True
135
- ).get_result ()
149
+ allow_overwrite = True ).get_result ()
136
150
assert add_grammar_result is not None
137
151
138
152
get_grammar_result = self .speech_to_text .get_grammar (
139
153
customization_id ,
140
- grammar_name = 'test-add-grammar-python'
141
- ).get_result ()
154
+ grammar_name = 'test-add-grammar-python' ).get_result ()
142
155
assert get_grammar_result is not None
143
156
else :
144
157
print ('Deleting grammar' )
145
- delete_grammar_result = self . speech_to_text . delete_grammar (
146
- customization_id ,
147
- 'test-add-grammar-python'
148
- ). get_result ()
149
- assert delete_grammar_result is not None
158
+ try :
159
+ self . speech_to_text . delete_grammar (
160
+ customization_id , 'test-add-grammar-python' ). get_result ()
161
+ except ibm_watson . ApiException as ex :
162
+ print ( 'Could not delete grammar: {0}' . format ( ex . message ))
150
163
151
164
try :
152
165
self .speech_to_text .delete_language_model (customization_id )
0 commit comments