forked from lm-sys/FastChat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qa_browser.py
420 lines (335 loc) · 12.4 KB
/
qa_browser.py
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
"""
Usage:
python3 qa_browser.py --share
"""
import argparse
from collections import defaultdict
import re
import gradio as gr
from fastchat.llm_judge.common import (
load_questions,
load_model_answers,
load_single_model_judgments,
load_pairwise_model_judgments,
resolve_single_judgment_dict,
resolve_pairwise_judgment_dict,
get_single_judge_explanation,
get_pairwise_judge_explanation,
)
questions = []
model_answers = {}
model_judgments_normal_single = {}
model_judgments_math_single = {}
model_judgments_normal_pairwise = {}
model_judgments_math_pairwise = {}
question_selector_map = {}
category_selector_map = defaultdict(list)
def display_question(category_selector, request: gr.Request):
choices = category_selector_map[category_selector]
return gr.Dropdown(
value=choices[0],
choices=choices,
)
def display_pairwise_answer(
question_selector, model_selector1, model_selector2, request: gr.Request
):
q = question_selector_map[question_selector]
qid = q["question_id"]
ans1 = model_answers[model_selector1][qid]
ans2 = model_answers[model_selector2][qid]
chat_mds = pairwise_to_gradio_chat_mds(q, ans1, ans2)
gamekey = (qid, model_selector1, model_selector2)
judgment_dict = resolve_pairwise_judgment_dict(
q,
model_judgments_normal_pairwise,
model_judgments_math_pairwise,
multi_turn=False,
)
explanation = (
"##### Model Judgment (first turn)\n"
+ get_pairwise_judge_explanation(gamekey, judgment_dict)
)
judgment_dict_turn2 = resolve_pairwise_judgment_dict(
q,
model_judgments_normal_pairwise,
model_judgments_math_pairwise,
multi_turn=True,
)
explanation_turn2 = (
"##### Model Judgment (second turn)\n"
+ get_pairwise_judge_explanation(gamekey, judgment_dict_turn2)
)
return chat_mds + [explanation] + [explanation_turn2]
def display_single_answer(question_selector, model_selector1, request: gr.Request):
q = question_selector_map[question_selector]
qid = q["question_id"]
ans1 = model_answers[model_selector1][qid]
chat_mds = single_to_gradio_chat_mds(q, ans1)
gamekey = (qid, model_selector1)
judgment_dict = resolve_single_judgment_dict(
q, model_judgments_normal_single, model_judgments_math_single, multi_turn=False
)
explanation = "##### Model Judgment (first turn)\n" + get_single_judge_explanation(
gamekey, judgment_dict
)
judgment_dict_turn2 = resolve_single_judgment_dict(
q, model_judgments_normal_single, model_judgments_math_single, multi_turn=True
)
explanation_turn2 = (
"##### Model Judgment (second turn)\n"
+ get_single_judge_explanation(gamekey, judgment_dict_turn2)
)
return chat_mds + [explanation] + [explanation_turn2]
newline_pattern1 = re.compile("\n\n(\d+\. )")
newline_pattern2 = re.compile("\n\n(- )")
def post_process_answer(x):
"""Fix Markdown rendering problems."""
x = x.replace("\u2022", "- ")
x = re.sub(newline_pattern1, "\n\g<1>", x)
x = re.sub(newline_pattern2, "\n\g<1>", x)
return x
def pairwise_to_gradio_chat_mds(question, ans_a, ans_b, turn=None):
end = len(question["turns"]) if turn is None else turn + 1
mds = ["", "", "", "", "", "", ""]
for i in range(end):
base = i * 3
if i == 0:
mds[base + 0] = "##### User\n" + question["turns"][i]
else:
mds[base + 0] = "##### User's follow-up question \n" + question["turns"][i]
mds[base + 1] = "##### Assistant A\n" + post_process_answer(
ans_a["choices"][0]["turns"][i].strip()
)
mds[base + 2] = "##### Assistant B\n" + post_process_answer(
ans_b["choices"][0]["turns"][i].strip()
)
ref = question.get("reference", ["", ""])
ref_md = ""
if turn is None:
if ref[0] != "" or ref[1] != "":
mds[6] = f"##### Reference Solution\nQ1. {ref[0]}\nQ2. {ref[1]}"
else:
x = ref[turn] if turn < len(ref) else ""
if x:
mds[6] = f"##### Reference Solution\n{ref[turn]}"
else:
mds[6] = ""
return mds
def single_to_gradio_chat_mds(question, ans, turn=None):
end = len(question["turns"]) if turn is None else turn + 1
mds = ["", "", "", "", ""]
for i in range(end):
base = i * 2
if i == 0:
mds[base + 0] = "##### User\n" + question["turns"][i]
else:
mds[base + 0] = "##### User's follow-up question \n" + question["turns"][i]
mds[base + 1] = "##### Assistant A\n" + post_process_answer(
ans["choices"][0]["turns"][i].strip()
)
ref = question.get("reference", ["", ""])
ref_md = ""
if turn is None:
if ref[0] != "" or ref[1] != "":
mds[4] = f"##### Reference Solution\nQ1. {ref[0]}\nQ2. {ref[1]}"
else:
x = ref[turn] if turn < len(ref) else ""
if x:
mds[4] = f"##### Reference Solution\n{ref[turn]}"
else:
mds[4] = ""
return mds
def build_question_selector_map():
global question_selector_map, category_selector_map
# Build question selector map
for q in questions:
preview = f"{q['question_id']}: " + q["turns"][0][:128] + "..."
question_selector_map[preview] = q
category_selector_map[q["category"]].append(preview)
def build_pairwise_browser_tab():
global question_selector_map, category_selector_map
models = list(model_answers.keys())
num_sides = 2
num_turns = 2
side_names = ["A", "B"]
question_selector_choices = list(question_selector_map.keys())
category_selector_choices = list(category_selector_map.keys())
# Selectors
with gr.Row():
with gr.Column(scale=1, min_width=200):
category_selector = gr.Dropdown(
choices=category_selector_choices, label="Category", container=False
)
with gr.Column(scale=100):
question_selector = gr.Dropdown(
choices=question_selector_choices, label="Question", container=False
)
model_selectors = [None] * num_sides
with gr.Row():
for i in range(num_sides):
with gr.Column():
if i == 0:
value = models[0]
else:
value = "gpt-3.5-turbo"
model_selectors[i] = gr.Dropdown(
choices=models,
value=value,
label=f"Model {side_names[i]}",
container=False,
)
# Conversation
chat_mds = []
for i in range(num_turns):
chat_mds.append(gr.Markdown(elem_id=f"user_question_{i+1}"))
with gr.Row():
for j in range(num_sides):
with gr.Column(scale=100):
chat_mds.append(gr.Markdown())
if j == 0:
with gr.Column(scale=1, min_width=8):
gr.Markdown()
reference = gr.Markdown(elem_id=f"reference")
chat_mds.append(reference)
model_explanation = gr.Markdown(elem_id="model_explanation")
model_explanation2 = gr.Markdown(elem_id="model_explanation")
# Callbacks
category_selector.change(display_question, [category_selector], [question_selector])
question_selector.change(
display_pairwise_answer,
[question_selector] + model_selectors,
chat_mds + [model_explanation] + [model_explanation2],
)
for i in range(num_sides):
model_selectors[i].change(
display_pairwise_answer,
[question_selector] + model_selectors,
chat_mds + [model_explanation] + [model_explanation2],
)
return (category_selector,)
def build_single_answer_browser_tab():
global question_selector_map, category_selector_map
models = list(model_answers.keys())
num_sides = 1
num_turns = 2
side_names = ["A"]
question_selector_choices = list(question_selector_map.keys())
category_selector_choices = list(category_selector_map.keys())
# Selectors
with gr.Row():
with gr.Column(scale=1, min_width=200):
category_selector = gr.Dropdown(
choices=category_selector_choices, label="Category", container=False
)
with gr.Column(scale=100):
question_selector = gr.Dropdown(
choices=question_selector_choices, label="Question", container=False
)
model_selectors = [None] * num_sides
with gr.Row():
for i in range(num_sides):
with gr.Column():
model_selectors[i] = gr.Dropdown(
choices=models,
value=models[i] if len(models) > i else "",
label=f"Model {side_names[i]}",
container=False,
)
# Conversation
chat_mds = []
for i in range(num_turns):
chat_mds.append(gr.Markdown(elem_id=f"user_question_{i+1}"))
with gr.Row():
for j in range(num_sides):
with gr.Column(scale=100):
chat_mds.append(gr.Markdown())
if j == 0:
with gr.Column(scale=1, min_width=8):
gr.Markdown()
reference = gr.Markdown(elem_id=f"reference")
chat_mds.append(reference)
model_explanation = gr.Markdown(elem_id="model_explanation")
model_explanation2 = gr.Markdown(elem_id="model_explanation")
# Callbacks
category_selector.change(display_question, [category_selector], [question_selector])
question_selector.change(
display_single_answer,
[question_selector] + model_selectors,
chat_mds + [model_explanation] + [model_explanation2],
)
for i in range(num_sides):
model_selectors[i].change(
display_single_answer,
[question_selector] + model_selectors,
chat_mds + [model_explanation] + [model_explanation2],
)
return (category_selector,)
block_css = """
#user_question_1 {
background-color: #DEEBF7;
}
#user_question_2 {
background-color: #E2F0D9;
}
#reference {
background-color: #FFF2CC;
}
#model_explanation {
background-color: #FBE5D6;
}
"""
def load_demo():
dropdown_update = gr.Dropdown.update(value=list(category_selector_map.keys())[0])
return dropdown_update, dropdown_update
def build_demo():
build_question_selector_map()
with gr.Blocks(
title="MT-Bench Browser",
theme=gr.themes.Base(text_size=gr.themes.sizes.text_lg),
css=block_css,
) as demo:
gr.Markdown(
"""
# MT-Bench Browser
The code to generate answers and judgments is at [fastchat.llm_judge](https://github.com/lm-sys/FastChat/tree/main/fastchat/llm_judge).
"""
)
with gr.Tab("Single Answer Grading"):
(category_selector,) = build_single_answer_browser_tab()
with gr.Tab("Pairwise Comparison"):
(category_selector2,) = build_pairwise_browser_tab()
demo.load(load_demo, [], [category_selector, category_selector2])
return demo
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--host", type=str, default="0.0.0.0")
parser.add_argument("--port", type=int)
parser.add_argument("--share", action="store_true")
parser.add_argument("--bench-name", type=str, default="mt_bench")
args = parser.parse_args()
print(args)
question_file = f"data/{args.bench_name}/question.jsonl"
answer_dir = f"data/{args.bench_name}/model_answer"
pairwise_model_judgment_file = (
f"data/{args.bench_name}/model_judgment/gpt-4_pair.jsonl"
)
single_model_judgment_file = (
f"data/{args.bench_name}/model_judgment/gpt-4_single.jsonl"
)
# Load questions
questions = load_questions(question_file, None, None)
# Load answers
model_answers = load_model_answers(answer_dir)
# Load model judgments
model_judgments_normal_single = (
model_judgments_math_single
) = load_single_model_judgments(single_model_judgment_file)
model_judgments_normal_pairwise = (
model_judgments_math_pairwise
) = load_pairwise_model_judgments(pairwise_model_judgment_file)
demo = build_demo()
demo.queue(
default_concurrency_limit=10, status_update_rate=10, api_open=False
).launch(
server_name=args.host, server_port=args.port, share=args.share, max_threads=200
)