-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01_ocr_extract.mjs
More file actions
43 lines (34 loc) · 1.36 KB
/
01_ocr_extract.mjs
File metadata and controls
43 lines (34 loc) · 1.36 KB
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
// Extract text from a PDF or image using DeepRead OCR
import { readFileSync } from "fs";
const API_KEY = process.env.DEEPREAD_API_KEY;
const BASE = "https://api.deepread.tech";
const filePath = process.argv[2];
if (!API_KEY) { console.error("Set DEEPREAD_API_KEY"); process.exit(1); }
if (!filePath) { console.error("Usage: node 01_ocr_extract.mjs <file.pdf>"); process.exit(1); }
const form = new FormData();
form.append("file", new Blob([readFileSync(filePath)]), filePath.split("/").pop());
const { id: jobId } = await fetch(`${BASE}/v1/process`, {
method: "POST",
headers: { "X-API-Key": API_KEY },
body: form,
}).then(r => r.json());
console.log(`Submitted: ${jobId}`);
let delay = 5000;
while (true) {
await new Promise(r => setTimeout(r, delay));
const result = await fetch(`${BASE}/v1/jobs/${jobId}`, {
headers: { "X-API-Key": API_KEY },
}).then(r => r.json());
console.log(` Status: ${result.status}`);
if (result.status === "completed") {
const res = result.result;
console.log("\n--- Extracted Text ---");
console.log((res.text_preview || res.text || "").slice(0, 2000));
if (result.preview_url) console.log(`\nShareable preview: ${result.preview_url}`);
break;
} else if (result.status === "failed") {
console.error(`Failed: ${result.error || "Unknown error"}`);
break;
}
delay = Math.min(delay * 1.5, 15000);
}