Skip to content

Commit 3092a13

Browse files
committed
prune old code, add multi-language support html and login/security
1 parent d170c24 commit 3092a13

File tree

2 files changed

+43
-57
lines changed

2 files changed

+43
-57
lines changed

java-ai/src/main/java/oracleai/aiholo/AIHoloController.java

Lines changed: 41 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,11 @@ public String getValue() {
7878

7979
@GetMapping("/play")
8080
public String play(@RequestParam("question") String question,
81-
@RequestParam("languagecode") String languagecode,
81+
@RequestParam("selectedMode") String selectedMode,
82+
@RequestParam("languagecode") String languagecode,
8283
@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);
8586
theValue = "question";
8687
String filePath = "C:/Users/opc/aiholo_output.txt";
8788
try (FileWriter writer = new FileWriter(filePath)) {
@@ -92,95 +93,95 @@ public String play(@RequestParam("question") String question,
9293
} catch (IOException e) {
9394
return "Error writing to file: " + e.getMessage();
9495
}
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
9897
if (question.contains("use vectorrag")) {
9998
action = "vectorrag";
10099
question = question.replace("use vectorrag", "").trim();
101100
answer = executeSandbox(question);
102101
} else {
103-
if (question.contains("use narrate")) {
102+
if (selectedMode.contains("use narrate")) {
104103
action = "narrate";
105-
question = question.replace("use narrate", "").trim();
104+
// question = question.replace("use narrate", "").trim();
106105
} else {
107106
question = question.replace("use chat", "").trim();
108107
}
109108
try (Connection connection = dataSource.getConnection();
110109
PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
111-
System.out.println("Database Connection Established: " + connection);
110+
System.out.println("Database Connection : " + connection);
112111
String response = null;
113112
preparedStatement.setString(1, question);
114113
preparedStatement.setString(2, action);
115-
116114
try (ResultSet resultSet = preparedStatement.executeQuery()) {
117115
if (resultSet.next()) {
118116
response = resultSet.getString(1); // Retrieve AI response from the first column
119117
}
120118
}
121119
answer = response;
122-
123120
} 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());
125122
return "Database Connection Failed!";
126123
}
127-
// String answer = "トム・ハンクス主演の映画は何ですか";
128124
}
129-
System.out.println("about tp sendAudioToAudio2Face answer: " + answer);
130125
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) {
132144
RestTemplate restTemplate = new RestTemplate();
133145
String baseUrl = "http://localhost:8011/A2F/Player/";
134146

135-
// ✅ Step 1: SetRootPath
136147
String setRootPathUrl = baseUrl + "SetRootPath";
137148
Map<String, Object> rootPathPayload = new HashMap<>();
138149
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");
141150
rootPathPayload.put("dir_path", "C:/Users/opc/src/github.com/paulparkinson/oracle-ai-for-sustainable-dev/java-ai");
142151
sendPostRequest(restTemplate, setRootPathUrl, rootPathPayload);
143152

144-
// ✅ Step 2: SetTrack
145153
String setTrackUrl = baseUrl + "SetTrack";
146154
Map<String, Object> trackPayload = new HashMap<>();
147155
trackPayload.put("a2f_player", "/World/audio2face/Player");
148156
trackPayload.put("file_name", fileName);
149157
trackPayload.put("time_range", new int[] { 0, -1 });
150158
sendPostRequest(restTemplate, setTrackUrl, trackPayload);
151159

152-
// ✅ Step 3: PlayTrack
153160
String playTrackUrl = baseUrl + "Play";
154161
Map<String, Object> playPayload = new HashMap<>();
155162
playPayload.put("a2f_player", "/World/audio2face/Player");
156163
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;
168164
}
169165

170-
// 📡 Helper function to send HTTP POST requests
171166
private void sendPostRequest(RestTemplate restTemplate, String url, Map<String, Object> payload) {
172167
HttpHeaders headers = new HttpHeaders();
173168
headers.setContentType(MediaType.APPLICATION_JSON);
174169
HttpEntity<Map<String, Object>> request = new HttpEntity<>(payload, headers);
175170

176171
ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class);
177172
if (response.getStatusCode().is2xxSuccessful()) {
178-
System.out.println("Successfully sent request to: " + url);
173+
System.out.println("Successfully sent request to: " + url);
179174
} 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());
181176
}
182177
}
183178

