File size: 11,216 Bytes
a52a4e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Usage:
python3 qa_browser.py --share
"""

# import argparse
from collections import defaultdict
import re

import gradio as gr
import json

MAX_py_file_paths_depth = 3




py_file_paths = {}

comment = {"title2comment": {}, "path2comment": {}}
model_judgments_normal = {}
model_judgments_math = {}

def load_py_file_paths(data_path):
    global py_file_paths
    py_file_paths = []
    with open(data_path, "r") as f:
        py_file_paths = f.readlines()
        py_file_paths = [json.loads(line) for line in py_file_paths]
        py_file_paths = {line["path"]: line['content'] for line in py_file_paths}
    return py_file_paths

def load_comment(data_path):
    global comment
    comment = {"title2comment": {}, "path2comment": {}}
    title2comment = {}
    path2comment = {}
    with open(data_path, "r") as f:
        data_f = f.readlines()
        data_f = [json.loads(line) for line in data_f]
        for line in data_f:
            title2comment[line["title"]] = line
            path2comment[line["class_link"]] = line
    comment["title2comment"] = title2comment
    comment["path2comment"] = path2comment
    return comment

def display_question(question_selector,file_path_selector, request: gr.Request):
    if question_selector == None:
        return [gr.Dropdown.update(
        value=file_path_selector
    )] +  [""] * 4

    choice = comment['title2comment'][question_selector]['class_link'].split("#")[0]
    lines = comment['title2comment'][question_selector]['class_link'].split("#")[1].replace("L","")

    question = comment['title2comment'][question_selector]['comment']
    code_path = choice
    test_path = comment['title2comment'][question_selector]['test_file_path']

    highlight_lines = [int(line) for line in lines.split("-")]
    class_link = comment['title2comment'][question_selector]['class_link']
    question_mds,code_mds = to_gradio_chat_mds(question, code_path, highlight_lines, class_link, test_path)
    return [gr.Dropdown.update(
        value=choice
    )] +  question_mds + code_mds

def display_answer(question_selector,file_path_selector, request: gr.Request):
    choice = comment['title2comment'][question_selector]['class_link'].split("#")[0]
    lines = comment['title2comment'][question_selector]['class_link'].split("#")[1].replace("L","")

    question = comment['title2comment'][question_selector]['comment']
    code_path = choice
    test_path = comment['title2comment'][question_selector]['test_file_path']

    highlight_lines = [int(line) for line in lines.split("-")]
    class_link = comment['title2comment'][question_selector]['class_link']
    if comment['title2comment'][question_selector]['class_link'].split("#")[0] != file_path_selector:
        highlight_lines = None
        class_link = None
        test_path = None
        code_path = file_path_selector
    question_mds,code_mds = to_gradio_chat_mds(question, code_path, highlight_lines, class_link, test_path)
    return code_mds

# def display_answer(
#     file_path_selector, request: gr.Request
# ):
#     # if comment['title2comment'][question_selector]['class_link'].split("#")[0] == file_path_selector:
#     #     choice = question_selector
#     # else:
#     #     choice = None

#     code_path = file_path_selector
#     question = ""
#     highlight_lines = None
    
#     question_mds,code_mds = to_gradio_chat_mds(question, code_path, highlight_lines)

#     return code_mds


newline_pattern1 = re.compile("\n\n(\d+\. )")
newline_pattern2 = re.compile("\n\n(- )")


def to_gradio_chat_mds(question, code_path, highlight_lines = None, class_link = None, test_path = None):
    mds = [""] * 1 + [""] * 3
    mds[0] = question
    source_code = py_file_paths[code_path]
    if test_path:
        test_code = py_file_paths[test_path]
    else:
        test_code = [""]
    # no highlight, highlight, no highlight
    if not highlight_lines:
        # mds[1] = "".join(source_code)
        mds[1] = "#################################################################\n" + "# Please Check the `Full Code in the File` column\n" + "#################################################################\n"
        # mds[3] = ""
    else:
        highlight_lines[0] -= 1
        mds[1] = "#################################################################\n" + "# From " + class_link + "\n" + f"# From Line {highlight_lines[0]+1} to Line {highlight_lines[1]}\n" + "#################################################################\n"
        mds[1] += "".join(source_code[highlight_lines[0]:highlight_lines[1]])
    mds[2] = "".join(source_code)
    mds[3] = "".join(test_code)

    return mds[:1], mds[1:]


def build_pairwise_browser_tab():
    file_path_list = list(py_file_paths.keys())
    question_list = list(comment['title2comment'].keys())
    num_py_file_paths = MAX_py_file_paths_depth

    # Build question selector map
    with gr.Row():
        with gr.Column():
            question_selector = gr.Dropdown(
                choices=question_list,
                label="Question",
                container=False
            )
            # locate_button = gr.Button(
            #     text="Locate",
            #     label="Locate",
            #     type="default"
            #     )
        with gr.Column():
            file_path_selector = gr.Dropdown(
                choices=file_path_list,
                label="File Path",
                container=False
            )

    question_mds = []
    code_mds = []
    with gr.Row():
        with gr.Column():
            question_mds.append(gr.Code(language="markdown"))
        with gr.Column():
            with gr.Tab("Infilling Code"):
            # add code highlight
                code_mds.append(gr.Code(language="python",elem_id="locate_code"))
            with gr.Tab("Full Code in the File"):
                code_mds.append(gr.Code(language="python"))
            with gr.Tab("Test Code"):
                code_mds.append(gr.Code(language="python"))

    # Callbacks
    question_selector.change(display_question, [question_selector, file_path_selector], [file_path_selector] + question_mds + code_mds)
    file_path_selector.change(display_answer, [question_selector,file_path_selector], code_mds)

    return (question_selector,)




code_highlight_css = """
#chatbot .hll { background-color: #ffffcc }
#chatbot .c { color: #408080; font-style: italic }
#chatbot .err { border: 1px solid #FF0000 }
#chatbot .k { color: #008000; font-weight: bold }
#chatbot .o { color: #666666 }
#chatbot .ch { color: #408080; font-style: italic }
#chatbot .cm { color: #408080; font-style: italic }
#chatbot .cp { color: #BC7A00 }
#chatbot .cpf { color: #408080; font-style: italic }
#chatbot .c1 { color: #408080; font-style: italic }
#chatbot .cs { color: #408080; font-style: italic }
#chatbot .gd { color: #A00000 }
#chatbot .ge { font-style: italic }
#chatbot .gr { color: #FF0000 }
#chatbot .gh { color: #000080; font-weight: bold }
#chatbot .gi { color: #00A000 }
#chatbot .go { color: #888888 }
#chatbot .gp { color: #000080; font-weight: bold }
#chatbot .gs { font-weight: bold }
#chatbot .gu { color: #800080; font-weight: bold }
#chatbot .gt { color: #0044DD }
#chatbot .kc { color: #008000; font-weight: bold }
#chatbot .kd { color: #008000; font-weight: bold }
#chatbot .kn { color: #008000; font-weight: bold }
#chatbot .kp { color: #008000 }
#chatbot .kr { color: #008000; font-weight: bold }
#chatbot .kt { color: #B00040 }
#chatbot .m { color: #666666 }
#chatbot .s { color: #BA2121 }
#chatbot .na { color: #7D9029 }
#chatbot .nb { color: #008000 }
#chatbot .nc { color: #0000FF; font-weight: bold }
#chatbot .no { color: #880000 }
#chatbot .nd { color: #AA22FF }
#chatbot .ni { color: #999999; font-weight: bold }
#chatbot .ne { color: #D2413A; font-weight: bold }
#chatbot .nf { color: #0000FF }
#chatbot .nl { color: #A0A000 }
#chatbot .nn { color: #0000FF; font-weight: bold }
#chatbot .nt { color: #008000; font-weight: bold }
#chatbot .nv { color: #19177C }
#chatbot .ow { color: #AA22FF; font-weight: bold }
#chatbot .w { color: #bbbbbb }
#chatbot .mb { color: #666666 }
#chatbot .mf { color: #666666 }
#chatbot .mh { color: #666666 }
#chatbot .mi { color: #666666 }
#chatbot .mo { color: #666666 }
#chatbot .sa { color: #BA2121 }
#chatbot .sb { color: #BA2121 }
#chatbot .sc { color: #BA2121 }
#chatbot .dl { color: #BA2121 }
#chatbot .sd { color: #BA2121; font-style: italic }
#chatbot .s2 { color: #BA2121 }
#chatbot .se { color: #BB6622; font-weight: bold }
#chatbot .sh { color: #BA2121 }
#chatbot .si { color: #BB6688; font-weight: bold }
#chatbot .sx { color: #008000 }
#chatbot .sr { color: #BB6688 }
#chatbot .s1 { color: #BA2121 }
#chatbot .ss { color: #19177C }
#chatbot .bp { color: #008000 }
#chatbot .fm { color: #0000FF }
#chatbot .vc { color: #19177C }
#chatbot .vg { color: #19177C }
#chatbot .vi { color: #19177C }
#chatbot .vm { color: #19177C }
#chatbot .il { color: #666666 }
"""
# .highlight  { background: #f8f8f8; }

table_css = """
table {
    line-height: 0em
}
"""
old_block_css = (
    code_highlight_css
    + """
