File size: 7,322 Bytes
4e93785
55f4d70
 
 
 
 
828190b
36fb388
f2539bf
 
 
1a1b9bb
4e93785
 
 
55f4d70
828190b
 
23ab980
4e93785
 
 
828190b
55f4d70
a2d883f
55f4d70
 
 
828190b
55f4d70
7f77e31
f2539bf
 
 
 
828190b
 
55f4d70
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36fb388
af3bf93
7f77e31
 
 
 
af3bf93
f2539bf
af3bf93
 
f2539bf
 
 
 
 
 
 
 
 
55f4d70
4e93785
a2d883f
 
 
 
 
36fb388
a2d883f
 
 
4e93785
a2d883f
 
 
 
4e93785
a2d883f
 
 
 
 
 
 
 
 
 
 
 
 
 
4e93785
 
 
 
 
 
 
f2539bf
 
36fb388
1a1b9bb
36fb388
 
 
1a1b9bb
af3bf93
f2539bf
 
36fb388
 
 
 
a2d883f
828190b
02d3620
828190b
 
 
 
a2d883f
828190b
 
 
 
982569e
8624024
828190b
982569e
 
828190b
 
 
4e93785
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36fb388
f2539bf
 
af3bf93
f2539bf
828190b
 
a2d883f
828190b
 
 
 
 
af3bf93
 
828190b
af3bf93
 
7f77e31
9f3c532
c9c684c
 
af3bf93
f2539bf
c9c684c
 
 
 
a2d883f
f2539bf
 
af3bf93
 
 
f2539bf
af3bf93
 
 
f2539bf
af3bf93
 
828190b
4e93785
 
 
 
 
 
 
 
 
 
 
 
 
80443f8
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
from fastapi import FastAPI
import gradio as gr
import subprocess
import sys
import os
import threading
import time
import uuid
import glob
import shutil
from pathlib import Path
from apscheduler.schedulers.background import BackgroundScheduler
import signal
import uvicorn


default_command = "bigcodebench.evaluate"
is_running = False
lock = threading.Lock()
process = None

app = FastAPI()

def generate_command(
    jsonl_file, split, subset, parallel,
    min_time_limit, max_as_limit, max_data_limit, max_stack_limit,
    check_gt_only, no_gt
):
    command = [default_command]
    
    if jsonl_file is not None:
        # Copy the uploaded file to the current directory
        local_filename = os.path.basename(jsonl_file.name)
        shutil.copy(jsonl_file.name, local_filename)
        command.extend(["--samples", local_filename])
    
    command.extend(["--split", split, "--subset", subset])
    
    if parallel is not None and parallel != 0:
        command.extend(["--parallel", str(int(parallel))])
    
    command.extend([
        "--min-time-limit", str(min_time_limit),
        "--max-as-limit", str(int(max_as_limit)),
        "--max-data-limit", str(int(max_data_limit)),
        "--max-stack-limit", str(int(max_stack_limit))
    ])
    
    if check_gt_only:
        command.append("--check-gt-only")
    
    if no_gt:
        command.append("--no-gt")
    
    return " ".join(command)


def cleanup_previous_files(jsonl_file):
    if jsonl_file is not None:
        file_list = ['Dockerfile', 'app.py', 'README.md', os.path.basename(jsonl_file.name), "__pycache__"]
    else:
        file_list = ['Dockerfile', 'app.py', 'README.md', "__pycache__"]
    for file in glob.glob("*"):
        try:
            if file not in file_list:
                os.remove(file)
        except Exception as e:
            print(f"Error during cleanup of {file}: {e}")

def find_result_file():
    json_files = glob.glob("*.json")
    if json_files:
        return max(json_files, key=os.path.getmtime)
    return None

def run_bigcodebench(command):
    global is_running, process
    with lock:
        if is_running:
            yield "A command is already running. Please wait for it to finish.\n"
            return
        is_running = True

    try:
        yield f"Executing command: {command}\n"
        
        process = subprocess.Popen(command.split(), stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True, preexec_fn=os.setsid)
        
        for line in process.stdout:
            yield line
        
        process.wait()
        
        if process.returncode != 0:
            yield f"Error: Command exited with status {process.returncode}\n"
        
        yield "Evaluation completed.\n"
        
        result_file = find_result_file()
        if result_file:
            yield f"Result file found: {result_file}\n"
        else:
            yield "No result file found.\n"
    finally:
        with lock:
            is_running = False
            process = None

