interface.py 4.11 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
15
#
# 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.

import os
luopl's avatar
luopl committed
16
import platform
chenych's avatar
chenych committed
17

chenych's avatar
chenych committed
18
from ..extras.misc import fix_proxy, is_env_enabled
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from ..extras.packages import is_gradio_available
from .common import save_config
from .components import (
    create_chat_box,
    create_eval_tab,
    create_export_tab,
    create_infer_tab,
    create_top,
    create_train_tab,
)
from .css import CSS
from .engine import Engine


if is_gradio_available():
    import gradio as gr


chenych's avatar
chenych committed
37
def create_ui(demo_mode: bool = False) -> "gr.Blocks":
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
38
    engine = Engine(demo_mode=demo_mode, pure_chat=False)
luopl's avatar
luopl committed
39
    hostname = os.getenv("HOSTNAME", os.getenv("COMPUTERNAME", platform.node())).split(".")[0]
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
40

luopl's avatar
luopl committed
41
    with gr.Blocks(title=f"LLaMA Board ({hostname})", css=CSS) as demo:
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
42
43
44
45
46
47
48
49
50
        if demo_mode:
            gr.HTML("<h1><center>LLaMA Board: A One-stop Web UI for Getting Started with LLaMA Factory</center></h1>")
            gr.HTML(
                '<h3><center>Visit <a href="https://github.com/hiyouga/LLaMA-Factory" target="_blank">'
                "LLaMA Factory</a> for details.</center></h3>"
            )
            gr.DuplicateButton(value="Duplicate Space for private use", elem_classes="duplicate-button")

        engine.manager.add_elems("top", create_top())
chenych's avatar
chenych committed
51
        lang: gr.Dropdown = engine.manager.get_elem_by_id("top.lang")
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72

        with gr.Tab("Train"):
            engine.manager.add_elems("train", create_train_tab(engine))

        with gr.Tab("Evaluate & Predict"):
            engine.manager.add_elems("eval", create_eval_tab(engine))

        with gr.Tab("Chat"):
            engine.manager.add_elems("infer", create_infer_tab(engine))

        if not demo_mode:
            with gr.Tab("Export"):
                engine.manager.add_elems("export", create_export_tab(engine))

        demo.load(engine.resume, outputs=engine.manager.get_elem_list(), concurrency_limit=None)
        lang.change(engine.change_lang, [lang], engine.manager.get_elem_list(), queue=False)
        lang.input(save_config, inputs=[lang], queue=False)

    return demo


chenych's avatar
chenych committed
73
def create_web_demo() -> "gr.Blocks":
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
74
    engine = Engine(pure_chat=True)
chenych's avatar
chenych committed
75
    hostname = os.getenv("HOSTNAME", os.getenv("COMPUTERNAME", platform.node())).split(".")[0]
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
76

chenych's avatar
chenych committed
77
    with gr.Blocks(title=f"LLaMA Factory Web Demo ({hostname})", css=CSS) as demo:
chenych's avatar
chenych committed
78
        lang = gr.Dropdown(choices=["en", "ru", "zh", "ko", "ja"], scale=1)
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
79
80
        engine.manager.add_elems("top", dict(lang=lang))

chenych's avatar
chenych committed
81
82
        _, _, chat_elems = create_chat_box(engine, visible=True)
        engine.manager.add_elems("infer", chat_elems)
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
83
84
85
86
87
88
89
90

        demo.load(engine.resume, outputs=engine.manager.get_elem_list(), concurrency_limit=None)
        lang.change(engine.change_lang, [lang], engine.manager.get_elem_list(), queue=False)
        lang.input(save_config, inputs=[lang], queue=False)

    return demo


chenych's avatar
chenych committed
91
def run_web_ui() -> None:
chenych's avatar
chenych committed
92
93
    gradio_ipv6 = is_env_enabled("GRADIO_IPV6")
    gradio_share = is_env_enabled("GRADIO_SHARE")
luopl's avatar
luopl committed
94
    server_name = os.getenv("GRADIO_SERVER_NAME", "[::]" if gradio_ipv6 else "0.0.0.0")
chenych's avatar
chenych committed
95
96
    print("Visit http://ip:port for Web UI, e.g., http://127.0.0.1:7860")
    fix_proxy(ipv6_enabled=gradio_ipv6)
chenych's avatar
chenych committed
97
98
99
100
    create_ui().queue().launch(share=gradio_share, server_name=server_name, inbrowser=True)


def run_web_demo() -> None:
chenych's avatar
chenych committed
101
102
    gradio_ipv6 = is_env_enabled("GRADIO_IPV6")
    gradio_share = is_env_enabled("GRADIO_SHARE")
luopl's avatar
luopl committed
103
    server_name = os.getenv("GRADIO_SERVER_NAME", "[::]" if gradio_ipv6 else "0.0.0.0")
chenych's avatar
chenych committed
104
105
    print("Visit http://ip:port for Web UI, e.g., http://127.0.0.1:7860")
    fix_proxy(ipv6_enabled=gradio_ipv6)
chenych's avatar
chenych committed
106
    create_web_demo().queue().launch(share=gradio_share, server_name=server_name, inbrowser=True)