File size: 9,572 Bytes
baeb9f1
7af62b1
1b24b13
d154956
1b24b13
 
815e7fc
 
1b24b13
baeb9f1
 
 
815e7fc
 
 
 
1b24b13
f622413
1b24b13
 
85b8fdd
 
 
 
 
 
 
 
 
 
 
 
 
 
fddb192
815e7fc
85b8fdd
f8e5276
 
 
fddb192
 
 
 
0958c91
 
d154956
 
 
 
1b24b13
 
 
 
815e7fc
0958c91
85b8fdd
d154956
1b24b13
 
d154956
 
7fbd3e2
815e7fc
 
 
 
 
 
d154956
 
1b24b13
 
7af62b1
1b24b13
 
 
 
7af62b1
1b24b13
 
 
85b8fdd
 
fddb192
85b8fdd
7af62b1
 
 
 
1b24b13
 
 
 
 
 
 
 
 
 
 
 
 
d154956
0958c91
1b24b13
815e7fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d154956
 
815e7fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d154956
 
815e7fc
 
 
 
 
 
 
 
 
 
 
 
 
 
bc37ba1
5df3f2f
1b24b13
 
 
7fbd3e2
 
 
 
 
 
 
 
1b24b13
d154956
1b24b13
 
7fbd3e2
815e7fc
 
 
1b24b13
 
 
 
 
 
7fbd3e2
 
815e7fc
 
 
 
 
 
 
 
 
1b24b13
 
fddb192
1b24b13
7af62b1
 
1b24b13
 
7af62b1
0958c91
7af62b1
1b24b13
 
7af62b1
 
1b24b13
 
7af62b1
1b24b13
7af62b1
 
 
1b24b13
 
85b8fdd
 
 
1b24b13
fddb192
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
import os
import random
import uuid
from datetime import datetime

import gradio as gr

from data_loader import load_data

HF_TOKEN = os.environ.get('HF_TOKEN')
HF_DATASET = os.environ.get('HF_DATASET')

data, models = load_data()

n_samples = len(data)
n_models = len(models)

saver = gr.HuggingFaceDatasetSaver(HF_TOKEN, HF_DATASET, private=True)


def convert_diff_to_unified(diff):
    result = "\n".join(
        [
            f'--- {modified_file["old_path"]}\n'
            f'+++ {modified_file["new_path"]}\n'
            f'{modified_file["diff"]}'
            for modified_file in diff
        ]
    )

    return result


def get_diff2html_view(raw_diff):
    html = f"""
    <div style='width:100%; height:1400px; overflow:auto; position: relative'>
        <div id='diff-raw' hidden>{raw_diff}</div> 
        <div class="d2h-view-wrapper">
            <div id='diff-view'></div>
        </div>
    </div>
    """

    return html


def get_github_link_md(repo, hash):
    return f'[See the commit on Github](https://github.com/{repo}/commit/{hash})'


def update_commit_view(sample_ind):
    if sample_ind >= n_samples:
        return None

    record = data[sample_ind]

    diff_view = get_diff2html_view(convert_diff_to_unified(record['mods']))

    repo_val = record['repo']
    hash_val = record['hash']
    github_link_md = get_github_link_md(repo_val, hash_val)

    diff_loaded_timestamp = datetime.now().isoformat()

    models_shuffled = models[:]
    random.shuffle(models_shuffled)

    commit_messages = tuple(record[model] for model in models_shuffled)

    return (github_link_md, diff_view, repo_val, hash_val, diff_loaded_timestamp) + commit_messages + tuple(
        models_shuffled)


def next_sample(current_sample_ind, shuffled_idx):
    if current_sample_ind == n_samples:
        return None

    current_sample_ind += 1
    updated_view = update_commit_view(shuffled_idx[current_sample_ind])
    return (current_sample_ind,) + updated_view


with open("head.html") as head_file:
    head_html = head_file.read()

