chatbot.py 4.19 KB
Newer Older
chenych's avatar
chenych committed
1
# Copyright 2025 the LlamaFactory team.
chenych's avatar
chenych committed
2
3
4
5
6
7
8
9
10
11
12
13
14
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

chenych's avatar
chenych committed
15
import json
chenych's avatar
chenych committed
16
from typing import TYPE_CHECKING
chenych's avatar
chenych committed
17
18
19

from ...data import Role
from ...extras.packages import is_gradio_available
chenych's avatar
chenych committed
20
from ..locales import ALERTS
chenych's avatar
chenych committed
21
22
23
24
25
26
27
28
29
30
31
32


if is_gradio_available():
    import gradio as gr


if TYPE_CHECKING:
    from gradio.components import Component

    from ..engine import Engine


chenych's avatar
chenych committed
33
def check_json_schema(text: str, lang: str) -> None:
chenych's avatar
chenych committed
34
    r"""Check if the json schema is valid."""
chenych's avatar
chenych committed
35
36
37
38
39
40
41
42
43
44
45
46
47
    try:
        tools = json.loads(text)
        if tools:
            assert isinstance(tools, list)
            for tool in tools:
                if "name" not in tool:
                    raise NotImplementedError("Name not found.")
    except NotImplementedError:
        gr.Warning(ALERTS["err_tool_name"][lang])
    except Exception:
        gr.Warning(ALERTS["err_json_schema"][lang])


chenych's avatar
chenych committed
48
49
def create_chat_box(
    engine: "Engine", visible: bool = False
chenych's avatar
chenych committed
50
) -> tuple["Component", "Component", dict[str, "Component"]]:
chenych's avatar
chenych committed
51
    lang = engine.manager.get_elem_by_id("top.lang")
chenych's avatar
chenych committed
52
    with gr.Column(visible=visible) as chat_box:
chenych's avatar
chenych committed
53
        chatbot = gr.Chatbot(type="messages", show_copy_button=True)
chenych's avatar
chenych committed
54
55
56
57
58
59
60
61
62
        messages = gr.State([])
        with gr.Row():
            with gr.Column(scale=4):
                with gr.Row():
                    with gr.Column():
                        role = gr.Dropdown(choices=[Role.USER.value, Role.OBSERVATION.value], value=Role.USER.value)
                        system = gr.Textbox(show_label=False)
                        tools = gr.Textbox(show_label=False, lines=3)

luopl's avatar
luopl committed
63
64
                    with gr.Column() as mm_box:
                        with gr.Tab("Image"):
chenych's avatar
chenych committed
65
                            image = gr.Image(type="pil")
luopl's avatar
luopl committed
66
67

                        with gr.Tab("Video"):
chenych's avatar
chenych committed
68
69
70
71
                            video = gr.Video()

                        with gr.Tab("Audio"):
                            audio = gr.Audio(type="filepath")
chenych's avatar
chenych committed
72
73
74
75
76

                query = gr.Textbox(show_label=False, lines=8)
                submit_btn = gr.Button(variant="primary")

            with gr.Column(scale=1):
chenych's avatar
chenych committed
77
                max_new_tokens = gr.Slider(minimum=8, maximum=8192, value=1024, step=1)
chenych's avatar
chenych committed
78
79
                top_p = gr.Slider(minimum=0.01, maximum=1.0, value=0.7, step=0.01)
                temperature = gr.Slider(minimum=0.01, maximum=1.5, value=0.95, step=0.01)
chenych's avatar
chenych committed
80
81
                skip_special_tokens = gr.Checkbox(value=True)
                escape_html = gr.Checkbox(value=True)
chenych's avatar
chenych committed
82
83
84
85
86
87
                clear_btn = gr.Button()

    tools.input(check_json_schema, inputs=[tools, engine.manager.get_elem_by_id("top.lang")])

    submit_btn.click(
        engine.chatter.append,
chenych's avatar
chenych committed
88
        [chatbot, messages, role, query, escape_html],
chenych's avatar
chenych committed
89
90
91
        [chatbot, messages, query],
    ).then(
        engine.chatter.stream,
chenych's avatar
chenych committed
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
        [
            chatbot,
            messages,
            lang,
            system,
            tools,
            image,
            video,
            audio,
            max_new_tokens,
            top_p,
            temperature,
            skip_special_tokens,
            escape_html,
        ],
chenych's avatar
chenych committed
107
108
109
110
111
112
113
114
115
116
117
118
        [chatbot, messages],
    )
    clear_btn.click(lambda: ([], []), outputs=[chatbot, messages])

    return (
        chatbot,
        messages,
        dict(
            chat_box=chat_box,
            role=role,
            system=system,
            tools=tools,
luopl's avatar
luopl committed
119
            mm_box=mm_box,
chenych's avatar
chenych committed
120
            image=image,
luopl's avatar
luopl committed
121
            video=video,
chenych's avatar
chenych committed
122
            audio=audio,
chenych's avatar
chenych committed
123
124
125
126
127
            query=query,
            submit_btn=submit_btn,
            max_new_tokens=max_new_tokens,
            top_p=top_p,
            temperature=temperature,
chenych's avatar
chenych committed
128
129
            skip_special_tokens=skip_special_tokens,
            escape_html=escape_html,
chenych's avatar
chenych committed
130
131
132
            clear_btn=clear_btn,
        ),
    )