Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
Pylogmon committed Jun 25, 2024
1 parent 06fb9c0 commit 962df47
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 85 deletions.
Binary file removed icon.png
Binary file not shown.
98 changes: 61 additions & 37 deletions info.json
Original file line number Diff line number Diff line change
@@ -1,37 +1,61 @@
{
"id": "plugin.com.pot-app.ocrspace",
"plugin_type": "recognize",
"icon": "icon.png",
"display": "OCR Space",
"homepage": "https://github.com/pot-app/pot-app-recognize-plugin-template",
"needs": [
{
"key": "engine",
"display": "Engine",
"type": "select",
"options": { "1": "Engine 1", "2": "Engine 2", "3": "Engine 3" }
},
{ "key": "apikey", "display": "Api Key", "type": "input" }
],
"language": {
"auto": "eng",
"zh_cn": "chs",
"zh_tw": "cht",
"en": "eng",
"ja": "jpn",
"ko": "kor",
"fr": "fre",
"es": "spa",
"ru": "rus",
"de": "ger",
"it": "ita",
"tr": "tur",
"pt_pt": "por",
"pt_br": "por",
"vi": "vie",
"th": "tai",
"ar": "ara",
"hi": "hin",
"fa": "per"
}
}
{
"id": "plugin.com.pot-app.openai_recognize",
"plugin_type": "recognize",
"icon": "openai.svg",
"display": "OpenAI",
"homepage": "https://github.com/pot-app/pot-app-recognize-plugin-openai",
"needs": [
{
"key": "model",
"display": "模型",
"type": "select",
"options": {
"gpt-4o": "GPT-4o",
"gpt-4-vision-preview": "GPT-4 Vision Preview"
}
},
{
"key": "requestPath",
"display": "请求路径",
"type": "input"
},
{
"key": "apiKey",
"display": "API Key",
"type": "input"
}
],
"language": {
"auto": "Auto",
"zh_cn": "Simplified Chinese",
"zh_tw": "Traditional Chinese",
"yue": "Cantonese",
"ja": "Japanese",
"en": "English",
"ko": "Korean",
"fr": "French",
"es": "Spanish",
"ru": "Russian",
"de": "German",
"it": "Italian",
"tr": "Turkish",
"pt_pt": "Portuguese",
"pt_br": "Brazilian Portuguese",
"vi": "Vietnamese",
"id": "Indonesian",
"th": "Thai",
"ms": "Malay",
"ar": "Arabic",
"hi": "Hindi",
"mn_mo": "Mongolian",
"mn_cy": "Mongolian(Cyrillic)",
"km": "Khmer",
"nb_no": "Norwegian Bokmål",
"nn_no": "Norwegian Nynorsk",
"fa": "Persian",
"sv": "Swedish",
"pl": "Polish",
"nl": "Dutch",
"uk": "Ukrainian"
}
}
110 changes: 62 additions & 48 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,63 @@
async function recognize(base64, lang, options) {
const { config, utils } = options;
const { tauriFetch } = utils;
let { apikey, engine } = config;
base64 = `data:image/png;base64,${base64}`;

if (apikey === undefined || apikey.length === 0) {
throw "apikey not found";
}
if (engine === undefined || engine.length === 0) {
engine = "1";
}

let res = await tauriFetch('https://api.ocr.space/parse/image', {
method: "POST",
header: {
apikey,
"content-type": "application/x-www-form-urlencoded"
},
body: {
type: "Form",
payload: {
base64Image: base64,
OCREngine: engine,
language: lang
}
}
})

if (res.ok) {
const { result } = res.data;
const { ErrorMessage, ParsedResults } = result;
if (ErrorMessage) {
throw ErrorMessage;
}
if (ParsedResults) {
let target = "";
for (let i in ParsedResults) {
const { ParsedText } = i;
target += ParsedText;
}
return target;
} else {
throw JSON.stringify(result);
}
} else {
throw JSON.stringify(res);
}
async function recognize(base64, _lang, options) {
const { config, utils } = options;
const { tauriFetch: fetch } = utils;
let { model = "gpt-4o", apiKey, requestPath } = config;

if (!/https?:\/\/.+/.test(requestPath)) {
requestPath = `https://${requestPath}`;
}
if (requestPath.endsWith('/')) {
requestPath = requestPath.slice(0, -1);
}
if (!requestPath.endsWith('/chat/completions')) {
requestPath += '/v1/chat/completions';
}

const headers = {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiKey}`
}

const body = {
model,
messages: [
{
"role": "system",
"content": [
{
"type": "text",
"text": "Just recognize the text in the image. Do not offer unnecessary explanations."
}
],
},
{
"role": "user",
"content": [
{
"type": "image_url",
"image_url": {
"url": `data:image/png;base64,${base64}`,
"detail": "high"
},
},
],
}
],
}
let res = await fetch(requestPath, {
method: 'POST',
url: requestPath,
headers: headers,
body: {
type: "Json",
payload: body
}
});

if (res.ok) {
let result = res.data;
return result.choices[0].message.content;
} else {
throw `Http Request Error\nHttp Status: ${res.status}\n${JSON.stringify(res.data)}`;
}
}
1 change: 1 addition & 0 deletions openai.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 962df47

Please sign in to comment.