File size: 9,561 Bytes
e4ba316
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4e5edbb
e4ba316
 
 
4e5edbb
e4ba316
 
 
 
4e5edbb
e4ba316
 
 
 
 
4e5edbb
 
e4ba316
 
4e5edbb
e4ba316
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4e5edbb
e4ba316
 
4e5edbb
 
 
e4ba316
 
 
 
 
 
 
 
 
 
 
 
4e5edbb
e4ba316
 
 
 
 
 
 
4e5edbb
e4ba316
 
 
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import json
import shutil
import tempfile
import traceback
import uuid
import zipfile
from pathlib import Path


APP_ROOT = Path(__file__).resolve().parent
RUNTIME_ARCHIVE = APP_ROOT / "lite_runtime.zip"


def _extract_runtime():
    with zipfile.ZipFile(RUNTIME_ARCHIVE) as archive:
        for member in archive.infolist():
            target = (APP_ROOT / member.filename).resolve()
            if APP_ROOT not in target.parents and target != APP_ROOT:
                raise RuntimeError("The evaluator runtime archive contains an unsafe path.")
        archive.extractall(APP_ROOT)


_extract_runtime()

import gradio as gr

from chinatravel.ui import content
from chinatravel.ui.archive import SubmissionArchiveError, extract_submission_archive
from chinatravel.ui.leaderboard import build_leaderboards
from eval_exp import evaluate


EVALUATION_SPLITS = ("easy", "human")
LEADERBOARD_SPLITS = ("easy", "human", "human1000")
LANGUAGE_SPLITS = {"zh": EVALUATION_SPLITS, "en": EVALUATION_SPLITS}
DEFAULT_SPLIT = "easy"
OVERVIEW_URL = (
    "https://huggingface.co/spaces/LAMDA-NeSy/ChinaTravel/resolve/"
    "main/images/overview.png"
)


class Arguments:
    def __init__(self, splits, result_dir, lang):
        self.splits = splits
        self.result_dir = result_dir
        self.lang = lang
        self.oracle_translation = True


def _upload_path(zip_file):
    if isinstance(zip_file, dict):
        return zip_file.get("path") or zip_file.get("name")
    return getattr(zip_file, "name", zip_file)


def splits_for_language(lang):
    return LANGUAGE_SPLITS.get(lang, ())


def update_evaluation_splits(language, current_split):
    choices = list(splits_for_language(language))
    value = current_split if current_split in choices else choices[0]
    return gr.update(choices=choices, value=value)


def handle_submission(zip_file, dataset_choice, language_choice):
    empty_progress = content.render_progress()
    empty_metrics = content.render_metrics()

    if zip_file is None:
        yield (
            content.render_status(
                "error",
                "Archive required",
                "Upload a ZIP archive containing prediction JSON files.",
            ),
            empty_progress,
            empty_metrics,
            None,
        )
        return
    if dataset_choice not in EVALUATION_SPLITS:
        yield (
            content.render_status(
                "error",
                "Unsupported split",
                "Browser evaluation is available for easy and human.",
            ),
            empty_progress,
            empty_metrics,
            None,
        )
        return
    if language_choice not in LANGUAGE_SPLITS:
        yield (
            content.render_status(
                "error",
                "Invalid language",
                "Select either Chinese or English evaluation.",
            ),
            empty_progress,
            empty_metrics,
            None,
        )
        return

    job_dir = Path(tempfile.mkdtemp(prefix="chinatravel-lite-"))
    submission_dir = job_dir / "submission"

    try:
        summary = extract_submission_archive(_upload_path(zip_file), submission_dir)
        yield (
            content.render_status(
                "running",
                "Evaluation started",
                f"Validated {summary.json_files} prediction files.",
            ),
            empty_progress,
            empty_metrics,
            None,
        )

        args = Arguments(dataset_choice, str(submission_dir), language_choice)
        stage_values = {"schema": 0, "commonsense": 0, "logic": 0}
        result = {}

        for update in evaluate(args, result):
            stage = update.get("stage", "")
            if stage in stage_values:
                stage_values[stage] = max(
                    0,
                    min(100, round(update.get("progress", 0))),
                )
                labels = {
                    "schema": "Checking output schema",
                    "commonsense": "Checking environment grounding",
                    "logic": "Checking hard constraints",
                }
                yield (
                    content.render_status(
                        "running",
                        labels[stage],
                        f"Split: {dataset_choice} · Language: {language_choice}",
                    ),
                    content.render_progress(
                        stage_values["schema"],
                        stage_values["commonsense"],
                        stage_values["logic"],
                    ),
                    empty_metrics,
                    None,
                )
            elif stage == "final":
                result.update(update.get("result", {}))

        output_path = job_dir / (
            f"evaluation-{dataset_choice}-{language_choice}-"
            f"{uuid.uuid4().hex[:8]}.json"
        )
        output_path.write_text(
            json.dumps(result, ensure_ascii=False, indent=2),
            encoding="utf-8",
        )

        matched = len(result.get("matched_uid", []))
        unmatched = len(result.get("unmatched_uid", []))
        yield (
            content.render_status(
                "complete",
                "Evaluation complete",
                f"Matched {matched} predictions; {unmatched} expected UIDs were missing.",
            ),
            content.render_progress(100, 100, 100),
            content.render_metrics(result),
            str(output_path),
        )
    except SubmissionArchiveError as exc:
        yield (
            content.render_status("error", "Archive rejected", str(exc)),
            empty_progress,
            empty_metrics,
            None,
        )
    except Exception as exc:
        traceback.print_exc()
        yield (
            content.render_status("error", "Evaluation failed", str(exc)),
            empty_progress,
            empty_metrics,
            None,
        )
    finally:
        shutil.rmtree(submission_dir, ignore_errors=True)


