File size: 2,352 Bytes
19d9370 b0bcd3c 21cd50a b0bcd3c 21cd50a b0bcd3c d3ee456 b0bcd3c b0ebcf0 b0bcd3c 19d9370 |
1 2 3 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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
import gradio as gr
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
fw_modelcard = "amurienne/gallek-m2m100"
bw_modelcard = "amurienne/kellag-m2m100"
fw_model = AutoModelForSeq2SeqLM.from_pretrained(fw_modelcard)
fw_tokenizer = AutoTokenizer.from_pretrained(fw_modelcard)
fw_translation_pipeline = pipeline("translation", model=fw_model, tokenizer=fw_tokenizer, src_lang='fr', tgt_lang='br', max_length=400, device="cpu")
bw_model = AutoModelForSeq2SeqLM.from_pretrained(bw_modelcard)
bw_tokenizer = AutoTokenizer.from_pretrained(bw_modelcard)
bw_translation_pipeline = pipeline("translation", model=bw_model, tokenizer=bw_tokenizer, src_lang='br', tgt_lang='fr', max_length=400, device="cpu")
# translation function
def translate(text, direction):
if direction == "fr_to_br":
return fw_translation_pipeline("traduis de français en breton: " + text)[0]['translation_text']
else:
return bw_translation_pipeline("treiñ eus ar galleg d'ar brezhoneg: " + text)[0]['translation_text']
# function to switch translation direction
def switch_direction(direction):
return "br_to_fr" if direction == "fr_to_br" else "fr_to_br"
# function to update labels dynamically
def update_labels(direction, input_text, output_text):
if direction == "br_to_fr":
return gr.Textbox(output_text, label="Breton"), gr.Textbox(input_text, label="French")
else:
return gr.Textbox(output_text, label="French"), gr.Textbox(input_text, label="Breton")
with gr.Blocks(theme=gr.themes.Soft()) as demo:
gr.Markdown("# Gallek French ↔️ Breton Translation Demo\nPart of the [GweLLM](https://github.com/blackccpie/GweLLM) project")
direction = gr.State("fr_to_br") # default direction is French to Breton
input_text = gr.Textbox(label="French")
output_text = gr.Textbox(label="Breton")
with gr.Row():
translate_btn = gr.Button("Translate", variant='primary', scale=2)
switch_btn = gr.Button("Switch Direction 🔃", variant='secondary', scale=1)
# translation logic
translate_btn.click(translate, [input_text, direction], output_text)
# switch direction logic
switch_btn.click(switch_direction, direction, direction).then(
update_labels, [direction, input_text, output_text], [input_text, output_text]
)
demo.launch()
|