Update plugins/deepseek.js

#1
Files changed (1) hide show
  1. plugins/deepseek.js +85 -85
plugins/deepseek.js CHANGED
@@ -1,86 +1,86 @@
1
- const fetch = require('node-fetch');
2
- const FormData = require('form-data');
3
- const path = require('path');
4
-
5
- module.exports = {
6
- path: '/deepseek-ocr',
7
- method: ['GET', 'POST'], // Mendukung dua metode
8
- description: 'OCR DeepSeek: Kirim URL via GET (?image=) atau File via POST.',
9
- handler: async (request, reply) => {
10
- const WORKER_PREFIX = "https://proxy2.afjasaku.workers.dev/?u=";
11
- const HF_DOMAIN = "https://merterbak-deepseek-ocr-demo.hf.space";
12
- const API_BASE = `${WORKER_PREFIX}${encodeURIComponent(HF_DOMAIN + "/gradio_api")}`;
13
- const session_hash = Math.random().toString(36).substring(2);
14
-
15
- try {
16
- let fileData;
17
- let fileName;
18
-
19
- if (request.method === 'POST') {
20
- const data = await request.file(); // Membutuhkan @fastify/multipart
21
- if (!data) throw new Error("No file uploaded");
22
- fileData = await data.toBuffer();
23
- fileName = data.filename;
24
- } else {
25
- const { image } = request.query;
26
- if (!image) throw new Error("Query 'image' (URL) diperlukan");
27
- const res = await fetch(image);
28
- fileData = await res.buffer();
29
- fileName = "url_upload.png";
30
- }
31
-
32
- // 1. Upload ke Gradio
33
- const form = new FormData();
34
- form.append('files', fileData, { filename: fileName });
35
-
36
- const uploadRes = await fetch(`${API_BASE}/upload?upload_id=${session_hash}`, {
37
- method: "POST",
38
- body: form,
39
- headers: form.getHeaders()
40
- });
41
-
42
- const uploadData = await uploadRes.json();
43
- const remotePath = uploadData[0];
44
-
45
- // 2. Join Antrean
46
- const payload = {
47
- "data": [null, {
48
- "path": remotePath,
49
- "url": `${HF_DOMAIN}/gradio_api/file=${remotePath}`,
50
- "orig_name": fileName,
51
- "size": fileData.length,
52
- "mime_type": "image/png",
53
- "meta": { "_type": "gradio.FileData" }
54
- }, "📋 Markdown", null, 1],
55
- "fn_index": 6,
56
- "session_hash": session_hash
57
- };
58
-
59
- await fetch(`${API_BASE}/queue/join`, {
60
- method: "POST",
61
- headers: { "Content-Type": "application/json" },
62
- body: JSON.stringify(payload)
63
- });
64
-
65
- // 3. Stream Response
66
- return new Promise((resolve, reject) => {
67
- const streamUrl = `${API_BASE}/queue/data?session_hash=${session_hash}`;
68
- fetch(streamUrl).then(res => {
69
- res.body.on('data', (chunk) => {
70
- const lines = chunk.toString().split("\n");
71
- for (let line of lines) {
72
- if (line.includes('"process_completed"')) {
73
- const json = JSON.parse(line.replace("data: ", ""));
74
- resolve({ status: "success", result: json.output.data[0] });
75
- }
76
- }
77
- });
78
- });
79
- setTimeout(() => reject(new Error("Timeout")), 60000);
80
- });
81
-
82
- } catch (err) {
83
- return { status: "error", message: err.message };
84
- }
85
- }
86
  };
 
1
+ const fetch = require('node-fetch');
2
+ const FormData = require('form-data');
3
+ const path = require('path');
4
+
5
+ module.exports = {
6
+ path: '/deepseek-ocr',
7
+ method: ['GET', 'POST'], // Mendukung dua metode
8
+ description: 'OCR DeepSeek: Kirim URL via GET (?image=) atau File via POST.',
9
+ handler: async (request, reply) => {
10
+ const WORKER_PREFIX = "https://proxy2.auriel.workers.dev/?u=";
11
+ const HF_DOMAIN = "https://merterbak-deepseek-ocr-demo.hf.space";
12
+ const API_BASE = `${WORKER_PREFIX}${encodeURIComponent(HF_DOMAIN + "/gradio_api")}`;
13
+ const session_hash = Math.random().toString(36).substring(2);
14
+
15
+ try {
16
+ let fileData;
17
+ let fileName;
18
+
19
+ if (request.method === 'POST') {
20
+ const data = await request.file(); // Membutuhkan @fastify/multipart
21
+ if (!data) throw new Error("No file uploaded");
22
+ fileData = await data.toBuffer();
23
+ fileName = data.filename;
24
+ } else {
25
+ const { image } = request.query;
26
+ if (!image) throw new Error("Query 'image' (URL) diperlukan");
27
+ const res = await fetch(image);
28
+ fileData = await res.buffer();
29
+ fileName = "url_upload.png";
30
+ }
31
+
32
+ // 1. Upload ke Gradio
33
+ const form = new FormData();
34
+ form.append('files', fileData, { filename: fileName });
35
+
36
+ const uploadRes = await fetch(`${API_BASE}/upload?upload_id=${session_hash}`, {
37
+ method: "POST",
38
+ body: form,
39
+ headers: form.getHeaders()
40
+ });
41
+
42
+ const uploadData = await uploadRes.json();
43
+ const remotePath = uploadData[0];
44
+
45
+ // 2. Join Antrean
46
+ const payload = {
47
+ "data": [null, {
48
+ "path": remotePath,
49
+ "url": `${HF_DOMAIN}/gradio_api/file=${remotePath}`,
50
+ "orig_name": fileName,
51
+ "size": fileData.length,
52
+ "mime_type": "image/png",
53
+ "meta": { "_type": "gradio.FileData" }
54
+ }, "📋 Markdown", null, 1],
55
+ "fn_index": 6,
56
+ "session_hash": session_hash
57
+ };
58
+
59
+ await fetch(`${API_BASE}/queue/join`, {
60
+ method: "POST",
61
+ headers: { "Content-Type": "application/json" },
62
+ body: JSON.stringify(payload)
63
+ });
64
+
65
+ // 3. Stream Response
66
+ return new Promise((resolve, reject) => {
67
+ const streamUrl = `${API_BASE}/queue/data?session_hash=${session_hash}`;
68
+ fetch(streamUrl).then(res => {
69
+ res.body.on('data', (chunk) => {
70
+ const lines = chunk.toString().split("\n");
71
+ for (let line of lines) {
72
+ if (line.includes('"process_completed"')) {
73
+ const json = JSON.parse(line.replace("data: ", ""));
74
+ resolve({ status: "success", result: json.output.data[0] });
75
+ }
76
+ }
77
+ });
78
+ });
79
+ setTimeout(() => reject(new Error("Timeout")), 60000);
80
+ });
81
+
82
+ } catch (err) {
83
+ return { status: "error", message: err.message };
84
+ }
85
+ }
86
  };