Spaces:
Running
Running
File size: 6,419 Bytes
5ae02e5 23dd469 5ae02e5 23dd469 5ae02e5 23dd469 5ae02e5 23dd469 5ae02e5 23dd469 5ae02e5 23dd469 5ae02e5 23dd469 5ae02e5 23dd469 5ae02e5 23dd469 5ae02e5 23dd469 5ae02e5 23dd469 5ae02e5 |
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 |
import gradio as gr
import torch
import torchaudio
import scipy.io.wavfile
import numpy as np
from transformers import AutoProcessor, SeamlessM4Tv2Model
from pathlib import Path
from typing import Optional, Union
class SeamlessTranslator:
def __init__(self, model_name: str = "facebook/seamless-m4t-v2-large"):
try:
self.processor = AutoProcessor.from_pretrained(model_name)
self.model = SeamlessM4Tv2Model.from_pretrained(model_name)
self.sample_rate = self.model.config.sampling_rate
except Exception as e:
raise RuntimeError(f"Failed to initialize model: {str(e)}")
# Available language pairs
self.language_codes = {
"English": "eng",
"Spanish": "spa",
"French": "fra",
"German": "deu",
"Italian": "ita",
"Portuguese": "por",
"Russian": "rus",
"Chinese": "cmn",
"Japanese": "jpn",
"Korean": "kor",
"Arabic": "ara",
"Hindi": "hin",
}
def translate_text(self, text: str, src_lang: str, tgt_lang: str) -> tuple[int, np.ndarray]:
try:
inputs = self.processor(text=text, src_lang=src_lang, return_tensors="pt")
audio_array = self.model.generate(**inputs, tgt_lang=tgt_lang)[0].cpu().numpy().squeeze()
return self.sample_rate, audio_array
except Exception as e:
raise RuntimeError(f"Text translation failed: {str(e)}")
def translate_audio(self, audio_path: str, tgt_lang: str) -> tuple[int, np.ndarray]:
try:
# Load and resample audio
audio, orig_freq = torchaudio.load(audio_path)
audio = torchaudio.functional.resample(
audio,
orig_freq=orig_freq,
new_freq=16_000
)
# Process and generate translation
inputs = self.processor(audios=audio, return_tensors="pt")
audio_array = self.model.generate(**inputs, tgt_lang=tgt_lang)[0].cpu().numpy().squeeze()
return self.sample_rate, audio_array
except Exception as e:
raise RuntimeError(f"Audio translation failed: {str(e)}")
class GradioInterface:
def __init__(self):
self.translator = SeamlessTranslator()
self.languages = list(self.translator.language_codes.keys())
def text_to_speech(self, text: str, src_lang: str, tgt_lang: str) -> tuple[int, np.ndarray]:
src_code = self.translator.language_codes[src_lang]
tgt_code = self.translator.language_codes[tgt_lang]
return self.translator.translate_text(text, src_code, tgt_code)
def speech_to_speech(self, audio_path: str, tgt_lang: str) -> tuple[int, np.ndarray]:
tgt_code = self.translator.language_codes[tgt_lang]
return self.translator.translate_audio(audio_path, tgt_code)
def launch(self):
# Create the Gradio interface
with gr.Blocks(title="SeamlessM4T Translator") as demo:
gr.Markdown("# π SeamlessM4T Translator")
gr.Markdown("Translate text or speech to different languages using Meta's SeamlessM4T model")
with gr.Tabs():
# Text-to-Speech tab
with gr.TabItem("Text to Speech"):
with gr.Row():
with gr.Column():
text_input = gr.Textbox(
label="Input Text",
placeholder="Enter text to translate...",
lines=3
)
src_lang = gr.Dropdown(
choices=self.languages,
value="English",
label="Source Language"
)
tgt_lang_text = gr.Dropdown(
choices=self.languages,
value="Spanish",
label="Target Language"
)
translate_btn = gr.Button("Translate", variant="primary")
with gr.Column():
audio_output = gr.Audio(
label="Translated Speech",
type="numpy"
)
translate_btn.click(
fn=self.text_to_speech,
inputs=[text_input, src_lang, tgt_lang_text],
outputs=audio_output
)
# Speech-to-Speech tab
with gr.TabItem("Speech to Speech"):
with gr.Row():
with gr.Column():
audio_input = gr.Audio(
label="Input Speech",
type="filepath"
)
tgt_lang_speech = gr.Dropdown(
choices=self.languages,
value="Spanish",
label="Target Language"
)
translate_audio_btn = gr.Button("Translate", variant="primary")
with gr.Column():
audio_output_s2s = gr.Audio(
label="Translated Speech",
type="numpy"
)
translate_audio_btn.click(
fn=self.speech_to_speech,
inputs=[audio_input, tgt_lang_speech],
outputs=audio_output_s2s
)
gr.Markdown(
"""
### Notes
- Text-to-Speech: Enter text and select source/target languages
- Speech-to-Speech: Upload an audio file and select target language
- Processing may take a few moments depending on input length
"""
)
# Launch the interface
demo.launch(share=True)
if __name__ == "__main__":
interface = GradioInterface()
interface.launch() |