Spaces:
Runtime error
Runtime error
edchengg
commited on
Commit
·
ed7b79a
1
Parent(s):
6bc77e1
Intial
Browse files- app.py +83 -1
- flores200_codes.py +0 -0
- requirements.txt +0 -0
app.py
CHANGED
@@ -1,3 +1,85 @@
|
|
|
|
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
2 |
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import torch
|
3 |
import gradio as gr
|
4 |
+
import time
|
5 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
|
6 |
+
from flores200_codes import flores_codes
|
7 |
|
8 |
+
|
9 |
+
def load_models():
|
10 |
+
# build model and tokenizer
|
11 |
+
model_name_dict = {
|
12 |
+
'nllb-3.3B': "models/ychenNLP/nllb-200-3.3b-ep",
|
13 |
+
}
|
14 |
+
|
15 |
+
model_dict = {}
|
16 |
+
|
17 |
+
for call_name, real_name in model_name_dict.items():
|
18 |
+
print('\tLoading model: %s' % call_name)
|
19 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(real_name)
|
20 |
+
tokenizer = AutoTokenizer.from_pretrained(real_name)
|
21 |
+
model_dict[call_name+'_model'] = model
|
22 |
+
model_dict[call_name+'_tokenizer'] = tokenizer
|
23 |
+
|
24 |
+
return model_dict
|
25 |
+
|
26 |
+
|
27 |
+
def translation(source, target, text):
|
28 |
+
if len(model_dict) == 2:
|
29 |
+
model_name = 'nllb-3.3B'
|
30 |
+
|
31 |
+
start_time = time.time()
|
32 |
+
source = flores_codes[source]
|
33 |
+
target = flores_codes[target]
|
34 |
+
|
35 |
+
model = model_dict[model_name + '_model']
|
36 |
+
tokenizer = model_dict[model_name + '_tokenizer']
|
37 |
+
|
38 |
+
translator = pipeline('translation', model=model, tokenizer=tokenizer, src_lang=source, tgt_lang=target)
|
39 |
+
output = translator(text, max_length=400)
|
40 |
+
|
41 |
+
end_time = time.time()
|
42 |
+
|
43 |
+
full_output = output
|
44 |
+
output = output[0]['translation_text']
|
45 |
+
result = {'inference_time': end_time - start_time,
|
46 |
+
'source': source,
|
47 |
+
'target': target,
|
48 |
+
'result': output,
|
49 |
+
'full_output': full_output}
|
50 |
+
return result
|
51 |
+
|
52 |
+
|
53 |
+
if __name__ == '__main__':
|
54 |
+
print('\tinit models')
|
55 |
+
|
56 |
+
global model_dict
|
57 |
+
|
58 |
+
model_dict = load_models()
|
59 |
+
|
60 |
+
# define gradio demo
|
61 |
+
lang_codes = list(flores_codes.keys())
|
62 |
+
|
63 |
+
inputs = [gr.inputs.Dropdown(lang_codes, default='English', label='Source'),
|
64 |
+
gr.inputs.Dropdown(lang_codes, default='Chinese (Simplified)', label='Target'),
|
65 |
+
gr.inputs.Textbox(lines=5, label="Input text"),
|
66 |
+
]
|
67 |
+
|
68 |
+
outputs = gr.outputs.JSON()
|
69 |
+
|
70 |
+
title = "NLLB 3.3B"
|
71 |
+
|
72 |
+
demo_status = "Demo is running on CPU"
|
73 |
+
description = f"{demo_status}"
|
74 |
+
examples = [
|
75 |
+
['English', 'Chinese (Simplified)', 'i would like to find flights from [0] columbus [\0] to [1] minneapolis [\1] on [2] monday [\2] [3] june [\3] [4] fourteenth [\4] [5] early [\5] in the [6] morning [\6] [7] or [\7] in the [8] evening [\8] [9] sunday [\9] [10] june [\10] [11] thirteenth [\11] thank you']
|
76 |
+
]
|
77 |
+
|
78 |
+
gr.Interface(translation,
|
79 |
+
inputs,
|
80 |
+
outputs,
|
81 |
+
title=title,
|
82 |
+
description=description,
|
83 |
+
examples=examples,
|
84 |
+
examples_per_page=50,
|
85 |
+
).launch()
|
flores200_codes.py
ADDED
File without changes
|
requirements.txt
ADDED
File without changes
|