Skip to content

Commit 534d0df

Browse files
committed
new aiholo speech impl
1 parent 3313232 commit 534d0df

File tree

2 files changed

+356
-0
lines changed

2 files changed

+356
-0
lines changed
Lines changed: 326 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,326 @@
1+
package oracleai.aiholo;
2+
3+
import java.io.FileOutputStream;
4+
import java.io.FileWriter;
5+
import java.io.IOException;
6+
import java.io.OutputStream;
7+
8+
import org.json.JSONObject;
9+
import org.springframework.web.bind.annotation.*;
10+
import org.springframework.web.client.RestTemplate;
11+
12+
import com.google.cloud.texttospeech.v1.AudioEncoding;
13+
import com.google.cloud.texttospeech.v1.SsmlVoiceGender;
14+
import com.google.cloud.texttospeech.v1.SynthesisInput;
15+
import com.google.cloud.texttospeech.v1.SynthesizeSpeechResponse;
16+
import com.google.cloud.texttospeech.v1.TextToSpeechClient;
17+
import com.google.cloud.texttospeech.v1.VoiceSelectionParams;
18+
import com.google.protobuf.ByteString;
19+
import com.google.cloud.texttospeech.v1.AudioConfig;
20+
21+
import org.springframework.beans.factory.annotation.Autowired;
22+
23+
import javax.sql.*;
24+
25+
import java.sql.*;
26+
import java.util.HashMap;
27+
import java.util.Map;
28+
import org.springframework.http.*;
29+
import org.springframework.stereotype.Service;
30+
31+
@RestController
32+
@RequestMapping("/aiholo")
33+
// @CrossOrigin(origins = "*")
34+
public class AIHoloController {
35+
private String theValue = "mirrorme";
36+
37+
38+
private static final String API_URL = "http://129.x.x.x/v1/chat/completions?client=server";
39+
private static final String AUTH_TOKEN = "Bearer asdf";
40+
41+
@Autowired
42+
private DataSource dataSource;
43+
44+
@GetMapping("/set")
45+
public String setValue(@RequestParam("value") String value) { // TTSoutput.wav
46+
theValue = value;
47+
System.out.println("EchoController set: " + theValue);
48+
String filePath = "C:/Users/opc/aiholo_output.txt";
49+
try (FileWriter writer = new FileWriter(filePath)) {
50+
JSONObject json = new JSONObject();
51+
json.put("data", value); // Store the response inside JSON
52+
writer.write(json.toString());
53+
writer.flush();
54+
} catch (IOException e) {
55+
return "Error writing to file: " + e.getMessage();
56+
}
57+
58+
if (value.equals("mirrorme") || value.equals("question"))
59+
return "「ミラーミー」モードが正常に有効化されました";
60+
else
61+
return "set successfully: " + theValue;
62+
63+
}
64+
65+
@GetMapping("/get")
66+
public String getValue() {
67+
System.out.println("EchoController get: " + theValue);
68+
return theValue;
69+
}
70+
71+
static String sql = """
72+
SELECT DBMS_CLOUD_AI.GENERATE(
73+
prompt => ?,
74+
profile_name => 'AIHOLO',
75+
action => ?
76+
) FROM dual
77+
""";
78+
79+
@GetMapping("/play")
80+
public String play(@RequestParam("question") String question,
81+
@RequestParam("languagecode") String languagecode,
82+
@RequestParam("voicename") String voicename) throws Exception {
83+
System.out.println("play question: " + question + " languagecode:"+ languagecode);
84+
85+
theValue = "question";
86+
String filePath = "C:/Users/opc/aiholo_output.txt";
87+
try (FileWriter writer = new FileWriter(filePath)) {
88+
JSONObject json = new JSONObject();
89+
json.put("data", theValue); // Store the response inside JSON
90+
writer.write(json.toString());
91+
writer.flush();
92+
} catch (IOException e) {
93+
return "Error writing to file: " + e.getMessage();
94+
}
95+
96+
String answer = "I'm sorry. I couldn't find an answer", action = "chat";
97+
98+
if (question.contains("use vectorrag")) {
99+
action = "vectorrag";
100+
question = question.replace("use vectorrag", "").trim();
101+
answer = executeSandbox(question);
102+
} else {
103+
if (question.contains("use narrate")) {
104+
action = "narrate";
105+
question = question.replace("use narrate", "").trim();
106+
} else {
107+
question = question.replace("use chat", "").trim();
108+
}
109+
try (Connection connection = dataSource.getConnection();
110+
PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
111+
System.out.println("✅ Database Connection Established: " + connection);
112+
String response = null;
113+
preparedStatement.setString(1, question);
114+
preparedStatement.setString(2, action);
115+
116+
try (ResultSet resultSet = preparedStatement.executeQuery()) {
117+
if (resultSet.next()) {
118+
response = resultSet.getString(1); // Retrieve AI response from the first column
119+
}
120+
}
121+
answer = response;
122+
123+
} catch (SQLException e) {
124+
System.err.println("❌ Failed to connect to the database: " + e.getMessage());
125+
return "Database Connection Failed!";
126+
}
127+
// String answer = "トム・ハンクス主演の映画は何ですか";
128+
}
129+
System.out.println("about tp sendAudioToAudio2Face answer: " + answer);
130+
String fileName = "output.wav";
131+
TTS(answer, languagecode, voicename);
132+
RestTemplate restTemplate = new RestTemplate();
133+
String baseUrl = "http://localhost:8011/A2F/Player/";
134+
135+
// ✅ Step 1: SetRootPath
136+
String setRootPathUrl = baseUrl + "SetRootPath";
137+
Map<String, Object> rootPathPayload = new HashMap<>();
138+
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+
rootPathPayload.put("dir_path", "C:/Users/opc/src/github.com/paulparkinson/oracle-ai-for-sustainable-dev/java-ai");
142+
sendPostRequest(restTemplate, setRootPathUrl, rootPathPayload);
143+
144+
// ✅ Step 2: SetTrack
145+
String setTrackUrl = baseUrl + "SetTrack";
146+
Map<String, Object> trackPayload = new HashMap<>();
147+
trackPayload.put("a2f_player", "/World/audio2face/Player");
148+
trackPayload.put("file_name", fileName);
149+
trackPayload.put("time_range", new int[] { 0, -1 });
150+
sendPostRequest(restTemplate, setTrackUrl, trackPayload);
151+
152+
// ✅ Step 3: PlayTrack
153+
String playTrackUrl = baseUrl + "Play";
154+
Map<String, Object> playPayload = new HashMap<>();
155+
playPayload.put("a2f_player", "/World/audio2face/Player");
156+
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+
}
169+
170+
// 📡 Helper function to send HTTP POST requests
171+
private void sendPostRequest(RestTemplate restTemplate, String url, Map<String, Object> payload) {
172+
HttpHeaders headers = new HttpHeaders();
173+
headers.setContentType(MediaType.APPLICATION_JSON);
174+
HttpEntity<Map<String, Object>> request = new HttpEntity<>(payload, headers);
175+
176+
ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class);
177+
if (response.getStatusCode().is2xxSuccessful()) {
178+
System.out.println("✅ Successfully sent request to: " + url);
179+
} else {
180+
System.err.println("❌ Failed to send request to " + url + ". Response: " + response.getBody());
181+
}
182+
}
183+
184+
public String executeSandbox(String cummulativeResult) {
185+
System.out.println("isRag is true, using AI sandbox: " + cummulativeResult);
186+
187+
// Remove "use RAG" references
188+
// cummulativeResult = cummulativeResult.replace("use RAG", "").replace("use rag", "").trim();
189+
// cummulativeResult += " . Make answer one sentence that is shorter than 50 words";
190+
191+
// Prepare request body
192+
Map<String, Object> payload = new HashMap<>();
193+
Map<String, String> message = new HashMap<>();
194+
message.put("role", "user");
195+
message.put("content", cummulativeResult);
196+
payload.put("messages", new Object[] { message });
197+
198+
// Convert payload to JSON
199+
JSONObject jsonPayload = new JSONObject(payload);
200+
201+
// Set headers
202+
HttpHeaders headers = new HttpHeaders();
203+
headers.setContentType(MediaType.APPLICATION_JSON);
204+
headers.set("Authorization", AUTH_TOKEN);
205+
headers.set("Accept", "application/json");
206+
207+
HttpEntity<String> request = new HttpEntity<>(jsonPayload.toString(), headers);
208+
209+
RestTemplate restTemplate = new RestTemplate();
210+
ResponseEntity<String> response = restTemplate.exchange(API_URL, HttpMethod.POST, request, String.class);
211+
212+
String latestAnswer;
213+
if (response.getStatusCode() == HttpStatus.OK) {
214+
JSONObject responseData = new JSONObject(response.getBody());
215+
latestAnswer = responseData.getJSONArray("choices").getJSONObject(0).getJSONObject("message")
216+
.getString("content");
217+
218+
System.out.println("RAG Full Response latest_answer: " + latestAnswer);
219+
return latestAnswer;
220+
} else {
221+
System.out.println("Failed to fetch data: " + response.getStatusCode() + " " + response.getBody());
222+
return " I'm sorry, I couldn't find an answer";
223+
}
224+
}
225+
// `https://141.148.204.74:8444/aiholo/tts?textToConvert=${encodeURIComponent(textToConvert)}&languageCode=${encodeURIComponent(languageCode)}&ssmlGender=${encodeURIComponent(ssmlGender)}&voiceName=${encodeURIComponent(voiceName)}`;
226+
227+
228+
public void TTS(String text, String languageCode, String voicename) throws Exception {
229+
try (TextToSpeechClient textToSpeechClient = TextToSpeechClient.create()) {
230+
System.out.println("in TTS languagecode:" + languageCode + " text:"+text);
231+
SynthesisInput input = SynthesisInput.newBuilder().setText(
232+
// "最受欢迎的游戏是Pods Of Kon。").build();
233+
text).build();
234+
// "最も人気のあるビデオゲームは「Pods Of Kon」です。").build();
235+
236+
VoiceSelectionParams voice =
237+
VoiceSelectionParams.newBuilder()
238+
.setLanguageCode(languageCode)
239+
// .setLanguageCode("ja-JP")
240+
// .setLanguageCode("en-US")
241+
.setSsmlGender(SsmlVoiceGender.FEMALE)
242+
// .setSsmlGender(SsmlVoiceGender.NEUTRAL)
243+
// .setName("pt-BR-Wavenet-D") // tts-pt-BRFEMALEpt-BR-Wavenet-D_Bem-vindo
244+
.setName(voicename) // "Kore" tts-pt-BRFEMALEpt-BR-Wavenet-D_Bem-vindo
245+
.build();
246+
247+
248+
// Select the type of audio file you want returned
249+
AudioConfig audioConfig =
250+
AudioConfig.newBuilder()
251+
.setAudioEncoding(AudioEncoding.LINEAR16) // wav
252+
// .setAudioEncoding(AudioEncoding.MP3)
253+
.build();
254+
255+
// Perform the text-to-speech request on the text input with the selected voice parameters and
256+
// audio file type
257+
SynthesizeSpeechResponse response =
258+
textToSpeechClient.synthesizeSpeech(input, voice, audioConfig);
259+
260+
// Get the audio contents from the response
261+
ByteString audioContents = response.getAudioContent();
262+
263+
// Write the response to the output file.
264+
try (OutputStream out = new FileOutputStream("output.wav")) {
265+
out.write(audioContents.toByteArray());
266+
System.out.println("Audio content written to file \"output.wav\"");
267+
}
268+
}
269+
}
270+
// `https://141.148.204.74:8444/aiholo/tts?textToConvert=${encodeURIComponent(textToConvert)}&languageCode=${encodeURIComponent(languageCode)}&ssmlGender=${encodeURIComponent(ssmlGender)}&voiceName=${encodeURIComponent(voiceName)}`;
271+
272+
273+
@GetMapping("/tts")
274+
public ResponseEntity<byte[]> tts(@RequestParam("textToConvert") String textToConvert,
275+
@RequestParam("languageCode") String languageCode,
276+
@RequestParam("ssmlGender") String ssmlGender,
277+
@RequestParam("voiceName") String voiceName) throws Exception {
278+
String info= "tts for textToConvert " + textToConvert;
279+
System.out.println("in TTS GCP info:" + info);
280+
// Instantiates a client
281+
try (TextToSpeechClient textToSpeechClient = TextToSpeechClient.create()) {
282+
System.out.println("in TTS GCP textToSpeechClient:" + textToSpeechClient + " languagecode:" + languageCode);
283+
SynthesisInput input = SynthesisInput.newBuilder().setText(textToConvert).build();
284+
VoiceSelectionParams voice =
285+
VoiceSelectionParams.newBuilder()
286+
.setLanguageCode(languageCode)
287+
// .setSsmlGender(SsmlVoiceGender.NEUTRAL)
288+
.setSsmlGender(SsmlVoiceGender.FEMALE)
289+
.setName(voiceName)
290+
// .setName("pt-BR-Wavenet-A")
291+
.build();
292+
AudioConfig audioConfig =
293+
AudioConfig.newBuilder()
294+
.setAudioEncoding(AudioEncoding.LINEAR16) // wav
295+
// .setAudioEncoding(AudioEncoding.MP3)
296+
.build();
297+
SynthesizeSpeechResponse response =
298+
textToSpeechClient.synthesizeSpeech(input, voice, audioConfig);
299+
ByteString audioContents = response.getAudioContent();
300+
byte[] audioData = audioContents.toByteArray();
301+
302+
// Set response headers
303+
HttpHeaders headers = new HttpHeaders();
304+
headers.set(HttpHeaders.CONTENT_TYPE, "audio/mpeg");
305+
headers.set(HttpHeaders.CONTENT_DISPOSITION,
306+
"attachment; filename=\"tts-" + languageCode + "" + ssmlGender+ "" + voiceName + "_" +
307+
getFirst10Chars(textToConvert) + ".mp3\"");
308+
309+
return new ResponseEntity<>(audioData, headers, HttpStatus.OK);
310+
311+
// try (OutputStream out = new FileOutputStream("output.wav")) {
312+
// out.write(audioContents.toByteArray());
313+
// System.out.println("Audio content written to file \"output.wav\"");
314+
// }
315+
}
316+
317+
// return "succesful " + info;
318+
}
319+
320+
public static String getFirst10Chars(String textToConvert) {
321+
if (textToConvert == null || textToConvert.isEmpty()) {
322+
return "";
323+
}
324+
return textToConvert.length() > 10 ? textToConvert.substring(0, 10) : textToConvert;
325+
}
326+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package oracleai.aiholo;
2+
3+
import oracle.jdbc.pool.OracleDataSource;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.Configuration;
6+
7+
import javax.sql.DataSource;
8+
import java.sql.Connection;
9+
import java.sql.SQLException;
10+
11+
@Configuration
12+
public class DataSourceConfiguration {
13+
14+
@Bean
15+
public DataSource dataSource() throws SQLException {
16+
OracleDataSource dataSource = new OracleDataSource();
17+
dataSource.setUser("moviestream");
18+
dataSource.setPassword("Welcome12345");
19+
20+
// ✅ Ensure the correct service name and TNS_ADMIN wallet location
21+
dataSource.setURL("jdbc:oracle:thin:@selectaidb_high?TNS_ADMIN=C:/Users/opc/Downloads/Wallet_SelectAIDB");
22+
23+
// ✅ Check if the connection is successful (print the first query)
24+
try (Connection connection = dataSource.getConnection()) {
25+
System.out.println("✅ Successfully connected to Oracle DB: " + connection.getMetaData().getDatabaseProductVersion());
26+
}
27+
28+
return dataSource;
29+
}
30+
}

0 commit comments

Comments
 (0)