svjack commited on
Commit
319536d
1 Parent(s): 0f7e048

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -0
app.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ '''
2
+ from easynmt import EasyNMT
3
+ import os
4
+ trans_model = EasyNMT("m2m_100_1.2B")
5
+ '''
6
+
7
+ from hf_hub_ctranslate2 import MultiLingualTranslatorCT2fromHfHub
8
+ from transformers import AutoTokenizer
9
+ import gradio as gr
10
+
11
+ tokenizer = AutoTokenizer.from_pretrained(f"facebook/m2m100_1.2B")
12
+
13
+ model = MultiLingualTranslatorCT2fromHfHub(
14
+ model_name_or_path="michaelfeil/ct2fast-m2m100_1.2B", device="cpu", compute_type="int8",
15
+ tokenizer=tokenizer
16
+ )
17
+
18
+ '''
19
+ outputs = model.generate(
20
+ ["In this scenario, we see three individuals - two women and one man. The first woman (W1) is walking on footpath while holding her mobile phone in hand with earphones plugged into it. She seems deep in thought or enjoying music from her device. Behind W1, there's another female pedestrian (W2), who appears slightly distracted as she looks around while strolling alongside a tree-lined pathway filled with greenery. Meanwhile, standing by himself near the edge of a cliff overlooking vast fields full of crops like wheat and corn, dressed formally but looking somewhat uncertain/anxious is the third individual, represented by the last emoji depicting him buttoning his suit jacket"],
21
+ src_lang=["en"],
22
+ tgt_lang=["zh"]
23
+ )
24
+ '''
25
+
26
+ example_sample = [
27
+ ["What is the US currency?","en","zh"],
28
+ ["What is the US currency?","en","ja"],
29
+ ["美国的通货是什么?", "zh", "en"]
30
+ ]
31
+
32
+ def demo_func(src_question, src_lang, tgt_lang):
33
+ assert type(src_question) == type("")
34
+ assert type(src_lang) == type("")
35
+ assert type(tgt_lang) == type("")
36
+ if "[SEP]" in src_question:
37
+ src_question = list(filter(lambda xx: xx ,map(lambda x: x.strip() ,
38
+ src_question.split("[SEP]"))))
39
+ else:
40
+ src_question = [src_question]
41
+ '''
42
+ tgt_question = trans_model.translate(
43
+ src_question,
44
+ source_lang=src_lang, target_lang = tgt_lang
45
+ )
46
+ '''
47
+ tgt_question = model.generate(
48
+ src_question,
49
+ src_lang=[src_lang],
50
+ tgt_lang=[tgt_lang]
51
+ )
52
+ assert type(tgt_question) == type([])
53
+ tgt_question = "[SEP]".join(tgt_question)
54
+ return {
55
+ "Target Question": tgt_question
56
+ }
57
+
58
+
59
+ demo = gr.Interface(
60
+ fn=demo_func,
61
+ inputs=[gr.Text(label = "Source Question"),
62
+ gr.Text(label = "Source Language", value = "en"),
63
+ gr.Text(label = "Target Language", value = "zh")
64
+ ],
65
+ outputs="json",
66
+ title=f"cTranslate 🐱⚡️ demonstration",
67
+ examples=example_sample if example_sample else None,
68
+ cache_examples = False
69
+ )
70
+
71
+ demo.launch(server_name=None, server_port=None)