import gradio as gr
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
model_path = "anzorq/m2m100_418M_ft_ru-kbd_44K"
src_lang="ru"
tgt_lang="zu"
tokenizer = AutoTokenizer.from_pretrained(model_path, src_lang=src_lang)
model = AutoModelForSeq2SeqLM.from_pretrained(model_path)
def translate(text, num_beams=4, num_return_sequences=4):
inputs = tokenizer(text, return_tensors="pt")
translated_tokens = model.generate(
**inputs, forced_bos_token_id=tokenizer.lang_code_to_id[tgt_lang], num_beams=num_beams, num_return_sequences=num_return_sequences
)
translations = []
for translation in tokenizer.batch_decode(translated_tokens, skip_special_tokens=True):
translations.append(translation)
return "\n".join(["• " + translation for translation in translations])
output = gr.outputs.Textbox()
with gr.Accordion("Advanced Options"):
num_beams = gr.inputs.Slider(2, 10, step=1, label="Number of beams", default=4)
num_return_sequences = gr.inputs.Slider(2, 10, step=1, label="Number of returned sequences", default=4)
iface = gr.Interface(fn=translate, inputs=["text", num_beams, num_return_sequences], outputs=output)
iface.launch()
# import gradio as gr
# title = "Русско-черкесский переводчик"
# description = "Demo of a Russian-Circassian (Kabardian dialect) translator.
It is based on Facebook's M2M-100 model machine learning model, and has been trained on 45,000 Russian-Circassian sentence pairs.
It can also translate from 100 other languages to Circassian (English, French, Spanish, etc.), but less accurately.
The data corpus is constantly being expanded, and we need help in finding sentence sources, OCR, data cleaning, etc.
If you are interested in helping out with this project, please contact me at the link below.
This is only a demo, not a finished product. Translation quality is still low and will improve with time and more data.
45,000 sentence pairs is not enough to create an accurate machine translation model, and more data is needed.
You can help by finding sentence sources (books, web pages, etc.), scanning books, OCRing documents, data cleaning, and other tasks.
If you are interested in helping out with this project, contact me at the link below."
# article = """