Skip to content

Commit 633b136

Browse files
committed
feat(mistral_ocr): Add outfile option and handle empty input
Introduces the ability to save OCR output to a file and adds robustness for empty file inputs. Increments version to 0.2.1.
1 parent 837fedf commit 633b136

5 files changed

Lines changed: 26 additions & 6 deletions

File tree

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ Tune is a versatile toolkit designed for developers and users to effectively int
295295
### `mistral_ocr`
296296
Extract text from documents and images using the [Mistral OCR API](https://mistral.ai/). Requires a `MISTRAL_KEY` set in `.env`.
297297

298-
Supports documents: `.pdf`, `.docx`, `.pptx`, `.txt`, `.epub`, `.xml`, `.rtf`, `.odt`, `.bib`, `.fb2`, `.ipynb`, `.tex`, `.opml`, `.1`, `.man`
298+
Supports documents: `.pdf`, `.docx`, `.xlsx`, `.pptx`, `.txt`, `.epub`, `.xml`, `.rtf`, `.odt`, `.bib`, `.fb2`, `.ipynb`, `.tex`, `.opml`, `.1`, `.man`
299299

300300
Supports images: `.jpg`, `.jpeg`, `.png`, `.avif`, `.tiff`, `.gif`, `.heic`, `.heif`, `.bmp`, `.webp`
301301

@@ -326,6 +326,17 @@ tool_result:
326326
Your disk usage has exceeded 90%. Please free up space to avoid performance issues.
327327
```
328328

329+
You can also save the extracted text to a file using the `outfile` parameter:
330+
331+
```chat
332+
user: @mistral_ocr
333+
extract text from report.pdf and save it to report.md
334+
335+
tool_call: mistral_ocr {"filename":"report.pdf","outfile":"report.md"}
336+
tool_result:
337+
saved to report.md
338+
```
339+
329340
### `websearch`
330341
Search the web with web enabled llms
331342
Supports search with `perplexity/sonar`, `perplexity/sonar-pro`, `gpt-4o-search-preview`, `gpt-4o-mini-search-preview` models via the `model` parameter (defaults to `perplexity/sonar`).

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "tune-basic-toolset",
3-
"version": "0.2.0",
3+
"version": "0.2.1",
44
"description": "Basic toolset for tune",
55
"main": "src/index.js",
66
"files": [

src/linenum.proc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module.exports = async function(node, args) {
1010
const res = Object.assign({}, node);
1111
res.read = async function(args) {
1212
let contents = await node.read(args);
13-
contents = contents.split("\n");
13+
contents = (contents || "").split("\n");
1414
const pad = `${contents.length}`.length;
1515
return contents.map((item, index) => {
1616
const lineNum = `${index + 1}`.padEnd(pad + 1, " ") + "|";

src/mistral_ocr.schema.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
"filename": {
77
"type": "string",
88
"description": "Path to the file to perform OCR on"
9+
},
10+
"outfile": {
11+
"type": "string",
12+
"description": "save ocr result to this filename"
913
}
1014
},
1115
"required": ["filename"]

src/mistral_ocr.tool.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const MIME_TYPES = {
3131
'.webp': { kind: 'image', mime: 'image/webp' },
3232
};
3333

34-
module.exports = async function mistralOcr({ filename }, ctx) {
34+
module.exports = async function mistralOcr({ filename, outfile }, ctx) {
3535
const apiKey = await ctx.read("MISTRAL_KEY");
3636
if (!apiKey) {
3737
throw new Error("MISTRAL_KEY is not set");
@@ -73,7 +73,12 @@ module.exports = async function mistralOcr({ filename }, ctx) {
7373
throw new Error(`Mistral OCR error: ${response.status} ${response.statusText} - ${errorText}`);
7474
}
7575

76-
const result = await response.json();
76+
let result = await response.json();
77+
result = result.pages.map(page => page.markdown).join("\n\n")
78+
if (outfile) {
79+
await ctx.write(outfile, result)
80+
return `saved to ${outfile}`
81+
}
7782

78-
return result.pages.map(page => page.markdown).join("\n\n");
83+
return result;
7984
};

0 commit comments

Comments
 (0)