File size: 9,441 Bytes
32f0b26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<script lang="ts">
    import { onMount } from "svelte";
    import ModelPerf from "./ModelPerf.svelte";
    import Button, { Label } from "@smui/button";
    import DataTable, { Head, Body, Row, Cell } from "@smui/data-table";
    import LinearProgress from '@smui/linear-progress';
    import IconButton from '@smui/icon-button';
    import { user } from './stores/cur_user_store.js';

    export let mode;
    export let model_name;

    let to_label = {};
    let promise = Promise.resolve(null);
    let n_complete_ratings;
    let n_unsure_ratings;

    // Get current user
    let cur_user;
    user.subscribe(value => {
		cur_user = value;
	});

    function getCommentsToLabel(cur_mode, n) {
        if (cur_mode == "train") {
            let req_params = {
                n: n,
            };
            let params = new URLSearchParams(req_params).toString();
            fetch("./get_comments_to_label?" + params)
                .then((r) => r.text())
                .then(function (r_orig) {
                    let r = JSON.parse(r_orig);
                    r["to_label"].forEach((key) => (to_label[key] = null));
                });
        } else if (cur_mode == "view") {
            if (model_name != "" && model_name != undefined) {
                promise = getModel(cur_mode);
            }
        }
    }
    onMount(async () => {
        getCommentsToLabel(mode, 40);
    });

    function handleLoadCommentsButton(n = 5) {
        getCommentsToLabel("train", n);
    }

    function handleTrainModelButton() {
        promise = getModel("train");
    }

    function getCompleteRatings() {
        let ratings = getRatings();
        let complete_ratings = Object.entries(ratings).filter(([key, value]) => value != "-1");
        let unsure_ratings = Object.entries(ratings).filter(([key, value]) => value == "-1");
        n_complete_ratings = complete_ratings.length;
        n_unsure_ratings = unsure_ratings.length;
    }

    function getRatings() {
        // Get rating for each comment
        let ratings = {};
        Object.entries(to_label).forEach(function ([comment, orig_rating], i) {
            var radio_btns = document.getElementsByName(
                "comment_" + i.toString()
            );
            let length = radio_btns.length;
            for (var i = 0; i < length; i++) {
                if (radio_btns[i].checked) {
                    ratings[comment] = radio_btns[i].value;
                    break;
                }
            }
        });
        return ratings;
    }

    async function getModel(cur_mode) {
        let ratings = null;
        if (cur_mode == "train") {
            ratings = getRatings();
            ratings = JSON.stringify(ratings);
        }

        let req_params = {
            model_name: model_name,
            ratings: ratings,
            mode: cur_mode,
            user: cur_user,
        };
        let params = new URLSearchParams(req_params).toString();
        const response = await fetch("./get_personalized_model?" + params);
        const text = await response.text();
        const data = JSON.parse(text);
        to_label = data["ratings_prev"];
        console.log(data);
        return data;
    }
</script>

<div>
    <div class="label_table spacing_vert">
        <DataTable
            table$aria-label="Comments to label"
            style="width: 100%;"
            stickyHeader
        >
            <Head>
                <Row>
                    <Cell style="width: 50%">Comment</Cell>
                    <Cell style="background-color: #c3ecdb">
                        0: <br>Not-at-all toxic<br>(Keep)<br>
                    </Cell>
                    <Cell style="background-color: white">
                        1: <br>Slightly toxic<br>(Keep)<br>
                    </Cell>
                    <Cell style="background-color: #ffa894">
                        2: <br>Moderately toxic<br>(Delete)<br>
                    </Cell>
                    <Cell style="background-color: #ff7a5c">
                        3: <br>Very toxic<br>(Delete)<br>
                    </Cell>
                    <Cell style="background-color: #d62728">
                        4: <br>Extremely toxic<br>(Delete)<br>
                    </Cell>
                    <Cell style="background-color: #808080">
                        <br>Unsure<br>(Skip)<br>
                    </Cell>
                </Row>
            </Head>
            <Body>
                {#if to_label}
                    {#each Object.keys(to_label) as comment, i}
                        <Row>
                            <Cell>
                                <div class="spacing_vert">{comment}</div>
                            </Cell>
                            <Cell>
                                <label>
                                    <input
                                        name="comment_{i}"
                                        type="radio"
                                        value="0"
                                        checked={to_label[comment] == "0"}
                                    />
                                    <span />
                                </label>
                            </Cell>
                            <Cell>
                                <label>
                                    <input
                                        name="comment_{i}"
                                        type="radio"
                                        value="1"
                                        checked={to_label[comment] == "1"}
                                    />
                                    <span />
                                </label>
                            </Cell>
                            <Cell>
                                <label>
                                    <input
                                        name="comment_{i}"
                                        type="radio"
                                        value="2"
                                        checked={to_label[comment] == "2"}
                                    />
                                    <span />
                                </label>
                            </Cell>
                            <Cell>
                                <label>
                                    <input
                                        name="comment_{i}"
                                        type="radio"
                                        value="3"
                                        checked={to_label[comment] == "3"}
                                    />
                                    <span />
                                </label>
                            </Cell>
                            <Cell>
                                <label>
                                    <input
                                        name="comment_{i}"
                                        type="radio"
                                        value="4"
                                        checked={to_label[comment] == "4"}
                                    />
                                    <span />
                                </label>
                            </Cell>
                            <Cell>
                                <label>
                                    <input
                                        name="comment_{i}"
                                        type="radio"
                                        value="-1"
                                        checked={to_label[comment] == "-1"}
                                        on:click={() => handleLoadCommentsButton(1)}
                                    />
                                    <span />
                                </label>
                            </Cell>
                        </Row>
                    {/each}
                {/if}
            </Body>
        </DataTable>
    </div>

    {#key n_complete_ratings}
    {#if n_complete_ratings}
    <div class="spacing_vert_40">
        <p>Number labeled: {n_complete_ratings}</p>
        <p>Number unsure: {n_unsure_ratings}</p>
    </div>
    {/if}
    {/key}

    <div class="spacing_vert_40">
        <Button on:click={handleTrainModelButton} variant="outlined" disabled={(!n_complete_ratings) || (n_complete_ratings < 40)}>
            <Label>Train Model</Label>
        </Button>
        <Button on:click={getCompleteRatings} variant="outlined">
            <Label>Get Number of Comments Labeled</Label>
        </Button>
        <Button on:click={() => handleLoadCommentsButton(5)} variant="outlined">
            <Label>Fetch More Comments To Label</Label>
        </Button>
    </div>


    <!-- Performance -->
    {#await promise}
        <div class="app_loading spacing_vert_20">
            <LinearProgress indeterminate />
        </div>
    {:then perf_results}
        {#if perf_results}
            <div class="spacing_vert_20">
                <ModelPerf data={perf_results} />
            </div>
        {/if}
    {:catch error}
        <p style="color: red">{error.message}</p>
    {/await}
</div>

<style>
    :global(html) {
        height: auto;
        width: auto;
        position: static;
    }
    :global(#sapper),
    :global(body) {
        display: block;
        height: auto;
    }
</style>