Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -22,8 +22,15 @@ Tonic's Unity On Device!🚀 on your own data & in your own way by cloning
|
|
22 |
TeamTonic is always making cool demos! Join our active builder's community on Discord: [Discord](https://discord.gg/GWpVpekp) On 🤗Huggingface: [TeamTonic](https://huggingface.co/TeamTonic) & [MultiTransformer](https://huggingface.co/MultiTransformer) On ðŸŒGithub: [Polytonic](https://github.com/tonic-ai) & contribute to 🌟 [PolyGPT](https://github.com/tonic-ai/polygpt-alpha)"
|
23 |
"""
|
24 |
|
|
|
|
|
25 |
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
27 |
if not os.path.exists(output_dir):
|
28 |
os.makedirs(output_dir)
|
29 |
sample_rate, audio_data = audio_input
|
@@ -31,12 +38,15 @@ def save_audio(audio_input, output_dir="saved_audio"):
|
|
31 |
file_path = os.path.join(output_dir, file_name)
|
32 |
sf.write(file_path, audio_data, sample_rate)
|
33 |
|
34 |
-
|
|
|
|
|
|
|
35 |
|
36 |
def speech_to_text(audio_data, tgt_lang):
|
37 |
file_path = save_audio(audio_data)
|
38 |
audio_input, _ = torchaudio.load(file_path)
|
39 |
-
s2t_model = torch.jit.load("unity_on_device.ptl")
|
40 |
with torch.no_grad():
|
41 |
model_output = s2t_model(audio_input, tgt_lang=languages[tgt_lang])
|
42 |
transcribed_text = model_output[0] if model_output else ""
|
@@ -47,7 +57,7 @@ def speech_to_text(audio_data, tgt_lang):
|
|
47 |
def speech_to_speech_translation(audio_data, tgt_lang):
|
48 |
file_path = save_audio(audio_data)
|
49 |
audio_input, _ = torchaudio.load(file_path)
|
50 |
-
s2st_model = torch.jit.load("unity_on_device.ptl")
|
51 |
with torch.no_grad():
|
52 |
translated_text, units, waveform = s2st_model(audio_input, tgt_lang=languages[tgt_lang])
|
53 |
output_file = "/tmp/result.wav"
|
|
|
22 |
TeamTonic is always making cool demos! Join our active builder's community on Discord: [Discord](https://discord.gg/GWpVpekp) On 🤗Huggingface: [TeamTonic](https://huggingface.co/TeamTonic) & [MultiTransformer](https://huggingface.co/MultiTransformer) On ðŸŒGithub: [Polytonic](https://github.com/tonic-ai) & contribute to 🌟 [PolyGPT](https://github.com/tonic-ai/polygpt-alpha)"
|
23 |
"""
|
24 |
|
25 |
+
def save_and_resample_audio(input_audio_path, output_audio_path, resample_rate=16000):
|
26 |
+
waveform, sample_rate = torchaudio.load(input_audio_path)
|
27 |
|
28 |
+
resampler = torchaudio.transforms.Resample(sample_rate, resample_rate, dtype=waveform.dtype)
|
29 |
+
resampled_waveform = resampler(waveform)
|
30 |
+
|
31 |
+
torchaudio.save(output_audio_path, resampled_waveform, resample_rate)
|
32 |
+
|
33 |
+
def save_audio(audio_input, output_dir="saved_audio", resample_rate=16000):
|
34 |
if not os.path.exists(output_dir):
|
35 |
os.makedirs(output_dir)
|
36 |
sample_rate, audio_data = audio_input
|
|
|
38 |
file_path = os.path.join(output_dir, file_name)
|
39 |
sf.write(file_path, audio_data, sample_rate)
|
40 |
|
41 |
+
resampled_file_path = os.path.join(output_dir, f"resampled_{file_name}")
|
42 |
+
save_and_resample_audio(file_path, resampled_file_path, resample_rate)
|
43 |
+
|
44 |
+
return resampled_file_path
|
45 |
|
46 |
def speech_to_text(audio_data, tgt_lang):
|
47 |
file_path = save_audio(audio_data)
|
48 |
audio_input, _ = torchaudio.load(file_path)
|
49 |
+
s2t_model = torch.jit.load("unity_on_device.ptl", map_location=torch.device('cpu'))
|
50 |
with torch.no_grad():
|
51 |
model_output = s2t_model(audio_input, tgt_lang=languages[tgt_lang])
|
52 |
transcribed_text = model_output[0] if model_output else ""
|
|
|
57 |
def speech_to_speech_translation(audio_data, tgt_lang):
|
58 |
file_path = save_audio(audio_data)
|
59 |
audio_input, _ = torchaudio.load(file_path)
|
60 |
+
s2st_model = torch.jit.load("unity_on_device.ptl", map_location=torch.device('cpu'))
|
61 |
with torch.no_grad():
|
62 |
translated_text, units, waveform = s2st_model(audio_input, tgt_lang=languages[tgt_lang])
|
63 |
output_file = "/tmp/result.wav"
|