File size: 2,289 Bytes
b1a85eb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# !/usr/bin/env python
# -*- coding:utf-8 -*-
# ==================================================================
# [CreatedDate]  : Thursday, 1970-01-01 08:00:00
# [Author]       : shixiaofeng
# [Descriptions] :
# ==================================================================
# [ChangeLog]:
# [Date]    	[Author]	[Comments]
# ------------------------------------------------------------------


import json
import logging
import time

import gradio as gr
import requests

logger = logging.getLogger('gradio')
gr.close_all()

host = "127.0.0.1:9172"


def tableqa(input_question, history=""):
    logger.info("run tableqa")
    if history == "":
        history_sql = None
    else:
        history_sql = json.loads(history.replace("\'", "\""))
    data = {"raw_data": {'question': input_question, 'history_sql': history_sql}}

    ts = time.time()
    r = requests.post(f"http://{host}/tableqa", json=data)
    response = json.loads(r.text)
    print("response", response)
    te = time.time()
    print("run inference_mask_sam success [{}], time_Cost is [{}]".format(response["code"] == 200, te-ts))

    if response["code"] == 200:
        df_value = response["result"]["select_df"]
        df = {"data":df_value["rows"],"headers":df_value["header_name"]}
        return [df, response["result"]["sql_string"], response["result"]["sql_query"], response["result"]["history"], response["result"]["query_result"]]
    else:
        return ["1", "2", "3", "4",]


# iface = gr.Interface(fn=greet, inputs="text", outputs=["输出sql语句","输出可执行sql语句","执行结果"])
iface = gr.Interface(fn=tableqa, inputs=[gr.Textbox(label="input_question", info="请输入想要查询的问题."),
                                         gr.Textbox(label="history sql", info="上下文对话历史信息.")],
                      outputs=[gr.DataFrame(label="索引到的数据库"),
                               gr.Textbox(label="输出sql语句"),
                               gr.Textbox(label="输出可执行sql语句"),
                               gr.Textbox(label="多轮对话历史sql"),
                               gr.Textbox(label="SQL执行结果")]
                     )

iface.launch(enable_queue=False, server_name="0.0.0.0", server_port=9176, debug=True)