Translation app is now bidirectional (uses both Gallek & Kellag models)
Browse files
app.py
CHANGED
@@ -2,29 +2,56 @@ import gradio as gr
|
|
2 |
|
3 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
|
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 |
demo.launch()
|
|
|
2 |
|
3 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
|
4 |
|
5 |
+
fw_modelcard = "amurienne/gallek-m2m100"
|
6 |
+
bw_modelcard = "amurienne/kellag-m2m100"
|
7 |
+
|
8 |
+
fw_model = AutoModelForSeq2SeqLM.from_pretrained(fw_modelcard)
|
9 |
+
fw_tokenizer = AutoTokenizer.from_pretrained(fw_modelcard)
|
10 |
+
|
11 |
+
fw_translation_pipeline = pipeline("translation", model=fw_model, tokenizer=fw_tokenizer, src_lang='fr', tgt_lang='br', max_length=400, device="cuda")
|
12 |
+
|
13 |
+
bw_model = AutoModelForSeq2SeqLM.from_pretrained(bw_modelcard)
|
14 |
+
bw_tokenizer = AutoTokenizer.from_pretrained(bw_modelcard)
|
15 |
+
|
16 |
+
bw_translation_pipeline = pipeline("translation", model=bw_model, tokenizer=bw_tokenizer, src_lang='br', tgt_lang='fr', max_length=400, device="cuda")
|
17 |
+
|
18 |
+
# translation function
|
19 |
+
def translate(text, direction):
|
20 |
+
if direction == "fr_to_br":
|
21 |
+
return fw_translation_pipeline("traduis de français en breton: " + text)[0]['translation_text']
|
22 |
+
else:
|
23 |
+
return bw_translation_pipeline("treiñ eus ar galleg d'ar brezhoneg: " + text)[0]['translation_text']
|
24 |
+
|
25 |
+
# function to switch translation direction
|
26 |
+
def switch_direction(direction):
|
27 |
+
return "br_to_fr" if direction == "fr_to_br" else "fr_to_br"
|
28 |
+
|
29 |
+
# function to update labels dynamically
|
30 |
+
def update_labels(direction, input_text, output_text):
|
31 |
+
if direction == "br_to_fr":
|
32 |
+
return gr.Textbox(output_text, label="Breton"), gr.Textbox(input_text, label="French")
|
33 |
+
else:
|
34 |
+
return gr.Textbox(output_text, label="French"), gr.Textbox(input_text, label="Breton")
|
35 |
+
|
36 |
+
with gr.Blocks() as demo:
|
37 |
+
|
38 |
+
gr.Markdown("# Gallek French ↔️ Breton Translation Demo")
|
39 |
+
|
40 |
+
direction = gr.State("fr_to_br") # default direction is French to Breton
|
41 |
+
|
42 |
+
input_text = gr.Textbox(label="French")
|
43 |
+
output_text = gr.Textbox(label="Breton")
|
44 |
+
|
45 |
+
with gr.Row():
|
46 |
+
translate_btn = gr.Button("Translate", variant='primary', scale=2)
|
47 |
+
switch_btn = gr.Button("Switch Direction 🔃", variant='secondary', scale=1)
|
48 |
+
|
49 |
+
# translation logic
|
50 |
+
translate_btn.click(translate, [input_text, direction], output_text)
|
51 |
+
|
52 |
+
# switch direction logic
|
53 |
+
switch_btn.click(switch_direction, direction, direction).then(
|
54 |
+
update_labels, [direction, input_text, output_text], [input_text, output_text]
|
55 |
+
)
|
56 |
|
57 |
demo.launch()
|