File size: 4,368 Bytes
2e77168
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f5c4c58
2e77168
 
 
 
 
 
 
 
 
 
 
 
f5c4c58
2e77168
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import time

import gradio as gr
import modelscope_studio.components.antdx as antdx
import modelscope_studio.components.base as ms
import modelscope_studio.components.pro as pro
from modelscope_studio.components.pro.chatbot import (
    ChatbotActionConfig, ChatbotBotConfig, ChatbotMarkdownConfig,
    ChatbotPromptsConfig, ChatbotUserConfig, ChatbotWelcomeConfig)


def welcome_prompt_select(chatbot_value, e: gr.EventData):
    chatbot_value.append({
        "role":
        "user",
        "content":
        e._data["payload"][0]["value"]["description"]
    })
    chatbot_value.append({
        "role": "assistant",
        "loading": True,
        "status": "pending",
    })
    yield gr.update(value=chatbot_value)

    time.sleep(1)
    chatbot_value[-1]["loading"] = False
    chatbot_value[-1]["status"] = 'done'
    chatbot_value[-1]["content"] = """
<think>Thought Content</think>

Content
"""
    yield gr.update(value=chatbot_value)


def retry(chatbot_value, e: gr.EventData):
    index = e._data["payload"][0]["index"]
    chatbot_value[index]["content"] = "Regenerated Content"
    return gr.update(value=chatbot_value)


with gr.Blocks() as demo, ms.Application(), antdx.XProvider():
    chatbot = pro.Chatbot(
        markdown_config=ChatbotMarkdownConfig(allow_tags=["think"]),
        welcome_config=ChatbotWelcomeConfig(
            variant="borderless",
            icon=
            "https://mdn.alipayobjects.com/huamei_iwk9zp/afts/img/A*s5sNRo5LjfQAAAAAAAAAAAAADgCCAQ/fmt.webp",
            title="Hello I'm Chatbot",
            description="Let's start a conversation.",
            prompts=ChatbotPromptsConfig(
                title="How can I help you today?",
                styles={
                    "list": {
                        "width": '100%',
                    },
                    "item": {
                        "flex": 1,
                    },
                },
                items=[{
                    "label":
                    "πŸ“… Make a plan",
                    "children": [{
                        "description":
                        "Help me with a plan to start a business"
                    }, {
                        "description":
                        "Help me with a plan to achieve my goals"
                    }, {
                        "description":
                        "Help me with a plan for a successful interview"
                    }]
                }, {
                    "label":
                    "πŸ–‹ Help me write",
                    "children": [{
                        "description":
                        "Help me write a story with a twist ending"
                    }, {
                        "description":
                        "Help me write a blog post on mental health"
                    }, {
                        "description":
                        "Help me write a letter to my future self"
                    }]
                }])),
        user_config=ChatbotUserConfig(
            actions=[
                "copy", "edit",
                ChatbotActionConfig(
                    action="delete",
                    popconfirm=dict(
                        title="Delete the message",
                        description="Are you sure to delete this message?",
                        okButtonProps=dict(danger=True)))
            ],
            avatar="https://api.dicebear.com/7.x/miniavs/svg?seed=3"),
        bot_config=ChatbotBotConfig(
            # custom actions
            actions=[
                "copy", "like", "dislike", "retry", "edit",
                ChatbotActionConfig(
                    action="delete",
                    popconfirm=dict(
                        title="Delete the message",
                        description="Are you sure to delete this message?",
                        okButtonProps=dict(danger=True)))
            ],
            avatar=
            "https://mdn.alipayobjects.com/huamei_iwk9zp/afts/img/A*s5sNRo5LjfQAAAAAAAAAAAAADgCCAQ/fmt.webp"
        ),
        height=600)

    chatbot.welcome_prompt_select(fn=welcome_prompt_select,
                                  inputs=[chatbot],
                                  outputs=[chatbot])
    chatbot.retry(fn=retry, inputs=[chatbot], outputs=[chatbot])

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