File size: 2,781 Bytes
d88e289
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import time

import gradio as gr
import modelscope_studio.components.antd as antd
import modelscope_studio.components.antdx as antdx
import modelscope_studio.components.base as ms
import modelscope_studio.components.pro as pro


def submit(input_value, chatbot_value):
    chatbot_value.append({
        "role":
        "user",
        "content": [{
            "type": "text",
            "content": input_value["text"]
        }, {
            "type": "file",
            "content": [file for file in input_value["files"]]
        }]
    })
    chatbot_value.append({
        "role": "assistant",
        "loading": True,
        "status": "pending"
    })
    yield gr.update(value=None, loading=True), gr.update(value=chatbot_value)
    time.sleep(2)
    chatbot_value[-1]["loading"] = False
    chatbot_value[-1]["content"] = "Hello"
    chatbot_value[-1]["status"] = "done"
    yield gr.update(loading=False), gr.update(value=chatbot_value)


def cancel(chatbot_value):
    chatbot_value[-1]["loading"] = False
    chatbot_value[-1]["footer"] = "canceled"
    chatbot_value[-1]["status"] = "done"
    return gr.update(loading=False), gr.update(value=chatbot_value)


def clear():
    return gr.update(value=None)


with gr.Blocks() as demo, ms.Application(), antdx.XProvider():
    with antd.Flex(elem_style=dict(height=300), vertical=True):
        chatbot = pro.Chatbot(height=0,
                              elem_style=dict(flex=1),
                              value=[{
                                  "role": "user",
                                  "content": "Hello"
                              }, {
                                  "role": "assistant",
                                  "content": "Hello"
                              }])
        with pro.MultimodalInput(
                mode="block",
                auto_size=dict(minRows=2, maxRows=6),
                upload_config=dict(
                    upload_button_tooltip="Attachments")) as input:
            with ms.Slot("footer"):
                with antd.Tooltip("Clear History"):
                    with antd.Button(value=None,
                                     variant="text",
                                     color="default") as clear_btn:
                        with ms.Slot("icon"):
                            antd.Icon("ClearOutlined")
    submit_event = input.submit(fn=submit,
                                inputs=[input, chatbot],
                                outputs=[input, chatbot])
    input.cancel(fn=cancel,
                 cancels=[submit_event],
                 inputs=[chatbot],
                 outputs=[input, chatbot],
                 queue=False)
    clear_btn.click(fn=clear, outputs=[chatbot])

if __name__ == "__main__":
    demo.queue().launch()