Spaces:
Sleeping
Sleeping
Update app
Browse files
app.py
CHANGED
@@ -1,64 +1,86 @@
|
|
1 |
import gradio as gr
|
2 |
-
from
|
|
|
|
|
|
|
|
|
3 |
|
4 |
-
"""
|
5 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
6 |
-
"""
|
7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
8 |
|
|
|
|
|
|
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
system_message,
|
14 |
-
max_tokens,
|
15 |
-
temperature,
|
16 |
-
top_p,
|
17 |
-
):
|
18 |
-
messages = [{"role": "system", "content": system_message}]
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
-
|
|
|
27 |
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
stream=True,
|
34 |
-
temperature=temperature,
|
35 |
-
top_p=top_p,
|
36 |
-
):
|
37 |
-
token = message.choices[0].delta.content
|
38 |
|
39 |
-
|
40 |
-
|
|
|
41 |
|
|
|
42 |
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
50 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
52 |
-
gr.Slider(
|
53 |
-
minimum=0.1,
|
54 |
-
maximum=1.0,
|
55 |
-
value=0.95,
|
56 |
-
step=0.05,
|
57 |
-
label="Top-p (nucleus sampling)",
|
58 |
-
),
|
59 |
-
],
|
60 |
-
)
|
61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
|
63 |
if __name__ == "__main__":
|
64 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import AutoModelForSeq2SeqLM, NllbTokenizer
|
3 |
+
import torch
|
4 |
+
from sacremoses import MosesPunctNormalizer
|
5 |
+
import re
|
6 |
+
import unicodedata
|
7 |
|
8 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
|
|
|
|
|
9 |
|
10 |
+
# Load the big model
|
11 |
+
big_tokenizer = NllbTokenizer.from_pretrained("hunterschep/amis-zh-3.3B")
|
12 |
+
big_model = AutoModelForSeq2SeqLM.from_pretrained("hunterschep/amis-zh-3.3B").to(device)
|
13 |
|
14 |
+
# Load the small model
|
15 |
+
small_tokenizer = NllbTokenizer.from_pretrained("hunterschep/amis-zh-600M")
|
16 |
+
small_model = AutoModelForSeq2SeqLM.from_pretrained("hunterschep/amis-zh-600M").to(device)
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
+
# Fix tokenizers
|
19 |
+
def fix_tokenizer(tokenizer, new_lang='ami_Latn'):
|
20 |
+
old_len = len(tokenizer) - int(new_lang in tokenizer.added_tokens_encoder)
|
21 |
+
tokenizer.lang_code_to_id[new_lang] = old_len - 1
|
22 |
+
tokenizer.id_to_lang_code[old_len - 1] = new_lang
|
23 |
+
tokenizer.fairseq_tokens_to_ids["<mask>"] = len(tokenizer.sp_model) + len(tokenizer.lang_code_to_id) + tokenizer.fairseq_offset
|
24 |
+
tokenizer.fairseq_tokens_to_ids.update(tokenizer.lang_code_to_id)
|
25 |
+
tokenizer.fairseq_ids_to_tokens = {v: k for k, v in tokenizer.fairseq_tokens_to_ids.items()}
|
26 |
+
if new_lang not in tokenizer._additional_special_tokens:
|
27 |
+
tokenizer._additional_special_tokens.append(new_lang)
|
28 |
+
tokenizer.added_tokens_encoder = {}
|
29 |
+
tokenizer.added_tokens_decoder = {}
|
30 |
|
31 |
+
fix_tokenizer(big_tokenizer)
|
32 |
+
fix_tokenizer(small_tokenizer)
|
33 |
|
34 |
+
# Translation function
|
35 |
+
def translate(text, model_type, src_lang, tgt_lang):
|
36 |
+
tokenizer, model = (big_tokenizer, big_model) if model_type == "Large" else (small_tokenizer, small_model)
|
37 |
+
if src_lang == "zho_Hant":
|
38 |
+
text = preproc_chinese(text)
|
39 |
+
tokenizer.src_lang = src_lang
|
40 |
+
tokenizer.tgt_lang = tgt_lang
|
41 |
+
inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True, max_length=1024)
|
42 |
+
model.eval()
|
43 |
+
result = model.generate(
|
44 |
+
**inputs.to(model.device),
|
45 |
+
forced_bos_token_id=tokenizer.convert_tokens_to_ids(tgt_lang),
|
46 |
+
max_new_tokens=256,
|
47 |
+
num_beams=4
|
48 |
+
)
|
49 |
+
return tokenizer.batch_decode(result, skip_special_tokens=True)[0]
|
50 |
|
51 |
+
# Preprocessing for Chinese
|
52 |
+
mpn_chinese = MosesPunctNormalizer(lang="zh")
|
53 |
+
mpn_chinese.substitutions = [(re.compile(r), sub) for r, sub in mpn_chinese.substitutions]
|
|
|
|
|
|
|
|
|
|
|
54 |
|
55 |
+
def get_non_printing_char_replacer(replace_by=" "):
|
56 |
+
non_printable_map = {ord(c): replace_by for c in (chr(i) for i in range(sys.maxunicode + 1)) if unicodedata.category(c) in {"C", "Cc", "Cf", "Cs", "Co", "Cn"}}
|
57 |
+
return lambda line: line.translate(non_printable_map)
|
58 |
|
59 |
+
replace_nonprint = get_non_printing_char_replacer(" ")
|
60 |
|
61 |
+
def preproc_chinese(text):
|
62 |
+
clean = text
|
63 |
+
for pattern, sub in mpn_chinese.substitutions:
|
64 |
+
clean = pattern.sub(sub, clean)
|
65 |
+
clean = replace_nonprint(clean)
|
66 |
+
return unicodedata.normalize("NFKC", clean)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
|
68 |
+
# Gradio interface
|
69 |
+
def switch_direction(src_lang, tgt_lang):
|
70 |
+
return tgt_lang, src_lang
|
71 |
+
|
72 |
+
with gr.Blocks() as demo:
|
73 |
+
gr.Markdown("# AMIS - Chinese Translation Tool")
|
74 |
+
model_type = gr.Radio(choices=["Small", "Large"], value="Small", label="Model Type")
|
75 |
+
src_lang = gr.Radio(choices=["zho_Hant", "ami_Latn"], value="zho_Hant", label="Source Language")
|
76 |
+
tgt_lang = gr.Radio(choices=["ami_Latn", "zho_Hant"], value="ami_Latn", label="Target Language")
|
77 |
+
input_text = gr.Textbox(label="Input Text", placeholder="Enter text here...")
|
78 |
+
output_text = gr.Textbox(label="Translated Text", interactive=False)
|
79 |
+
translate_btn = gr.Button("Translate")
|
80 |
+
switch_btn = gr.Button("Switch Direction")
|
81 |
+
|
82 |
+
translate_btn.click(translate, inputs=[input_text, model_type, src_lang, tgt_lang], outputs=output_text)
|
83 |
+
switch_btn.click(switch_direction, inputs=[src_lang, tgt_lang], outputs=[src_lang, tgt_lang])
|
84 |
|
85 |
if __name__ == "__main__":
|
86 |
demo.launch()
|