-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
50 lines (44 loc) · 1.61 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import os
#os.system("pip install flagai==1.5.0")
import gradio as gr
from translate import *
from generate import *
example_sample = [
["Which keyboard do you prefer?", "en", 2],
["東京と京都のどちらが住みやすいですか。", "ja", 2],
["程序员要掌握哪些技能?", "zh", 2],
]
def demo_func(question, lang, max_times):
assert type(question) == type("")
max_times = max(int(max_times), 1)
if not lang.startswith("zh"):
zh_question = translate_func(question, lang, "zh")
zh_question = zh_question["Target Question"]
else:
zh_question = question
gen_output = generate_func(zh_question, max_times)
gen_output = gen_output["Dialogue Context"]
assert type(gen_output) == type([])
zh_gen_output = "[SEP]".join(gen_output)
if not lang.startswith("zh"):
lang_question = translate_func(zh_gen_output, "zh", lang)
lang_question = lang_question["Target Question"]
else:
lang_question = zh_gen_output
l = list(filter(lambda y: y ,map(lambda x: x.strip() ,lang_question.split("[SEP]"))))
assert type(l) == type([])
return {
"Dialogue Context": l
}
demo = gr.Interface(
fn=demo_func,
inputs=[gr.Text(label = "Question"),
gr.Text(label = "Language", value = "en"),
gr.Number(label = "Forward Times", value = 2)
],
outputs="json",
title=f"Open Dialogue Generator by GLM ⚡️ demonstration",
examples=example_sample if example_sample else None,
cache_examples = False
)
demo.launch(server_name=None, server_port=None)