-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
144 lines (116 loc) · 3.88 KB
/
index.ts
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
import "@logseq/libs"
import { SettingSchemaDesc } from '@logseq/libs/dist/LSPlugin';
const settingsSchema: SettingSchemaDesc[] = [
{
key: "HuggingFace User Access Token",
type: "string",
default: "",
title: "HuggingFace User Access Token",
description:
" Paste your HuggingFace User Access Token. For more information https://huggingface.co/docs/hub/security-tokens",
},
{
key: "Use Local API",
type: "boolean",
default: false,
title: "Use Local API",
description: "Toggle to use the local API instead of HuggingFace API"
}
]
async function query_huggingface(data: any) {
const access_token = logseq.settings!["HuggingFace User Access Token"]
if (!access_token) {
console.error("Access token not found. Please enter the user token in the settings.");
logseq.UI.showMsg('Access token not found. Please enter the user token in the settings.', 'error')
return;
}
const response = await fetch(
"https://api-inference.huggingface.co/models/Norm/nougat-latex-base",
{
headers: { Authorization: `Bearer ${access_token}`},
method: "POST",
body: data,
}
);
const result = await response.json();
// If there is an error, wait for the estimated time and retry
if (result.error) {
console.log('error: ', result.error);
console.log('estimated_time: ', result.estimated_time);
// Convert the estimated_time from seconds to milliseconds
const waitTime = result.estimated_time * 1000;
return new Promise((resolve) => {
setTimeout(() => {
resolve(query_huggingface(data));
}, waitTime);
});
}
// If there is no error, return the result
console.log('result: ',result);
console.log('result[0].generated_text: ', result[0].generated_text);
return result[0].generated_text;
}
async function query_local(data: any) {
const formData = new FormData();
formData.append("file", data);
try {
const response = await fetch(
"http://localhost/upload_latex_image/",
{
method: "POST",
body: formData,
}
);
if (!response.ok) {
throw new Error(`API returned status code ${response.status}`);
}
const result = await response.text();
return result;
} catch (error) {
console.error("Local API request failed: ", error);
logseq.UI.showMsg('Local API request failed: ' + error.message, 'error');
}
}
async function formula_ocr() {
try {
// Necessary to focus the window before reading the clipboard
window.focus();
const clipboardItem = await navigator.clipboard.read()
if (!clipboardItem) {
console.error('Clipboard item is empty')
return
}
console.log('Clipboard item: ', clipboardItem)
const data = await clipboardItem[0].getType('image/png').catch(err => {
throw new Error('Clipboard item is not an image')
})
console.log('Clipboard data: ', data)
const useLocalAPI = logseq.settings!["Use Local API"]
const ocrResult = useLocalAPI ? await query_local(data) : await query_huggingface(data)
return ocrResult
} catch (err) {
logseq.UI.showMsg('Reading image failed: ' + err, 'error')
}
}
async function main() {
logseq.useSettingsSchema(settingsSchema);
logseq.Editor.registerSlashCommand(
'display-formula-ocr',
async () => {
const text = await formula_ocr()
const latexText = `$$${text}$$`;
await logseq.Editor.insertAtEditingCursor(latexText)
},
)
logseq.Editor.registerSlashCommand(
'inline-formula-ocr',
async () => {
const text = await formula_ocr()
const latexText = `$${text}$`;
await logseq.Editor.insertAtEditingCursor(latexText)
},
)
console.log('Formula OCR plugin loaded')
}
// bootstrap
logseq.ready(main).catch(console.error)