Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract JSON from OCR #83

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions node-zerox/src/openAI.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,76 @@
import { CompletionArgs, CompletionResponse } from "./types";
import { convertKeysToSnakeCase, encodeImageToBase64 } from "./utils";
import axios from "axios";
import { nanoid } from "nanoid";

const markdownToJson = async (markdownString: string) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: i would move this to the utils folder

/**
* Bypassing typescript transpiler using eval to use dynamic imports
*
* Source: https://stackoverflow.com/a/70546326
*/
const { unified } = await eval(`import('unified')`);
const { default: remarkParse } = await eval(`import('remark-parse')`);
const { remarkGfm } = await eval(`import('remark-gfm')`);

const parsedMd = unified()
.use(remarkParse) // Parse Markdown to AST
.use(remarkGfm)
.parse(markdownString);

const parentIdManager: string[] = [];

let depths = [0];

const jsonObj = parsedMd.children.map((node: any) => {
const isHeading = node.type === "heading";
if (isHeading && node.depth <= (depths.at(-1) || 0)) {
parentIdManager.pop();
// TODO: keep removing depth number till it reaches the one less than node.depth
depths.pop();
}
const processedNode = processNode(node, parentIdManager.at(-1));

if (isHeading) {
parentIdManager.push(processedNode.id);
if (depths.at(-1) !== node.depth) depths.push(node.depth);
}

return processedNode;
});

return jsonObj;
};

const type: Record<string, string> = {
heading: "heading",
text: "text",
};

const processNode = (node: any, parentId?: string) => {
let value: any;

if (node.type === "heading") {
value = node.children
.map((childNode: any) => processText(childNode))
.join(" ");
} else if (node.type === "paragraph") {
value = node.children
.map((childNode: any) => processText(childNode))
.join(" ");
}

return {
id: nanoid(),
parentId,
type: type[node.type as string] || type.text,
value,
};
};

const processText = (node: any) => {
return node.value;
};

export const getCompletion = async ({
apiKey,
Expand Down Expand Up @@ -58,6 +128,9 @@ export const getCompletion = async ({

const data = response.data;

// const jsonOutput = await markdownToJson(data.choices[0].message.content);
// console.log("====>>>>", JSON.stringify(jsonOutput, null, 2));

return {
content: data.choices[0].message.content,
inputTokens: data.usage.prompt_tokens,
Expand Down
Loading