File size: 1,677 Bytes
7360ef0
0a632f8
 
 
 
 
 
 
890265a
0a632f8
70fbba8
0a632f8
 
 
 
 
9ebd691
70fbba8
0a632f8
 
cbe21ec
0a632f8
890265a
fb47976
7f0560a
fb47976
 
7f0560a
 
 
 
 
 
 
890265a
 
7f0560a
0a632f8
 
 
 
 
 
 
 
 
6b720a2
 
bc9e4ea
 
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
from typing import Callable, Dict, Literal, Union

import gradio as gr


def get_app(
    models: list[str],
    default_model: str,
    src: Union[Callable[[str, str | None], gr.Blocks], Literal["models"], Dict[str, gr.Blocks]],
    accept_token: bool = False,
    dropdown_label: str = "Select Model",
    **kwargs,
) -> gr.Blocks:
    def update_model(new_model: str) -> list[gr.Column]:
        return [gr.Column(visible=model_name == new_model) for model_name in models]

    with gr.Blocks(fill_height=True) as demo:
        model = gr.Dropdown(label=dropdown_label, choices=models, value=default_model)

        columns = []
        for model_name in models:
            with gr.Column(visible=model_name == default_model) as column:
                if isinstance(src, dict):
                    if ':' in model_name:
                        block = src[model_name]
                    else:
                        model_key = f"qwen:{model_name}"
                        block = src.get(model_key, src.get(model_name))
                    
                    if isinstance(block, gr.Blocks):
                        block.render()
                    else:
                        # Handle the case where block is a function
                        block().render()
                else:
                    gr.load(name=model_name, src=src, accept_token=accept_token, **kwargs)
                columns.append(column)

        model.change(
            fn=update_model,
            inputs=model,
            outputs=columns,
            api_name=False,
            queue=False,
        )

    for fn in demo.fns.values():
        fn.api_name = False

    return demo