|
70 | 70 | import com.ibm.watson.developer_cloud.speech_to_text.v1.model.UpgradeLanguageModelOptions; |
71 | 71 | import com.ibm.watson.developer_cloud.speech_to_text.v1.model.Word; |
72 | 72 | import com.ibm.watson.developer_cloud.speech_to_text.v1.model.Words; |
| 73 | +import com.ibm.watson.developer_cloud.speech_to_text.v1.websocket.RecognizeCallback; |
| 74 | +import com.ibm.watson.developer_cloud.speech_to_text.v1.websocket.SpeechToTextWebSocketListener; |
73 | 75 | import com.ibm.watson.developer_cloud.util.GsonSingleton; |
74 | 76 | import com.ibm.watson.developer_cloud.util.RequestUtils; |
75 | 77 | import com.ibm.watson.developer_cloud.util.ResponseConverterUtils; |
76 | 78 | import com.ibm.watson.developer_cloud.util.Validator; |
77 | | -import okhttp3.MultipartBody; |
78 | | -import okhttp3.RequestBody; |
| 79 | +import okhttp3.HttpUrl; |
| 80 | +import okhttp3.OkHttpClient; |
| 81 | +import okhttp3.Request; |
| 82 | +import okhttp3.WebSocket; |
79 | 83 |
|
80 | 84 | /** |
81 | 85 | * The IBM® Speech to Text service provides APIs that use IBM's speech-recognition capabilities to produce |
@@ -264,7 +268,9 @@ public ServiceCall<SpeechRecognitionResults> recognize(RecognizeOptions recogniz |
264 | 268 | Validator.notNull(recognizeOptions, "recognizeOptions cannot be null"); |
265 | 269 | String[] pathSegments = { "v1/recognize" }; |
266 | 270 | RequestBuilder builder = RequestBuilder.post(RequestBuilder.constructHttpUrl(getEndPoint(), pathSegments)); |
267 | | - builder.header("Content-Type", recognizeOptions.contentType()); |
| 271 | + if (recognizeOptions.contentType() != null) { |
| 272 | + builder.header("Content-Type", recognizeOptions.contentType()); |
| 273 | + } |
268 | 274 | if (recognizeOptions.model() != null) { |
269 | 275 | builder.query("model", recognizeOptions.model()); |
270 | 276 | } |
@@ -317,6 +323,58 @@ public ServiceCall<SpeechRecognitionResults> recognize(RecognizeOptions recogniz |
317 | 323 | return createServiceCall(builder.build(), ResponseConverterUtils.getObject(SpeechRecognitionResults.class)); |
318 | 324 | } |
319 | 325 |
|
| 326 | + /** |
| 327 | + * Sends audio and returns transcription results for recognition requests over a WebSocket connection. Requests and |
| 328 | + * responses are enabled over a single TCP connection that abstracts much of the complexity of the request to offer |
| 329 | + * efficient implementation, low latency, high throughput, and an asynchronous response. By default, only final |
| 330 | + * results are returned for any request; to enable interim results, set the interimResults parameter to true. |
| 331 | + * |
| 332 | + * The service imposes a data size limit of 100 MB per utterance (per recognition request). You can send multiple |
| 333 | + * utterances over a single WebSocket connection. The service automatically detects the endianness of the incoming |
| 334 | + * audio and, for audio that includes multiple channels, downmixes the audio to one-channel mono during transcoding. |
| 335 | + * (For the audio/l16 format, you can specify the endianness.) |
| 336 | + * |
| 337 | + * @param recognizeOptions the recognize options |
| 338 | + * @param callback the {@link RecognizeCallback} instance where results will be sent |
| 339 | + * @return the {@link WebSocket} |
| 340 | + */ |
| 341 | + public WebSocket recognizeUsingWebSocket(RecognizeOptions recognizeOptions, RecognizeCallback callback) { |
| 342 | + Validator.notNull(recognizeOptions, "recognizeOptions cannot be null"); |
| 343 | + Validator.notNull(recognizeOptions.audio(), "audio cannot be null"); |
| 344 | + Validator.notNull(callback, "callback cannot be null"); |
| 345 | + |
| 346 | + HttpUrl.Builder urlBuilder = HttpUrl.parse(getEndPoint() + "/v1/recognize").newBuilder(); |
| 347 | + |
| 348 | + if (recognizeOptions.model() != null) { |
| 349 | + urlBuilder.addQueryParameter("model", recognizeOptions.model()); |
| 350 | + } |
| 351 | + if (recognizeOptions.customizationId() != null) { |
| 352 | + urlBuilder.addQueryParameter("customization_id", recognizeOptions.customizationId()); |
| 353 | + } |
| 354 | + if (recognizeOptions.languageCustomizationId() != null) { |
| 355 | + urlBuilder.addQueryParameter("language_customization_id", recognizeOptions.languageCustomizationId()); |
| 356 | + } |
| 357 | + if (recognizeOptions.acousticCustomizationId() != null) { |
| 358 | + urlBuilder.addQueryParameter("acoustic_customization_id", recognizeOptions.acousticCustomizationId()); |
| 359 | + } |
| 360 | + if (recognizeOptions.baseModelVersion() != null) { |
| 361 | + urlBuilder.addQueryParameter("base_model_version", recognizeOptions.baseModelVersion()); |
| 362 | + } |
| 363 | + if (recognizeOptions.customizationWeight() != null) { |
| 364 | + urlBuilder.addQueryParameter("customization_weight", |
| 365 | + String.valueOf(recognizeOptions.customizationWeight())); |
| 366 | + } |
| 367 | + |
| 368 | + String url = urlBuilder.toString().replace("https://", "wss://"); |
| 369 | + Request.Builder builder = new Request.Builder().url(url); |
| 370 | + |
| 371 | + setAuthentication(builder); |
| 372 | + setDefaultHeaders(builder); |
| 373 | + |
| 374 | + OkHttpClient client = configureHttpClient(); |
| 375 | + return client.newWebSocket(builder.build(), new SpeechToTextWebSocketListener(recognizeOptions, callback)); |
| 376 | + } |
| 377 | + |
320 | 378 | /** |
321 | 379 | * Check a job. |
322 | 380 | * |
@@ -468,7 +526,9 @@ public ServiceCall<RecognitionJob> createJob(CreateJobOptions createJobOptions) |
468 | 526 | Validator.notNull(createJobOptions, "createJobOptions cannot be null"); |
469 | 527 | String[] pathSegments = { "v1/recognitions" }; |
470 | 528 | RequestBuilder builder = RequestBuilder.post(RequestBuilder.constructHttpUrl(getEndPoint(), pathSegments)); |
471 | | - builder.header("Content-Type", createJobOptions.contentType()); |
| 529 | + if (createJobOptions.contentType() != null) { |
| 530 | + builder.header("Content-Type", createJobOptions.contentType()); |
| 531 | + } |
472 | 532 | if (createJobOptions.model() != null) { |
473 | 533 | builder.query("model", createJobOptions.model()); |
474 | 534 | } |
@@ -884,11 +944,7 @@ public ServiceCall<Void> addCorpus(AddCorpusOptions addCorpusOptions) { |
884 | 944 | if (addCorpusOptions.allowOverwrite() != null) { |
885 | 945 | builder.query("allow_overwrite", String.valueOf(addCorpusOptions.allowOverwrite())); |
886 | 946 | } |
887 | | - MultipartBody.Builder multipartBuilder = new MultipartBody.Builder(); |
888 | | - multipartBuilder.setType(MultipartBody.FORM); |
889 | | - RequestBody corpusFileBody = RequestUtils.inputStreamBody(addCorpusOptions.corpusFile(), "text/plain"); |
890 | | - multipartBuilder.addFormDataPart("corpus_file", addCorpusOptions.corpusFilename(), corpusFileBody); |
891 | | - builder.body(multipartBuilder.build()); |
| 947 | + builder.body(RequestUtils.inputStreamBody(addCorpusOptions.corpusFile(), "text/plain")); |
892 | 948 | return createServiceCall(builder.build(), ResponseConverterUtils.getVoid()); |
893 | 949 | } |
894 | 950 |
|
|
0 commit comments