Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -9,16 +9,15 @@ device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
|
9 |
# Load Whisper large-v2 model for multilingual speech translation
|
10 |
asr_pipe = pipeline("automatic-speech-recognition", model="openai/whisper-large-v2", device=device)
|
11 |
|
12 |
-
# Load MMS TTS model for multilingual text-to-speech
|
13 |
-
processor = SpeechT5Processor.from_pretrained("facebook/mms-tts-
|
14 |
-
model = SpeechT5ForTextToSpeech.from_pretrained("facebook/mms-tts-
|
15 |
-
vocoder = SpeechT5HifiGan.from_pretrained("facebook/mms-tts-
|
16 |
|
17 |
-
# Define supported languages
|
18 |
LANGUAGES = {
|
19 |
-
"
|
20 |
-
"
|
21 |
-
"Japanese": "jpn", "Korean": "kor"
|
22 |
}
|
23 |
|
24 |
def translate(audio, source_lang, target_lang):
|
@@ -38,11 +37,11 @@ def translate(audio, source_lang, target_lang):
|
|
38 |
|
39 |
def synthesise(text, target_lang):
|
40 |
inputs = processor(text=text, return_tensors="pt")
|
41 |
-
speech = model.generate_speech(inputs["input_ids"].to(device), vocoder=vocoder, language=target_lang)
|
42 |
return speech.cpu()
|
43 |
|
44 |
def speech_to_speech_translation(audio, source_lang, target_lang):
|
45 |
-
translated_text = translate(audio, source_lang, target_lang)
|
46 |
synthesised_speech = synthesise(translated_text, target_lang)
|
47 |
synthesised_speech = (synthesised_speech.numpy() * 32767).astype(np.int16)
|
48 |
return 16000, synthesised_speech
|
|
|
9 |
# Load Whisper large-v2 model for multilingual speech translation
|
10 |
asr_pipe = pipeline("automatic-speech-recognition", model="openai/whisper-large-v2", device=device)
|
11 |
|
12 |
+
# Load MMS TTS model for multilingual text-to-speech (using German model as base)
|
13 |
+
processor = SpeechT5Processor.from_pretrained("facebook/mms-tts-deu")
|
14 |
+
model = SpeechT5ForTextToSpeech.from_pretrained("facebook/mms-tts-deu").to(device)
|
15 |
+
vocoder = SpeechT5HifiGan.from_pretrained("facebook/mms-tts-deu").to(device)
|
16 |
|
17 |
+
# Define supported languages (adjust based on the languages supported by the model)
|
18 |
LANGUAGES = {
|
19 |
+
"German": "deu", "English": "eng", "French": "fra", "Spanish": "spa",
|
20 |
+
"Italian": "ita", "Portuguese": "por", "Polish": "pol", "Turkish": "tur"
|
|
|
21 |
}
|
22 |
|
23 |
def translate(audio, source_lang, target_lang):
|
|
|
37 |
|
38 |
def synthesise(text, target_lang):
|
39 |
inputs = processor(text=text, return_tensors="pt")
|
40 |
+
speech = model.generate_speech(inputs["input_ids"].to(device), vocoder=vocoder, language=LANGUAGES[target_lang])
|
41 |
return speech.cpu()
|
42 |
|
43 |
def speech_to_speech_translation(audio, source_lang, target_lang):
|
44 |
+
translated_text = translate(audio, LANGUAGES[source_lang], LANGUAGES[target_lang])
|
45 |
synthesised_speech = synthesise(translated_text, target_lang)
|
46 |
synthesised_speech = (synthesised_speech.numpy() * 32767).astype(np.int16)
|
47 |
return 16000, synthesised_speech
|