forked from zyddnys/manga-image-translator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui.html
327 lines (310 loc) · 13.1 KB
/
ui.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Image/Manga Translator</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@unocss/reset/tailwind.min.css" />
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/petite-vue.iife.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@unocss/[email protected]/uno.global.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@iconify/[email protected]/dist/iconify.min.js"></script>
<style>
[v-cloak],
[un-cloak] {
display: none;
}
</style>
</head>
<body>
<form
action="#"
class="flex py-8 w-full min-h-100vh justify-center items-center"
@submit.prevent="onsubmit"
@vue:mounted="onmounted"
v-scope
v-cloak
un-cloak
>
<div class="flex flex-col w-85ch h-full justify-center gap-2">
<h1 class="text-center text-lg font-light">Image/Manga Translator</h1>
<div class="flex mx-4 justify-start items-end">
<div class="flex gap-4">
<div class="flex items-center" title="Detection resolution">
<i class="iconify" data-icon="carbon:fit-to-screen"></i>
<div class="relative">
<select class="w-9ch appearance-none bg-transparent border-b border-gray-300" v-model="detectionResolution">
<option value="S">1024px</option>
<option value="M">1536px</option>
<option value="L">2048px</option>
<option value="X">2560px</option>
</select>
<i class="iconify absolute top-1.5 right-1 pointer-events-none" data-icon="carbon:chevron-down"></i>
</div>
</div>
<div class="flex items-center gap-1" title="Text detector">
<i class="iconify" data-icon="carbon:search-locate"></i>
<div class="relative">
<select class="w-9ch appearance-none bg-transparent border-b border-gray-300" v-model="textDetector">
<option value="auto">Default</option>
<option value="ctd">CTD</option>
</select>
<i class="iconify absolute top-1.5 right-1 pointer-events-none" data-icon="carbon:chevron-down"></i>
</div>
</div>
<div class="flex items-center gap-1" title="Render text orientation">
<i class="iconify" data-icon="carbon:text-align-left"></i>
<div class="relative">
<select class="w-12ch appearance-none bg-transparent border-b border-gray-300" v-model="renderTextDirection">
<option value="auto">Auto</option>
<option value="ctd">Horizontal</option>
</select>
<i class="iconify absolute top-1.5 right-1 pointer-events-none" data-icon="carbon:chevron-down"></i>
</div>
</div>
<div class="flex items-center gap-1" title="Translator">
<i class="iconify" data-icon="carbon:operations-record"></i>
<div class="relative">
<select class="w-9ch appearance-none bg-transparent border-b border-gray-300" v-model="translator">
<option value="youdao">Youdao</option>
<option value="baidu">Baidu</option>
<option value="google">Google</option>
<option value="deepl">DeepL</option>
<option value="papago">Papago</option>
</select>
<i class="iconify absolute top-1.5 right-1 pointer-events-none" data-icon="carbon:chevron-down"></i>
</div>
</div>
<div class="flex items-center gap-1" title="Target language">
<i class="iconify" data-icon="carbon:language"></i>
<div class="relative">
<select class="w-15ch appearance-none bg-transparent border-b border-gray-300" v-model="targetLanguage">
<option value="CHS">简体中文</option>
<option value="CHT">繁體中文</option>
<option value="JPN">日本語</option>
<option value="ENG">English</option>
<option value="KOR">한국어</option>
<option value="VIN">Tiếng Việt</option>
<option value="CSY">čeština</option>
<option value="NLD">Nederlands</option>
<option value="FRA">français</option>
<option value="DEU">Deutsch</option>
<option value="HUN">magyar nyelv</option>
<option value="ITA">italiano</option>
<option value="PLK">polski</option>
<option value="PTB">português</option>
<option value="ROM">limba română</option>
<option value="RUS">русский язык</option>
<option value="ESP">español</option>
<option value="TRK">Türk dili</option>
</select>
<i class="iconify absolute top-1.5 right-1 pointer-events-none" data-icon="carbon:chevron-down"></i>
</div>
</div>
</div>
</div>
<div v-if="result" class="flex flex-col items-center">
<img class="my-2" :src="resultUri" />
<button class="px-2 py-1 text-center rounded-md text-blue-800 border-2 border-blue-300" @click="clear">Upload another</button>
</div>
<div v-else-if="status" class="grid w-full h-116 place-content-center rounded-2xl border-2 border-dashed border-gray-600">
<div class="flex flex-col items-center gap-2">
<i class="iconify w-8 h-8 text-gray-500 animate-spin" data-icon="carbon:progress-bar-round"></i>
<div>{{ statusText }}</div>
</div>
</div>
<label
v-else
class="grid w-full h-116 place-content-center rounded-2xl border-2 border-dashed border-gray-600 cursor-pointer"
for="file"
@dragenter.prevent
@dragover.prevent
@dragleave.prevent
@drop.prevent="ondrop"
>
<div v-if="file" class="flex flex-col items-center gap-2">
<div><span class="iconify-inline inline-block mr-2 scale-125" data-icon="carbon:image-search"></span>File Preview</div>
<img class="max-w-72 max-h-72" :src="fileUri" />
<button type="submit" class="px-2 py-1 rounded-md text-blue-800 border-2 border-blue-300">Translate</button>
<div class="text-sm text-gray-600">Click the empty space or paste/drag a new one to replace</div>
</div>
<div v-else class="flex flex-col items-center gap-2">
<i class="iconify w-8 h-8 text-gray-500" data-icon="carbon:cloud-upload"></i>
<div>Paste an image, click to select one or drag and drop here</div>
</div>
<input id="file" type="file" accept="image/png,image/jpeg,image/bmp" class="hidden" @change="onfilechange" />
</label>
<div class="flex justify-center gap-2">
<div>
Please consider supporting us by
<a class="underline underline-blue-400" href="https://ko-fi.com/voilelabs" target="_blank" rel="noopener noreferrer">Ko-fi</a>
or
<a class="underline underline-blue-400" href="https://www.patreon.com/voilelabs" target="_blank" rel="noopener noreferrer"
>Patreon</a
>!
</div>
<a
class="underline underline-blue-400"
href="https://github.com/zyddnys/manga-image-translator"
target="_blank"
rel="noopener noreferrer"
>Source Code</a
>
</div>
</div>
</form>
<script>
const BASE_URI = '/'
const acceptTypes = ['image/png', 'image/jpeg', 'image/bmp']
function formatSize(bytes) {
const k = 1024
const sizes = ['B', 'KB', 'MB', 'GB', 'TB']
if (bytes === 0) return '0B'
const i = Math.floor(Math.log(bytes) / Math.log(k))
return `${(bytes / k ** i).toFixed(2)}${sizes[i]}`
}
function formatProgress(loaded, total) {
return `${formatSize(loaded)}/${formatSize(total)}`
}
PetiteVue.createApp({
onmounted() {
window.addEventListener('paste', this.onpaste)
},
file: null,
get fileUri() {
return this.file ? URL.createObjectURL(this.file) : null
},
detectionResolution: 'M',
textDetector: 'auto',
renderTextDirection: 'auto',
translator: 'youdao',
targetLanguage: 'CHS',
ondrop(e) {
const file = e.dataTransfer?.files?.[0]
if (file && acceptTypes.includes(file.type)) {
this.file = file
}
},
onfilechange(e) {
const file = e.target.files?.[0]
if (file && acceptTypes.includes(file.type)) {
this.file = file
}
},
onpaste(e) {
const items = (e.clipboardData || e.originalEvent.clipboardData).items
for (const item of items) {
if (item.kind === 'file') {
const file = item.getAsFile()
if (!file || !acceptTypes.includes(file.type)) continue
this.file = file
}
}
},
progress: null,
status: null,
queuePos: null,
get statusText() {
switch (this.status) {
case 'upload': {
if (this.progress) {
return `Uploading (${this.progress})`
} else {
return 'Uploading'
}
}
case 'error-upload':
return 'Upload failed, please try again'
case 'download':
if (this.progress) {
return `Downloading (${this.progress})`
} else {
return 'Downloading'
}
case 'pending':
if (this.queuePos) {
return `Queuing, your position is ${this.queuePos}`
} else {
return 'Processing'
}
case 'detection':
return 'Detecting texts'
case 'ocr':
return 'Running OCR'
case 'mask_generation':
return 'Generating text mask'
case 'inpainting':
return 'Running inpainting'
case 'translating':
return 'Translating'
case 'render':
return 'Rendering translated texts'
case 'error':
return 'Something went wrong (maybe your language is not supported), please try again'
case 'error-lang':
return 'Your target language is not supported'
}
},
result: null,
get resultUri() {
return this.result ? URL.createObjectURL(this.result) : null
},
onsubmit(e) {
if (!this.file) return
this.progress = null
this.queuePos = null
this.status = 'upload'
const formData = new FormData()
formData.append('file', this.file)
formData.append('size', this.detectionResolution)
formData.append('detector', this.textDetector)
formData.append('dir', this.renderTextDirection)
formData.append('translator', this.translator)
formData.append('tgt_lang', this.targetLanguage)
const xhr = new XMLHttpRequest()
xhr.open('POST', `${BASE_URI}submit`, true)
xhr.upload.onprogress = (e) => {
if (e.lengthComputable) this.progress = formatProgress(e.loaded, e.total)
}
xhr.onload = async () => {
if (xhr.status !== 200) {
this.status = 'error-upload'
return
}
this.status = 'pending'
const { task_id } = JSON.parse(xhr.responseText)
for (;;) {
const timer = new Promise((resolve) => setTimeout(resolve, 500))
const { state, waiting } = await (await fetch(`${BASE_URI}task-state?taskid=${task_id}`)).json()
if (state === 'finished') {
this.progress = null
this.status = 'download'
const xhrDownload = new XMLHttpRequest()
xhrDownload.open('GET', `${BASE_URI}result/${task_id}`, true)
xhrDownload.responseType = 'blob'
xhrDownload.onprogress = (e) => {
if (e.lengthComputable) this.progress = formatProgress(e.loaded, e.total)
}
xhrDownload.onload = () => {
this.result = xhrDownload.response
this.status = null
}
xhrDownload.send()
break
}
this.status = state
this.queuePos = waiting
if (state === 'error' || state == 'error-lang') {
break
}
await timer
}
}
xhr.send(formData)
},
clear() {
this.file = null
this.result = null
},
}).mount()
</script>
</body>
</html>