Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,48 +1,58 @@
|
|
| 1 |
-
import
|
| 2 |
-
import torchaudio
|
| 3 |
import torch
|
|
|
|
|
|
|
| 4 |
from transformers import AutoProcessor, SeamlessM4TModel
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
def translate(text_input,
|
| 11 |
-
|
|
|
|
| 12 |
|
|
|
|
| 13 |
if text_input:
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
return "No input provided.", None
|
| 31 |
|
| 32 |
-
|
| 33 |
fn=translate,
|
| 34 |
inputs=[
|
| 35 |
-
gr.Textbox(label="Input Text
|
| 36 |
gr.Audio(type="filepath", label="Input Audio (optional)"),
|
| 37 |
-
gr.
|
|
|
|
|
|
|
| 38 |
],
|
| 39 |
outputs=[
|
| 40 |
-
gr.Textbox(label="
|
| 41 |
gr.Audio(label="Translated Speech")
|
| 42 |
],
|
| 43 |
-
title="
|
| 44 |
-
|
| 45 |
-
)
|
| 46 |
|
| 47 |
if __name__ == "__main__":
|
| 48 |
-
|
|
|
|
| 1 |
+
import os
|
|
|
|
| 2 |
import torch
|
| 3 |
+
import torchaudio
|
| 4 |
+
import gradio as gr
|
| 5 |
from transformers import AutoProcessor, SeamlessM4TModel
|
| 6 |
|
| 7 |
+
MODEL_NAME = "facebook/hf-seamless-m4t-medium"
|
| 8 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 9 |
+
|
| 10 |
+
processor = AutoProcessor.from_pretrained(MODEL_NAME)
|
| 11 |
+
model = SeamlessM4TModel.from_pretrained(MODEL_NAME).to(device).eval()
|
| 12 |
|
| 13 |
+
def translate(text_input, audio_input, source_lang, target_lang, auto_detect):
|
| 14 |
+
outputs = []
|
| 15 |
+
src = None if auto_detect else source_lang
|
| 16 |
|
| 17 |
+
# From text input
|
| 18 |
if text_input:
|
| 19 |
+
inputs = processor(text=text_input, src_lang=src, return_tensors="pt").to(device)
|
| 20 |
+
output = model.generate(**inputs, tgt_lang=target_lang)
|
| 21 |
+
text_out = processor.decode(output[0].tolist(), skip_special_tokens=True)
|
| 22 |
+
speech_out = output[1].cpu().numpy().squeeze()
|
| 23 |
+
outputs.append((f"Text translated", text_out, (16000, speech_out)))
|
| 24 |
+
|
| 25 |
+
# From audio input
|
| 26 |
+
elif audio_input:
|
| 27 |
+
waveform, sr = torchaudio.load(audio_input)
|
| 28 |
+
waveform = torchaudio.functional.resample(waveform, sr, 16000)
|
| 29 |
+
inputs = processor(audios=waveform, src_lang=src, return_tensors="pt").to(device)
|
| 30 |
+
output = model.generate(**inputs, tgt_lang=target_lang)
|
| 31 |
+
text_out = processor.decode(output[0].tolist(), skip_special_tokens=True)
|
| 32 |
+
speech_out = output[1].cpu().numpy().squeeze()
|
| 33 |
+
outputs.append((f"Audio translated", text_out, (16000, speech_out)))
|
| 34 |
+
|
| 35 |
+
if outputs:
|
| 36 |
+
_, txt, aud = outputs[0]
|
| 37 |
+
return txt, aud
|
| 38 |
|
| 39 |
return "No input provided.", None
|
| 40 |
|
| 41 |
+
iface = gr.Interface(
|
| 42 |
fn=translate,
|
| 43 |
inputs=[
|
| 44 |
+
gr.Textbox(label="Input Text (optional)"),
|
| 45 |
gr.Audio(type="filepath", label="Input Audio (optional)"),
|
| 46 |
+
gr.Textbox(label="Source Language (e.g. eng)"),
|
| 47 |
+
gr.Textbox(label="Target Language (e.g. fra)"),
|
| 48 |
+
gr.Checkbox(label="Auto-detect source language")
|
| 49 |
],
|
| 50 |
outputs=[
|
| 51 |
+
gr.Textbox(label="Translated Text"),
|
| 52 |
gr.Audio(label="Translated Speech")
|
| 53 |
],
|
| 54 |
+
title="iVoice Translate (Text + Speech)"
|
| 55 |
+
).queue()
|
|
|
|
| 56 |
|
| 57 |
if __name__ == "__main__":
|
| 58 |
+
iface.launch(server_name="0.0.0.0", share=True, server_port=int(os.environ.get("PORT", 7860)))
|