@@ -78,10 +78,11 @@ public String getValue() {
78
78
79
79
@ GetMapping ("/play" )
80
80
public String play (@ RequestParam ("question" ) String question ,
81
- @ RequestParam ("languagecode" ) String languagecode ,
81
+ @ RequestParam ("selectedMode" ) String selectedMode ,
82
+ @ RequestParam ("languagecode" ) String languagecode ,
82
83
@ RequestParam ("voicename" ) String voicename ) throws Exception {
83
- System .out .println ("play question: " + question + " languagecode:" + languagecode );
84
-
84
+ System .out .println (
85
+ "play question: " + question + " selectedMode: " + selectedMode + " languagecode:" + languagecode );
85
86
theValue = "question" ;
86
87
String filePath = "C:/Users/opc/aiholo_output.txt" ;
87
88
try (FileWriter writer = new FileWriter (filePath )) {
@@ -92,95 +93,95 @@ public String play(@RequestParam("question") String question,
92
93
} catch (IOException e ) {
93
94
return "Error writing to file: " + e .getMessage ();
94
95
}
95
-
96
- String answer = "I'm sorry. I couldn't find an answer" , action = "chat" ;
97
-
96
+ String answer = "I'm sorry. I couldn't find an answer" , action = "chat" ; //TODO, this should be in correct language
98
97
if (question .contains ("use vectorrag" )) {
99
98
action = "vectorrag" ;
100
99
question = question .replace ("use vectorrag" , "" ).trim ();
101
100
answer = executeSandbox (question );
102
101
} else {
103
- if (question .contains ("use narrate" )) {
102
+ if (selectedMode .contains ("use narrate" )) {
104
103
action = "narrate" ;
105
- question = question .replace ("use narrate" , "" ).trim ();
104
+ // question = question.replace("use narrate", "").trim();
106
105
} else {
107
106
question = question .replace ("use chat" , "" ).trim ();
108
107
}
109
108
try (Connection connection = dataSource .getConnection ();
110
109
PreparedStatement preparedStatement = connection .prepareStatement (sql )) {
111
- System .out .println ("✅ Database Connection Established : " + connection );
110
+ System .out .println ("Database Connection : " + connection );
112
111
String response = null ;
113
112
preparedStatement .setString (1 , question );
114
113
preparedStatement .setString (2 , action );
115
-
116
114
try (ResultSet resultSet = preparedStatement .executeQuery ()) {
117
115
if (resultSet .next ()) {
118
116
response = resultSet .getString (1 ); // Retrieve AI response from the first column
119
117
}
120
118
}
121
119
answer = response ;
122
-
123
120
} catch (SQLException e ) {
124
- System .err .println ("❌ Failed to connect to the database: " + e .getMessage ());
121
+ System .err .println ("Failed to connect to the database: " + e .getMessage ());
125
122
return "Database Connection Failed!" ;
126
123
}
127
- // String answer = "トム・ハンクス主演の映画は何ですか";
128
124
}
129
- System .out .println ("about tp sendAudioToAudio2Face answer: " + answer );
130
125
String fileName = "output.wav" ;
131
- TTS (answer , languagecode , voicename );
126
+ System .out .println ("about to TTS and sendAudioToAudio2Face for answer: " + answer );
127
+ TTS (fileName , answer , languagecode , voicename );
128
+ sendToAudio2Face (fileName );
129
+ return answer ;
130
+ }
131
+
132
+
133
+
134
+
135
+
136
+
137
+
138
+
139
+
140
+
141
+
142
+
143
+ private void sendToAudio2Face (String fileName ) {
132
144
RestTemplate restTemplate = new RestTemplate ();
133
145
String baseUrl = "http://localhost:8011/A2F/Player/" ;
134
146
135
- // ✅ Step 1: SetRootPath
136
147
String setRootPathUrl = baseUrl + "SetRootPath" ;
137
148
Map <String , Object > rootPathPayload = new HashMap <>();
138
149
rootPathPayload .put ("a2f_player" , "/World/audio2face/Player" );
139
- // rootPathPayload.put("dir_path",
140
- // "C:/Users/opc/Downloads/aiholo/oracle-ai-for-sustainable-dev/interactive-ai-holograms/python-realtimespeech-selectai");
141
150
rootPathPayload .put ("dir_path" , "C:/Users/opc/src/github.com/paulparkinson/oracle-ai-for-sustainable-dev/java-ai" );
142
151
sendPostRequest (restTemplate , setRootPathUrl , rootPathPayload );
143
152
144
- // ✅ Step 2: SetTrack
145
153
String setTrackUrl = baseUrl + "SetTrack" ;
146
154
Map <String , Object > trackPayload = new HashMap <>();
147
155
trackPayload .put ("a2f_player" , "/World/audio2face/Player" );
148
156
trackPayload .put ("file_name" , fileName );
149
157
trackPayload .put ("time_range" , new int [] { 0 , -1 });
150
158
sendPostRequest (restTemplate , setTrackUrl , trackPayload );
151
159
152
- // ✅ Step 3: PlayTrack
153
160
String playTrackUrl = baseUrl + "Play" ;
154
161
Map <String , Object > playPayload = new HashMap <>();
155
162
playPayload .put ("a2f_player" , "/World/audio2face/Player" );
156
163
sendPostRequest (restTemplate , playTrackUrl , playPayload );
157
- // Thread.sleep(1000 * 10);
158
-
159
- // try (FileWriter writer = new FileWriter(filePath)) {
160
- // JSONObject json = new JSONObject();
161
- // json.put("data", "mirrorme"); // Store the response inside JSON
162
- // writer.write(json.toString());
163
- // writer.flush();
164
- // } catch (IOException e) {
165
- // return "Error writing to file: " + e.getMessage();
166
- // }
167
- return " 答え : " + answer ;
168
164
}
169
165
170
- // 📡 Helper function to send HTTP POST requests
171
166
private void sendPostRequest (RestTemplate restTemplate , String url , Map <String , Object > payload ) {
172
167
HttpHeaders headers = new HttpHeaders ();
173
168
headers .setContentType (MediaType .APPLICATION_JSON );
174
169
HttpEntity <Map <String , Object >> request = new HttpEntity <>(payload , headers );
175
170
176
171
ResponseEntity <String > response = restTemplate .postForEntity (url , request , String .class );
177
172
if (response .getStatusCode ().is2xxSuccessful ()) {
178
- System .out .println ("✅ Successfully sent request to: " + url );
173
+ System .out .println ("Successfully sent request to: " + url );
179
174
} else {
180
- System .err .println ("❌ Failed to send request to " + url + ". Response: " + response .getBody ());
175
+ System .err .println ("Failed to send request to " + url + ". Response: " + response .getBody ());
181
176
}
182
177
}
183
178
179
+
180
+
181
+
182
+
183
+
184
+
184
185
public String executeSandbox (String cummulativeResult ) {
185
186
System .out .println ("isRag is true, using AI sandbox: " + cummulativeResult );
186
187
@@ -225,59 +226,44 @@ public String executeSandbox(String cummulativeResult) {
225
226
// `https://141.148.204.74:8444/aiholo/tts?textToConvert=${encodeURIComponent(textToConvert)}&languageCode=${encodeURIComponent(languageCode)}&ssmlGender=${encodeURIComponent(ssmlGender)}&voiceName=${encodeURIComponent(voiceName)}`;
226
227
227
228
228
- public void TTS (String text , String languageCode , String voicename ) throws Exception {
229
+ public void TTS (String fileName , String text , String languageCode , String voicename ) throws Exception {
229
230
try (TextToSpeechClient textToSpeechClient = TextToSpeechClient .create ()) {
230
231
System .out .println ("in TTS languagecode:" + languageCode + " text:" +text );
231
232
SynthesisInput input = SynthesisInput .newBuilder ().setText (
232
233
// "最受欢迎的游戏是Pods Of Kon。").build();
233
234
text ).build ();
234
235
// "最も人気のあるビデオゲームは「Pods Of Kon」です。").build();
235
-
236
236
VoiceSelectionParams voice =
237
237
VoiceSelectionParams .newBuilder ()
238
- .setLanguageCode (languageCode )
239
- // .setLanguageCode("ja-JP")
240
- // .setLanguageCode("en-US")
241
- .setSsmlGender (SsmlVoiceGender .FEMALE )
242
- // .setSsmlGender(SsmlVoiceGender.NEUTRAL)
238
+ .setLanguageCode (languageCode ) //ja-JP, en-US, ...
239
+ .setSsmlGender (SsmlVoiceGender .FEMALE ) // NEUTRAL, MALE
243
240
// .setName("pt-BR-Wavenet-D") // tts-pt-BRFEMALEpt-BR-Wavenet-D_Bem-vindo
244
241
.setName (voicename ) // "Kore" tts-pt-BRFEMALEpt-BR-Wavenet-D_Bem-vindo
245
242
.build ();
246
243
247
-
248
- // Select the type of audio file you want returned
249
244
AudioConfig audioConfig =
250
245
AudioConfig .newBuilder ()
251
246
.setAudioEncoding (AudioEncoding .LINEAR16 ) // wav
252
247
// .setAudioEncoding(AudioEncoding.MP3)
253
248
.build ();
254
-
255
- // Perform the text-to-speech request on the text input with the selected voice parameters and
256
- // audio file type
257
249
SynthesizeSpeechResponse response =
258
250
textToSpeechClient .synthesizeSpeech (input , voice , audioConfig );
259
-
260
- // Get the audio contents from the response
261
251
ByteString audioContents = response .getAudioContent ();
262
-
263
- // Write the response to the output file.
264
- try (OutputStream out = new FileOutputStream ("output.wav" )) {
252
+ try (OutputStream out = new FileOutputStream (fileName )) {
265
253
out .write (audioContents .toByteArray ());
266
- System .out .println ("Audio content written to file \" output.wav \" " );
254
+ System .out .println ("Audio content written to file:" + fileName );
267
255
}
268
256
}
269
257
}
270
- // `https://141.148.204.74:8444/aiholo/tts?textToConvert=${encodeURIComponent(textToConvert)}&languageCode=${encodeURIComponent(languageCode)}&ssmlGender=${encodeURIComponent(ssmlGender)}&voiceName=${encodeURIComponent(voiceName)}`;
271
-
272
258
259
+ // `https://host:port/aiholo/tts?textToConvert=${encodeURIComponent(textToConvert)}&languageCode=${encodeURIComponent(languageCode)}&ssmlGender=${encodeURIComponent(ssmlGender)}&voiceName=${encodeURIComponent(voiceName)}`;
273
260
@ GetMapping ("/tts" )
274
261
public ResponseEntity <byte []> tts (@ RequestParam ("textToConvert" ) String textToConvert ,
275
262
@ RequestParam ("languageCode" ) String languageCode ,
276
263
@ RequestParam ("ssmlGender" ) String ssmlGender ,
277
264
@ RequestParam ("voiceName" ) String voiceName ) throws Exception {
278
265
String info = "tts for textToConvert " + textToConvert ;
279
266
System .out .println ("in TTS GCP info:" + info );
280
- // Instantiates a client
281
267
try (TextToSpeechClient textToSpeechClient = TextToSpeechClient .create ()) {
282
268
System .out .println ("in TTS GCP textToSpeechClient:" + textToSpeechClient + " languagecode:" + languageCode );
283
269
SynthesisInput input = SynthesisInput .newBuilder ().setText (textToConvert ).build ();
0 commit comments