|
| 1 | +/* |
| 2 | + * Copyright 2022 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package dialogflow.cx; |
| 18 | + |
| 19 | +// [START dialogflow_cx_v3_detect_intent_audio_input] |
| 20 | + |
| 21 | +import com.google.api.gax.rpc.ApiException; |
| 22 | +import com.google.cloud.dialogflow.cx.v3.AudioEncoding; |
| 23 | +import com.google.cloud.dialogflow.cx.v3.AudioInput; |
| 24 | +import com.google.cloud.dialogflow.cx.v3.DetectIntentRequest; |
| 25 | +import com.google.cloud.dialogflow.cx.v3.DetectIntentResponse; |
| 26 | +import com.google.cloud.dialogflow.cx.v3.InputAudioConfig; |
| 27 | +import com.google.cloud.dialogflow.cx.v3.QueryInput; |
| 28 | +import com.google.cloud.dialogflow.cx.v3.QueryResult; |
| 29 | +import com.google.cloud.dialogflow.cx.v3.SessionName; |
| 30 | +import com.google.cloud.dialogflow.cx.v3.SessionsClient; |
| 31 | +import com.google.cloud.dialogflow.cx.v3.SessionsSettings; |
| 32 | +import com.google.protobuf.ByteString; |
| 33 | +import java.io.FileInputStream; |
| 34 | +import java.io.IOException; |
| 35 | + |
| 36 | +public class DetectIntentAudioInput { |
| 37 | + |
| 38 | + // DialogFlow API Detect Intent sample with Audio input. |
| 39 | + public static void main(String[] args) throws IOException, ApiException { |
| 40 | + /** TODO (developer): replace these values with your own values */ |
| 41 | + String projectId = "my-project-id"; |
| 42 | + String locationId = "global"; |
| 43 | + String agentId = "my-agent-id"; |
| 44 | + String audioFileName = "resources/book_a_room.wav"; |
| 45 | + int sampleRateHertz = 16000; |
| 46 | + /* |
| 47 | + * A session ID is a string of at most 36 bytes in size. |
| 48 | + * Your system is responsible for generating unique session IDs. |
| 49 | + * They can be random numbers, hashed end-user identifiers, |
| 50 | + * or any other values that are convenient for you to generate. |
| 51 | + */ |
| 52 | + String sessionId = "my-UUID"; |
| 53 | + String languageCode = "en"; |
| 54 | + |
| 55 | + detectIntent( |
| 56 | + projectId, locationId, agentId, audioFileName, sampleRateHertz, sessionId, languageCode); |
| 57 | + } |
| 58 | + |
| 59 | + public static void detectIntent( |
| 60 | + String projectId, |
| 61 | + String locationId, |
| 62 | + String agentId, |
| 63 | + String audioFileName, |
| 64 | + int sampleRateHertz, |
| 65 | + String sessionId, |
| 66 | + String languageCode) |
| 67 | + throws IOException, ApiException { |
| 68 | + |
| 69 | + SessionsSettings.Builder sessionsSettingsBuilder = SessionsSettings.newBuilder(); |
| 70 | + if (locationId.equals("global")) { |
| 71 | + sessionsSettingsBuilder.setEndpoint("dialogflow.googleapis.com:443"); |
| 72 | + } else { |
| 73 | + sessionsSettingsBuilder.setEndpoint(locationId + "-dialogflow.googleapis.com:443"); |
| 74 | + } |
| 75 | + SessionsSettings sessionsSettings = sessionsSettingsBuilder.build(); |
| 76 | + |
| 77 | + // Instantiates a client by setting the session name. |
| 78 | + // Format:`projects/<ProjectID>/locations/<LocationID>/agents/<AgentID>/sessions/<SessionID>` |
| 79 | + try (SessionsClient sessionsClient = SessionsClient.create(sessionsSettings)) { |
| 80 | + SessionName session = |
| 81 | + SessionName.ofProjectLocationAgentSessionName(projectId, locationId, agentId, sessionId); |
| 82 | + |
| 83 | + // TODO : Uncomment if you want to print session path |
| 84 | + // System.out.println("Session Path: " + session.toString()); |
| 85 | + InputAudioConfig inputAudioConfig = |
| 86 | + InputAudioConfig.newBuilder() |
| 87 | + .setAudioEncoding(AudioEncoding.AUDIO_ENCODING_LINEAR_16) |
| 88 | + .setSampleRateHertz(sampleRateHertz) |
| 89 | + .build(); |
| 90 | + |
| 91 | + try (FileInputStream audioStream = new FileInputStream(audioFileName)) { |
| 92 | + // Subsequent requests must **only** contain the audio data. |
| 93 | + // Following messages: audio chunks. We just read the file in fixed-size chunks. In reality |
| 94 | + // you would split the user input by time. |
| 95 | + byte[] buffer = new byte[4096]; |
| 96 | + int bytes = audioStream.read(buffer); |
| 97 | + AudioInput audioInput = |
| 98 | + AudioInput.newBuilder() |
| 99 | + .setAudio(ByteString.copyFrom(buffer, 0, bytes)) |
| 100 | + .setConfig(inputAudioConfig) |
| 101 | + .build(); |
| 102 | + QueryInput queryInput = |
| 103 | + QueryInput.newBuilder() |
| 104 | + .setAudio(audioInput) |
| 105 | + .setLanguageCode("en-US") // languageCode = "en-US" |
| 106 | + .build(); |
| 107 | + |
| 108 | + DetectIntentRequest request = |
| 109 | + DetectIntentRequest.newBuilder() |
| 110 | + .setSession(session.toString()) |
| 111 | + .setQueryInput(queryInput) |
| 112 | + .build(); |
| 113 | + |
| 114 | + // Performs the detect intent request. |
| 115 | + DetectIntentResponse response = sessionsClient.detectIntent(request); |
| 116 | + |
| 117 | + // Display the query result. |
| 118 | + QueryResult queryResult = response.getQueryResult(); |
| 119 | + |
| 120 | + System.out.println("===================="); |
| 121 | + System.out.format( |
| 122 | + "Detected Intent: %s (confidence: %f)\n", |
| 123 | + queryResult.getTranscript(), queryResult.getIntentDetectionConfidence()); |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | +} |
| 128 | +// [END dialogflow_cx_v3_detect_intent_audio_input] |
0 commit comments