File size: 12,406 Bytes
651793a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
import base64
import os
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
from modelscope_studio.components.pro.chatbot import (ChatbotBotConfig,
                                                      ChatbotPromptsConfig,
                                                      ChatbotWelcomeConfig)
from modelscope_studio.components.pro.multimodal_input import \
  MultimodalInputUploadConfig
from openai import OpenAI

client = OpenAI(
    base_url='https://api-inference.modelscope.cn/v1/',
    api_key=os.getenv("MODELSCOPE_ACCESS_TOKEN"),  # ModelScope Token
)

model = "Qwen/Qwen2.5-72B-Instruct"


def prompt_select(input_value, e: gr.EventData):
    input_value["text"] = e._data["payload"][0]["value"]["description"]
    return gr.update(value=input_value)


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


def cancel(chatbot_value):
    chatbot_value[-1]["loading"] = False
    chatbot_value[-1]["status"] = "done"
    chatbot_value[-1]["footer"] = "Chat completion paused"

    return gr.update(value=chatbot_value), gr.update(loading=False), gr.update(
        disabled=False)


def retry(chatbot_value, e: gr.EventData):
    index = e._data["payload"][0]["index"]
    chatbot_value = chatbot_value[:index]

    yield gr.update(loading=True), gr.update(value=chatbot_value), gr.update(
        disabled=True)
    for chunk in submit(None, chatbot_value):
        yield chunk


def format_history(history):
    messages = [{"role": "system", "content": "You are a helpful assistant."}]
    for item in history:
        if item["role"] == "user":
            messages.append({
                "role":
                "user",
                "content": [{
                    "type": "text",
                    "text": item["content"]
                }]
            })
        elif item["role"] == "assistant":
            # ignore thought message
            messages.append({"role": "assistant", "content": item["content"]})
    return messages


def submit(input_value, chatbot_value):
    if input_value is not None:
        chatbot_value.append({
            "role": "user",
            "content": input_value["text"],
        })
    history_messages = format_history(chatbot_value)
    chatbot_value.append({
        "role": "assistant",
        "content": "",
        "loading": True,
        "status": "pending"
    })
    yield {
        input: gr.update(value=None, loading=True),
        clear_btn: gr.update(disabled=True),
        chatbot: gr.update(value=chatbot_value)
    }

    try:
        response = client.chat.completions.create(model=model,
                                                  messages=history_messages,
                                                  stream=True)
        start_time = time.time()

        for chunk in response:
            chatbot_value[-1]["content"] += chunk.choices[0].delta.content
            chatbot_value[-1]["loading"] = False

            yield {chatbot: gr.update(value=chatbot_value)}

        chatbot_value[-1]["footer"] = "{:.2f}".format(time.time() -
                                                      start_time) + 's'
        chatbot_value[-1]["status"] = "done"
        yield {
            clear_btn: gr.update(disabled=False),
            input: gr.update(loading=False),
            chatbot: gr.update(value=chatbot_value),
        }
    except Exception as e:
        chatbot_value[-1]["loading"] = False
        chatbot_value[-1]["status"] = "done"
        chatbot_value[-1]["content"] = "Failed to respond, please try again."
        yield {
            clear_btn: gr.update(disabled=False),
            input: gr.update(loading=False),
            chatbot: gr.update(value=chatbot_value),
        }
        raise e


def close_copilot():
    return gr.update(md=24), gr.update(md=0), gr.update(elem_style=dict(
        display=""))


def open_copilot():
    return gr.update(md=16), gr.update(md=8), gr.update(elem_style=dict(
        display="none"))


def resize_window(e: gr.EventData):
    screen_width = e._data["screen"]["width"]
    is_mobile = screen_width < 768
    return gr.update(visible=False if is_mobile else True), gr.update(
        visible=False if is_mobile else True)


css = """
.copilot-container {
    height: calc(100vh - 32px - 21px - 16px);
}

.copilot-container .copilot {
    height: 100%;
    padding: 10px;
    background: var(--background-fill-secondary);
    border-top: 1px solid var(--border-color-primary);
    border-right: 1px solid var(--border-color-primary);
    border-bottom: 1px solid var(--border-color-primary);
}


.copilot-container .content-body {
    height: 100%;
    overflow: auto;
}

@media (max-width: 768px) {
    .copilot-container {
        height: auto;
    }
    .copilot-container .copilot {
        height: 600px;
        border-left: 1px solid var(--border-color-primary);
    }
    .copilot-container .content-body {
        height: auto;
        max-height: 400px;
    }
}
"""

