api_doc_demo.html 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document OCR Demo</title>
  7. <style>
  8. body {
  9. font-family: Arial, sans-serif;
  10. }
  11. #log {
  12. white-space: pre-wrap;
  13. background-color: #f4f4f4;
  14. padding: 10px;
  15. border: 1px solid #ccc;
  16. margin-top: 10px;
  17. height: 300px;
  18. overflow-y: auto;
  19. }
  20. </style>
  21. </head>
  22. <body>
  23. <h1>Document OCR Demo</h1>
  24. <form id="taskForm">
  25. <label for="base_url">Base URL:</label>
  26. <input type="text" id="base_url" name="base_url" value="http://127.0.0.1:1224" required>
  27. <br><br>
  28. <label for="file_path">File Path:</label>
  29. <input type="file" id="file_path" name="file_path" required>
  30. <br><br>
  31. <label for="password">Password:</label>
  32. <input type="text" id="password" name="password">
  33. <br><br>
  34. <button type="submit">Start Task</button>
  35. <button type="button" id="stopClearTask">Stop/Clear Task</button>
  36. </form>
  37. <div id="log"></div>
  38. <div id="result"></div>
  39. <script>
  40. let currentTaskId = null; // 记录当前任务ID
  41. let isTaskRunning = false; // 记录任务状态
  42. document.getElementById('taskForm').addEventListener('submit', async function (event) {
  43. event.preventDefault();
  44. const log = document.getElementById('log');
  45. const result = document.getElementById('result');
  46. log.innerHTML = '';
  47. result.innerHTML = '';
  48. const baseUrl = document.getElementById('base_url').value;
  49. const fileInput = document.getElementById('file_path').files[0];
  50. const password = document.getElementById('password').value;
  51. if (!fileInput) {
  52. log.innerHTML += "Please select a file.\n";
  53. return;
  54. }
  55. const missionOptions = {
  56. "doc.extractionMode": "fullPage",
  57. "password": password,
  58. };
  59. const downloadOptions = {
  60. "file_types": [
  61. "pdfLayered",
  62. ],
  63. // ↓ `ingore_blank` is a typo. If you are using Umi-OCR version 2.1.4 or earlier, please use this incorrect spelling.
  64. // ↓ If you are using the latest code-built version of Umi-OCR, please use the corrected spelling `ignore_blank`.
  65. "ingore_blank": false,
  66. };
  67. try {
  68. log.innerHTML += "===== 1. Upload file, get task ID =====\n";
  69. const formData = new FormData();
  70. formData.append('file', fileInput);
  71. formData.append('json', JSON.stringify(missionOptions));
  72. let response = await fetch(`${baseUrl}/api/doc/upload`, {
  73. method: 'POST',
  74. body: formData
  75. });
  76. if (!response.ok) {
  77. throw new Error(`Task submission failed: ${response.statusText}`);
  78. }
  79. const resData = await response.json();
  80. if (resData.code !== 100) {
  81. throw new Error(`Task submission failed: ${JSON.stringify(resData)}`);
  82. }
  83. currentTaskId = resData.data;
  84. isTaskRunning = true;
  85. log.innerHTML += `Task ID: ${currentTaskId}\n`;
  86. log.innerHTML += "===== 2. Poll task status until OCR task ends =====\n";
  87. while (true) {
  88. await new Promise(resolve => setTimeout(resolve, 1000));
  89. if (!isTaskRunning) return;
  90. response = await fetch(`${baseUrl}/api/doc/result`, {
  91. method: 'POST',
  92. headers: {
  93. 'Content-Type': 'application/json'
  94. },
  95. body: JSON.stringify({
  96. id: currentTaskId,
  97. is_data: true,
  98. format: 'text',
  99. is_unread: true
  100. })
  101. });
  102. if (!response.ok) {
  103. throw new Error(`Failed to get task status: ${response.statusText}`);
  104. }
  105. const statusData = await response.json();
  106. if (statusData.code !== 100) {
  107. throw new Error(`Failed to get task status: ${JSON.stringify(statusData)}`);
  108. }
  109. log.innerHTML += `Progress: ${statusData.processed_count}/${statusData.pages_count}\n`;
  110. if (statusData.data) {
  111. log.innerHTML += `${statusData.data}\n========================\n`;
  112. }
  113. if (statusData.is_done) {
  114. if (statusData.state !== 'success') {
  115. throw new Error(`Task execution failed: ${statusData.message}`);
  116. }
  117. log.innerHTML += "OCR task completed.\n";
  118. break;
  119. }
  120. }
  121. if (!isTaskRunning) return;
  122. log.innerHTML += "===== 3. Generate target file, get download link =====\n";
  123. downloadOptions.id = currentTaskId;
  124. response = await fetch(`${baseUrl}/api/doc/download`, {
  125. method: 'POST',
  126. headers: {
  127. 'Content-Type': 'application/json'
  128. },
  129. body: JSON.stringify(downloadOptions)
  130. });
  131. if (!response.ok) {
  132. throw new Error(`Failed to get download URL: ${response.statusText}`);
  133. }
  134. const downloadData = await response.json();
  135. if (downloadData.code !== 100) {
  136. throw new Error(`Failed to get download URL: ${JSON.stringify(downloadData)}`);
  137. }
  138. const downloadUrl = downloadData.data;
  139. const fileName = downloadData.name;
  140. if (!isTaskRunning) return;
  141. log.innerHTML += "===== 4. Download target file =====\n";
  142. log.innerHTML += `URL: ${downloadUrl}\n`;
  143. result.innerHTML = `<a href="${downloadUrl}" target="_blank">Download ${fileName}</a>`;
  144. log.innerHTML += "======================\nProcess completed.\n";
  145. isTaskRunning = false;
  146. } catch (error) {
  147. log.innerHTML += `Error: ${error.message}\n`;
  148. isTaskRunning = false;
  149. }
  150. });
  151. document.getElementById('stopClearTask').addEventListener('click', async function () {
  152. const log = document.getElementById('log');
  153. const baseUrl = document.getElementById('base_url').value;
  154. if (!currentTaskId) {
  155. log.innerHTML += "No task has been submitted yet.\n";
  156. return;
  157. }
  158. log.innerHTML = '';
  159. if (isTaskRunning) {
  160. isTaskRunning = false;
  161. log.innerHTML += "Task stopped.\n";
  162. }
  163. try {
  164. const response = await fetch(`${baseUrl}/api/doc/clear/${currentTaskId}`, {
  165. method: 'GET'
  166. });
  167. const resData = await response.json();
  168. if (resData.code === 100) {
  169. log.innerHTML += "Cleanup completed.\n";
  170. } else {
  171. log.innerHTML += `Error during cleanup: ${JSON.stringify(resData)}\n`;
  172. }
  173. currentTaskId = null;
  174. } catch (error) {
  175. log.innerHTML += `Error: ${error.message}\n`;
  176. }
  177. });
  178. </script>
  179. </body>
  180. </html>