from transformers import pipeline import gradio as gr def translate(from_text, language): # Load translation pipelines lazily if language == "English to Hausa": translator_en_to_ha = pipeline('translation', model='facebook/nllb-200-distilled-600M') result = translator_en_to_ha(from_text, src_lang="eng_Latn", tgt_lang="hau_Latn")[0]['translation_text'] return result elif language == "English to Igbo": translator_en_to_ig = pipeline('translation', model='facebook/nllb-200-distilled-600M') result = translator_en_to_ig(from_text, src_lang="eng_Latn", tgt_lang="ibo_Latn")[0]['translation_text'] return result elif language == "English to Yoruba": translator_en_to_yo = pipeline('translation', model='facebook/nllb-200-distilled-600M') result = translator_en_to_yo(from_text, src_lang="eng_Latn", tgt_lang="yor_Latn")[0]['translation_text'] return result elif language == "Hausa to English": translator_ha_to_en = pipeline('translation', model='facebook/nllb-200-distilled-600M') result = translator_ha_to_en(from_text, src_lang="hau_Latn", tgt_lang="eng_Latn")[0]['translation_text'] return result elif language == "Igbo to English": translator_ig_to_en = pipeline('translation', model='facebook/nllb-200-distilled-600M') result = translator_ig_to_en(from_text, src_lang="ibo_Latn", tgt_lang="eng_Latn")[0]['translation_text'] return result elif language == "Yoruba to English": translator_yo_to_en = pipeline('translation', model='facebook/nllb-200-distilled-600M') result = translator_yo_to_en(from_text, src_lang="yor_Latn", tgt_lang="eng_Latn")[0]['translation_text'] return result # Set up Gradio interface with dropdown and simplified output interface = gr.Interface( fn=translate, inputs=[ gr.Textbox(lines=2, placeholder="Text to translate", label="Input Text"), gr.Dropdown(["English to Hausa", "English to Igbo", "English to Yoruba", "Hausa to English", "Igbo to English", "Yoruba to English"], label="Translation Direction", value="English to Hausa") ], outputs=gr.Textbox(label="Translation Result") # Single output box for the selected translation ) # Launch the interface interface.launch()