def kill_process():
    global process
    if process:
        os.killpg(os.getpgid(process.pid), signal.SIGTERM)
        process = None

def stream_logs(command, jsonl_file=None):
    global is_running
        
    if is_running:
        yield "A command is already running. Please wait for it to finish.\n"
        return
    
    cleanup_previous_files(jsonl_file)
    yield "Cleaned up previous files.\n"

    log_content = []
    for log_line in run_bigcodebench(command):
        log_content.append(log_line)
        yield "".join(log_content)
        
with gr.Blocks() as demo:
    gr.Markdown("# BigCodeBench Evaluator")
    
    with gr.Row():
        jsonl_file = gr.File(label="Upload JSONL file", file_types=[".jsonl"])
        split = gr.Dropdown(choices=["complete", "instruct"], label="Split", value="complete")
        subset = gr.Dropdown(choices=["hard"], label="Subset", value="hard")
    
    with gr.Row():
        parallel = gr.Number(label="Parallel (optional)", precision=0)
        min_time_limit = gr.Number(label="Min Time Limit", value=1, precision=1)
        max_as_limit = gr.Number(label="Max AS Limit", value=25*1024, precision=0)
    
    with gr.Row():
        max_data_limit = gr.Number(label="Max Data Limit", value=25*1024, precision=0)
        max_stack_limit = gr.Number(label="Max Stack Limit", value=10, precision=0)
        check_gt_only = gr.Checkbox(label="Check GT Only")
        no_gt = gr.Checkbox(label="No GT")
    
    kill_process_btn = gr.Button("Kill Process", visible=False)
    kill_process_btn.click(kill_process)

    # Add this JavaScript to handle window closing
    gr.HTML("""
    <script>
    window.addEventListener('beforeunload', function (e) {
        fetch('/kill_process', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            body: JSON.stringify({}),
        });
    });
    </script>
    """)
    
    command_output = gr.Textbox(label="Command", value=default_command, interactive=False)
    with gr.Row():
        submit_btn = gr.Button("Run Evaluation")
        download_btn = gr.DownloadButton(label="Download Result")
    log_output = gr.Textbox(label="Execution Logs", lines=20)
    
    input_components = [
        jsonl_file, split, subset, parallel,
        min_time_limit, max_as_limit, max_data_limit, max_stack_limit,
        check_gt_only, no_gt
    ]
    
    for component in input_components:
        component.change(generate_command, inputs=input_components, outputs=command_output)
        
    
    def start_evaluation(command, jsonl_file, subset, split):
        extra = subset + "_" if subset != "full" else ""
        if jsonl_file is not None:
            result_path = os.path.basename(jsonl_file.name).replace(".jsonl", f"_{extra}eval_results.json")
        else:
            result_path = None

        for log in stream_logs(command, jsonl_file):
            if jsonl_file is not None:
                yield log, gr.update(value=result_path, label=result_path), gr.update()
            else:
                yield log, gr.update(), gr.update()
        is_running = False
        result_file = find_result_file()
        if result_file:
            return gr.update(label="Evaluation completed. Result file found."), gr.update(value=result_file)
                    # gr.Button(visible=False)#,
                    # gr.DownloadButton(label="Download Result", value=result_file, visible=True))
        else:
            return gr.update(label="Evaluation completed. No result file found."), gr.update(value=result_path)
                    # gr.Button("Run Evaluation", visible=True),
                    # gr.DownloadButton(visible=False))
    submit_btn.click(start_evaluation,
                 inputs=[command_output, jsonl_file, subset, split],
                 outputs=[log_output, download_btn])

@app.post("/kill_process")
async def api_kill_process():
    kill_process()
    return {"status": "success"}

# demo.queue(max_size=300).launch(
#     share=True, 
#     server_name="0.0.0.0", 
#     server_port=7860,
#     additional_routes={"/kill_process": kill_process_api}
# )
app = gr.mount_gradio_app(app, demo, path="/gradio")
uvicorn.run(app, host="0.0.0.0", port=7860)
scheduler = BackgroundScheduler()