Skip to content

Commit bd5a28b

Browse files
committed
financial app work
1 parent 1bb2f47 commit bd5a28b

File tree

14 files changed

+115
-77
lines changed

14 files changed

+115
-77
lines changed

interactive-ai-holograms/java-version/src/main/java/oracleai/aiholo/AIHoloController.java

Lines changed: 115 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,15 @@
4242
import java.sql.*;
4343
import java.util.HashMap;
4444
import java.util.Map;
45+
4546
import org.springframework.http.*;
4647
import org.springframework.stereotype.Service;
4748
import org.springframework.web.multipart.MultipartFile;
49+
50+
import java.nio.file.Files;
51+
import java.nio.file.Paths;
52+
import java.nio.charset.StandardCharsets;
53+
4854
@Controller
4955
@RequestMapping("/aiholo")
5056
// @CrossOrigin(origins = "*")
@@ -54,7 +60,24 @@ public class AIHoloController {
5460
private static final String SANDBOX_API_URL = System.getenv("SANDBOX_API_URL");
5561
private static final String AI_OPTIMZER = System.getenv("AI_OPTIMZER");
5662
static final String AUDIO_DIR_PATH = System.getenv("AUDIO_DIR_PATH");
57-
static int currentAnswerIntro = 0;
63+
private static int currentAnswerIntro = 0;
64+
private static String aiholo_prompt_additions = "";
65+
66+
static {
67+
// Check for aiholo_prompt_additions.txt in AUDIO_DIR_PATH at startup
68+
if (AUDIO_DIR_PATH != null) {
69+
try {
70+
java.nio.file.Path additionsPath = Paths.get(AUDIO_DIR_PATH, "aiholo_prompt_additions.txt");
71+
if (Files.exists(additionsPath)) {
72+
aiholo_prompt_additions = new String(Files.readAllBytes(additionsPath), StandardCharsets.UTF_8).trim();
73+
System.out.println("Loaded aiholo_prompt_additions: " + aiholo_prompt_additions);
74+
}
75+
} catch (Exception e) {
76+
System.err.println("Could not load aiholo_prompt_additions.txt: " + e.getMessage());
77+
}
78+
}
79+
}
80+
5881
private static final String DEFAULT_LANGUAGE_CODE = "es-ES";
5982
private static final String DEFAULT_VOICE_NAME = "es-ES-Wavenet-D";
6083
private final static String sql = """
@@ -94,38 +117,38 @@ private void startInactivityMonitor() {
94117

95118
@GetMapping("")
96119
public String home(@RequestParam(value = "languageCode", defaultValue = "en-US") String languageCode, Model model) {
97-
System.out.println("AIHolo root languageCode = " + languageCode );
120+
System.out.println("AIHolo root languageCode = " + languageCode);
98121
this.languageCode = languageCode;
99122
model.addAttribute("languageCode", languageCode);
100123
if (languageCode.equals("pt-BR"))
101124
model.addAttribute("voiceName", "pt-BR-Wavenet-D");
102125
else if (languageCode.equals("es-ES"))
103126
model.addAttribute("voiceName", "es-ES-Wavenet-D");
104-
else if (languageCode.equals("zh-SG") )
127+
else if (languageCode.equals("zh-SG"))
105128
model.addAttribute("voiceName", "cmn-CN-Wavenet-A");
106-
else if (languageCode.equals("de-DE") )
129+
else if (languageCode.equals("de-DE"))
107130
model.addAttribute("voiceName", "de-DE-Wavenet-A");
108-
else if (languageCode.equals("es-MX") )
131+
else if (languageCode.equals("es-MX"))
109132
model.addAttribute("voiceName", "es-US-Wavenet-A");
110-
else if (languageCode.equals("it-IT") )
133+
else if (languageCode.equals("it-IT"))
111134
model.addAttribute("voiceName", "it-IT-Wavenet-A");
112-
else if (languageCode.equals("fr-FR") )
135+
else if (languageCode.equals("fr-FR"))
113136
model.addAttribute("voiceName", "fr-FR-Wavenet-A");
114-
else if (languageCode.equals("ro-RO") )
137+
else if (languageCode.equals("ro-RO"))
115138
model.addAttribute("voiceName", "ro-RO-Wavenet-A");
116-
else if (languageCode.equals("en-AU") )
139+
else if (languageCode.equals("en-AU"))
117140
model.addAttribute("voiceName", "en-AU-Wavenet-A");
118-
else if (languageCode.equals("ga-GA") )
141+
else if (languageCode.equals("ga-GA"))
119142
model.addAttribute("voiceName", "ga-GA-Wavenet-A");
120-
else if (languageCode.equals("ar-AE") )
143+
else if (languageCode.equals("ar-AE"))
121144
model.addAttribute("voiceName", "ar-AE-Wavenet-A");
122-
else if (languageCode.equals("ja-JP") )
145+
else if (languageCode.equals("ja-JP"))
123146
model.addAttribute("voiceName", "ja-JP-Wavenet-A");
124-
else if (languageCode.equals("hi-IN") )
147+
else if (languageCode.equals("hi-IN"))
125148
model.addAttribute("voiceName", "hi-IN-Wavenet-A");
126-
else if (languageCode.equals("he-IL") )
149+
else if (languageCode.equals("he-IL"))
127150
model.addAttribute("voiceName", "he-IL-Wavenet-A");
128-
else if (languageCode.equals("en-US") )
151+
else if (languageCode.equals("en-US"))
129152
model.addAttribute("voiceName", "Aoede");
130153
// model.addAttribute("voiceName", "en-US-Chirp3-HD-Aoede");
131154
else if (languageCode.equals("en-GB"))
@@ -135,7 +158,6 @@ else if (languageCode.equals("en-GB"))
135158
}
136159

137160

138-
139161
@GetMapping("/explainer")
140162
@ResponseBody
141163
public String explainer() throws Exception {
@@ -144,7 +166,7 @@ public String explainer() throws Exception {
144166
String filePath = "C:/Users/opc/aiholo_output.txt";
145167
try (FileWriter writer = new FileWriter(filePath)) {
146168
JSONObject json = new JSONObject();
147-
json.put("data", theValue); // Store the response inside JSON
169+
json.put("data", theValue);
148170
writer.write(json.toString());
149171
writer.flush();
150172
} catch (IOException e) {
@@ -154,13 +176,31 @@ public String explainer() throws Exception {
154176
return "Explained";
155177
}
156178

179+
@GetMapping("/leia")
180+
@ResponseBody
181+
public String leia() throws Exception {
182+
System.out.println("AIHoloController.leia");
183+
theValue = "leia";
184+
String filePath = "C:/Users/opc/aiholo_output.txt";
185+
try (FileWriter writer = new FileWriter(filePath)) {
186+
JSONObject json = new JSONObject();
187+
json.put("data", theValue);
188+
writer.write(json.toString());
189+
writer.flush();
190+
} catch (IOException e) {
191+
return "Error writing to file: " + e.getMessage();
192+
}
193+
// TTSAndAudio2Face.sendToAudio2Face("explainer-leia.wav");
194+
return "leia hologram";
195+
}
196+
157197

158198
@GetMapping("/play")
159199
@ResponseBody
160200
public String play(@RequestParam("question") String question,
161-
@RequestParam("selectedMode") String selectedMode,
162-
@RequestParam("languageCode") String languageCode,
163-
@RequestParam("voiceName") String voicename) throws Exception {
201+
@RequestParam("selectedMode") String selectedMode,
202+
@RequestParam("languageCode") String languageCode,
203+
@RequestParam("voiceName") String voicename) throws Exception {
164204
System.out.println(
165205
"play question: " + question + " selectedMode: " + selectedMode +
166206
" languageCode:" + languageCode + " voicename:" + voicename);
@@ -211,7 +251,8 @@ public String play(@RequestParam("question") String question,
211251
String action = "chat";
212252
String answer;
213253
if (languageCode.equals("pt-BR")) answer = "Desculpe. Não consegui encontrar uma resposta no banco de dados";
214-
else if (languageCode.equals("es-ES")) answer = "Lo siento, no pude encontrar una respuesta en la base de datos.";
254+
else if (languageCode.equals("es-ES"))
255+
answer = "Lo siento, no pude encontrar una respuesta en la base de datos.";
215256
else if (languageCode.equals("en-GB")) answer = "Sorry, I couldn't find an answer in the database.";
216257
else if (languageCode.equals("zh-SG")) answer = "抱歉,我在数据库中找不到答案";
217258
else answer = "I'm sorry. I couldn't find an answer in the database";
@@ -226,8 +267,7 @@ public String play(@RequestParam("question") String question,
226267
} else {
227268
question = question.replace("use chat", "").trim();
228269
}
229-
question += ". If the question is something like \"Where can I find free resources to learn about new\n" +
230-
"technology trends?\" Then incorporate Oracle Academy information in the answer but balance the answer with other non-Oracle information. Respond in 20 words or less";
270+
question += ". Respond in 20 words or less. " + aiholo_prompt_additions;
231271
try (Connection connection = dataSource.getConnection();
232272
PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
233273
System.out.println("Database Connection : " + connection);
@@ -247,31 +287,35 @@ public String play(@RequestParam("question") String question,
247287
}
248288
String fileName = "output.wav";
249289

250-
// Strip out any "A:", "A2:", "A3:", etc. at the beginning of the answer string
251-
if (answer != null) {
252-
answer = answer.replaceFirst("^A\\d*:\\s*", "");
253-
}
290+
// Strip out any "A:", "A2:", "A3:", etc. at the beginning of the answer string
291+
if (answer != null) {
292+
answer = answer.replaceFirst("^A\\d*:\\s*", "");
293+
}
254294

255295
System.out.println("about to TTS and sendAudioToAudio2Face for answer: " + answer);
256296
TTSAndAudio2Face.processMetahuman(fileName, answer, languageCode, voicename);
297+
if(answer.toLowerCase().contains("leia") || answer.toLowerCase().contains("star wars")) {
298+
Thread.sleep(5);
299+
leia();
300+
}
257301
return answer;
258302
}
259303

260304

261305
/**
262-
curl -X 'POST' \
263-
'http://host/v1/chat/completions?client=server' \
264-
-H 'accept: application/json' \
265-
-H 'Authorization: Bearer bearer' \
266-
-H 'Content-Type: application/json' \
267-
-d '{
268-
"messages": [
269-
{
270-
"role": "user",
271-
"content": "What are Alternative Dispute Resolution"
272-
}
273-
]
274-
}'
306+
* curl -X 'POST' \
307+
* 'http://host/v1/chat/completions?client=server' \
308+
* -H 'accept: application/json' \
309+
* -H 'Authorization: Bearer bearer' \
310+
* -H 'Content-Type: application/json' \
311+
* -d '{
312+
* "messages": [
313+
* {
314+
* "role": "user",
315+
* "content": "What are Alternative Dispute Resolution"
316+
* }
317+
* ]
318+
* }'
275319
*/
276320

277321
public String executeSandbox(String cummulativeResult) {
@@ -280,7 +324,7 @@ public String executeSandbox(String cummulativeResult) {
280324
Map<String, String> message = new HashMap<>();
281325
message.put("role", "user");
282326
message.put("content", cummulativeResult);
283-
payload.put("messages", new Object[] { message });
327+
payload.put("messages", new Object[]{message});
284328
JSONObject jsonPayload = new JSONObject(payload);
285329
HttpHeaders headers = new HttpHeaders();
286330
headers.setContentType(MediaType.APPLICATION_JSON);
@@ -309,15 +353,14 @@ public String executeSandbox(String cummulativeResult) {
309353
*/
310354

311355

312-
313356
// `https://host:port/aiholo/tts?textToConvert=${encodeURIComponent(textToConvert)}
314357
// &languageCode=${encodeURIComponent(languageCode)}&ssmlGender=${encodeURIComponent(ssmlGender)}
315358
// &voiceName=${encodeURIComponent(voiceName)}`;
316359
@GetMapping("/tts")
317-
public ResponseEntity<byte[]> ttsAndReturnAudioFile(@RequestParam("textToConvert") String textToConvert,
318-
@RequestParam("languageCode") String languageCode,
319-
@RequestParam("ssmlGender") String ssmlGender,
320-
@RequestParam("voiceName") String voiceName) throws Exception {
360+
public ResponseEntity<byte[]> ttsAndReturnAudioFile(@RequestParam("textToConvert") String textToConvert,
361+
@RequestParam("languageCode") String languageCode,
362+
@RequestParam("ssmlGender") String ssmlGender,
363+
@RequestParam("voiceName") String voiceName) throws Exception {
321364
System.out.println("TTS GCP textToConvert = " + textToConvert + ", languageCode = " + languageCode +
322365
", ssmlGender = " + ssmlGender + ", voiceName = " + voiceName);
323366
try (TextToSpeechClient textToSpeechClient = TextToSpeechClient.create()) {
@@ -341,14 +384,13 @@ public ResponseEntity<byte[]> ttsAndReturnAudioFile(@RequestParam("textToConver
341384
HttpHeaders headers = new HttpHeaders();
342385
headers.set(HttpHeaders.CONTENT_TYPE, "audio/mpeg");
343386
headers.set(HttpHeaders.CONTENT_DISPOSITION,
344-
"attachment; filename=\"tts-" + languageCode + "" + ssmlGender+ "" + voiceName + "_" +
387+
"attachment; filename=\"tts-" + languageCode + "" + ssmlGender + "" + voiceName + "_" +
345388
getFirst10Chars(textToConvert) + ".mp3\"");
346389
return new ResponseEntity<>(audioData, headers, HttpStatus.OK);
347390
}
348391
}
349392

350393

351-
352394
// Vector embedding, store, langchain, etc. stuff...
353395

354396

@@ -384,7 +426,7 @@ public String langchain(@RequestParam("question") String question,
384426
@RequestParam("selectedMode") String selectedMode,
385427
@RequestParam("languageCode") String languageCode,
386428
@RequestParam("voiceName") String voicename) throws Exception {
387-
EmbeddingSearchRequest embeddingSearchRequest = null;
429+
EmbeddingSearchRequest embeddingSearchRequest = null;
388430
OracleEmbeddingStore embeddingStore =
389431
OracleEmbeddingStore.builder()
390432
.dataSource(dataSource)
@@ -404,12 +446,9 @@ public String langchain(@RequestParam("question") String question,
404446
}
405447

406448

407-
408449
//set/get etc utilites to end....
409450

410451

411-
412-
413452
public static String getFirst10Chars(String textToConvert) {
414453
if (textToConvert == null || textToConvert.isEmpty()) {
415454
return "";
@@ -472,29 +511,28 @@ public String playArbitrary(
472511
}
473512

474513
/**
475-
en-US (American English):
476-
• en-US-Neural2-F 
477-
• en-US-Neural2-G 
478-
• en-US-Neural2-H
479-
• en-US-Neural2-I 
480-
• en-US-Neural2-J 
481-
• en-US-Standard-C 
482-
• en-US-Standard-E
483-
• en-US-Standard-G 
484-
• en-US-Standard-I
485-
• en-US-Wavenet-C 
486-
• en-US-Wavenet-E 
487-
• en-US-Wavenet-G
488-
• en-US-Wavenet-I
489-
490-
en-GB (British English):
491-
• en-GB-Neural2-C 
492-
• en-GB-Neural2-E 
493-
• en-GB-Standard-A 
494-
• en-GB-Standard-C 
495-
• en-GB-Standard-E
496-
• en-GB-Wavenet-A 
497-
• en-GB-Wavenet-C 
498-
• en-GB-Wavenet-E
499-
514+
* en-US (American English):
515+
* • en-US-Neural2-F 
516+
* • en-US-Neural2-G 
517+
* • en-US-Neural2-H
518+
* • en-US-Neural2-I 
519+
* • en-US-Neural2-J 
520+
* • en-US-Standard-C 
521+
* • en-US-Standard-E
522+
* • en-US-Standard-G 
523+
* • en-US-Standard-I
524+
* • en-US-Wavenet-C 
525+
* • en-US-Wavenet-E 
526+
* • en-US-Wavenet-G
527+
* • en-US-Wavenet-I
528+
* <p>
529+
* en-GB (British English):
530+
* • en-GB-Neural2-C 
531+
* • en-GB-Neural2-E 
532+
* • en-GB-Standard-A 
533+
* • en-GB-Standard-C 
534+
* • en-GB-Standard-E
535+
* • en-GB-Wavenet-A 
536+
* • en-GB-Wavenet-C 
537+
* • en-GB-Wavenet-E
500538
*/
Loading

0 commit comments

Comments
 (0)