55import pytest
66import threading
77
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' )
1011class TestSpeechToTextV1 (TestCase ):
1112 text_to_speech = None
1213 custom_models = None
@@ -17,12 +18,11 @@ class TestSpeechToTextV1(TestCase):
1718 def setup_class (cls ):
1819 cls .speech_to_text = ibm_watson .SpeechToTextV1 ()
1920 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'
2423 })
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 ()
2626 cls .create_custom_model = cls .speech_to_text .create_language_model (
2727 name = "integration_test_model" ,
2828 base_model_name = "en-US_BroadbandModel" ).get_result ()
@@ -36,22 +36,27 @@ def teardown_class(cls):
3636 def test_models (self ):
3737 output = self .speech_to_text .list_models ().get_result ()
3838 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 ()
4041 assert model is not None
4142 try :
4243 self .speech_to_text .get_model ('bogus' )
4344 except Exception as e :
4445 assert 'X-global-transaction-id:' in str (e )
4546
4647 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 ()
4850 assert len (current_custom_models ['customizations' ]) - len (
4951 self .custom_models .get ('customizations' )) >= 1
5052
5153 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 :
5357 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 ()
5560 assert output ['results' ][0 ]['alternatives' ][0 ][
5661 'transcript' ] == 'thunderstorms could produce large hail isolated tornadoes and heavy rain '
5762
@@ -60,7 +65,8 @@ def test_recognitions(self):
6065 assert output is not None
6166
6267 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 ()
6470 assert not output ['corpora' ]
6571
6672 def test_acoustic_model (self ):
@@ -83,7 +89,9 @@ def test_acoustic_model(self):
8389 get_acoustic_model ['customization_id' ]).get_result ()
8490
8591 def test_recognize_using_websocket (self ):
92+
8693 class MyRecognizeCallback (RecognizeCallback ):
94+
8795 def __init__ (self ):
8896 RecognizeCallback .__init__ (self )
8997 self .error = None
@@ -96,14 +104,19 @@ def on_transcription(self, transcript):
96104 self .transcript = transcript
97105
98106 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 :
100110 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 ))
102114 t .start ()
103115 t .join ()
104116 assert test_callback .error is None
105117 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 '
107120
108121 def test_custom_grammars (self ):
109122 customization_id = None
@@ -116,37 +129,35 @@ def test_custom_grammars(self):
116129 print ('Creating a new custom model' )
117130 create_custom_model_for_grammar = self .speech_to_text .create_language_model (
118131 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' ]
122135
123136 grammars = self .speech_to_text .list_grammars (
124- customization_id
125- ).get_result ()['grammars' ]
137+ customization_id ).get_result ()['grammars' ]
126138
127139 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 :
129144 add_grammar_result = self .speech_to_text .add_grammar (
130145 customization_id ,
131146 grammar_name = 'test-add-grammar-python' ,
132147 grammar_file = grammar_file ,
133148 content_type = 'application/srgs+xml' ,
134- allow_overwrite = True
135- ).get_result ()
149+ allow_overwrite = True ).get_result ()
136150 assert add_grammar_result is not None
137151
138152 get_grammar_result = self .speech_to_text .get_grammar (
139153 customization_id ,
140- grammar_name = 'test-add-grammar-python'
141- ).get_result ()
154+ grammar_name = 'test-add-grammar-python' ).get_result ()
142155 assert get_grammar_result is not None
143156 else :
144157 print ('Deleting grammar' )
145158 try :
146- delete_grammar_result = self .speech_to_text .delete_grammar (
147- customization_id ,
148- 'test-add-grammar-python'
149- ).get_result ()
159+ self .speech_to_text .delete_grammar (
160+ customization_id , 'test-add-grammar-python' ).get_result ()
150161 except ibm_watson .ApiException as ex :
151162 print ('Could not delete grammar: {0}' .format (ex .message ))
152163
0 commit comments