File size: 2,140 Bytes
630cbf4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import time

import gradio as gr

import modelscope_gradio_components as mgr


def resolve_assets(relative_path):
    return os.path.join(os.path.dirname(__file__), "../resources",
                        relative_path)


conversation = [
    [
        None,
        {
            # bot 第一句话关闭打字机效果,直接输入内容
            "text": "Hello I'm a chatbot",
            "flushing": False
        }
    ],
]


def get_last_bot_message(chatbot):
    return chatbot[-1][1]


def create_music_bot_message(text: str):
    return {
        "text": text,
    }


def create_image_bot_message(text: str):
    return {
        "text": text,
    }


def submit(_input, _chatbot):
    _chatbot.append([_input, None])
    yield gr.update(interactive=False, value=None), _chatbot
    _chatbot[-1][1] = [
        "Hello",
        create_image_bot_message("Hello"),
        create_music_bot_message("Hello")
    ]

    time.sleep(2)
    get_last_bot_message(_chatbot)[1][
        "text"] = f"""Hello, I\'m a image bot\n![image]({resolve_assets("user.jpeg")})"""
    get_last_bot_message(_chatbot)[2][
        "text"] = f"""Hello, I\'m a music bot <audio src="{resolve_assets("audio.wav")}"></audio>"""
    yield {
        chatbot: _chatbot,
    }


def flushed():
    return gr.update(interactive=True)


with gr.Blocks() as demo:
    chatbot = mgr.Chatbot(
        value=conversation,
        avatar_image_width=40,
        avatar_images=[
            resolve_assets('user.jpeg'),
            # default bot avatar and name
            [{
                "name": "bot",
                "avatar": resolve_assets('bot.jpeg')
            }, {
                "name": "image bot",
                "avatar": resolve_assets('image-bot.jpeg')
            }, {
                "name": "music bot",
                "avatar": resolve_assets('music-bot.jpeg')
            }]
        ],
        height=600,
    )

    input = mgr.MultimodalInput()
    input.submit(fn=submit, inputs=[input, chatbot], outputs=[input, chatbot])
    chatbot.flushed(fn=flushed, outputs=[input])

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