File size: 1,674 Bytes
da4df12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
51
52
53
54
55
56
57
58
59
import gradio as gr
import os
from predict import *

from transformers import T5ForConditionalGeneration
from transformers import T5TokenizerFast as T5Tokenizer
import pandas as pd
model = "svjack/comet-atomic-zh"
device = "cpu"
#device = "cuda:0"
tokenizer = T5Tokenizer.from_pretrained(model)
model = T5ForConditionalGeneration.from_pretrained(model).to(device).eval()

NEED_PREFIX = '以下事件有哪些必要的先决条件:'
EFFECT_PREFIX = '下面的事件发生后可能会发生什么:'
INTENT_PREFIX = '以下事件的动机是什么:'
REACT_PREFIX = '以下事件发生后,你有什么感觉:'

obj = Obj(model, tokenizer, device)

text0 = "X吃到了一顿大餐。"
text1 = "X和Y一起搭了个积木。"

example_sample = [
    [text0, False],
    [text1, False],
]

def demo_func(event, do_sample):
    #event = "X吃到了一顿大餐。"
    times = 1
    df = pd.DataFrame(
    pd.Series(
        [NEED_PREFIX, EFFECT_PREFIX, INTENT_PREFIX, REACT_PREFIX]
    ).map(
        lambda x: (x, [obj.predict(
            "{}{}".format(x, event), do_sample = do_sample
        )[0] for _ in range(times)][0])
    ).values.tolist()
    )
    df.columns = ["PREFIX", "PRED"]
    l = df.apply(lambda x: x.to_dict(), axis = 1).values.tolist()
    return {
        "Output": l
    }

demo = gr.Interface(
        fn=demo_func,
        inputs=[gr.Text(label = "Event"),
                gr.Checkbox(label="do sample"),
        ],
        outputs="json",
        title=f"Chinese Comet Atomic 🐰 demonstration",
        examples=example_sample if example_sample else None,
        cache_examples = False
    )

demo.launch(server_name=None, server_port=None)