with gr.Blocks(theme=gr.themes.Soft(), head=head_html, css="style_overrides.css") as application:
    repo_val = gr.Textbox(interactive=False, label='repo', visible=False)
    hash_val = gr.Textbox(interactive=False, label='hash', visible=False)
    shuffled_idx_val = gr.JSON(visible=False)

    with gr.Row():
        current_sample_sld = gr.Slider(minimum=0, maximum=n_samples, step=1,
                                       value=0,
                                       interactive=False,
                                       label='sample_ind',
                                       info=f"Samples labeled/skipped (out of {n_samples})",
                                       show_label=False,
                                       container=False,
                                       scale=5)
        with gr.Column(scale=1):
            skip_btn = gr.Button("Skip the current sample")
    with gr.Row():
        with gr.Column(scale=2):
            github_link = gr.Markdown()
            diff_view = gr.HTML()
        with gr.Column(scale=1):
            commit_msg = []
            is_correct = []
            has_what = []
            has_why = []
            is_not_verbose = []
            has_headline = []
            easy_to_read = []
            overall_rating = []
            comments = []
            model_name = []

            for model_ind in range(n_models):
                with gr.Tab(f"Message #{model_ind + 1}"):
                    commit_msg.append(gr.TextArea(label="Commit message (can be scrollable)",
                                                  interactive=False,
                                                  ))
                    gr.Markdown("## Please, rate your level of agreement with each statement\n"
                                "\n"
                                "*1 - strongly disagree, 2 - disagree, 3 - agree, 4 - strongly agree*")

                    is_correct.append(gr.Slider(
                        info='The information provided in the commit message is consistent with the code changes.',
                        label=f'is_correct_{model_ind}',
                        show_label=False,
                        minimum=1,
                        step=1,
                        interactive=True,
                        maximum=4))

                    has_what.append(gr.Slider(
                        info='The commit message answers the question of WHAT changes have been made.',
                        label=f'has_what_{model_ind}',
                        show_label=False,
                        minimum=1,
                        step=1,
                        interactive=True,
                        maximum=4))

                    has_why.append(gr.Slider(
                        info='The commit message answers the question of WHY these changes have been made.',
                        label=f'has_why_{model_ind}',
                        show_label=False,
                        minimum=1,
                        step=1,
                        interactive=True,
                        maximum=4))

                    is_not_verbose.append(gr.Slider(
                        info='The commit message can be substantially shortened without loss of important information.',
                        label=f'is_not_verbose_{model_ind}',
                        show_label=False,
                        minimum=1,
                        step=1,
                        interactive=True,
                        maximum=4))

                    has_headline.append(gr.Slider(
                        info='The commit message includes a short headline that provides a good overview of the '
                             'changes.',
                        label=f'has_headline_{model_ind}',
                        show_label=False,
                        minimum=1,
                        step=1,
                        interactive=True,
                        maximum=4))

                    easy_to_read.append(gr.Slider(
                        info='The commit message is easy to read and to understand.',
                        label=f'easy_to_read_{model_ind}',
                        show_label=False,
                        minimum=1,
                        step=1,
                        interactive=True,
                        maximum=4))

                    overall_rating.append(gr.Slider(
                        info='Please, describe your overall impression of the commit message (1 - very bad, 5 - very '
                             'good)',
                        label=f'overall_rating_{model_ind}',
                        show_label=False,
                        minimum=1,
                        step=1,
                        interactive=True,
                        maximum=5))

                    comments.append(gr.Textbox(
                        info='Additional comments on the commit message',
                        label=f'comments_{model_ind}',
                        show_label=False,
                        interactive=True))

                    model_name.append(gr.Textbox(interactive=False, label=f'model_{model_ind}', visible=False))

            submit_btn = gr.Button("Submit all and continue")
            session_val = gr.Textbox(info='Session', interactive=False, container=True, show_label=False,
                                     label='session')

            with gr.Row(visible=False):
                sample_loaded_timestamp = gr.Textbox(info="Sample loaded", label='loaded_ts', interactive=False,
                                                     container=True, show_label=False)
                sample_submitted_timestamp = gr.Textbox(info="Current time",
                                                        interactive=False, container=True, show_label=False,
                                                        value=lambda: datetime.now().isoformat(), every=1.0,
                                                        label='submitted_ts')

    commit_view = [
        github_link,
        diff_view,
        repo_val,
        hash_val,
        sample_loaded_timestamp,
        *commit_msg,
        *model_name
    ]

    feedback_form = [
        session_val,
        repo_val,
        hash_val,
        sample_loaded_timestamp,
        sample_submitted_timestamp,
        *is_correct,
        *has_what,
        *has_why,
        *is_not_verbose,
        *has_headline,
        *easy_to_read,
        *overall_rating,
        *comments,
        *model_name
    ]

    saver.setup([current_sample_sld] + feedback_form, "feedback")

    skip_btn.click(next_sample, inputs=[current_sample_sld, shuffled_idx_val],
                   outputs=[current_sample_sld] + commit_view)


    def submit(current_sample, shuffled_idx, *args):
        saver.flag((current_sample,) + args)
        return next_sample(current_sample, shuffled_idx)


    submit_btn.click(submit, inputs=[current_sample_sld, shuffled_idx_val] + feedback_form,
                     outputs=[current_sample_sld] + commit_view)


    def init_session(current_sample):
        session = str(uuid.uuid4())
        shuffled_idx = list(range(n_samples))
        random.shuffle(shuffled_idx)
        return (session, shuffled_idx) + update_commit_view(shuffled_idx[current_sample])


    application.load(init_session,
                     inputs=[current_sample_sld],
                     outputs=[session_val, shuffled_idx_val] + commit_view, )

application.launch()