File size: 9,032 Bytes
d1ed69b
3119795
d1ed69b
 
3119795
 
d1ed69b
3119795
 
d1ed69b
 
 
 
 
 
 
3119795
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d1ed69b
 
 
 
 
 
 
 
 
 
 
 
 
3119795
 
 
 
 
d1ed69b
 
3119795
 
 
 
 
 
 
 
 
d1ed69b
 
3119795
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d1ed69b
 
 
 
 
 
 
 
3119795
d1ed69b
 
3119795
 
d1ed69b
 
 
 
 
 
3119795
d1ed69b
 
 
 
 
 
 
 
 
 
3119795
 
d1ed69b
 
 
 
 
 
 
 
3119795
 
 
 
 
 
 
 
d1ed69b
3119795
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import os
import sys
import pathlib
import shutil
import threading
import multiprocessing
import io
import yaml
import gradio as gr
from loguru import logger
from yourbench.pipeline import run_pipeline

UPLOAD_DIRECTORY = pathlib.Path("/app/uploaded_files")
UPLOAD_DIRECTORY.mkdir(parents=True, exist_ok=True)
CONFIG_PATH = pathlib.Path("/app/yourbench_config.yml")

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

import subprocess
import io
import os
import time

class SubprocessManager:
    def __init__(self, command):
        self.command = command
        self.process = None
        self.output_stream = io.StringIO()

    def start_process(self):
        """Start the subprocess."""
        if self.is_running():
            logger.info("Process is already running")
            return

        self.process = subprocess.Popen(
            self.command,
            stdout=subprocess.PIPE,
            stderr=subprocess.STDOUT,  # Combine stderr with stdout
            text=True,
            bufsize=1,  # Line-buffered
            start_new_session=True  # Start the process in a new session
        )
        os.set_blocking(self.process.stdout.fileno(), False)
        logger.info("Started the process")

    def read_and_get_output(self):
        """Read available subprocess output and return the captured output."""
        if self.process and self.process.stdout:
            try:
                while True:
                    line = self.process.stdout.readline()
                    if line:
                        self.output_stream.write(line)  # Capture in StringIO
                    else:
                        break
            except BlockingIOError:
                pass
        return self.output_stream.getvalue()

    def stop_process(self):
        """Terminate the subprocess."""
        if not self.is_running():
            logger.info("Started the process")
            return
        logger.info("Sending SIGTERM to the Process")
        self.process.terminate()
        exit_code = self.process.wait() # Wait for process to terminate
        logger.info(f"Process stopped exit code {exit_code}")
        #return exit_code

    def kill_process(self):
        """Forcefully kill the subprocess."""
        if not self.is_running():
            logger.info("Process is not running")
            return
        logger.info("Sending SIGKILL to the Process")
        self.process.kill()
        exit_code = self.process.wait() # Wait for process to be killed
        logger.info(f"Process killed exit code {exit_code}")
        #return exit_code

    def is_running(self):
        """Check if the subprocess is still running."""
        return self.process and self.process.poll() is None


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