with gr.Blocks(css=css) as demo, ms.Application() as app, antdx.XProvider():
    with antd.Row(elem_classes="copilot-container", wrap=True):
        # Content column
        with antd.Col(md=16, xs=24,
                      elem_style=dict(height="100%")) as content_col:
            with ms.AutoLoading(elem_style=dict(height="100%")):
                with antd.Card(elem_style=dict(height="100%",
                                               borderRadius=0,
                                               display="flex",
                                               flexDirection="column"),
                               class_names=dict(body="content-body")):
                    # Title
                    with ms.Slot("title"):
                        with antd.Typography.Title(level=1,
                                                   elem_style=dict(
                                                       margin=0,
                                                       textAlign="center",
                                                       fontSize=30)):
                            ms.Text("🤖 Copilot Template")

                    # Copilot button
                    with ms.Slot("extra"):
                        copilot_open_btn = antd.Button(
                            "✨ AI Copilot",
                            shape="round",
                            variant='filled',
                            color="primary",
                            elem_style=dict(display="none"))

                    # Content
                    ms.Markdown("""
<img src="https://mdn.alipayobjects.com/huamei_iwk9zp/afts/img/A*48RLR41kwHIAAAAAAAAAAAAADgCCAQ/fmt.webp" style="width: 100%;" />
<h4>What is the RICH design paradigm?</h4>
<div>
RICH is an AI interface design paradigm we propose, similar to how the WIMP paradigm
relates to graphical user interfaces.
</div>
<br />
<div>
The ACM SIGCHI 2005 (the premier conference on human-computer interaction) defined
that the core issues of human-computer interaction can be divided into three levels:
</div>
<ul>
<li>
    Interface Paradigm Layer: Defines the design elements of human-computer
    interaction interfaces, guiding designers to focus on core issues.
</li>
<li>
    User model layer: Build an interface experience evaluation model to measure the
    quality of the interface experience.
</li>
<li>
    Software framework layer: The underlying support algorithms and data structures
    for human-computer interfaces, which are the contents hidden behind the front-end
    interface.
</li>
</ul>
<div>
The interface paradigm is the aspect that designers need to focus on and define the
most when a new human-computer interaction technology is born. The interface
paradigm defines the design elements that designers should pay attention to, and
based on this, it is possible to determine what constitutes good design and how to
achieve it.
</div>
""")

        # Copilot column
        with antd.Col(md=8, xs=24,
                      elem_style=dict(height="100%")) as copilot_col:
            with ms.AutoLoading(elem_style=dict(height="100%")):
                with antd.Flex(
                        vertical=True,
                        gap="small",
                        elem_classes="copilot",
                ):
                    with antd.Flex(justify="space-between", align="center"):
                        antd.Typography.Title("✨ AI Copilot",
                                              level=4,
                                              elem_style=dict(margin=0))
                        with antd.Flex(align="center", gap="small"):
                            with antd.Button(
                                    variant="text",
                                    color="default") as copilot_close_btn:
                                with ms.Slot("icon"):
                                    antd.Icon("CloseOutlined")
                    antd.Divider(size="small")
                    chatbot = pro.Chatbot(
                        # for flex=1 to fill the remaining space
                        height=0,
                        elem_style=dict(flex=1),
                        welcome_config=ChatbotWelcomeConfig(
                            variant="filled",
                            title="👋🏻 Hello, I'm AI Copilot",
                            description="Enter a prompt to get started",
                            prompts=ChatbotPromptsConfig(
                                title="I can help: ",
                                vertical=True,
                                items=[{
                                    "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"
                                }])),
                        user_config=dict(
                            avatar=
                            "https://api.dicebear.com/7.x/miniavs/svg?seed=3"),
                        bot_config=ChatbotBotConfig(
                            header="Copilot",
                            actions=["copy", "retry"],
                            avatar=
                            "https://api.dicebear.com/7.x/miniavs/svg?seed=2"),
                    )

                    with pro.MultimodalInput(
                            upload_config=MultimodalInputUploadConfig(
                                allow_upload=False)) as input:
                        with ms.Slot("prefix"):
                            with antd.Button(value=None,
                                             color="default",
                                             variant="text") as clear_btn:
                                with ms.Slot("icon"):
                                    antd.Icon("ClearOutlined")
        clear_btn.click(fn=clear, outputs=[chatbot])
        submit_event = input.submit(fn=submit,
                                    inputs=[input, chatbot],
                                    outputs=[input, chatbot, clear_btn])
        input.cancel(fn=cancel,
                     inputs=[chatbot],
                     outputs=[chatbot, input, clear_btn],
                     cancels=[submit_event],
                     queue=False)
        chatbot.retry(fn=retry,
                      inputs=[chatbot],
                      outputs=[input, chatbot, clear_btn])
        chatbot.welcome_prompt_select(fn=prompt_select,
                                      inputs=[input],
                                      outputs=[input])
        copilot_open_btn.click(
            fn=open_copilot,
            outputs=[content_col, copilot_col, copilot_open_btn])
        copilot_close_btn.click(
            fn=close_copilot,
            outputs=[content_col, copilot_col, copilot_open_btn])
        gr.on([app.mount, app.resize],
              fn=resize_window,
              outputs=[copilot_open_btn, copilot_close_btn])
if __name__ == "__main__":
    demo.queue().launch()