-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubmit-review.js
264 lines (249 loc) · 9.67 KB
/
submit-review.js
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
import { pipeline } from "https://cdn.jsdelivr.net/npm/@huggingface/[email protected]"
async function step_selection(description, steps) {
const selector = await pipeline('question-answering', 'Xenova/distilbert-base-uncased-distilled-squad');
const pred = await selector(
"Which of the following steps best match the description ```"+description+"```?",
String(steps)
)
const answer = pred["answer"];
let i = 0;
let num = "";
while (i < answer.length) {
if (!isNaN(parseInt(answer[i]))) {
num += answer[i];
} else if (num != "") {
break;
}
i++;
}
if (num == "") {
num = 0;
} else {
num = parseInt(num);
}
console.log("step selection "+num)
return num;
}
async function im_analyze(im) {
document.getElementById("fb_sum").innerHTML = "[Image description]: ";
const imtext = await pipeline('image-to-text', 'Xenova/vit-gpt2-image-captioning');
const text = await imtext(im);
document.getElementById("fb_sum").innerHTML += text[0].generated_text;
const detect = await pipeline('object-detection', 'Xenova/detr-resnet-50');
const out = await detect(im, {threshold: 0.9});
document.getElementById("fb_sum").innerHTML += "\n[Image components] \n"
out.forEach((lab, i) => {document.getElementById("fb_sum").innerHTML += lab.label + ": (" + lab.score + ")\n"});
}
async function aud_analyze(aud) {
document.getElementById("fb_sum").innerHTML = "[Audio transcription]: ";
const listener = await pipeline('automatic-speech-recognition', 'Xenova/whisper-tiny.en');
const text = await listener(aud);
document.getElementById("fb_sum").innerHTML += text[0].text;
analyze_brief(text[0].text);
}
async function general_feedback(content) {
const brand = document.getElementById("brand").value;
console.log("general")
const reader = await pipeline('summarization', 'Xenova/distilbart-cnn-6-6');
// const brand = document.getElementById("brand").value;
const out = await reader(content, {max_token: 200});
console.log(out)
document.getElementById("fb_sum").innerText = out[0].summary_text;
const checker1 = await pipeline('sentiment-analysis', 'Xenova/bert-base-multilingual-uncased-sentiment');
const pred1 = await checker1(content);
let color = "";
let stm = "";
switch (pred1[0].label[0]) {
case '1': {
color = "salmon";
stm = ["Very negative", "Seems you feel unplesant about '"+brand+"' product/features. Reconsider whether creating the content or not."]
break;
}
case '2': {
color = "lightsalmon";
stm = ["Negative", "Make sure not to mention too much disadvantage of '"+brand+"' product/features"]
break;
}
case '3': {
color = "azure";
stm = ["Neutral", "Adjust your attitude toward '"+brand+"' more positive if you're satisfied with their product/features"]
break;
}
case '4': {
color = "greenyellow";
stm = ["Potitive", "Good, '"+brand+"' would like positive voice about their product/features."]
break;
}
case '5': {
color = "lightgreen";
stm = ["Very positive", "Wonderful, '"+brand+"' would appreciate your satisfaction about their product/features. "]
break;
}
}
document.getElementById("fb_gen").style.backgroundColor = color;
document.getElementById("fb_gen").innerHTML = "[Sentiment] The overall tone of the submitted content is "+stm[0]+" ("+pred1[0].label+"/5) "+stm[1]+"\n";
const checker2 = await pipeline('question-answering', 'Xenova/distilbert-base-uncased-distilled-squad');
const pred3 = await checker2(
"Is there any harmful information in the given content that might be negative to "+brand+" reputation or safety?",
"Given content: "+content
)
document.getElementById("fb_gen").innerHTML += "[Brand safety issues] "+pred3.answer;
const pred2 = await checker2(
"What are some key selling points of "+brand+" product or features mentioned in the given content?",
"Given content: "+content
)
document.getElementById("fb_gen").innerHTML += "\n[Selling points] "+pred2.answer;
}
async function brief_feedback(content, brief) {
const checker = await pipeline('question-answering', 'Xenova/distilbert-base-uncased-distilled-squad');
const ok = await checker(
"Based on the given content, what are some bullet points to achieve high quality?",
"Given content: "+brief
);
const avoid = await checker(
"Based on the given content, what are the bullet points containing actions be avoided?",
"Given content: "+brief
);
const p_check = await checker(
"Based on the given guidance, what aspect is missing from the content ```"+content+"```",
"Given guuidance: "+ok.anser
);
const n_check = await checker(
"Based on the given instructions, what aspect should not be appearing in the content ```"+content+"```",
"Given instructions: "+avoid.anser
);
document.getElementById("fb_doc").innerHTML = "The following should be done (with more extent) to improve quality: "+p_check.answer + "\nThe following should be avoided: " + n_check.answer;
}
async function content_review() {
var file = document.getElementById("media").files[0];
let mtype = document.getElementById("mtype").value;
if (file) {
var reader = new FileReader();
["text", "image", "video", "audio"].forEach((m, i) => {document.getElementById("s_"+m).style.display = "none"});
document.getElementById("s_"+mtype).style.display = "block";
if (mtype == "text") {
reader.readAsText(file, "UTF-8");
} else {
reader.readAsDataURL(file);
}
reader.onload = function (evt) {
switch (mtype) {
case "text": {
document.getElementById("s_text").innerHTML = evt.target.result;
general_feedback(evt.target.result);
analyze_brief(evt.target.result);
break;
}
case "image": {
document.getElementById("s_image").src = evt.target.result;
im_analyze(evt.target.result);
break;
}
case "audio": {
document.getElementById("s_audio_source").src = evt.target.result;
document.getElementById("s_audio").load();
aud_analyze(evt.target.result);
break;
}
case "video": {
document.getElementById("s_video_source").src = evt.target.result;
document.getElementById("s_video").load();
break;
}
}
}
reader.onerror = function (evt) {
alert("error reading file");
}
}
}
async function analyze_brief(content) {
if (document.getElementById("brief").innerText != "") {
return
}
var file = document.getElementById("brief_doc").files[0];
if (file) {
var reader = new FileReader();
reader.readAsText(file, "UTF-8");
reader.onload = async function (evt) {
const brief = await process_brief(evt.target.result);
brief_feedback(content, brief);
}
reader.onerror = function (evt) {
document.getElementById("brief").innerHTML = "error reading reference brief";
}
}
async function process_brief(brief) {
const steps = [];
let i = 0;
let step = "";
let n_steps = 1;
let body = 0;
while (i < brief.length) {
let sl = 5+String(n_steps).length;
if (brief.substring(i, i+sl).toLowerCase() == "step "+n_steps) {
n_steps += 1;
step = brief.substring(i, i+sl);
i += sl;
continue;
}
if (brief[i] == "\n") {
if (step != "") {
steps.push(step.substring(5));
body = i+1;
}
step = "";
} else if (step != "") {
step += brief[i];
}
i += 1;
}
if (step != "") {
steps.push(step);
}
n_steps = await step_selection(document.getElementById("desc").innerText, steps);
i = body;
step = n_steps;
const sl = 5+String(n_steps).length;
while (i < brief.length) {
if (brief.substring(i, i+sl).toLowerCase() == "step "+step) {
if (step != n_steps)
break;
step += 1;
body = i;
i += sl;
} else {
i += 1;
}
}
let j = body;
const segs = [];
let seg = "";
let start = false;
while (j < i) {
if (brief.substring(j, j+7) == "<aside>") {
j += 7;
start = true;
continue
} else if (brief.substring(j, j+8) == "</aside>") {
if (seg) {
segs.push(seg);
}
j += 8;
seg = "";
start = false;
continue;
}
if (start) {
seg += brief[j];
}
j += 1;
}
if (seg != "") {
segs.push(seg);
}
document.getElementById("brief").innerText = segs;
return segs;
}
}
window.content_review = content_review;