-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
api_doc_demo.html
213 lines (180 loc) · 7.84 KB
/
api_doc_demo.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document OCR Demo</title>
<style>
body {
font-family: Arial, sans-serif;
}
#log {
white-space: pre-wrap;
background-color: #f4f4f4;
padding: 10px;
border: 1px solid #ccc;
margin-top: 10px;
height: 300px;
overflow-y: auto;
}
</style>
</head>
<body>
<h1>Document OCR Demo</h1>
<form id="taskForm">
<label for="base_url">Base URL:</label>
<input type="text" id="base_url" name="base_url" value="http://127.0.0.1:1224" required>
<br><br>
<label for="file_path">File Path:</label>
<input type="file" id="file_path" name="file_path" required>
<br><br>
<label for="password">Password:</label>
<input type="text" id="password" name="password">
<br><br>
<button type="submit">Start Task</button>
<button type="button" id="stopClearTask">Stop/Clear Task</button>
</form>
<div id="log"></div>
<div id="result"></div>
<script>
let currentTaskId = null; // 记录当前任务ID
let isTaskRunning = false; // 记录任务状态
document.getElementById('taskForm').addEventListener('submit', async function (event) {
event.preventDefault();
const log = document.getElementById('log');
const result = document.getElementById('result');
log.innerHTML = '';
result.innerHTML = '';
const baseUrl = document.getElementById('base_url').value;
const fileInput = document.getElementById('file_path').files[0];
const password = document.getElementById('password').value;
if (!fileInput) {
log.innerHTML += "Please select a file.\n";
return;
}
const missionOptions = {
"doc.extractionMode": "fullPage",
"password": password,
};
const downloadOptions = {
"file_types": [
"pdfLayered",
],
// ↓ `ingore_blank` is a typo. If you are using Umi-OCR version 2.1.4 or earlier, please use this incorrect spelling.
// ↓ If you are using the latest code-built version of Umi-OCR, please use the corrected spelling `ignore_blank`.
"ingore_blank": false,
};
try {
log.innerHTML += "===== 1. Upload file, get task ID =====\n";
const formData = new FormData();
formData.append('file', fileInput);
formData.append('json', JSON.stringify(missionOptions));
let response = await fetch(`${baseUrl}/api/doc/upload`, {
method: 'POST',
body: formData
});
if (!response.ok) {
throw new Error(`Task submission failed: ${response.statusText}`);
}
const resData = await response.json();
if (resData.code !== 100) {
throw new Error(`Task submission failed: ${JSON.stringify(resData)}`);
}
currentTaskId = resData.data;
isTaskRunning = true;
log.innerHTML += `Task ID: ${currentTaskId}\n`;
log.innerHTML += "===== 2. Poll task status until OCR task ends =====\n";
while (true) {
await new Promise(resolve => setTimeout(resolve, 1000));
if (!isTaskRunning) return;
response = await fetch(`${baseUrl}/api/doc/result`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
id: currentTaskId,
is_data: true,
format: 'text',
is_unread: true
})
});
if (!response.ok) {
throw new Error(`Failed to get task status: ${response.statusText}`);
}
const statusData = await response.json();
if (statusData.code !== 100) {
throw new Error(`Failed to get task status: ${JSON.stringify(statusData)}`);
}
log.innerHTML += `Progress: ${statusData.processed_count}/${statusData.pages_count}\n`;
if (statusData.data) {
log.innerHTML += `${statusData.data}\n========================\n`;
}
if (statusData.is_done) {
if (statusData.state !== 'success') {
throw new Error(`Task execution failed: ${statusData.message}`);
}
log.innerHTML += "OCR task completed.\n";
break;
}
}
if (!isTaskRunning) return;
log.innerHTML += "===== 3. Generate target file, get download link =====\n";
downloadOptions.id = currentTaskId;
response = await fetch(`${baseUrl}/api/doc/download`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(downloadOptions)
});
if (!response.ok) {
throw new Error(`Failed to get download URL: ${response.statusText}`);
}
const downloadData = await response.json();
if (downloadData.code !== 100) {
throw new Error(`Failed to get download URL: ${JSON.stringify(downloadData)}`);
}
const downloadUrl = downloadData.data;
const fileName = downloadData.name;
if (!isTaskRunning) return;
log.innerHTML += "===== 4. Download target file =====\n";
log.innerHTML += `URL: ${downloadUrl}\n`;
result.innerHTML = `<a href="${downloadUrl}" target="_blank">Download ${fileName}</a>`;
log.innerHTML += "======================\nProcess completed.\n";
isTaskRunning = false;
} catch (error) {
log.innerHTML += `Error: ${error.message}\n`;
isTaskRunning = false;
}
});
document.getElementById('stopClearTask').addEventListener('click', async function () {
const log = document.getElementById('log');
const baseUrl = document.getElementById('base_url').value;
if (!currentTaskId) {
log.innerHTML += "No task has been submitted yet.\n";
return;
}
log.innerHTML = '';
if (isTaskRunning) {
isTaskRunning = false;
log.innerHTML += "Task stopped.\n";
}
try {
const response = await fetch(`${baseUrl}/api/doc/clear/${currentTaskId}`, {
method: 'GET'
});
const resData = await response.json();
if (resData.code === 100) {
log.innerHTML += "Cleanup completed.\n";
} else {
log.innerHTML += `Error during cleanup: ${JSON.stringify(resData)}\n`;
}
currentTaskId = null;
} catch (error) {
log.innerHTML += `Error: ${error.message}\n`;
}
});
</script>
</body>
</html>