pre {
    white-space: pre-wrap;       /* Since CSS 2.1 */
    white-space: -moz-pre-wrap;  /* Mozilla, since 1999 */
    white-space: -pre-wrap;      /* Opera 4-6 */
    white-space: -o-pre-wrap;    /* Opera 7 */
    word-wrap: break-word;       /* Internet Explorer 5.5+ */
}
#notice_markdown th {
    display: none;
}
#notice_markdown td {
    padding-top: 8px;
    padding-bottom: 8px;
}
#leaderboard_markdown td {
    padding-top: 8px;
    padding-bottom: 8px;
}
"""
)
block_css = old_block_css + (
    """
#locate_code {
    background-color: #DEEBF7;
}
#user_question {
    background-color: #E2F0D9;
}
#reference {
    background-color: #FFF2CC;
}
#model_explanation {
    background-color: #FBE5D6;
}
"""
)


def load_demo():
    dropdown_update = gr.Dropdown.update(value=list(comment['title2comment'].keys())[0])
    return dropdown_update


def build_demo():
    with gr.Blocks(
        title="Class-level Repo Code Generation Benchmark (numpy-ml)",
        theme=gr.themes.Base(text_size=gr.themes.sizes.text_lg),
        css=block_css,
    ) as demo:
        
        (question_selector,) = build_pairwise_browser_tab()

        demo.load(load_demo, [], [question_selector])

        thanks_txt = gr.Markdown(
            """
            # Acknowledgement
            This benchmark is based on the [numpy-ml](https://github.com/ddbourgin/numpy-ml) project.
            """
        )

    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)

py_file_paths_path = "numpyml-display/all_py_content.jsonl"
comment_path = "numpyml-display/final_dataset.jsonl"

# Load py_file_paths
py_file_paths = load_py_file_paths(py_file_paths_path)

# Load answers
comment = load_comment(comment_path)

demo = build_demo()
# demo.queue(concurrency_count=10, status_update_rate=10, api_open=False).launch()
demo.launch()