leaderboards = build_leaderboards(LEADERBOARD_SPLITS)

with gr.Blocks(
    title="ChinaTravel Benchmark",
    theme=gr.themes.Base(
        primary_hue=gr.themes.colors.red,
        secondary_hue=gr.themes.colors.emerald,
        neutral_hue=gr.themes.colors.gray,
    ),
    css=content.CSS,
    fill_width=True,
) as demo:
    gr.HTML(content.HEADER_HTML)

    with gr.Tabs(elem_id="primary-tabs"):
        with gr.Tab("Leaderboard"):
            gr.HTML(content.LEADERBOARD_HEADER_HTML)
            with gr.Tabs(elem_id="track-tabs"):
                with gr.Tab("Standard"), gr.Tabs(elem_classes=["ct-split-tabs"]):
                    for split in LEADERBOARD_SPLITS:
                        with gr.Tab(split):
                            gr.HTML(leaderboards[split]["standard"])
                with gr.Tab("Oracle-assisted"), gr.Tabs(
                    elem_classes=["ct-split-tabs"]
                ):
                    for split in LEADERBOARD_SPLITS:
                        with gr.Tab(split):
                            gr.HTML(leaderboards[split]["oracle"])
            gr.HTML(content.LEADERBOARD_NOTE_HTML)

        with gr.Tab("Evaluate"):
            gr.HTML(content.EVALUATOR_HEADER_HTML)
            with gr.Row(elem_classes=["ct-workbench"]):
                with gr.Column(scale=4, min_width=280):
                    dataset_choice = gr.Dropdown(
                        list(EVALUATION_SPLITS),
                        label="Evaluation split",
                        value=DEFAULT_SPLIT,
                        interactive=True,
                    )
                    language_choice = gr.Radio(
                        choices=[("Chinese", "zh"), ("English", "en")],
                        value="zh",
                        label="Evaluation language",
                        interactive=True,
                    )
                    language_choice.change(
                        update_evaluation_splits,
                        inputs=[language_choice, dataset_choice],
                        outputs=dataset_choice,
                    )
                    zip_input = gr.File(
                        label="Prediction archive",
                        file_types=[".zip"],
                        type="filepath",
                    )
                    submit_btn = gr.Button(
                        "Run evaluation",
                        variant="primary",
                        elem_classes=["ct-submit-button"],
                    )
                    gr.HTML(content.EVALUATOR_RESOURCES_HTML)

                with gr.Column(scale=7, min_width=340):
                    status = gr.HTML(content.render_status())
                    progress = gr.HTML(content.render_progress())
                    metrics = gr.HTML(content.render_metrics())
                    result_file = gr.File(
                        label="Evaluation report",
                        interactive=False,
                    )

            submit_btn.click(
                handle_submission,
                inputs=[zip_input, dataset_choice, language_choice],
                outputs=[status, progress, metrics, result_file],
            )

        with gr.Tab("Benchmark"):
            gr.HTML(content.ABOUT_HTML)
            gr.Image(
                value=OVERVIEW_URL,
                show_label=False,
                interactive=False,
                elem_classes=["overview-image"],
            )

    gr.HTML(content.FOOTER_HTML)


demo.launch(show_error=False)