File size: 5,405 Bytes
bae4131
3119795
 
d1ed69b
bae4131
d1ed69b
5289522
bae4131
 
 
 
 
 
 
 
 
6454c0e
d1ed69b
6454c0e
3119795
 
 
 
 
 
bae4131
 
 
 
 
 
 
 
 
 
3d76e98
23510fc
bae4131
 
 
3d76e98
4a9b060
3d76e98
 
 
 
 
 
 
 
 
 
23510fc
bae4131
816857c
bae4131
 
 
 
 
 
 
 
 
 
 
 
23510fc
d1ed69b
23510fc
 
 
3119795
bae4131
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23510fc
bae4131
 
 
 
 
 
23510fc
bae4131
23510fc
5289522
bae4131
 
3d76e98
bae4131
 
 
 
 
 
 
 
23510fc
816857c
 
 
bae4131
 
 
 
 
23510fc
d1ed69b
bae4131
 
 
 
 
d1ed69b
 
 
 
bae4131
 
 
b975b7b
 
 
 
 
 
 
 
 
 
bae4131
 
b975b7b
 
 
 
 
d1ed69b
6454c0e
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
import os
import sys
import gradio as gr
from loguru import logger
from huggingface_hub import HfApi, whoami

from yourbench_space.config import generate_base_config, save_config
from yourbench_space.utils import (
    CONFIG_PATH,
    UPLOAD_DIRECTORY,
    BASE_API_URLS,
    AVAILABLE_MODELS,
    DEFAULT_MODEL,
    SubprocessManager,
    save_files,
)

UPLOAD_DIRECTORY.mkdir(parents=True, exist_ok=True)

logger.remove()
logger.add(sys.stderr, level="INFO")

command = ["uv", "run", "yourbench", f"--config={CONFIG_PATH}"]
manager = SubprocessManager(command)


def prepare_task(oauth_token: gr.OAuthToken | None, model_token: str):
    new_env = os.environ.copy()
    # Override env token, when running in gradio space
    if oauth_token:
        new_env["HF_TOKEN"] = oauth_token.token
    new_env["MODEL_API_KEY"] = model_token
    manager.start_process(custom_env=new_env)


def update_hf_org_dropdown(oauth_token: gr.OAuthToken | None):
    if oauth_token is None:
        print(
            "Please, deploy this on Spaces and log in to view the list of available organizations"
        )
        return gr.Dropdown([], label="Organization")

    try:
        user_info = whoami(oauth_token.token)
        org_names = [org["name"] for org in user_info.get("orgs", [])]
        user_name = user_info.get("name", "Unknown User")
        org_names.insert(0, user_name)
        return gr.Dropdown(org_names, value=user_name, label="Organization")
    
    except Exception as e:
        print(f"Error retrieving user info: {e}")
        return gr.Dropdown([], label="Organization") 


config_output = gr.Code(label="Generated Config", language="yaml")
model_name = gr.Dropdown(
    label="Model Name",
    value=DEFAULT_MODEL,
    choices=AVAILABLE_MODELS,
    allow_custom_value=True,
)
base_url = gr.Textbox(
    label="Model API Base URL",
    value=BASE_API_URLS["huggingface"],
    info="Use a custom API base URL for Hugging Face Inference Endpoints",
)

with gr.Blocks() as app:
    gr.Markdown("## YourBench Configuration")
    with gr.Row():
        login_btn = gr.LoginButton()

    with gr.Tab("Configuration"):
        with gr.Accordion("Hugging Face"):
            hf_org_dropdown = gr.Dropdown(
                list(),
                label="Organization",
                allow_custom_value=True,
            )
            app.load(update_hf_org_dropdown, inputs=None, outputs=hf_org_dropdown)

            hf_dataset_prefix = gr.Textbox(
                label="Dataset Prefix",
                value="yourbench",
                info="Prefix applied to all datasets",
            )
            private_dataset = gr.Checkbox(
                label="Private Dataset",
                value=True,
                info="Create private datasets (recommended by default)",
            )

        with gr.Accordion("Model"):
            model_name.render()

            provider = gr.Radio(
                ["huggingface", "openrouter", "openai"],
                value="huggingface",
                label="Inference Provider",
            )

            def set_base_url(provider):
                return gr.Textbox(
                    label="Model API Base URL", value=BASE_API_URLS.get(provider, "")
                )

            provider.change(fn=set_base_url, inputs=provider, outputs=base_url)
            model_api_key = gr.Textbox(label="Model API Key", type="password")
            base_url.render()
            max_concurrent_requests = gr.Radio(
                [8, 16, 32], value=16, label="Max Concurrent Requests"
            )

        preview_button = gr.Button("Generate New Config")
        preview_button.click(
            generate_base_config,
            inputs=[
                hf_org_dropdown,
                hf_dataset_prefix,
                model_name,
                provider,
                base_url,
                model_api_key,
                max_concurrent_requests,
                private_dataset,
            ],
            outputs=config_output,
        )

    with gr.Tab("Raw Configuration"):
        config_output.render()
        config_output.change(
            fn=save_config,
            inputs=[config_output],
            outputs=[gr.Textbox(label="Save Status")],
        )

    with gr.Tab("Files"):
        file_input = gr.File(
            label="Upload text files",
            file_count="multiple",
            file_types=[".txt", ".md", ".html"],
        )
        output = gr.Textbox(label="Log")
        file_input.upload(save_files, file_input, output)

    with gr.Tab("Run Generation"):
        log_output = gr.Code(
            label="Log Output", language=None, lines=20, interactive=False
        )
        log_timer = gr.Timer(0.05, active=True)
        log_timer.tick(manager.read_and_get_output, outputs=log_output)

        with gr.Row():
            process_status = gr.Checkbox(label="Process Status", interactive=False)
            status_timer = gr.Timer(0.05, active=True)
            status_timer.tick(manager.is_running, outputs=process_status)

        with gr.Row():
            start_button = gr.Button("Start Task")
            start_button.click(prepare_task, inputs=[model_api_key])

            stop_button = gr.Button("Stop Task")
            stop_button.click(manager.stop_process)

            kill_button = gr.Button("Kill Task")
            kill_button.click(manager.kill_process)

app.launch()