-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathunstructured.ts
90 lines (78 loc) · 2.34 KB
/
unstructured.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
import axios from 'axios';
import { ModelProvider } from './base';
import { htmlToMarkdown } from '../utils';
// Fast Pipeline: $1 per 1,000 pages
const COST_PER_PAGE = 20 / 1000;
enum UnstructuredTypes {
Title = 'Title',
Header = 'Header',
NarrativeText = 'NarrativeText',
}
interface UnstructuredElement {
text: string;
type: UnstructuredTypes;
metadata: {
filename: string;
filetype: string;
languages: string[];
page_number: number;
parent_id?: string;
text_as_html?: string;
};
element_id: string;
}
export class UnstructuredProvider extends ModelProvider {
constructor() {
super('unstructured');
}
async ocr(imagePath: string) {
try {
const start = performance.now();
const fileName = imagePath.split('/').pop()[0];
const formData = new FormData();
const response = await axios.get(imagePath, { responseType: 'arraybuffer' });
const fileData = Buffer.from(response.data);
formData.append('files', new Blob([fileData]), fileName);
const apiResponse = await axios.post(
'https://api.unstructuredapp.io/general/v0/general',
formData,
{
headers: {
accept: 'application/json',
'unstructured-api-key': process.env.UNSTRUCTURED_API_KEY,
},
},
);
const unstructuredResult = apiResponse.data as UnstructuredElement[];
// Format the result
let markdown = '';
if (Array.isArray(unstructuredResult)) {
markdown = unstructuredResult.reduce((acc, el) => {
if (el.type === UnstructuredTypes.Title) {
acc += `\n### ${el.text}\n`;
} else if (el.type === UnstructuredTypes.NarrativeText) {
acc += `\n${el.text}\n`;
} else if (el.metadata?.text_as_html) {
acc += htmlToMarkdown(el.metadata.text_as_html) + '\n';
} else if (el.text) {
acc += el.text + '\n';
}
return acc;
}, '');
} else {
markdown = JSON.stringify(unstructuredResult);
}
const end = performance.now();
return {
text: markdown,
usage: {
duration: end - start,
totalCost: COST_PER_PAGE, // the input is always 1 page.
},
};
} catch (error) {
console.error('Unstructured Error:', error);
throw error;
}
}
}