Spaces:
Running
Running
File size: 7,953 Bytes
5ae02e5 23dd469 5ae02e5 23dd469 2f8841d a58afc9 388fe1b 2f8841d a58afc9 2f8841d 5ae02e5 a58afc9 2f8841d a58afc9 2f8841d a58afc9 2f8841d a58afc9 2f8841d a58afc9 2f8841d 2a6eab9 2f8841d aa1a596 a58afc9 aa1a596 a58afc9 2f8841d 6418068 2f8841d 5ae02e5 6418068 2f8841d 58e0199 2f8841d 58e0199 2f8841d 6418068 23dd469 2f8841d 7f1ced9 2f8841d 6db90a4 2f8841d 6418068 23dd469 2f8841d 6db90a4 2a6eab9 2f8841d 58e0199 6db90a4 6418068 2f8841d 58e0199 2f8841d 7f1ced9 2f8841d 7f1ced9 a58afc9 2f8841d 2a6eab9 6418068 a58afc9 2f8841d a58afc9 2f8841d 58e0199 2f8841d 58e0199 a58afc9 58e0199 7f1ced9 2f8841d 2a6eab9 2f8841d 58e0199 2f8841d a58afc9 7f1ced9 2f8841d a58afc9 2f8841d a58afc9 2f8841d a58afc9 2f8841d a58afc9 2f8841d a58afc9 2f8841d a58afc9 2a6eab9 388fe1b 23dd469 a58afc9 6418068 aa1a596 |
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 |
import gradio as gr
import torch
import torchaudio
import numpy as np
from transformers import AutoProcessor, SeamlessM4Tv2Model
class TranslationModel:
def __init__(self):
self.model_name = "facebook/seamless-m4t-v2-large"
print("Loading model...")
self.processor = AutoProcessor.from_pretrained(self.model_name)
self.model = SeamlessM4Tv2Model.from_pretrained(self.model_name)
self.sample_rate = self.model.config.sampling_rate
self.languages = {
"English": "eng",
"Spanish": "spa",
"French": "fra",
"German": "deu",
"Italian": "ita",
"Portuguese": "por",
"Russian": "rus",
"Chinese": "cmn",
"Japanese": "jpn",
"Korean": "kor"
}
def translate_text(self, text, src_lang, tgt_lang, progress=gr.Progress()):
try:
progress(0.3, desc="Processing...")
inputs = self.processor(text=text, src_lang=self.languages[src_lang], return_tensors="pt")
progress(0.6, desc="Generating...")
audio_array = self.model.generate(**inputs, tgt_lang=self.languages[tgt_lang])[0].cpu().numpy().squeeze()
progress(1.0, desc="Complete")
return (self.sample_rate, audio_array)
except Exception as e:
raise gr.Error(str(e))
def translate_audio(self, audio_path, tgt_lang, progress=gr.Progress()):
if not audio_path:
raise gr.Error("Please upload an audio file")
try:
progress(0.3, desc="Processing...")
audio, orig_freq = torchaudio.load(audio_path)
audio = torchaudio.functional.resample(audio, orig_freq=orig_freq, new_freq=16000)
progress(0.6, desc="Translating...")
inputs = self.processor(audios=audio, return_tensors="pt")
audio_array = self.model.generate(**inputs, tgt_lang=self.languages[tgt_lang])[0].cpu().numpy().squeeze()
progress(1.0, desc="Complete")
return (self.sample_rate, audio_array)
except Exception as e:
raise gr.Error(str(e))
css = """
:root {
--primary-color: #2D3648;
--secondary-color: #5E6AD2;
--background-color: #F5F7FF;
--text-color: #2D3648;
--border-radius: 12px;
--spacing: 20px;
}
.gradio-container {
background-color: var(--background-color) !important;
}
.main-container {
max-width: 1200px !important;
margin: 0 auto !important;
padding: var(--spacing) !important;
}
.app-header {
text-align: center;
padding: 40px 20px;
background: linear-gradient(45deg, var(--primary-color), var(--secondary-color));
border-radius: var(--border-radius);
color: white !important;
margin-bottom: var(--spacing);
}
.app-title {
font-size: 2.5em;
font-weight: 700;
margin-bottom: 10px;
color: white !important;
}
.app-subtitle {
font-size: 1.2em;
opacity: 0.9;
color: white !important;
}
.content-block {
background: white;
padding: var(--spacing);
border-radius: var(--border-radius);
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
margin-bottom: var(--spacing);
}
.gr-button {
background: var(--secondary-color) !important;
border: none !important;
color: white !important;
}
.gr-button:hover {
box-shadow: 0 4px 10px rgba(94, 106, 210, 0.3) !important;
transform: translateY(-1px);
}
.gr-input, .gr-select {
border-radius: 8px !important;
border: 2px solid #E5E7EB !important;
padding: 12px !important;
}
.gr-input:focus, .gr-select:focus {
border-color: var(--secondary-color) !important;
box-shadow: 0 0 0 3px rgba(94, 106, 210, 0.1) !important;
}
.gr-form {
background: white !important;
padding: var(--spacing) !important;
border-radius: var(--border-radius) !important;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05) !important;
}
.gr-box {
border-radius: var(--border-radius) !important;
border: none !important;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05) !important;
}
.footer {
text-align: center;
color: var(--text-color);
padding: var(--spacing);
opacity: 0.8;
}
/* Custom Tabs Styling */
.tab-nav {
background: white !important;
padding: 10px !important;
border-radius: var(--border-radius) !important;
margin-bottom: var(--spacing) !important;
}
.tab-nav button {
border-radius: 8px !important;
padding: 12px 24px !important;
}
.tab-nav button.selected {
background: var(--secondary-color) !important;
color: white !important;
}
"""
def create_ui():
model = TranslationModel()
with gr.Blocks(css=css, title="AI Language Translator") as demo:
gr.HTML(
"""
<div class="app-header">
<div class="app-title">AI Language Translator</div>
<div class="app-subtitle">Powered by Neural Machine Translation</div>
</div>
"""
)
with gr.Tabs():
# Text Translation Tab
with gr.Tab("Text to Speech"):
with gr.Column(variant="panel"):
gr.Markdown("### Enter Text")
text_input = gr.Textbox(
label="",
placeholder="Type or paste your text here...",
lines=4
)
with gr.Row():
src_lang = gr.Dropdown(
choices=sorted(model.languages.keys()),
value="English",
label="From"
)
tgt_lang = gr.Dropdown(
choices=sorted(model.languages.keys()),
value="Spanish",
label="To"
)
translate_btn = gr.Button("Translate", size="lg")
gr.Markdown("### Translation Output")
audio_output = gr.Audio(
label="",
type="numpy",
show_download_button=True
)
# Audio Translation Tab
with gr.Tab("Speech to Speech"):
with gr.Column(variant="panel"):
gr.Markdown("### Upload Audio")
audio_input = gr.Audio(
label="",
type="filepath",
sources=["upload", "microphone"]
)
tgt_lang_audio = gr.Dropdown(
choices=sorted(model.languages.keys()),
value="English",
label="Translate to"
)
translate_audio_btn = gr.Button("Translate Audio", size="lg")
gr.Markdown("### Translation Output")
audio_output_from_audio = gr.Audio(
label="",
type="numpy",
show_download_button=True
)
gr.HTML(
"""
<div class="footer">
Built with ❤️ using Meta's SeamlessM4T and Gradio
</div>
"""
)
# Event handlers
translate_btn.click(
fn=model.translate_text,
inputs=[text_input, src_lang, tgt_lang],
outputs=audio_output
)
translate_audio_btn.click(
fn=model.translate_audio,
inputs=[audio_input, tgt_lang_audio],
outputs=audio_output_from_audio
)
return demo
if __name__ == "__main__":
demo = create_ui()
demo.queue()
demo.launch() |