Skip to content

Commit f526f39

Browse files
author
Rodrigo Chafik Choueiri
committed
changed to use connection with thick mode with an ATP using mTls enabled, upated some classes oci_ai_speech_realtime and wav format is not available
1 parent f34cfb8 commit f526f39

File tree

1 file changed

+44
-46
lines changed

1 file changed

+44
-46
lines changed

python-realtimeSTT-NL2SQL-TTS/src/RealtimeSTT_NL2SQL_TTS_AudioPlayback.py

Lines changed: 44 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
import oci
1212
from oci.config import from_file
1313
from oci.auth.signers.security_token_signer import SecurityTokenSigner
14-
from oci.ai_speech_realtime import (
15-
RealtimeClient,
16-
RealtimeClientListener,
14+
from oci_ai_speech_realtime import (
15+
RealtimeSpeechClient,
16+
RealtimeSpeechClientListener,
1717
RealtimeParameters,
1818
)
1919
from aiohttp import web
@@ -27,13 +27,17 @@
2727
compartment_id = os.getenv('COMPARTMENT_ID')
2828
print(f"compartment_id: {compartment_id}")
2929

30+
# oracle atp with mTls enabled, use Thick mode [oracledb.init_oracle_client] to load all libraries neeeded
31+
# download from https://www.oracle.com/database/technologies/instant-client/winx64-64-downloads.html
32+
# main doc for mTLS https://docs.oracle.com/en/cloud/paas/autonomous-database/serverless/adbsb/connecting-python-mtls.html
33+
oracledb.init_oracle_client(lib_dir=r"C:\Users\Rodrigo Chafik\Downloads\instantclient-basic-windows.x64-23.7.0.25.01\instantclient_23_7")
3034
connection = oracledb.connect(
31-
user="moviestream",
32-
password="Welcome12345",
33-
dsn="selectaidb_high",
34-
config_dir="/Users/pparkins/Downloads/Wallet_SelectAIDB",
35-
wallet_location="/Users/pparkins/Downloads/Wallet_SelectAIDB",
36-
wallet_password="Welcome12345"
35+
user="aiuser",
36+
password="OracleATeam#2025",
37+
dsn="chafik_high",
38+
config_dir=r"D:\aTeam\Wallet_chafik",
39+
wallet_location=r"D:\aTeam\Wallet_chafik",
40+
wallet_password="ateamOracle@2024"
3741
)
3842
print(f"Successfully connected to Oracle Database Connection: {connection}")
3943

@@ -105,7 +109,7 @@ def play_audio(file_path):
105109
print(f"Error playing audio: {e}")
106110

107111

108-
class SpeechListener(RealtimeClientListener):
112+
class SpeechListener(RealtimeSpeechClientListener):
109113
def on_result(self, result):
110114
global cummulativeResult, isSelect, isNarrate, isShowSQL, isRunSQL, isExplainSQL, last_result_time
111115
if result["transcriptions"][0]["isFinal"]:
@@ -174,7 +178,7 @@ async def check_idle():
174178

175179

176180
def authenticator():
177-
config = from_file("~/.oci/config", "MYSPEECHAIPROFILE")
181+
config = from_file("~/.oci/config", "DEFAULT")
178182
with open(config["security_token_file"], "r") as f:
179183
token = f.readline()
180184
private_key = oci.signer.load_private_key_from_file(config["key_file"])
@@ -199,7 +203,7 @@ def executeSelectAI():
199203

200204
query = """SELECT DBMS_CLOUD_AI.GENERATE(
201205
prompt => :prompt,
202-
profile_name => 'AIHOLO',
206+
profile_name => 'GENAI',
203207
action => :action)
204208
FROM dual"""
205209

@@ -219,32 +223,36 @@ def executeSelectAI():
219223

220224
if selectai_action in ("showsql", "runsql", "explainsql"):
221225
return
222-
# API key-based authentication...
223-
config = oci.config.from_file("~/.oci/config", "DEFAULT")
224-
speech_client = AIServiceSpeechClient(config)
225-
226-
text_to_speech = SynthesizeSpeechDetails(
227-
text=f" {latest_answer}",
228-
is_stream_enabled=False,
229-
configuration=oci.ai_speech.models.TtsOracleConfiguration(
230-
model_family="ORACLE",
231-
# Brian Annabelle Bob Stacy Phil Cindy Brad
232-
model_details=oci.ai_speech.models.TtsOracleTts2NaturalModelDetails(voice_id="Brian"),
233-
speech_settings=oci.ai_speech.models.TtsOracleSpeechSettings(
234-
speech_mark_types=["WORD"]
235-
),
236-
)
237-
)
238-
239-
response = speech_client.synthesize_speech(synthesize_speech_details=text_to_speech)
240-
241-
with open("TTSoutput.wav", "wb") as audio_file:
242-
audio_file.write(response.data.content)
226+
# API key-based authentication, using phoenix OCI Region - https://docs.oracle.com/en-us/iaas/Content/speech/using/speech.htm#ser-limits
227+
config = oci.config.from_file("~/.oci/config", "TESTES")
228+
229+
ai_speech_client = oci.ai_speech.AIServiceSpeechClient(config)
230+
synthesize_speech_response = ai_speech_client.synthesize_speech(
231+
synthesize_speech_details=oci.ai_speech.models.SynthesizeSpeechDetails(
232+
text=f" {latest_answer}",
233+
is_stream_enabled=True,
234+
compartment_id=compartment_id,
235+
configuration=oci.ai_speech.models.TtsOracleConfiguration(
236+
model_family="ORACLE",
237+
model_details=oci.ai_speech.models.TtsOracleTts1StandardModelDetails(
238+
model_name="TTS_1_STANDARD",
239+
voice_id="Bob"),
240+
speech_settings=oci.ai_speech.models.TtsOracleSpeechSettings(
241+
text_type="SSML",
242+
sample_rate_in_hz=28000,
243+
output_format="PCM",
244+
speech_mark_types=["WORD"])),
245+
audio_config=oci.ai_speech.models.TtsBaseAudioConfig(
246+
config_type="BASE_AUDIO_CONFIG")
247+
) )
248+
249+
with open("TTSoutput.pcm", "wb") as audio_file:
250+
audio_file.write(synthesize_speech_response.data.content)
243251

244252
print("Speech synthesis completed and saved as TTSoutput.wav")
245253

246254
# Play the generated speech
247-
play_audio("TTSoutput.wav")
255+
play_audio("TTSoutput.pcm")
248256

249257
except Exception as e:
250258
print(f"An error occurred: {e}")
@@ -271,8 +279,8 @@ async def handle_request(request):
271279
)
272280
realtime_speech_parameters.final_silence_threshold_in_ms = 2000
273281

274-
realtime_speech_url = "wss://realtime.aiservice.us-phoenix-1.oci.oraclecloud.com"
275-
client = RealtimeClient(
282+
realtime_speech_url = "wss://realtime.aiservice.us-ashburn-1.oci.oraclecloud.com"
283+
client = RealtimeSpeechClient(
276284
config=config,
277285
realtime_speech_parameters=realtime_speech_parameters,
278286
listener=SpeechListener(),
@@ -281,16 +289,6 @@ async def handle_request(request):
281289
compartment_id=compartment_id,
282290
)
283291

284-
# Instance, resource principal, or session token-based authentication (as shown below) can also be used
285-
# client = RealtimeClient(
286-
# config=config,
287-
# realtime_speech_parameters=realtime_speech_parameters,
288-
# listener=SpeechListener(),
289-
# service_endpoint=realtime_speech_url,
290-
# signer=authenticator(),
291-
# compartment_id=compartment_id,
292-
# )
293-
294292
loop.create_task(send_audio(client))
295293
loop.create_task(check_idle())
296294

0 commit comments

Comments
 (0)