def generate_config(hf_token, hf_org, model_name, provider, base_url, api_key, max_concurrent_requests):
    config = {
        "hf_configuration": {
            "token": hf_token,
            "private": True,
            "hf_organization": hf_org
        },
        "model_list": [{
            "model_name": model_name,
            "provider": provider,
            "base_url": base_url,
            "api_key": api_key,
            "max_concurrent_requests": max_concurrent_requests
        }],
        "model_roles": {role: [model_name] for role in [
            "ingestion", "summarization", "single_shot_question_generation",
            "multi_hop_question_generation", "answer_generation", "judge_answers"
        ]},
        "inference_config": {"max_concurrent_requests": 16},
        "pipeline": {
            "ingestion": {
                "source_documents_dir": "/app/uploaded_files",
                "output_dir": "/app/ingested",
                "run": True
            },
            "upload_ingest_to_hub": {
                "source_documents_dir": "/app/ingested",
                "hub_dataset_name": "test_ingested_documents",
                "local_dataset_path": "/app/ingested_dataset",
                "run": True
            },
            "summarization": {
                "source_dataset_name": "test_ingested_documents",
                "output_dataset_name": "test_summaries",
                "local_dataset_path": "/results/test_summaries",
                "concat_existing_dataset": False,
                "run": True
            },
            "chunking": {
                "source_dataset_name": "test_summaries",
                "output_dataset_name": "test_chunked_documents",
                "local_dataset_path": "/results/test_chunked_documents",
                "concat_existing_dataset": False,
                "chunking_configuration": {
                    "l_min_tokens": 64,
                    "l_max_tokens": 128,
                    "tau_threshold": 0.3,
                    "h_min": 2,
                    "h_max": 4
                },
                "run": True
            },
            "single_shot_question_generation": {
                "source_dataset_name": "test_chunked_documents",
                "output_dataset_name": "test_single_shot_questions",
                "local_dataset_path": "/results/test_single_shot_questions",
                "diversification_seed": "24 year old adult",
                "concat_existing_dataset": False,
                "run": True
            },
            "multi_hop_question_generation": {
                "source_dataset_name": "test_chunked_documents",
                "output_dataset_name": "test_multi_hop_questions",
                "local_dataset_path": "/results/test_multi_hop_questions",
                "concat_existing_dataset": False,
                "run": True
            },
            "answer_generation": {
                "run": True,
                "question_dataset_name": "test_single_shot_questions",
                "output_dataset_name": "test_answered_questions",
                "local_dataset_path": "/results/test_answered_questions",
                "concat_existing_dataset": False,
                "strategies": [{
                    "name": "zeroshot",
                    "prompt": "ZEROSHOT_QA_USER_PROMPT",
                    "model_name": model_name
                }, {
                    "name": "gold",
                    "prompt": "GOLD_QA_USER_PROMPT",
                    "model_name": model_name
                }]
            },
            "judge_answers": {
                "run": True,
                "source_judge_dataset_name": "test_answered_questions",
                "output_judged_dataset_name": "test_judged_comparisons",
                "local_dataset_path": "/results/test_judged_comparisons",
                "concat_existing_dataset": False,
                "comparing_strategies": [["zeroshot", "gold"]],
                "chunk_column_index": 0,
                "random_seed": 42
            }
        }
    }
    return yaml.dump(config, default_flow_style=False)

def save_config(yaml_text):
    with open(CONFIG_PATH, "w") as file:
        file.write(yaml_text)
    return "✅ Config saved!"

def save_files(files: list[str]):
    saved_paths = [shutil.move(str(pathlib.Path(file)), str(UPLOAD_DIRECTORY / pathlib.Path(file).name)) for file in files]
    return f"Files saved to: {', '.join(saved_paths)}"

app = gr.Blocks()

with app:
    gr.Markdown("## YourBench Configuration")
    
    with gr.Tab("Configuration"):
        hf_token = gr.Textbox(label="HF Token")
        hf_org = gr.Textbox(label="HF Organization")
        model_name = gr.Textbox(label="Model Name")
        provider = gr.Dropdown(["openrouter", "openai", "huggingface"], value="huggingface", label="Provider")
        base_url = gr.Textbox(label="Base URL")
        api_key = gr.Textbox(label="API Key")
        max_concurrent_requests = gr.Dropdown([8, 16, 32], value=16, label="Max Concurrent Requests")
        config_output = gr.Code(label="Generated Config", language="yaml")
        preview_button = gr.Button("Generate Config")
        save_button = gr.Button("Save Config")
        
        preview_button.click(generate_config, inputs=[hf_token, hf_org, model_name, provider, base_url, api_key, max_concurrent_requests], outputs=config_output)
        save_button.click(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)
        start_button = gr.Button("Start Task")
        start_button.click(manager.start_process)
        timer = gr.Timer(0.1, active=True)
        timer.tick(manager.read_and_get_output, outputs=log_output)

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

app.launch()