File size: 697 Bytes
3636d87
217f461
3636d87
 
f667279
 
217f461
3636d87
f667279
3636d87
 
 
 
 
 
f667279
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# https://huggingface.co/spaces/kumshing/chinese-to-other
import gradio as gr
# Load model directly
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
tokenizer = AutoTokenizer.from_pretrained("facebook/m2m100_418M")
model = AutoModelForSeq2SeqLM.from_pretrained("facebook/m2m100_418M")


def chineseToOther(chinese_text, lang = "en"):
    okenizer.src_lang = "zh"
    encoded_zh = tokenizer(chinese_text, return_tensors="pt")
    generated_tokens = model.generate(**encoded_zh, forced_bos_token_id=tokenizer.get_lang_id(lang))
    translated_text = tokenizer.batch_decode(generated_tokens, skip_special_tokens=True)
    return translated_text

gr.ChatInterface(chineseToOther).launch()