Spaces:
Runtime error
Runtime error
Add application file
Browse files
app.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 4 |
+
|
| 5 |
+
tokenizer = AutoTokenizer.from_pretrained("t5-small")
|
| 6 |
+
model = AutoModelForSeq2SeqLM.from_pretrained("t5-small")
|
| 7 |
+
|
| 8 |
+
def translate_text(text):
|
| 9 |
+
inputs = tokenizer.encode("translate English to French: " + text, return_tensors="pt")
|
| 10 |
+
outputs = model.generate(inputs, max_length=128, num_beams=4, early_stopping=True)
|
| 11 |
+
translated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 12 |
+
return translated_text
|
| 13 |
+
|
| 14 |
+
output_1 = gr.Textbox(label="Speech to Text")
|
| 15 |
+
output_2 = gr.Textbox(label="Speech Translation")
|
| 16 |
+
|
| 17 |
+
generator = gr.Interface.load("huggingface/facebook/wav2vec2-base-960h",
|
| 18 |
+
inputs="microphone",
|
| 19 |
+
outputs=output_1,
|
| 20 |
+
title="Speech-to-text",
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
translator = gr.Interface(fn=translate_text,
|
| 24 |
+
inputs=output_1,
|
| 25 |
+
outputs=output_2,
|
| 26 |
+
title="English to French Translator",
|
| 27 |
+
description="Translate English speech to French text using the T5-small model.",
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
gr.Series(generator, translator).launch()
|