|
import json |
|
|
|
import gradio as gr |
|
import torch |
|
|
|
from transformers import pipeline |
|
|
|
text_translator = pipeline( |
|
"translation", |
|
model="facebook/nllb-200-distilled-600M", |
|
torch_dtype=torch.bfloat16) |
|
|
|
|
|
with open('language.json', 'r') as file: |
|
language_data = json.load(file) |
|
|
|
def get_FLORES_code_from_language(language): |
|
for entry in language_data: |
|
if entry['Language'].lower() == language.lower(): |
|
return entry['FLORES-200 code'] |
|
return None |
|
|
|
|
|
def translate_text(text, destination_language): |
|
dest_code = get_FLORES_code_from_language(destination_language) |
|
if dest_code is None: |
|
return "Invalid destination language." |
|
|
|
translation = text_translator(text, src_lang="eng_Latn", tgt_lang=dest_code) |
|
return translation[0]["translation_text"] |
|
|
|
gr.close_all() |
|
|
|
|
|
demo = gr.Interface(fn=translate_text, |
|
inputs=[gr.Textbox(label="Input text for translation",lines=6), gr.Dropdown( |
|
["Arabic", "Afrikaans", "Bengali", "Greek", "Estonian", "Portuguese", "Spanish"], |
|
label="Select Destination Language") |
|
], |
|
outputs=[gr.Textbox(label="Translated Text",lines=4)], |
|
title="@caesar-2series: Multilingual Language Interpreter", |
|
description="Translations from English into a few foreign languages") |
|
demo.launch() |