Jordan Myers
commited on
Commit
•
ce1c4dd
1
Parent(s):
d0b92d8
first work
Browse files- app.py +58 -0
- requirements.txt +3 -0
app.py
ADDED
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
|
3 |
+
import torch
|
4 |
+
|
5 |
+
# this model was loaded from https://hf.co/models
|
6 |
+
model = AutoModelForSeq2SeqLM.from_pretrained("Jayyydyyy/m2m100_418m_tokipona")
|
7 |
+
tokenizer = AutoTokenizer.from_pretrained("facebook/m2m100_418M")
|
8 |
+
device = 0 if torch.cuda.is_available() else -1
|
9 |
+
LANGS = ["English", "toki pona"]
|
10 |
+
LANG_CODES = {
|
11 |
+
"English":"en",
|
12 |
+
"toki pona":"tl"
|
13 |
+
}
|
14 |
+
|
15 |
+
def translate(text, src_lang, tgt_lang):
|
16 |
+
"""
|
17 |
+
Translate the text from source lang to target lang
|
18 |
+
"""
|
19 |
+
|
20 |
+
src = LANG_CODES.get(src_lang)
|
21 |
+
tgt = LANG_CODES.get(tgt_lang)
|
22 |
+
|
23 |
+
tokenizer.src_lang = src
|
24 |
+
tokenizer.tgt_lang = tgt
|
25 |
+
|
26 |
+
ins = tokenizer(text, return_tensors='pt').to(device)
|
27 |
+
|
28 |
+
gen_args = {
|
29 |
+
'return_dict_in_generate': True,
|
30 |
+
'output_scores': True,
|
31 |
+
'output_hidden_states': True,
|
32 |
+
'length_penalty': 0.0, # don't encourage longer or shorter output,
|
33 |
+
'num_return_sequences': 3,
|
34 |
+
'num_beams':3,
|
35 |
+
'forced_bos_token_id': tokenizer.lang_code_to_id[tgt]
|
36 |
+
}
|
37 |
+
|
38 |
+
|
39 |
+
outs = model.generate(**{**ins, **gen_args})
|
40 |
+
output = tokenizer.batch_decode(outs.sequences, skip_special_tokens=True)
|
41 |
+
|
42 |
+
return output
|
43 |
+
|
44 |
+
app = gr.Interface(
|
45 |
+
fn=translate,
|
46 |
+
inputs=[
|
47 |
+
gr.components.Textbox(label="Text"),
|
48 |
+
gr.components.Dropdown(label="Source Language", choices=LANGS),
|
49 |
+
gr.components.Dropdown(label="Target Language", choices=LANGS),
|
50 |
+
],
|
51 |
+
outputs=["text"],
|
52 |
+
examples=[["This is an example!", "English", "toki pona"]],
|
53 |
+
cache_examples=False,
|
54 |
+
title="A simple English / toki pona Neural Translation App",
|
55 |
+
description="A simple English / toki pona Neural Translation App"
|
56 |
+
)
|
57 |
+
|
58 |
+
app.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
torch
|
2 |
+
transformers
|
3 |
+
gradio
|