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