179+
180+
181+
182+
183+
184+
184185
public String executeSandbox(String cummulativeResult) {
185186
System.out.println("isRag is true, using AI sandbox: " + cummulativeResult);
186187

@@ -225,59 +226,44 @@ public String executeSandbox(String cummulativeResult) {
225226
// `https://141.148.204.74:8444/aiholo/tts?textToConvert=${encodeURIComponent(textToConvert)}&languageCode=${encodeURIComponent(languageCode)}&ssmlGender=${encodeURIComponent(ssmlGender)}&voiceName=${encodeURIComponent(voiceName)}`;
226227

227228

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 {
229230
try (TextToSpeechClient textToSpeechClient = TextToSpeechClient.create()) {
230231
System.out.println("in TTS languagecode:" + languageCode + " text:"+text);
231232
SynthesisInput input = SynthesisInput.newBuilder().setText(
232233
// "最受欢迎的游戏是Pods Of Kon。").build();
233234
text).build();
234235
// "最も人気のあるビデオゲームは「Pods Of Kon」です。").build();
235-
236236
VoiceSelectionParams voice =
237237
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
243240
// .setName("pt-BR-Wavenet-D") // tts-pt-BRFEMALEpt-BR-Wavenet-D_Bem-vindo
244241
.setName(voicename) // "Kore" tts-pt-BRFEMALEpt-BR-Wavenet-D_Bem-vindo
245242
.build();
246243

247-
248-
// Select the type of audio file you want returned
249244
AudioConfig audioConfig =
250245
AudioConfig.newBuilder()
251246
.setAudioEncoding(AudioEncoding.LINEAR16) // wav
252247
// .setAudioEncoding(AudioEncoding.MP3)
253248
.build();
254-
255-
// Perform the text-to-speech request on the text input with the selected voice parameters and
256-
// audio file type
257249
SynthesizeSpeechResponse response =
258250
textToSpeechClient.synthesizeSpeech(input, voice, audioConfig);
259-
260-
// Get the audio contents from the response
261251
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)) {
265253
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);
267255
}
268256
}
269257
}
270-
// `https://141.148.204.74:8444/aiholo/tts?textToConvert=${encodeURIComponent(textToConvert)}&languageCode=${encodeURIComponent(languageCode)}&ssmlGender=${encodeURIComponent(ssmlGender)}&voiceName=${encodeURIComponent(voiceName)}`;
271-
272258

259+
// `https://host:port/aiholo/tts?textToConvert=${encodeURIComponent(textToConvert)}&languageCode=${encodeURIComponent(languageCode)}&ssmlGender=${encodeURIComponent(ssmlGender)}&voiceName=${encodeURIComponent(voiceName)}`;
273260
@GetMapping("/tts")
274261
public ResponseEntity<byte[]> tts(@RequestParam("textToConvert") String textToConvert,
275262
@RequestParam("languageCode") String languageCode,
276263
@RequestParam("ssmlGender") String ssmlGender,
277264
@RequestParam("voiceName") String voiceName) throws Exception {
278265
String info= "tts for textToConvert " + textToConvert;
279266
System.out.println("in TTS GCP info:" + info);
280-
// Instantiates a client
281267
try (TextToSpeechClient textToSpeechClient = TextToSpeechClient.create()) {
282268
System.out.println("in TTS GCP textToSpeechClient:" + textToSpeechClient + " languagecode:" + languageCode);
283269
SynthesisInput input = SynthesisInput.newBuilder().setText(textToConvert).build();

java-ai/src/main/resources/static/aiholo/AIHolo-Brazil.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,10 @@ <h1>Oracle Database hologramas de IA interativos</h1>
151151
}
152152

153153
const selectedMode = document.querySelector('input[name="mode"]:checked').value;
154-
const modifiedText = `${transcriptionText} . responda em 20 palavras ou menos. use ${selectedMode}`;
154+
const modifiedText = `${transcriptionText} . responda em 20 palavras ou menos`;
155155

156156
"responda em 20 palavras ou menos"
157-
const apiUrl = `http://141.148.204.74:8444/aiholo/play?question=${encodeURIComponent(modifiedText)}&languagecode=${encodeURIComponent("pt-BR")}&voicename=${encodeURIComponent("pt-BR-Wavenet-B")}`;
157+
const apiUrl = `http://141.148.204.74:8444/aiholo/play?question=${encodeURIComponent(modifiedText)}&selectedMode=${encodeURIComponent(selectedMode)}&languagecode=${encodeURIComponent("pt-BR")}&voicename=${encodeURIComponent("pt-BR-Wavenet-B")}`;
158158

159159
try {
160160
const response = await fetch(apiUrl, {

0 commit comments

Comments
 (0)