File size: 2,199 Bytes
223340a
 
 
 
 
 
 
 
 
37b9e99
223340a
37b9e99
 
223340a
 
 
 
 
 
 
 
 
 
 
 
37b9e99
 
223340a
37b9e99
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
223340a
37b9e99
 
223340a
 
 
 
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
60
61
62
63
64
'''
Author: Qiguang Chen
LastEditors: Qiguang Chen
Date: 2023-02-07 15:42:32
LastEditTime: 2023-02-19 21:04:03
Description: 

'''
import argparse
import gradio as gr

from common.config import Config
from common.model_manager import ModelManager
from common.utils import str2bool


parser = argparse.ArgumentParser()
parser.add_argument('--config_path', '-cp', type=str, default="config/examples/from_pretrained.yaml")
parser.add_argument('--push_to_public', '-p', type=str2bool, nargs='?',
                        const=True, default=False,
                        help="Push to public network.")
args = parser.parse_args()
config = Config.load_from_yaml(args.config_path)
config.base["train"] = False
config.base["test"] = False

model_manager = ModelManager(config)
model_manager.init_model()


def text_analysis(text):
    print(text)
    data = model_manager.predict(text)
    html = """<link href="https://cdn.staticfile.org/twitter-bootstrap/5.1.1/css/bootstrap.min.css" rel="stylesheet">
                <script src="https://cdn.staticfile.org/twitter-bootstrap/5.1.1/js/bootstrap.bundle.min.js"></script>"""
    html += """<div style="background: white; padding: 16px;"><b>Intent:</b>"""

    for intent in data["intent"]:
        html += """<button type="button" class="btn btn-white">
                        <span class="badge text-dark btn-light">""" + intent + """</span> </button>"""
    html += """<br /> <b>Slot:</b>"""
    for t, slot in zip(data["text"], data["slot"]):
        html += """<button type="button" class="btn btn-white">"""+t+"""<span class="badge text-dark" style="background-color: rgb(255, 255, 255);
                            color: rgb(62 62 62);
                            box-shadow: 2px 2px 7px 1px rgba(210, 210, 210, 0.42);">"""+slot+\
                            """</span>
                    </button>"""
    html+="</div>"
    return html


demo = gr.Interface(
    text_analysis,
    gr.Textbox(placeholder="Enter sentence here..."),
    ["html"],
    examples=[
        ["i would like to find a flight from charlotte to las vegas that makes a stop in st louis"],
    ],
)
if args.push_to_public:
    demo.launch(share=True)
else:
    demo.launch()