Update translator.py
Browse files- translator.py +22 -0
translator.py
CHANGED
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
|
4 |
+
# Create a translation pipeline for English to French
|
5 |
+
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-en-fr")
|
6 |
+
|
7 |
+
def translate(text):
|
8 |
+
translated_text = translator(text, max_length=40)[0]["translation_text"]
|
9 |
+
return translated_text
|
10 |
+
|
11 |
+
with gr.Blocks() as demo:
|
12 |
+
with gr.Row():
|
13 |
+
with gr.Column():
|
14 |
+
english = gr.Textbox(label="English text")
|
15 |
+
translate_btn = gr.Button(value="Translate")
|
16 |
+
with gr.Column():
|
17 |
+
french = gr.Textbox(label="French Text")
|
18 |
+
|
19 |
+
translate_btn.click(translate, inputs=english, outputs=french, api_name="translate-to-french")
|
20 |
+
examples = gr.Examples(examples=["I went to the supermarket yesterday.", "Helen is a good swimmer."], inputs=english)
|
21 |
+
|
22 |
+
demo.launch()
|