File size: 2,287 Bytes
556e052
 
d775f5c
 
556e052
d775f5c
556e052
d775f5c
 
556e052
d775f5c
 
ad079c2
d775f5c
 
ad079c2
d775f5c
 
ad079c2
d775f5c
 
 
 
 
 
ad079c2
d775f5c
 
ad079c2
d775f5c
 
 
 
 
 
ad079c2
d775f5c
 
 
 
ad079c2
d775f5c
 
 
 
 
 
ad079c2
d775f5c
 
ad079c2
 
d775f5c
 
 
ad079c2
d775f5c
 
 
 
 
 
 
 
ad079c2
d775f5c
 
 
 
ad079c2
d775f5c
 
 
 
 
 
 
 
 
 
 
852b083
 
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
import gradio as gr
import time
from utils import Bot
from utils.functions import make_documents, make_descriptions

def init_bot(file=None,title=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 file is None:
        return 'You must submit media file'
    
    if title is None:
        return 'You must submit the description of the media'
    
    file = file.name
    print(file)
    pdf = pdf.name
    file_description = make_descriptions(file, title)
    # print(file_description)
    documents = make_documents(pdf)
    
    # print(documents[0])
    global bot
    
    bot = Bot(
        openai_api_key=key,
        file_descriptions=file_description,
        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:
    
    key = gr.Textbox(label='OpenAI key')
    with gr.Tab("Chat bot initialization"):
            
        with gr.Row(variant='panel'):
            with gr.Column():
                with gr.Row():
                    title = gr.Textbox(label='File short description')
                with gr.Row():
                    file = gr.File(label='CSV or image', file_types=['.csv', 'image'])

            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=[file,title,pdf,key],outputs=init_output,api_name='init')
            
        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()