-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatto-speech-to-text.py
56 lines (45 loc) · 1.99 KB
/
watto-speech-to-text.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import json
from os.path import join, dirname
from ibm_watson import SpeechToTextV1
from ibm_watson.websocket import RecognizeCallback, AudioSource
import threading
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator
# Set up the authentication
authenticator = IAMAuthenticator('EVbgkVy_v-9PfBfZvHukYnW9bbq1-OGen1qA4taporwV') # Replace <api_key> with your API key
speech_to_text = SpeechToTextV1(authenticator=authenticator)
speech_to_text.set_service_url('https://api.us-south.speech-to-text.watson.cloud.ibm.com/instances/a2fb20ab-8b68-4ba2-b88a-34a972e0afa3') # Replace <service_url> with your service URL
# Set the audio file path
audio_file = '/Users/shan/Documents/Assets Backup/Watsonx-Support Ticket Insights/combined_audio.wav' # Replace with your audio file path
model = speech_to_text.get_model('en-US_BroadbandModel').get_result()
print(json.dumps(model, indent=2))
with open(join(dirname(__file__), '/Users/shan/Documents/Assets Backup/Watsonx-Support Ticket Insights/output_0.wav'),
'rb') as audio_file:
print(json.dumps(
speech_to_text.recognize(
audio=audio_file,
content_type='audio/wav',
timestamps=True,
word_confidence=True).get_result(),
indent=2))
# Perform transcription
with open(audio_file, 'rb') as audio_file:
response = speech_to_text.recognize(
audio=audio_file,
content_type='audio/wav',
model='en-US_BroadbandModel',
continuous=True
).get_result()
# Extract the transcriptions from the response
transcriptions = [result['alternatives'][0]['transcript'] for result in response['results']]
# # Print the transcriptions
# for transcription in transcriptions:
# print(transcription)
# Create a dictionary to store transcriptions
data = {
'transcriptions': transcriptions
}
# Save transcriptions to a JSON file
output_file = 'transcriptions.json'
with open(output_file, 'w') as f:
json.dump(data, f, indent=4)
print('Transcriptions saved to', output_file)