-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.py
275 lines (206 loc) · 8.97 KB
/
script.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import torch
from datasets import load_dataset
import io
import time
import librosa
import os
#from TTS.utils.synthesizer import Synthesizer
from indictrans2 import IndicTranslator
#from Indic_TTS.inference.src.inference import TextToSpeechEngine
from transformers import Wav2Vec2Processor, Wav2Vec2ForCTC
from scipy.io.wavfile import write
import soundfile as sf
DEFAULT_SAMPLING_RATE = 16000
os.environ['PT_HPU_ENABLE_GENERIC_STREAM'] = '1'
os.environ['PT_HPU_ENABLE_REFINE_DYNAMIC_SHAPES'] = '0'
import librosa
import torch
from transformers import Wav2Vec2ForCTC, Wav2Vec2Tokenizer
import time
import os
import librosa
import numpy as np
import pydub
from transformers import Wav2Vec2Processor, Wav2Vec2ForCTC
from datasets import load_dataset
import torch
import torchaudio
import soundfile as sf
import torchaudio.functional as F # For resampling the audio
from habana_frameworks.torch.hpex.experimental.transformer_engine import recipe
import habana_frameworks.torch.hpex.experimental.transformer_engine as te
import habana_quantization_toolkit
import soundfile as sf
import habana_frameworks.torch as ht
import habana_frameworks.torch.core as htcore
import habana_frameworks.torch.hpu.graphs as htgraphs
#from optimum.habana.transformers.modeling_utils import adapt_transformers_to_gaudi
import soundfile as sf
#adapt_transformers_to_gaudi()
is_long_audio = True
hpu = torch.device('hpu')
cpu = torch.device('cpu')
# Load the audio file
audio_path = "/root/akarx/Bhashini_Pipeline/TTS/Indic-TTS/inference/modi_legit.wav"
speech, rate = librosa.load(audio_path, sr=16000)
# Initialize the processor and model
print("=================================================")
print("\n=================================================")
model_id = "ai4bharat/indicwav2vec-hindi"
processor = Wav2Vec2Processor.from_pretrained(model_id)
model = Wav2Vec2ForCTC.from_pretrained(model_id)
model = model.eval().to(torch.device('hpu'))
# Define the threshold in seconds
# For descent result 1000000 for hindi
if (is_long_audio):
threshold_ms = 1000000 # Example threshold of 1000 milliseconds
threshold_s = threshold_ms / 10.0
# Calculate the duration of each segment
segment_duration_s = threshold_s
num_segments = int(np.ceil(len(speech) / segment_duration_s))
# print(num_segments)
# Divide the audio into segments
num_segments = 4
segments = np.array_split(speech, num_segments)
# Initialize an empty list to store the transcriptions
all_transcriptions = []
import time
start = time.time()
for i, segment in enumerate(segments):
# Process each segment
start_time = time.time()
# Convert the segment to the required format
input_values = processor(segment, sampling_rate=16000, return_tensors='pt', padding="max_length", max_length=200000).input_values
# Move the input values to the HPU device
input_values = input_values.to(torch.device('hpu'), non_blocking=True)
# Perform inference
with torch.no_grad():
logits = model(input_values).logits.to(torch.device('cpu'), non_blocking=True)
time.sleep(0.05)
predicted_ids = torch.argmax(logits, dim=-1)
transcription = processor.decode(predicted_ids[0])
# Append the transcription to the list
all_transcriptions.append(transcription)
all_transcriptions.append(" ")
#print(f"Segment {i+1} processed in {time.time() - start_time:.2f} seconds.")
#print(f"Transcription: {transcription}\n")
end = time.time()
# Combine the transcriptions from each segment
final_transcription = " ".join(all_transcriptions)
print("=================================================")
print("\n=================================================\n")
print("WARMING UP ")
print("\nFinal Transcription:", final_transcription)
print("\ntotal time taken", end - start)
print("=================================================")
print("\n=================================================\n")
start = time.time()
for i, segment in enumerate(segments):
# Process each segment
start_time = time.time()
# Convert the segment to the required format
input_values = processor(segment, sampling_rate=16000, return_tensors='pt', padding="max_length", max_length=200000).input_values
# Move the input values to the HPU device
input_values = input_values.to(torch.device('hpu'), non_blocking=True)
# Perform inference
with torch.no_grad():
logits = model(input_values).logits.to(torch.device('cpu'), non_blocking=True)
time.sleep(0.05)
predicted_ids = torch.argmax(logits, dim=-1)
transcription = processor.decode(predicted_ids[0])
# Append the transcription to the list
all_transcriptions.append(transcription)
#print(f"Segment {i+1} processed in {time.time() - start_time:.2f} seconds.")
#print(f"Transcription: {transcription}\n")
end = time.time()
print("=================================================")
print("\n=================================================\n")
# Combine the transcriptions from each segment
final_transcription = " ".join(all_transcriptions)
print("ACTUAL OUTPUT")
print("\nFinal Transcription:", final_transcription)
print("\ntotal time taken", end - start)
else:
input_values = processor(speech, sampling_rate = 16000, return_tensors = 'pt', padding="max_length", max_length=200000).input_values
input_values = input_values.to(hpu, non_blocking=True)
start = time.time()
with torch.autocast(device_type="hpu"):
logits = model(input_values).logits.to(cpu, non_blocking=True)
end = time.time()
print("first total time taken", end-start)
predicted_ids = torch.argmax(logits, dim =-1)
transcriptions = processor.decode(predicted_ids[0])
print(transcriptions)
import time
start = time.time()
input_values = processor(speech, sampling_rate = 16000, return_tensors = 'pt', padding="max_length", max_length=200000).input_values
input_values = input_values.to(hpu, non_blocking=True)
with torch.autocast(device_type="hpu"):
logits = model(input_values).logits.to(cpu, non_blocking=True)
end = time.time()
print("second total time taken", end-start)
time.sleep(0.05)
predicted_ids = torch.argmax(logits, dim =-1)
transcriptions = processor.decode(predicted_ids[0])
print(transcriptions)
if(not is_long_audio):
final_transcription = transcriptions
print("=================================================")
print("\n=================================================\n")
translator = IndicTranslator("indic-indic", "ai4bharat/indictrans2-indic-indic-1B")
src_lang, tgt_lang = "hin_Deva", "ben_Beng"
#ben_translations = translator.batch_translate(sentences, src_lang, tgt_lang)
print("TRANSLATION STARTED FROM HINDI TO BENGALI")
start_time = time.time()
ben_single_translations = translator.single_translate(final_transcription, src_lang, tgt_lang)
end_time = time.time()
print("=================================================")
print("\n=================================================\n")
print("Modi ji's voice in bengali:", ben_single_translations)
print("Total time taken for translation", end_time - start_time)
print("=================================================")
print("\n=================================================\n")
#complete_text = "\n".join(ben_translations)
import io
from TTS.utils.synthesizer import Synthesizer
from src.inference import TextToSpeechEngine
import scipy.io.wavfile
# Initialize Bengali model
DEFAULT_SAMPLING_RATE = 16000
lang = "bn"
ben_model = Synthesizer(
tts_checkpoint=f'checkpoints/{lang}/fastpitch/best_model.pth',
tts_config_path=f'checkpoints/{lang}/fastpitch/config.json',
tts_speakers_file=f'checkpoints/{lang}/fastpitch/speakers.pth',
# tts_speakers_file=None,
tts_languages_file=None,
vocoder_checkpoint=f'checkpoints/{lang}/hifigan/best_model.pth',
vocoder_config=f'checkpoints/{lang}/hifigan/config.json',
encoder_checkpoint="",
encoder_config="",
use_cuda=False,
)
# Setup TTS Engine
models = {
"bn": ben_model
}
engine = TextToSpeechEngine(models)
# Bengali TTS inference
print("=================================================")
print("\n=================================================\n")
print("CONVERTING TRANSLATED TEXT INTO SPEECH \n\n")
start_time = time.time()
bengali_raw_audio = engine.infer_from_text(
input_text=ben_single_translations,
lang="bn",
speaker_name="male"
)
end_time = time.time()
print("Total time taken", end_time - start_time)
print("=================================================")
print("\n=================================================\n")
byte_io = io.BytesIO()
scipy.io.wavfile.write(byte_io, DEFAULT_SAMPLING_RATE, bengali_raw_audio)
print("SAVING AUDIO FILE :)")
with open("bengali_audio.wav", "wb") as f:
f.write(byte_io.read())