CoolFace
Apppublic

Walid-Ahmed/MultiLanguage_Translator

sourceHugging Faceapache-2.0updated 2y agoView on Hugging Face
0likes
app.py54 linesDownload Raw Back to root
1import torch2import gradio as gr3import json4from transformers import pipeline5 6# Use a pipeline as a high-level helper7text_translator = pipeline("translation", model="facebook/nllb-200-distilled-600M", torch_dtype=torch.bfloat16)8 9# Load the JSON data from the file10with open('language.json', 'r') as file:11    language_data = json.load(file)12 13 14def get_FLORES_code_from_language(language):15    for entry in language_data:16        if entry['Language'].lower() == language.lower():17            return entry['FLORES-200 code']18    return None19 20 21def translate_text(text, destination_language):22    dest_code = get_FLORES_code_from_language(destination_language)23    print(f"Destination Language: {destination_language}, Code: {dest_code}")24    translation = text_translator(text, src_lang="eng_Latn", tgt_lang=dest_code)25    translated_text = translation[0]["translation_text"]26    print(f"Translated Text: {translated_text}")27 28    # For Arabic, add HTML to force RTL display29    if destination_language.lower() in ["egyptian arabic", "arabic"]:30        translated_text = f'<div style="text-align: right; direction: rtl;">{translated_text}</div>'31 32    return translated_text33 34 35# Create the Gradio interface36def translate(text, destination_language):37    return translate_text(text, destination_language)38 39 40language_options = [entry['Language'] for entry in language_data]41 42iface = gr.Interface(43    fn=translate,44    inputs=[45        gr.Textbox(lines=2, placeholder="Enter text here..."),46        gr.Dropdown(choices=language_options, value="English", label="Destination Language")47    ],48    outputs=gr.HTML(label="Translated Text"),49    title="Text Translator",50    description="Enter text and choose the destination language to get the translation."51)52 53iface.launch()54