UmarBaba1 commited on
Commit
f68d722
·
verified ·
1 Parent(s): 06c3dc0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -47
app.py CHANGED
@@ -1,47 +1,44 @@
1
-
2
- from transformers import pipeline
3
- import gradio as gr
4
-
5
- # Load translation pipelines
6
- translator_en_to_ha = pipeline('translation', model='facebook/nllb-200-distilled-600M')
7
- translator_en_to_ig = pipeline('translation', model='facebook/nllb-200-distilled-600M')
8
- translator_en_to_yo = pipeline('translation', model='facebook/nllb-200-distilled-600M')
9
-
10
- translator_ha_to_en = pipeline('translation', model='facebook/nllb-200-distilled-600M')
11
- translator_ig_to_en = pipeline('translation', model='facebook/nllb-200-distilled-600M')
12
- translator_yo_to_en = pipeline('translation', model='facebook/nllb-200-distilled-600M')
13
-
14
- def translate(from_text, language):
15
- if language == "English to Hausa":
16
- result = translator_en_to_ha(from_text, src_lang="eng_Latn", tgt_lang="hau_Latn")[0]['translation_text']
17
- return result
18
- elif language == "English to Igbo":
19
- result = translator_en_to_ig(from_text, src_lang="eng_Latn", tgt_lang="ibo_Latn")[0]['translation_text']
20
- return result
21
- elif language == "English to Yoruba":
22
- result = translator_en_to_yo(from_text, src_lang="eng_Latn", tgt_lang="yor_Latn")[0]['translation_text']
23
- return result
24
- elif language == "Hausa to English":
25
- result = translator_ha_to_en(from_text, src_lang="hau_Latn", tgt_lang="eng_Latn")[0]['translation_text']
26
- return result
27
- elif language == "Igbo to English":
28
- result = translator_ig_to_en(from_text, src_lang="ibo_Latn", tgt_lang="eng_Latn")[0]['translation_text']
29
- return result
30
- elif language == "Yoruba to English":
31
- result = translator_yo_to_en(from_text, src_lang="yor_Latn", tgt_lang="eng_Latn")[0]['translation_text']
32
- return result
33
-
34
- # Set up Gradio interface with dropdown and simplified output
35
- interface = gr.Interface(
36
- fn=translate,
37
- inputs=[
38
- gr.Textbox(lines=2, placeholder="Text to translate", label="Input Text"),
39
- gr.Dropdown(["English to Hausa", "English to Igbo", "English to Yoruba",
40
- "Hausa to English", "Igbo to English", "Yoruba to English"],
41
- label="Translation Direction", value="English to Hausa")
42
- ],
43
- outputs=gr.Textbox(label="Translation Result") # Single output box for the selected translation
44
- )
45
-
46
- # Launch the interface
47
- interface.launch()
 
1
+ from transformers import pipeline
2
+ import gradio as gr
3
+
4
+ def translate(from_text, language):
5
+ # Load translation pipelines lazily
6
+ 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 result
10
+ 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 result
14
+ 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 result
18
+ 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 result
22
+ 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 result
26
+ 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 result
30
+
31
+ # Set up Gradio interface with dropdown and simplified output
32
+ interface = 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 translation
41
+ )
42
+
43
+ # Launch the interface
44
+ interface.launch()