UmarBaba1/NaijaLanguagesTranslator
0
1from transformers import pipeline2import gradio as gr3 4def translate(from_text, language):5 # Load translation pipelines lazily6 if language == "English to Hausa":7 translator_en_to_ha = pipeline('translation', model='facebook/nllb-200-distilled-600M')8 result = translator_en_to_ha(from_text, src_lang="eng_Latn", tgt_lang="hau_Latn")[0]['translation_text']9 return result10 elif language == "English to Igbo":11 translator_en_to_ig = pipeline('translation', model='facebook/nllb-200-distilled-600M')12 result = translator_en_to_ig(from_text, src_lang="eng_Latn", tgt_lang="ibo_Latn")[0]['translation_text']13 return result14 elif language == "English to Yoruba":15 translator_en_to_yo = pipeline('translation', model='facebook/nllb-200-distilled-600M')16 result = translator_en_to_yo(from_text, src_lang="eng_Latn", tgt_lang="yor_Latn")[0]['translation_text']17 return result18 elif language == "Hausa to English":19 translator_ha_to_en = pipeline('translation', model='facebook/nllb-200-distilled-600M')20 result = translator_ha_to_en(from_text, src_lang="hau_Latn", tgt_lang="eng_Latn")[0]['translation_text']21 return result22 elif language == "Igbo to English":23 translator_ig_to_en = pipeline('translation', model='facebook/nllb-200-distilled-600M')24 result = translator_ig_to_en(from_text, src_lang="ibo_Latn", tgt_lang="eng_Latn")[0]['translation_text']25 return result26 elif language == "Yoruba to English":27 translator_yo_to_en = pipeline('translation', model='facebook/nllb-200-distilled-600M')28 result = translator_yo_to_en(from_text, src_lang="yor_Latn", tgt_lang="eng_Latn")[0]['translation_text']29 return result30 31# Set up Gradio interface with dropdown and simplified output32interface = gr.Interface(33 fn=translate,34 inputs=[35 gr.Textbox(lines=2, placeholder="Text to translate", label="Input Text"),36 gr.Dropdown(["English to Hausa", "English to Igbo", "English to Yoruba",37 "Hausa to English", "Igbo to English", "Yoruba to English"],38 label="Translation Direction", value="English to Hausa")39 ],40 outputs=gr.Textbox(label="Translation Result") # Single output box for the selected translation41)42 43# Launch the interface44interface.launch()45 