-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert_script.js
362 lines (315 loc) · 12.5 KB
/
convert_script.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
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
const token = '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'
let currentScreen = 'screen1';
function changeScreen(screen) {
const screen1Elements = document.querySelectorAll('#screen1');
const screen2Elements = document.querySelectorAll('#screen2');
const screen3Elements = document.querySelectorAll('#screen3');
const spinner = document.getElementById('spinner');
if (screen === 'screen1') {
screen1Elements.forEach(element => {
element.style.display = 'block';
});
screen2Elements.forEach(element => {
element.style.display = 'none';
});
screen3Elements.forEach(element => {
element.style.display = 'none';
});
spinner.style.display = 'none';
} else if (screen === 'screen2') {
screen1Elements.forEach(element => {
element.style.display = 'none';
});
screen2Elements.forEach(element => {
element.style.display = 'block';
});
} else if (screen === 'screen3') {
screen1Elements.forEach(element => {
element.style.display = 'none';
});
screen3Elements.forEach(element => {
element.style.display = 'flex';
});
if (document.getElementById("copied").src == '' && document.getElementById("copied").src != 'images/copied.svg') {
document.getElementById("copy").src='images/copy.svg';
}
spinner.style.display = 'block';
}
currentScreen = screen;
}
document.addEventListener('DOMContentLoaded', function () {
initApp();
});
function initApp() {
changeScreen('screen1');
document.getElementById('imageInput').addEventListener('change', handleImage);
const languagePicker = document.getElementById('languagePicker');
// resize the language picker to fit the selected option
languagePicker.addEventListener('change', function() {
const selectedOption = this.options[this.selectedIndex];
const selectedOptionText = selectedOption.text;
const selectedOptionLength = selectedOptionText.length;
const maxOptionWidth = selectedOptionLength > 13 ? 13 : selectedOptionLength;
this.style.maxWidth = `${maxOptionWidth}rem`;
});
const languageDictionary = {
"Chinese (Simplified)": ["chi_sim","zh-TW"],
"English": ["eng","en"],
"Russian": ["rus","ru"],
// "Afrikaans": "afr",
// "Amharic": "amh",
"Arabic": ["ara","ar"],
// "Assamese": "asm",
// "Azerbaijani": "aze",
// "Azerbaijani - Cyrilic": "aze_cyrl",
// "Belarusian": "bel",
// "Bengali": "ben",
"Tibetan": "bod",
"Bosnian": "bos",
"Breton": "bre",
"Bulgarian": "bul",
"Catalan; Valencian": "cat",
"Cebuano": "ceb",
"Czech": "ces",
"Chinese (Traditional)": "chi_tra",
"Cherokee": "chr",
"Corsican": "cos",
"Welsh": "cym",
"Danish": "dan",
"Danish (Fraktur contrib)": "dan_frak",
"German": "deu",
"German (Fraktur contrib)": "deu_frak",
"Dzongkha": "dzo",
// "Greek, Modern (1453-)": "ell",
// "English, Middle (1100-1500)": "enm",
// "Esperanto": "epo",
// "Math / equation detection module": "equ",
"Estonian": "est",
"Basque": "eus",
"Faroese": "fao",
"Persian": "fas",
"Filipino (old Tagalog)": "fil",
"Finnish": "fin",
"French": "fra",
"German - Fraktur": "frk",
// "French, Middle (ca.1400-1600)": "frm",
"Western Frisian": "fry",
"Scottish Gaelic": "gla",
"Irish": "gle",
"Galician": "glg",
// "Greek, Ancient (to 1453) (contrib)": "grc",
"Gujarati": "guj",
"Haitian; Haitian Creole": "hat",
"Hebrew": "heb",
"Hindi": "hin",
"Croatian": "hrv",
"Hungarian": "hun",
"Armenian": "hye",
"Inuktitut": "iku",
"Indonesian": "ind",
"Icelandic": "isl",
"Italian": "ita",
"Italian - Old": "ita_old",
// "Javanese": "jav",
// "Japanese": "jpn",
"Kannada": "kan",
"Georgian": "kat",
"Georgian - Old": "kat_old",
"Kazakh": "kaz",
"Central Khmer": "khm",
"Kirghiz; Kyrgyz": "kir",
"Kurmanji (Kurdish - Latin Script)": "kmr",
"Korean": "kor",
"Korean (vertical)": "kor_vert",
"Kurdish (Arabic Script)": "kur",
"Lao": "lao",
"Latin": "lat",
"Latvian": "lav",
"Lithuanian": "lit",
"Luxembourgish": "ltz",
"Malayalam": "mal",
"Marathi": "mar",
"Macedonian": "mkd",
"Maltese": "mlt",
"Mongolian": "mon",
"Maori": "mri",
"Malay": "msa",
"Burmese": "mya",
"Nepali": "nep",
"Dutch; Flemish": "nld",
"Norwegian": "nor",
// "Occitan (post 1500)": "oci",
"Oriya": "ori",
// "Orientation and script detection module": "osd",
"Panjabi; Punjabi": "pan",
"Polish": "pol",
"Portuguese": "por",
"Pushto; Pashto": "pus",
"Quechua": "que",
"Romanian/Moldavian/Moldovan": "ron",
"Russian": "rus",
"Sanskrit": "san",
"Sinhala; Sinhalese": "sin",
"Slovak": "slk",
"Slovak - Fraktur (contrib)": "slk_frak",
"Slovenian": "slv"
};
for (const language in languageDictionary) {
const option = document.createElement('option');
option.value = languageDictionary[language];
option.text = language;
languagePicker.appendChild(option);
}
// Function to take image input
function handleImage(e) {
const file = e.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = function (e) {
const img = new Image();
img.onload = function () {
processImage(img);
document.getElementById('uploaded_image').src = e.target.result; // display the image
document.getElementById('uploaded_image').style.display = 'block'; // display the image
changeScreen('screen2');
};
img.src = e.target.result;
};
reader.readAsDataURL(file);
}
document.getElementById('reupload').disabled = false; // Enable the button
}
let rawText;
// Function to process image
async function processImage(img) {
const selectedLanguage = languagePicker.value;
// Transform image to text using Tesseract
await Tesseract.recognize(
img,
selectedLanguage.split(',')[0],
{ logger: info => console.log(info) }
).then(({ data: { text } }) => {
document.getElementById('uploaded-selected-language-text').innerText = text;
document.getElementById('uploaded-selected-language-text-display').innerText = text;
}).catch(error => {
console.error('Error during OCR:', error);
});
rawText = document.getElementById('uploaded-selected-language-text').innerText;
console.log(rawText); //test
// Translate rawText into English
if (selectedLanguage.split(',')[0] != 'eng') {
const options = {
method: "POST",
url: "https://api.edenai.run/v2/translation/automatic_translation",
headers: {
authorization: "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiNGI5YzVlZjUtYzliYS00NDFkLTgzOGItYTQxYTFhNjM0MjhiIiwidHlwZSI6ImZyb250X2FwaV90b2tlbiJ9.EWFrVxdQGtV81PtQO2uo8GvPSBETqAcs9-_PMV8gBi0",
},
data: {
providers: "google",
text: rawText,
source_language: selectedLanguage.split(',')[1],
target_language: "en",
fallback_providers: "",
},
};
axios
.request(options)
.then((response) => {
document.getElementById('uploaded-english-text').innerText = response.data['google']['text'];
document.getElementById('uploaded-english-text-display').innerText = response.data['google']['text'];
})
.catch((error) => {
console.error(error);
});
console.log('Successfully loaded translation'); //test
} else {
document.getElementById('uploaded-english-text').innerText = rawText;
}
}
// copy to clipboard feature
document.getElementById('copy').addEventListener('click', function() {
const outputText = document.getElementById('output-text').innerText
const copied = document.getElementById('copied');
const copy = document.getElementById('copy');
// Copy the output text to clipboard
navigator.clipboard.writeText(outputText)
// Set the URL of copy to empty string
this.src = '';
// Set the URL of copied to 'images/copied.svg'
copied.src = 'images/copied.svg';
// Revert the URL of copy after 2 seconds
setTimeout(function() {
if (document.getElementById('spinner').style.display != 'none') {
return;
} else {
copied.src = '';
copy.src = 'images/copy.svg';
}
}, 2000);
});
// OpenAI API to summarize text into dictionary with
document.getElementById('convert').addEventListener('click', ConvertClicked);
function ConvertClicked() {
document.getElementById('output-text').innerText = ""
document.getElementById('copy').src = '';
document.getElementById("copied").src = '';
changeScreen('screen3');
let rawTextEnglish = document.getElementById('uploaded-english-text').innerText;
fetch('https://api.openai.com/v1/chat/completions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + token,
},
body: JSON.stringify({
model: 'gpt-3.5-turbo-1106',
messages: [{ role: 'user', content: rawTextEnglish+ ". Delete any gibberish and group this paragraph into a human profiler with keyword-value dictionary" }],
}),
})
.then(async response => {
const result = await response.json();
document.getElementById('spinner').style.display = 'none';
document.getElementById('output-text').innerText = result.choices[0].message.content;
document.getElementById('copy').src = 'images/copy.svg';
})
}
}
// Submit button
document.getElementById('submit').addEventListener('click', SubmitClicked);
async function SubmitClicked() {
changeScreen('screen2');
const selectedLanguage = languagePicker.value;
if (selectedLanguage.split(',')[0] === 'eng') {
const commentText = document.getElementById('comment_text').value;
document.getElementById('uploaded-english-text-display').innerText = commentText;
document.getElementById('uploaded-english-text').innerText = commentText;
} else{
const commentText = document.getElementById('comment_text').value;
document.getElementById('uploaded-selected-language-text-display').innerText = commentText;
// Translate rawText into English
const options = {
method: "POST",
url: "https://api.edenai.run/v2/translation/automatic_translation",
headers: {
authorization: "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiNGI5YzVlZjUtYzliYS00NDFkLTgzOGItYTQxYTFhNjM0MjhiIiwidHlwZSI6ImZyb250X2FwaV90b2tlbiJ9.EWFrVxdQGtV81PtQO2uo8GvPSBETqAcs9-_PMV8gBi0",
},
data: {
providers: "google",
text: commentText,
source_language: selectedLanguage.split(',')[1],
target_language: "en",
fallback_providers: "",
},
};
axios
.request(options)
.then((response) => {
document.getElementById('uploaded-english-text').innerText = response.data['google']['text'];
document.getElementById('uploaded-english-text-display').innerText = response.data['google']['text'];
})
.catch((error) => {
console.error(error);
});
console.log('Successfully loaded translation'); //test
}
}