import warnings warnings.filterwarnings('ignore') import gradio as gr import langid import langcodes from transformers import pipeline import torch from languages import languages # Initialize the translation pipeline translator = pipeline(task='translation', model="facebook/nllb-200-distilled-600M", torch_dtype=torch.bfloat16) # Define the translation function def translate_text(text, tgt_lang): # Detect source language src_lang_code, _ = langid.classify(text) tgt_lang_code = languages[tgt_lang] src_language_name = langcodes.Language.get(src_lang_code).language_name() # Translate text text_translated = translator(text, src_lang=src_lang_code, tgt_lang=tgt_lang_code) return src_language_name, text_translated[0]['translation_text'] detected_lang = gr.Textbox(label="Detected Source Language") # Create the Gradio interface iface = gr.Interface( fn=translate_text, inputs=[ gr.Textbox(lines=2, placeholder="Enter text here...", label = "Input Text.."), gr.Dropdown(list(languages.keys()), label="Target Language") ], outputs=[ detected_lang, gr.Textbox(label="Translation"), ], allow_flagging = 'never', description="Translates text from one language to another using the NLLB model with automatic source language detection." ) # Add Markdown content markdown_content_translation = gr.Markdown( """

Multilingual Machine Translation

Model: facebook/nllb-200-distilled-600M

Made By: Md. Mahmudun Nabi

""" ) # Combine the Markdown content and the demo interface translation_with_markdown = gr.Blocks() with translation_with_markdown: markdown_content_translation.render() iface.render() # Launch the Gradio app if __name__ == "__main__": translation_with_markdown.launch()