File size: 2,227 Bytes
d31e8ca
79787b2
d31e8ca
79787b2
d31e8ca
79787b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
afa6fc0
79787b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
afa6fc0
79787b2
 
afa6fc0
 
4d6afbd
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import gradio as gr
import time
from utils import Bot
from utils import make_documens, make_descriptions

def init_bot(table=None,tittle=None,pdf=None,key=None):
    
    if key is None:
        return 'You must submit OpenAI key'
    
    if pdf is None:
        return 'You must submit pdf file'
    
    if table is None:
        return 'You must submit table file'
    
    if tittle is None:
        return 'You must submit tittle of table'
    
    table = table.name
    pdf = pdf.name
    table_descriptions = make_descriptions(tittle=tittle,table=table)
    documents = make_documens(pdf)
    
    print(documents[0])
    global bot
    
    bot = Bot(
        openai_api_key=key,
        table_descriptions=table_descriptions,
        text_documents=documents,
        verbose=False
    )
    
    return 'Chat bot successfully initialized'

def msg_bot(history):
    message = history[-1][0]
    
    bot_message = bot(message)['output']
    history[-1][1] = ""
    for character in bot_message:
        history[-1][1] += character
        time.sleep(0.05)
        yield history

def user(user_message, history):
    return "", history + [[user_message, None]]

with gr.Blocks() as demo:
    
    with gr.Tab("Chat bot initialization"):
        
        key = gr.Textbox(label='OpenAI key')
    
        with gr.Row(variant='panel'):
            with gr.Column():
                with gr.Row():
                    title = gr.Textbox(label='Table tittle')
                with gr.Row():
                    table = gr.File(label='csv table')

            pdf = gr.File(label='pdf')
        with gr.Row(variant='panel'):

            init_button = gr.Button('submit')
            init_output = gr.Textbox(label="Initialization status")
            init_button.click(fn=init_bot,inputs=[table,title,pdf,key],outputs=init_output,api_name='init')
    
    
    
    with gr.Tab("Chat bot"):
    
        chatbot = gr.Chatbot()
        msg = gr.Textbox(label='Ask the bot')
        clear = gr.Button('Clear')
        msg.submit(user,[msg,chatbot],[msg,chatbot],queue=False).then(
            msg_bot, chatbot, chatbot
        )
        clear.click(lambda: None, None, chatbot, queue=False)

demo.queue()
demo.launch()