Henry Nunoo-Mensah commited on
Commit
21856b1
1 Parent(s): 9e39015

application file added

Browse files
Files changed (2) hide show
  1. .DS_Store +0 -0
  2. app.py +57 -0
.DS_Store ADDED
Binary file (6.15 kB). View file
 
app.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """translator.ipynb
3
+
4
+ Automatically generated by Colaboratory.
5
+
6
+ Original file is located at
7
+ https://colab.research.google.com/drive/1qSV5usAOci7XzgKcLVYpYMok5RpFBYSQ
8
+
9
+ # Text Translation
10
+ """
11
+
12
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
13
+ import gradio as gr
14
+ from keras.utils.generic_utils import default
15
+
16
+ #!pip install -U pip transformers
17
+ #!pip install sentencepiece
18
+
19
+ checkpoint = "facebook/nllb-200-distilled-600M"
20
+ # checkpoint = ‘facebook/nllb-200–1.3B’
21
+ # checkpoint = ‘facebook/nllb-200–3.3B’
22
+ # checkpoint = ‘facebook/nllb-200-distilled-1.3B’
23
+
24
+ model = AutoModelForSeq2SeqLM.from_pretrained(checkpoint)
25
+ tokenizer = AutoTokenizer.from_pretrained(checkpoint)
26
+
27
+ def translate(source, target, text):
28
+ formats = {'English':'eng_Latn', 'Asante': 'aka_Latn', 'Akuapem': 'twi_Latn', 'Ewe':'ewe_Latn', 'Hausa': 'hau_Latn'}
29
+ source_fmt = formats[source]
30
+ target_fmt = formats[target]
31
+ translator = pipeline('translation',
32
+ model=model,
33
+ tokenizer=tokenizer,
34
+ src_lang=source_fmt,
35
+ tgt_lang=target_fmt,
36
+ max_length = 400)
37
+
38
+ output = translator(text)
39
+ translated_text = output[0]['translation_text']
40
+ return translated_text
41
+
42
+ title = "Ananse AI | Ghanaian Language Translator"
43
+
44
+ demo = gr.Interface(
45
+ fn=translate,
46
+ inputs=[gr.inputs.Dropdown(choices=['Akuapem','Asante', 'English', 'Ewe','Hausa'], label='Source Language'), gr.inputs.Dropdown(choices=['Akuapem','Asante', 'English', 'Ewe','Hausa'], label='Target Language'), gr.inputs.Textbox(lines=5, label='Text to Translate')],
47
+ outputs=[gr.outputs.Textbox(label='Translated Text')],
48
+ title=title,
49
+ #description = 'This is a translator to translate popular Ghanaian languages.',
50
+ #article='Ananse AI | hnmensah',
51
+ examples = [['English','Asante','Kame went to Kaneshie to buy tomates.'],
52
+ ['English','Ewe','The event should be hosted at the Accra Mall.'],
53
+ ['English','Akuapem','The trader is suffering from Malaria so she did not go to work.'],
54
+ ['English','Hausa','The last person to get to the class will be sacked.']],
55
+ )
56
+
57
+ demo.launch(share=True, debug=False)