Spaces:
Build error
Build error
frogcho123
commited on
Commit
•
2bacaf7
1
Parent(s):
e6cfad1
Update app.py
Browse files
app.py
CHANGED
@@ -1,56 +1,56 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
import whisper
|
|
|
3 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
4 |
from gtts import gTTS
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
audio = whisper.pad_or_trim(audio)
|
19 |
-
mel = whisper.log_mel_spectrogram(audio).to(
|
20 |
-
_, probs =
|
21 |
options = whisper.DecodingOptions()
|
22 |
-
result = whisper.decode(
|
23 |
text = result.text
|
24 |
-
lang = max(probs, key=probs.get)
|
25 |
|
26 |
-
# Translate
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
encoded_bg = tokenizer(text, return_tensors="pt")
|
32 |
-
generated_tokens = model.generate(**encoded_bg)
|
33 |
-
translated_text = tokenizer.batch_decode(generated_tokens, skip_special_tokens=True)[0]
|
34 |
|
35 |
# Text-to-audio (TTS)
|
36 |
-
tts = gTTS(text=translated_text, lang=
|
37 |
-
output_file =
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
inputs =
|
44 |
-
|
45 |
-
|
46 |
-
]
|
47 |
-
|
48 |
-
|
49 |
-
gr.outputs.File(label="Translated Audio")
|
50 |
-
]
|
51 |
-
|
52 |
-
title = "Audio Translation"
|
53 |
-
description = "Upload an audio file, translate the speech to a target language, and download the translated audio."
|
54 |
-
|
55 |
-
gr.Interface(fn=translate_audio, inputs=inputs, outputs=outputs, title=title, description=description).launch()
|
56 |
|
|
|
1 |
+
import os
|
2 |
import gradio as gr
|
3 |
import whisper
|
4 |
+
import IPython
|
5 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
6 |
from gtts import gTTS
|
7 |
+
|
8 |
+
# Load the ASR model
|
9 |
+
asr_model = whisper.load_model("base")
|
10 |
+
|
11 |
+
# Load the translation model
|
12 |
+
translation_tokenizer = AutoTokenizer.from_pretrained("alirezamsh/small100")
|
13 |
+
translation_model = AutoModelForSeq2SeqLM.from_pretrained("alirezamsh/small100")
|
14 |
+
|
15 |
+
# Available target languages
|
16 |
+
available_languages = {
|
17 |
+
'Russian': 'ru',
|
18 |
+
'Spanish': 'es',
|
19 |
+
'English': 'en',
|
20 |
+
'Greek': 'gr'
|
21 |
+
}
|
22 |
+
|
23 |
+
# Function to translate the audio
|
24 |
+
def translate_audio(audio_file, target_language):
|
25 |
+
to_lang = available_languages[target_language]
|
26 |
+
|
27 |
+
# Auto to text (ASR)
|
28 |
+
audio = whisper.load_audio(audio_file.name)
|
29 |
audio = whisper.pad_or_trim(audio)
|
30 |
+
mel = whisper.log_mel_spectrogram(audio).to(asr_model.device)
|
31 |
+
_, probs = asr_model.detect_language(mel)
|
32 |
options = whisper.DecodingOptions()
|
33 |
+
result = whisper.decode(asr_model, mel, options)
|
34 |
text = result.text
|
|
|
35 |
|
36 |
+
# Translate the text
|
37 |
+
translation_tokenizer.src_lang = to_lang
|
38 |
+
encoded_bg = translation_tokenizer(text, return_tensors="pt")
|
39 |
+
generated_tokens = translation_model.generate(**encoded_bg)
|
40 |
+
translated_text = translation_tokenizer.batch_decode(generated_tokens, skip_special_tokens=True)[0]
|
|
|
|
|
|
|
41 |
|
42 |
# Text-to-audio (TTS)
|
43 |
+
tts = gTTS(text=translated_text, lang=to_lang)
|
44 |
+
output_file = "translated_audio.mp3"
|
45 |
+
tts.save(output_file)
|
46 |
+
return output_file
|
47 |
+
|
48 |
+
# Gradio interface
|
49 |
+
audio_input = gr.inputs.Audio(label="Upload audio file")
|
50 |
+
language_dropdown = gr.inputs.Dropdown(choices=list(available_languages.keys()), label="Select Target Language")
|
51 |
+
audio_output = gr.outputs.Audio(label="Translated audio file")
|
52 |
+
|
53 |
+
iface = gr.Interface(fn=translate_audio, inputs=[audio_input, language_dropdown], outputs=audio_output, title="Audio Translation Demo")
|
54 |
+
iface.launch()
|
55 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
|