svjack commited on
Commit
73803f6
·
1 Parent(s): 89b2591

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -0
app.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from predict import *
2
+ #from reconstructor import *
3
+ from transformers import BertTokenizer, GPT2LMHeadModel
4
+
5
+ import os
6
+ import gradio as gr
7
+
8
+ model_path = "svjack/gpt-dialogue"
9
+ tokenizer = BertTokenizer.from_pretrained(model_path)
10
+ model = GPT2LMHeadModel.from_pretrained(model_path)
11
+
12
+ obj = Obj(model, tokenizer)
13
+
14
+ example_sample = [
15
+ ["今天天气不错。", 128],
16
+ ["你饿吗?", 128],
17
+ ]
18
+
19
+ def demo_func(prefix, max_length):
20
+ max_length = max(int(max_length), 32)
21
+ x = obj.predict(prefix, max_length=max_length)[0]
22
+ y = list(map(lambda x: "".join(x).replace(" ", ""),batch_as_list(re.split(r"([。.??])" ,x), 2)))
23
+ #l = predict_split(y)
24
+ l = y
25
+ assert type(l) == type([])
26
+ return {
27
+ "Dialogue Context": l
28
+ }
29
+
30
+ demo = gr.Interface(
31
+ fn=demo_func,
32
+ inputs=[gr.Text(label = "Prefix"),
33
+ gr.Number(label = "Max Length", value = 128)
34
+ ],
35
+ outputs="json",
36
+ title=f"GPT Chinese Dialogue Generator 🐰 demonstration",
37
+ examples=example_sample if example_sample else None,
38
+ description = 'This _example_ was **drive** from <br/><b><h4>[https://github.com/svjack/Daliy-Dialogue](https://github.com/svjack/Daliy-Dialogue)</h4></b>\n',
39
+ cache_examples = False
40
+ )
41
+
42
+ demo.launch(server_name=None, server_port=None)