Spaces:
Sleeping
Sleeping
Update server.js
Browse files
server.js
CHANGED
|
@@ -1,52 +1,22 @@
|
|
| 1 |
-
const express = require(
|
|
|
|
| 2 |
const tesseract = require("node-tesseract-ocr");
|
| 3 |
-
const fs = require(
|
| 4 |
-
const path = require('path');
|
| 5 |
-
const { convert } = require('pdf-poppler');
|
| 6 |
|
| 7 |
const app = express();
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
const pdfPath = path.join(uploadDir, `file_${Date.now()}.pdf`);
|
| 21 |
-
fs.writeFileSync(pdfPath, Buffer.from(file, 'base64'));
|
| 22 |
-
|
| 23 |
-
// Convert PDF → PNG (halaman pertama)
|
| 24 |
-
const outputBase = pdfPath.replace('.pdf', '');
|
| 25 |
-
await convert(pdfPath, {
|
| 26 |
-
format: 'png',
|
| 27 |
-
out_dir: uploadDir,
|
| 28 |
-
out_prefix: path.basename(outputBase),
|
| 29 |
-
page: 1, // ambil halaman pertama
|
| 30 |
-
scale: 1024 // resolusi
|
| 31 |
-
});
|
| 32 |
-
|
| 33 |
-
const imagePath = path.join(uploadDir, `${path.basename(outputBase)}-1.png`);
|
| 34 |
-
|
| 35 |
-
// OCR dengan Tesseract
|
| 36 |
-
const { data: { text } } = await Tesseract.recognize(imagePath, 'ind+eng');
|
| 37 |
-
|
| 38 |
-
// Bersihkan file sementara
|
| 39 |
-
fs.unlinkSync(pdfPath);
|
| 40 |
-
fs.unlinkSync(imagePath);
|
| 41 |
-
|
| 42 |
-
res.json({ success: true, text });
|
| 43 |
-
} catch (error) {
|
| 44 |
-
res.status(500).json({ success: false, error: error.message });
|
| 45 |
-
}
|
| 46 |
});
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
app.listen(PORT, "0.0.0.0", () => {
|
| 51 |
-
console.log(`🚀 OCR server running on http://0.0.0.0:${PORT}`);
|
| 52 |
-
});
|
|
|
|
| 1 |
+
const express = require("express");
|
| 2 |
+
const multer = require("multer");
|
| 3 |
const tesseract = require("node-tesseract-ocr");
|
| 4 |
+
const fs = require("fs");
|
|
|
|
|
|
|
| 5 |
|
| 6 |
const app = express();
|
| 7 |
+
const upload = multer({ dest: "uploads/" });
|
| 8 |
+
|
| 9 |
+
app.post("/ocr", upload.single("file"), async (req, res) => {
|
| 10 |
+
try {
|
| 11 |
+
const config = { lang: "eng+ind" };
|
| 12 |
+
const text = await tesseract.recognize(req.file.path, config);
|
| 13 |
+
|
| 14 |
+
fs.unlinkSync(req.file.path); // hapus file setelah OCR
|
| 15 |
+
res.json({ success: true, text });
|
| 16 |
+
} catch (err) {
|
| 17 |
+
res.status(500).json({ success: false, error: err.message });
|
| 18 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
});
|
| 20 |
|
| 21 |
+
const port = process.env.PORT || 7860;
|
| 22 |
+
app.listen(port, () => console.log(`🚀 OCR server running on port ${port}`));
|
|
|
|
|
|
|
|
|