converter-studio-haystack 1.0.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- converter_studio_haystack-1.0.0/.gitignore +12 -0
- converter_studio_haystack-1.0.0/CHANGELOG.md +21 -0
- converter_studio_haystack-1.0.0/LICENSE +21 -0
- converter_studio_haystack-1.0.0/PKG-INFO +301 -0
- converter_studio_haystack-1.0.0/README.md +265 -0
- converter_studio_haystack-1.0.0/pyproject.toml +96 -0
- converter_studio_haystack-1.0.0/src/haystack_integrations/components/converters/converter_studio/__init__.py +18 -0
- converter_studio_haystack-1.0.0/src/haystack_integrations/components/converters/converter_studio/_shared.py +392 -0
- converter_studio_haystack-1.0.0/src/haystack_integrations/components/converters/converter_studio/_version.py +3 -0
- converter_studio_haystack-1.0.0/src/haystack_integrations/components/converters/converter_studio/document_converter.py +475 -0
- converter_studio_haystack-1.0.0/src/haystack_integrations/components/converters/converter_studio/file_converter.py +313 -0
- converter_studio_haystack-1.0.0/src/haystack_integrations/components/converters/converter_studio/py.typed +0 -0
- converter_studio_haystack-1.0.0/tests/__init__.py +0 -0
- converter_studio_haystack-1.0.0/tests/conftest.py +44 -0
- converter_studio_haystack-1.0.0/tests/fake_api.py +329 -0
- converter_studio_haystack-1.0.0/tests/test_document_converter.py +430 -0
- converter_studio_haystack-1.0.0/tests/test_file_converter.py +156 -0
- converter_studio_haystack-1.0.0/tests/test_pipelines.py +173 -0
- converter_studio_haystack-1.0.0/tests/test_shared.py +140 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this package are listed here. The format follows
|
|
4
|
+
[Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and the package uses
|
|
5
|
+
[semantic versioning](https://semver.org/).
|
|
6
|
+
|
|
7
|
+
## 1.0.0 - 2026-11-23
|
|
8
|
+
|
|
9
|
+
The first release.
|
|
10
|
+
|
|
11
|
+
- `ConverterStudioDocumentConverter`: files (paths, `ByteStream`s or links) to Haystack
|
|
12
|
+
`Document`s through Converter Studio's text conversions: PDFs (OCR for scans, `ocr="auto"`,
|
|
13
|
+
`"always"` or `"never"`), pictures by OCR, Word, OpenDocument, RTF and HTML documents, e-books,
|
|
14
|
+
spreadsheets (CSV) and presentations (through a PDF); plain-text formats read locally.
|
|
15
|
+
- `ConverterStudioFileConverter`: files to another format (`output_file_type`), as `ByteStream`s
|
|
16
|
+
with their MIME type and metadata.
|
|
17
|
+
- Both: `run_async` with `concurrency_limit`, `warm_up` / `warm_up_async` / `close` /
|
|
18
|
+
`close_async`, `to_dict` / `from_dict` with the API key as a `Secret`
|
|
19
|
+
(`CONVERTER_STUDIO_API_KEY`), `options` and `max_credits`, failures skipped with the API's
|
|
20
|
+
message (or raised with `raise_on_failure`), and the account's errors always raised.
|
|
21
|
+
- Tested with Haystack 2.20, 2.29, 3.0 and 3.5.
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Converter Studio
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: converter-studio-haystack
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Haystack components for Converter Studio: turn scanned PDFs, images, Office files and e-books into Documents (with OCR), or convert files to PDF, JPG, MP3 and 60 other formats.
|
|
5
|
+
Project-URL: Homepage, https://converter.studio
|
|
6
|
+
Project-URL: Documentation, https://converter.studio/developers
|
|
7
|
+
Project-URL: Source, https://github.com/tariksucu/converter-studio-haystack
|
|
8
|
+
Project-URL: Issues, https://github.com/tariksucu/converter-studio-haystack/issues
|
|
9
|
+
Project-URL: Changelog, https://github.com/tariksucu/converter-studio-haystack/blob/main/CHANGELOG.md
|
|
10
|
+
Author-email: Converter Studio <support@converter.studio>
|
|
11
|
+
License-Expression: MIT
|
|
12
|
+
License-File: LICENSE
|
|
13
|
+
Keywords: converter-studio,document-converter,file-conversion,haystack,haystack-ai,ocr,pdf,rag
|
|
14
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
15
|
+
Classifier: Framework :: AsyncIO
|
|
16
|
+
Classifier: Intended Audience :: Developers
|
|
17
|
+
Classifier: Operating System :: OS Independent
|
|
18
|
+
Classifier: Programming Language :: Python :: 3
|
|
19
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
23
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
24
|
+
Classifier: Programming Language :: Python :: 3.15
|
|
25
|
+
Classifier: Topic :: Multimedia :: Graphics :: Graphics Conversion
|
|
26
|
+
Classifier: Topic :: Office/Business
|
|
27
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
28
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
29
|
+
Classifier: Topic :: Text Processing
|
|
30
|
+
Classifier: Typing :: Typed
|
|
31
|
+
Requires-Python: >=3.11
|
|
32
|
+
Requires-Dist: converter-studio<2,>=1.0
|
|
33
|
+
Requires-Dist: haystack-ai<4,>=2.20
|
|
34
|
+
Requires-Dist: httpx<1,>=0.27
|
|
35
|
+
Description-Content-Type: text/markdown
|
|
36
|
+
|
|
37
|
+
# Converter Studio for Haystack
|
|
38
|
+
|
|
39
|
+
[Haystack](https://haystack.deepset.ai) components for [Converter Studio](https://converter.studio),
|
|
40
|
+
a file conversion service with OCR:
|
|
41
|
+
|
|
42
|
+
- **`ConverterStudioDocumentConverter`** turns files into Haystack `Document`s: scanned PDFs and
|
|
43
|
+
photos of pages (by OCR), PDFs with a text layer, Word, OpenDocument, RTF and HTML documents,
|
|
44
|
+
e-books (EPUB, MOBI, AZW3, FB2), spreadsheets and presentations.
|
|
45
|
+
- **`ConverterStudioFileConverter`** converts files to another format and returns them as
|
|
46
|
+
`ByteStream`s: Office files and e-books to PDF, PDFs to Word or JPG, HEIC and camera RAW photos
|
|
47
|
+
to JPG, videos to MP4 or MP3, and the other conversions of Converter Studio's 67 formats.
|
|
48
|
+
|
|
49
|
+
The conversions run on Converter Studio's servers, so there is nothing else to install: no
|
|
50
|
+
Tesseract, no LibreOffice, no Poppler, no FFmpeg.
|
|
51
|
+
|
|
52
|
+
```python
|
|
53
|
+
from haystack_integrations.components.converters.converter_studio import (
|
|
54
|
+
ConverterStudioDocumentConverter,
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
converter = ConverterStudioDocumentConverter() # the key comes from CONVERTER_STUDIO_API_KEY
|
|
58
|
+
documents = converter.run(sources=["scanned-contract.pdf", "minutes.docx"])["documents"]
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## Installation
|
|
62
|
+
|
|
63
|
+
```console
|
|
64
|
+
$ pip install converter-studio-haystack
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
Python 3.11 or later, Haystack 3 (`haystack-ai` 3.x; Haystack 2.20 and later work too). The
|
|
68
|
+
package installs the official [Converter Studio Python client](https://pypi.org/project/converter-studio/).
|
|
69
|
+
|
|
70
|
+
Create an API key on your [account page](https://converter.studio/account#api-keys) (keys start
|
|
71
|
+
with `cvs_live_`; keep them secret) and put it in the environment:
|
|
72
|
+
|
|
73
|
+
```console
|
|
74
|
+
$ export CONVERTER_STUDIO_API_KEY=cvs_live_...
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
Both components read the key with Haystack's `Secret` (`Secret.from_env_var("CONVERTER_STUDIO_API_KEY")`
|
|
78
|
+
by default), so it is never written into a serialized pipeline. The key is read when the
|
|
79
|
+
component is warmed up, not when it is created.
|
|
80
|
+
|
|
81
|
+
Every conversion uses your account's credits, the same as on the website: free accounts get
|
|
82
|
+
credits every day, Pro and Premium their monthly credits. Most documents cost 1 credit; reading a
|
|
83
|
+
picture with OCR costs 3, and a scanned PDF's OCR is priced by its number of pages. Failed
|
|
84
|
+
conversions cost nothing. See [pricing](https://converter.studio/pricing).
|
|
85
|
+
|
|
86
|
+
## Files to Documents
|
|
87
|
+
|
|
88
|
+
```python
|
|
89
|
+
from haystack_integrations.components.converters.converter_studio import (
|
|
90
|
+
ConverterStudioDocumentConverter,
|
|
91
|
+
)
|
|
92
|
+
|
|
93
|
+
converter = ConverterStudioDocumentConverter(ocr="auto")
|
|
94
|
+
result = converter.run(
|
|
95
|
+
sources=["invoice-scan.pdf", "handbook.epub", "whiteboard.heic", "deck.pptx"],
|
|
96
|
+
meta={"project": "onboarding"},
|
|
97
|
+
)
|
|
98
|
+
for document in result["documents"]:
|
|
99
|
+
print(document.meta["file_path"], document.meta["converter_studio"], document.content[:60])
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
Sources can be file paths, `ByteStream`s, or links (a `str` starting with `http://` or `https://`,
|
|
103
|
+
Google Drive, Dropbox and OneDrive share links included): Converter Studio downloads a link
|
|
104
|
+
itself, so the file never passes through your machine.
|
|
105
|
+
|
|
106
|
+
How each kind of file becomes text:
|
|
107
|
+
|
|
108
|
+
| Source | What happens |
|
|
109
|
+
| --------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------- |
|
|
110
|
+
| PDF | Its text layer (pages separated by `\f`, as Haystack's own PDF converters do); a scan without text is read with OCR (see `ocr`) |
|
|
111
|
+
| JPG, PNG, HEIC, TIFF, WebP, BMP | Read with OCR (the language is found on its own, or set `options={"ocrLanguage": "de"}`) |
|
|
112
|
+
| DOCX, DOC, ODT, RTF, HTML | Plain text |
|
|
113
|
+
| EPUB, MOBI, AZW3, FB2 | Plain text |
|
|
114
|
+
| XLSX, XLS, ODS | CSV text of the first sheet (`options={"sheet": 2}` for another) |
|
|
115
|
+
| PPTX, PPT, ODP, SVG, EPS, AI, camera RAW, GIF, AVIF | Made into a PDF, then its text (two conversions) |
|
|
116
|
+
| TXT, Markdown, CSV, JSON, XML, YAML, SRT, VTT, ASS | Read here, without a request and without credits |
|
|
117
|
+
| Audio, video, archives, fonts | Skipped with a warning (no text to read) |
|
|
118
|
+
|
|
119
|
+
`ocr` says when to use OCR (optical character recognition):
|
|
120
|
+
|
|
121
|
+
- `"auto"` (default): a PDF's own text first; only a PDF without any (a scan) is read again with
|
|
122
|
+
OCR. The first try costs nothing when it finds no text.
|
|
123
|
+
- `"always"`: PDFs go straight to OCR, for folders of scans.
|
|
124
|
+
- `"never"`: no OCR; scanned PDFs and pictures are skipped.
|
|
125
|
+
|
|
126
|
+
OCR reads up to 50 pages of a PDF. Its text keeps the page's lines and table columns, without page
|
|
127
|
+
separators.
|
|
128
|
+
|
|
129
|
+
Each `Document` carries the metadata Haystack's converters carry: a `ByteStream`'s own `meta`,
|
|
130
|
+
`file_path` (the file name, or the full path with `store_full_path=True`), `url` for a link, and
|
|
131
|
+
the `meta` you pass to `run` (a dictionary for all sources, or a list with one per source). It
|
|
132
|
+
also has `converter_studio`: `{"source_format": "pdf", "ocr": true}`, what the file was and
|
|
133
|
+
whether OCR read it.
|
|
134
|
+
|
|
135
|
+
## Files to other formats
|
|
136
|
+
|
|
137
|
+
```python
|
|
138
|
+
from haystack_integrations.components.converters.converter_studio import (
|
|
139
|
+
ConverterStudioFileConverter,
|
|
140
|
+
)
|
|
141
|
+
|
|
142
|
+
converter = ConverterStudioFileConverter(output_file_type="pdf")
|
|
143
|
+
result = converter.run(sources=["slides.pptx", "letter.docx"])
|
|
144
|
+
for stream in result["output"]:
|
|
145
|
+
print(stream.meta["converter_studio"]["file_name"], stream.mime_type, len(stream.data))
|
|
146
|
+
|
|
147
|
+
mp3 = converter.run(sources=["interview.mov"], output_file_type="mp3")["output"]
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
The results are `ByteStream`s with their MIME type, the source's metadata, and
|
|
151
|
+
`meta["converter_studio"]`: `source_format`, `output_format` and `file_name` (the result's name,
|
|
152
|
+
such as `slides.pdf`). They connect to anything that takes `ByteStream`s, for example a legacy
|
|
153
|
+
`.doc` to Haystack's `DOCXToDocument` (which needs `pip install python-docx`):
|
|
154
|
+
|
|
155
|
+
```python
|
|
156
|
+
from haystack import Pipeline
|
|
157
|
+
from haystack.components.converters import DOCXToDocument
|
|
158
|
+
from haystack_integrations.components.converters.converter_studio import (
|
|
159
|
+
ConverterStudioFileConverter,
|
|
160
|
+
)
|
|
161
|
+
|
|
162
|
+
pipeline = Pipeline()
|
|
163
|
+
pipeline.add_component("to_docx", ConverterStudioFileConverter(output_file_type="docx"))
|
|
164
|
+
pipeline.add_component("docx", DOCXToDocument())
|
|
165
|
+
pipeline.connect("to_docx.output", "docx.sources")
|
|
166
|
+
result = pipeline.run({"to_docx": {"sources": ["old-report.doc"]}})
|
|
167
|
+
print(result["docx"]["documents"][0].content[:80])
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
A PDF made into pictures (`jpg`, `png`, `webp`, `tiff`) comes back as one picture per page, in a
|
|
171
|
+
ZIP when it has more than one page. `options` takes the API's conversion settings, such as
|
|
172
|
+
`{"quality": "high"}`, `{"height": "720"}` for a video, or `{"ocr": True}` for a scanned PDF to
|
|
173
|
+
Word; the full list is on the [API page](https://converter.studio/developers).
|
|
174
|
+
|
|
175
|
+
## Example: index scanned PDFs with OCR
|
|
176
|
+
|
|
177
|
+
This pipeline reads every PDF in `scans/` (scanned or not), splits the text and writes it to an
|
|
178
|
+
in-memory document store, then finds passages with BM25. It needs only a Converter Studio key.
|
|
179
|
+
|
|
180
|
+
```python
|
|
181
|
+
from pathlib import Path
|
|
182
|
+
|
|
183
|
+
from haystack import Pipeline
|
|
184
|
+
from haystack.components.preprocessors import DocumentSplitter
|
|
185
|
+
from haystack.components.retrievers.in_memory import InMemoryBM25Retriever
|
|
186
|
+
from haystack.components.writers import DocumentWriter
|
|
187
|
+
from haystack.document_stores.in_memory import InMemoryDocumentStore
|
|
188
|
+
from haystack_integrations.components.converters.converter_studio import (
|
|
189
|
+
ConverterStudioDocumentConverter,
|
|
190
|
+
)
|
|
191
|
+
|
|
192
|
+
store = InMemoryDocumentStore()
|
|
193
|
+
indexing = Pipeline()
|
|
194
|
+
indexing.add_component("converter", ConverterStudioDocumentConverter(ocr="auto"))
|
|
195
|
+
indexing.add_component("splitter", DocumentSplitter(split_by="word", split_length=150))
|
|
196
|
+
indexing.add_component("writer", DocumentWriter(document_store=store))
|
|
197
|
+
indexing.connect("converter", "splitter")
|
|
198
|
+
indexing.connect("splitter", "writer")
|
|
199
|
+
|
|
200
|
+
indexing.run({"converter": {"sources": sorted(Path("scans").glob("*.pdf"))}})
|
|
201
|
+
print(store.count_documents(), "passages")
|
|
202
|
+
|
|
203
|
+
retriever = InMemoryBM25Retriever(document_store=store)
|
|
204
|
+
for document in retriever.run(query="invoice total", top_k=3)["documents"]:
|
|
205
|
+
print(
|
|
206
|
+
document.meta["file_path"], document.meta["converter_studio"]["ocr"], document.content[:80]
|
|
207
|
+
)
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
## Async pipelines
|
|
211
|
+
|
|
212
|
+
Both components have `run_async`, which converts `concurrency_limit` sources at a time (4 by
|
|
213
|
+
default), and `warm_up_async` / `close_async` for their HTTP clients:
|
|
214
|
+
|
|
215
|
+
```python
|
|
216
|
+
import asyncio
|
|
217
|
+
|
|
218
|
+
from haystack import Pipeline
|
|
219
|
+
from haystack_integrations.components.converters.converter_studio import (
|
|
220
|
+
ConverterStudioDocumentConverter,
|
|
221
|
+
)
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
async def main() -> None:
|
|
225
|
+
pipeline = Pipeline()
|
|
226
|
+
pipeline.add_component("converter", ConverterStudioDocumentConverter(concurrency_limit=8))
|
|
227
|
+
result = await pipeline.run_async({"converter": {"sources": ["a.pdf", "b.docx", "c.png"]}})
|
|
228
|
+
print(len(result["converter"]["documents"]))
|
|
229
|
+
await pipeline.close_async()
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+
asyncio.run(main())
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
(On Haystack 2, use `AsyncPipeline`.)
|
|
236
|
+
|
|
237
|
+
## Settings
|
|
238
|
+
|
|
239
|
+
Both components take:
|
|
240
|
+
|
|
241
|
+
| Parameter | Default | What it does |
|
|
242
|
+
| ------------------- | ------------------------------------------------- | ------------------------------------------------------------------------------------ |
|
|
243
|
+
| `api_key` | `Secret.from_env_var("CONVERTER_STUDIO_API_KEY")` | the API key |
|
|
244
|
+
| `base_url` | `None` | the API's address (`CONVERTER_STUDIO_BASE_URL` or `https://converter.studio/api`) |
|
|
245
|
+
| `options` | `None` | conversion settings sent with every conversion (ignored where they do not apply) |
|
|
246
|
+
| `max_credits` | `None` (the API's 20) | the most one conversion may cost and start on its own; a dearer one is skipped, free |
|
|
247
|
+
| `timeout` | `600.0` | seconds to wait for each conversion |
|
|
248
|
+
| `raise_on_failure` | `False` | raise instead of skipping a source that cannot be converted |
|
|
249
|
+
| `concurrency_limit` | `4` | sources converted at the same time by `run_async` |
|
|
250
|
+
|
|
251
|
+
`ConverterStudioDocumentConverter` also takes `ocr` (`"auto"`, `"always"`, `"never"`) and
|
|
252
|
+
`store_full_path`; `ConverterStudioFileConverter` takes `output_file_type`, which `run` can
|
|
253
|
+
override.
|
|
254
|
+
|
|
255
|
+
Both serialize with `to_dict` / `from_dict` and load from YAML (`Pipeline.loads`) without extra
|
|
256
|
+
settings: the package lives in Haystack's `haystack_integrations` namespace.
|
|
257
|
+
|
|
258
|
+
## Errors
|
|
259
|
+
|
|
260
|
+
A source that cannot be converted (a damaged file, a format that cannot become text or the format
|
|
261
|
+
asked for, a conversion over `max_credits`, a file over the plan's size limit) is skipped and
|
|
262
|
+
logged as a warning with the API's own message, the way Haystack's converters skip unreadable
|
|
263
|
+
files.
|
|
264
|
+
With `raise_on_failure=True` the error is raised instead.
|
|
265
|
+
|
|
266
|
+
Some problems would hit every following source too, so they always stop the run with an error
|
|
267
|
+
from the [Python client](https://pypi.org/project/converter-studio/) carrying the API's message
|
|
268
|
+
and code: no API key (`ValueError` at warm-up), a key that does not work
|
|
269
|
+
(`AuthenticationError`), not enough credits (`InsufficientCreditsError`), a plan that does not
|
|
270
|
+
allow it (`PermissionDeniedError`), too many requests (`RateLimitError`) and an API that cannot be
|
|
271
|
+
reached (`APIConnectionError`). Network errors and busy answers are retried first, and every
|
|
272
|
+
conversion is created with an `Idempotency-Key`, so a retry is never charged twice. Failed
|
|
273
|
+
conversions are not charged. The API key is never logged.
|
|
274
|
+
|
|
275
|
+
## Limits
|
|
276
|
+
|
|
277
|
+
- The largest file depends on the plan; files over 95 MB are uploaded in parts on their own.
|
|
278
|
+
- OCR reads up to 50 pages of a PDF; audio and video length depends on the plan.
|
|
279
|
+
- One file in, one file out: no merging or splitting of PDFs. A result is held in memory as a
|
|
280
|
+
`ByteStream`.
|
|
281
|
+
- Converter Studio keeps results for 6 hours, then deletes them.
|
|
282
|
+
|
|
283
|
+
## Privacy
|
|
284
|
+
|
|
285
|
+
Your files are sent to Converter Studio (converter.studio) to be converted; a link is downloaded
|
|
286
|
+
by Converter Studio. Uploaded files are deleted as soon as they are converted, and results 6 hours
|
|
287
|
+
later. Each request also says which client sent it (`X-Converter-Studio-Client: haystack/<version>`),
|
|
288
|
+
for usage statistics only. See the [privacy policy](https://converter.studio/privacy) and the
|
|
289
|
+
[terms](https://converter.studio/terms).
|
|
290
|
+
|
|
291
|
+
## Links
|
|
292
|
+
|
|
293
|
+
- Converter Studio: <https://converter.studio>
|
|
294
|
+
- API documentation: <https://converter.studio/developers>
|
|
295
|
+
- Pricing: <https://converter.studio/pricing>
|
|
296
|
+
- Python client: <https://pypi.org/project/converter-studio/>
|
|
297
|
+
- Source code and issues: <https://github.com/tariksucu/converter-studio-haystack>
|
|
298
|
+
- Changelog: <https://github.com/tariksucu/converter-studio-haystack/blob/main/CHANGELOG.md>
|
|
299
|
+
- Support: <support@converter.studio>
|
|
300
|
+
|
|
301
|
+
MIT licensed. © Converter Studio.
|
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
# Converter Studio for Haystack
|
|
2
|
+
|
|
3
|
+
[Haystack](https://haystack.deepset.ai) components for [Converter Studio](https://converter.studio),
|
|
4
|
+
a file conversion service with OCR:
|
|
5
|
+
|
|
6
|
+
- **`ConverterStudioDocumentConverter`** turns files into Haystack `Document`s: scanned PDFs and
|
|
7
|
+
photos of pages (by OCR), PDFs with a text layer, Word, OpenDocument, RTF and HTML documents,
|
|
8
|
+
e-books (EPUB, MOBI, AZW3, FB2), spreadsheets and presentations.
|
|
9
|
+
- **`ConverterStudioFileConverter`** converts files to another format and returns them as
|
|
10
|
+
`ByteStream`s: Office files and e-books to PDF, PDFs to Word or JPG, HEIC and camera RAW photos
|
|
11
|
+
to JPG, videos to MP4 or MP3, and the other conversions of Converter Studio's 67 formats.
|
|
12
|
+
|
|
13
|
+
The conversions run on Converter Studio's servers, so there is nothing else to install: no
|
|
14
|
+
Tesseract, no LibreOffice, no Poppler, no FFmpeg.
|
|
15
|
+
|
|
16
|
+
```python
|
|
17
|
+
from haystack_integrations.components.converters.converter_studio import (
|
|
18
|
+
ConverterStudioDocumentConverter,
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
converter = ConverterStudioDocumentConverter() # the key comes from CONVERTER_STUDIO_API_KEY
|
|
22
|
+
documents = converter.run(sources=["scanned-contract.pdf", "minutes.docx"])["documents"]
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Installation
|
|
26
|
+
|
|
27
|
+
```console
|
|
28
|
+
$ pip install converter-studio-haystack
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
Python 3.11 or later, Haystack 3 (`haystack-ai` 3.x; Haystack 2.20 and later work too). The
|
|
32
|
+
package installs the official [Converter Studio Python client](https://pypi.org/project/converter-studio/).
|
|
33
|
+
|
|
34
|
+
Create an API key on your [account page](https://converter.studio/account#api-keys) (keys start
|
|
35
|
+
with `cvs_live_`; keep them secret) and put it in the environment:
|
|
36
|
+
|
|
37
|
+
```console
|
|
38
|
+
$ export CONVERTER_STUDIO_API_KEY=cvs_live_...
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
Both components read the key with Haystack's `Secret` (`Secret.from_env_var("CONVERTER_STUDIO_API_KEY")`
|
|
42
|
+
by default), so it is never written into a serialized pipeline. The key is read when the
|
|
43
|
+
component is warmed up, not when it is created.
|
|
44
|
+
|
|
45
|
+
Every conversion uses your account's credits, the same as on the website: free accounts get
|
|
46
|
+
credits every day, Pro and Premium their monthly credits. Most documents cost 1 credit; reading a
|
|
47
|
+
picture with OCR costs 3, and a scanned PDF's OCR is priced by its number of pages. Failed
|
|
48
|
+
conversions cost nothing. See [pricing](https://converter.studio/pricing).
|
|
49
|
+
|
|
50
|
+
## Files to Documents
|
|
51
|
+
|
|
52
|
+
```python
|
|
53
|
+
from haystack_integrations.components.converters.converter_studio import (
|
|
54
|
+
ConverterStudioDocumentConverter,
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
converter = ConverterStudioDocumentConverter(ocr="auto")
|
|
58
|
+
result = converter.run(
|
|
59
|
+
sources=["invoice-scan.pdf", "handbook.epub", "whiteboard.heic", "deck.pptx"],
|
|
60
|
+
meta={"project": "onboarding"},
|
|
61
|
+
)
|
|
62
|
+
for document in result["documents"]:
|
|
63
|
+
print(document.meta["file_path"], document.meta["converter_studio"], document.content[:60])
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Sources can be file paths, `ByteStream`s, or links (a `str` starting with `http://` or `https://`,
|
|
67
|
+
Google Drive, Dropbox and OneDrive share links included): Converter Studio downloads a link
|
|
68
|
+
itself, so the file never passes through your machine.
|
|
69
|
+
|
|
70
|
+
How each kind of file becomes text:
|
|
71
|
+
|
|
72
|
+
| Source | What happens |
|
|
73
|
+
| --------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------- |
|
|
74
|
+
| PDF | Its text layer (pages separated by `\f`, as Haystack's own PDF converters do); a scan without text is read with OCR (see `ocr`) |
|
|
75
|
+
| JPG, PNG, HEIC, TIFF, WebP, BMP | Read with OCR (the language is found on its own, or set `options={"ocrLanguage": "de"}`) |
|
|
76
|
+
| DOCX, DOC, ODT, RTF, HTML | Plain text |
|
|
77
|
+
| EPUB, MOBI, AZW3, FB2 | Plain text |
|
|
78
|
+
| XLSX, XLS, ODS | CSV text of the first sheet (`options={"sheet": 2}` for another) |
|
|
79
|
+
| PPTX, PPT, ODP, SVG, EPS, AI, camera RAW, GIF, AVIF | Made into a PDF, then its text (two conversions) |
|
|
80
|
+
| TXT, Markdown, CSV, JSON, XML, YAML, SRT, VTT, ASS | Read here, without a request and without credits |
|
|
81
|
+
| Audio, video, archives, fonts | Skipped with a warning (no text to read) |
|
|
82
|
+
|
|
83
|
+
`ocr` says when to use OCR (optical character recognition):
|
|
84
|
+
|
|
85
|
+
- `"auto"` (default): a PDF's own text first; only a PDF without any (a scan) is read again with
|
|
86
|
+
OCR. The first try costs nothing when it finds no text.
|
|
87
|
+
- `"always"`: PDFs go straight to OCR, for folders of scans.
|
|
88
|
+
- `"never"`: no OCR; scanned PDFs and pictures are skipped.
|
|
89
|
+
|
|
90
|
+
OCR reads up to 50 pages of a PDF. Its text keeps the page's lines and table columns, without page
|
|
91
|
+
separators.
|
|
92
|
+
|
|
93
|
+
Each `Document` carries the metadata Haystack's converters carry: a `ByteStream`'s own `meta`,
|
|
94
|
+
`file_path` (the file name, or the full path with `store_full_path=True`), `url` for a link, and
|
|
95
|
+
the `meta` you pass to `run` (a dictionary for all sources, or a list with one per source). It
|
|
96
|
+
also has `converter_studio`: `{"source_format": "pdf", "ocr": true}`, what the file was and
|
|
97
|
+
whether OCR read it.
|
|
98
|
+
|
|
99
|
+
## Files to other formats
|
|
100
|
+
|
|
101
|
+
```python
|
|
102
|
+
from haystack_integrations.components.converters.converter_studio import (
|
|
103
|
+
ConverterStudioFileConverter,
|
|
104
|
+
)
|
|
105
|
+
|
|
106
|
+
converter = ConverterStudioFileConverter(output_file_type="pdf")
|
|
107
|
+
result = converter.run(sources=["slides.pptx", "letter.docx"])
|
|
108
|
+
for stream in result["output"]:
|
|
109
|
+
print(stream.meta["converter_studio"]["file_name"], stream.mime_type, len(stream.data))
|
|
110
|
+
|
|
111
|
+
mp3 = converter.run(sources=["interview.mov"], output_file_type="mp3")["output"]
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
The results are `ByteStream`s with their MIME type, the source's metadata, and
|
|
115
|
+
`meta["converter_studio"]`: `source_format`, `output_format` and `file_name` (the result's name,
|
|
116
|
+
such as `slides.pdf`). They connect to anything that takes `ByteStream`s, for example a legacy
|
|
117
|
+
`.doc` to Haystack's `DOCXToDocument` (which needs `pip install python-docx`):
|
|
118
|
+
|
|
119
|
+
```python
|
|
120
|
+
from haystack import Pipeline
|
|
121
|
+
from haystack.components.converters import DOCXToDocument
|
|
122
|
+
from haystack_integrations.components.converters.converter_studio import (
|
|
123
|
+
ConverterStudioFileConverter,
|
|
124
|
+
)
|
|
125
|
+
|
|
126
|
+
pipeline = Pipeline()
|
|
127
|
+
pipeline.add_component("to_docx", ConverterStudioFileConverter(output_file_type="docx"))
|
|
128
|
+
pipeline.add_component("docx", DOCXToDocument())
|
|
129
|
+
pipeline.connect("to_docx.output", "docx.sources")
|
|
130
|
+
result = pipeline.run({"to_docx": {"sources": ["old-report.doc"]}})
|
|
131
|
+
print(result["docx"]["documents"][0].content[:80])
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
A PDF made into pictures (`jpg`, `png`, `webp`, `tiff`) comes back as one picture per page, in a
|
|
135
|
+
ZIP when it has more than one page. `options` takes the API's conversion settings, such as
|
|
136
|
+
`{"quality": "high"}`, `{"height": "720"}` for a video, or `{"ocr": True}` for a scanned PDF to
|
|
137
|
+
Word; the full list is on the [API page](https://converter.studio/developers).
|
|
138
|
+
|
|
139
|
+
## Example: index scanned PDFs with OCR
|
|
140
|
+
|
|
141
|
+
This pipeline reads every PDF in `scans/` (scanned or not), splits the text and writes it to an
|
|
142
|
+
in-memory document store, then finds passages with BM25. It needs only a Converter Studio key.
|
|
143
|
+
|
|
144
|
+
```python
|
|
145
|
+
from pathlib import Path
|
|
146
|
+
|
|
147
|
+
from haystack import Pipeline
|
|
148
|
+
from haystack.components.preprocessors import DocumentSplitter
|
|
149
|
+
from haystack.components.retrievers.in_memory import InMemoryBM25Retriever
|
|
150
|
+
from haystack.components.writers import DocumentWriter
|
|
151
|
+
from haystack.document_stores.in_memory import InMemoryDocumentStore
|
|
152
|
+
from haystack_integrations.components.converters.converter_studio import (
|
|
153
|
+
ConverterStudioDocumentConverter,
|
|
154
|
+
)
|
|
155
|
+
|
|
156
|
+
store = InMemoryDocumentStore()
|
|
157
|
+
indexing = Pipeline()
|
|
158
|
+
indexing.add_component("converter", ConverterStudioDocumentConverter(ocr="auto"))
|
|
159
|
+
indexing.add_component("splitter", DocumentSplitter(split_by="word", split_length=150))
|
|
160
|
+
indexing.add_component("writer", DocumentWriter(document_store=store))
|
|
161
|
+
indexing.connect("converter", "splitter")
|
|
162
|
+
indexing.connect("splitter", "writer")
|
|
163
|
+
|
|
164
|
+
indexing.run({"converter": {"sources": sorted(Path("scans").glob("*.pdf"))}})
|
|
165
|
+
print(store.count_documents(), "passages")
|
|
166
|
+
|
|
167
|
+
retriever = InMemoryBM25Retriever(document_store=store)
|
|
168
|
+
for document in retriever.run(query="invoice total", top_k=3)["documents"]:
|
|
169
|
+
print(
|
|
170
|
+
document.meta["file_path"], document.meta["converter_studio"]["ocr"], document.content[:80]
|
|
171
|
+
)
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
## Async pipelines
|
|
175
|
+
|
|
176
|
+
Both components have `run_async`, which converts `concurrency_limit` sources at a time (4 by
|
|
177
|
+
default), and `warm_up_async` / `close_async` for their HTTP clients:
|
|
178
|
+
|
|
179
|
+
```python
|
|
180
|
+
import asyncio
|
|
181
|
+
|
|
182
|
+
from haystack import Pipeline
|
|
183
|
+
from haystack_integrations.components.converters.converter_studio import (
|
|
184
|
+
ConverterStudioDocumentConverter,
|
|
185
|
+
)
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
async def main() -> None:
|
|
189
|
+
pipeline = Pipeline()
|
|
190
|
+
pipeline.add_component("converter", ConverterStudioDocumentConverter(concurrency_limit=8))
|
|
191
|
+
result = await pipeline.run_async({"converter": {"sources": ["a.pdf", "b.docx", "c.png"]}})
|
|
192
|
+
print(len(result["converter"]["documents"]))
|
|
193
|
+
await pipeline.close_async()
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
asyncio.run(main())
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
(On Haystack 2, use `AsyncPipeline`.)
|
|
200
|
+
|
|
201
|
+
## Settings
|
|
202
|
+
|
|
203
|
+
Both components take:
|
|
204
|
+
|
|
205
|
+
| Parameter | Default | What it does |
|
|
206
|
+
| ------------------- | ------------------------------------------------- | ------------------------------------------------------------------------------------ |
|
|
207
|
+
| `api_key` | `Secret.from_env_var("CONVERTER_STUDIO_API_KEY")` | the API key |
|
|
208
|
+
| `base_url` | `None` | the API's address (`CONVERTER_STUDIO_BASE_URL` or `https://converter.studio/api`) |
|
|
209
|
+
| `options` | `None` | conversion settings sent with every conversion (ignored where they do not apply) |
|
|
210
|
+
| `max_credits` | `None` (the API's 20) | the most one conversion may cost and start on its own; a dearer one is skipped, free |
|
|
211
|
+
| `timeout` | `600.0` | seconds to wait for each conversion |
|
|
212
|
+
| `raise_on_failure` | `False` | raise instead of skipping a source that cannot be converted |
|
|
213
|
+
| `concurrency_limit` | `4` | sources converted at the same time by `run_async` |
|
|
214
|
+
|
|
215
|
+
`ConverterStudioDocumentConverter` also takes `ocr` (`"auto"`, `"always"`, `"never"`) and
|
|
216
|
+
`store_full_path`; `ConverterStudioFileConverter` takes `output_file_type`, which `run` can
|
|
217
|
+
override.
|
|
218
|
+
|
|
219
|
+
Both serialize with `to_dict` / `from_dict` and load from YAML (`Pipeline.loads`) without extra
|
|
220
|
+
settings: the package lives in Haystack's `haystack_integrations` namespace.
|
|
221
|
+
|
|
222
|
+
## Errors
|
|
223
|
+
|
|
224
|
+
A source that cannot be converted (a damaged file, a format that cannot become text or the format
|
|
225
|
+
asked for, a conversion over `max_credits`, a file over the plan's size limit) is skipped and
|
|
226
|
+
logged as a warning with the API's own message, the way Haystack's converters skip unreadable
|
|
227
|
+
files.
|
|
228
|
+
With `raise_on_failure=True` the error is raised instead.
|
|
229
|
+
|
|
230
|
+
Some problems would hit every following source too, so they always stop the run with an error
|
|
231
|
+
from the [Python client](https://pypi.org/project/converter-studio/) carrying the API's message
|
|
232
|
+
and code: no API key (`ValueError` at warm-up), a key that does not work
|
|
233
|
+
(`AuthenticationError`), not enough credits (`InsufficientCreditsError`), a plan that does not
|
|
234
|
+
allow it (`PermissionDeniedError`), too many requests (`RateLimitError`) and an API that cannot be
|
|
235
|
+
reached (`APIConnectionError`). Network errors and busy answers are retried first, and every
|
|
236
|
+
conversion is created with an `Idempotency-Key`, so a retry is never charged twice. Failed
|
|
237
|
+
conversions are not charged. The API key is never logged.
|
|
238
|
+
|
|
239
|
+
## Limits
|
|
240
|
+
|
|
241
|
+
- The largest file depends on the plan; files over 95 MB are uploaded in parts on their own.
|
|
242
|
+
- OCR reads up to 50 pages of a PDF; audio and video length depends on the plan.
|
|
243
|
+
- One file in, one file out: no merging or splitting of PDFs. A result is held in memory as a
|
|
244
|
+
`ByteStream`.
|
|
245
|
+
- Converter Studio keeps results for 6 hours, then deletes them.
|
|
246
|
+
|
|
247
|
+
## Privacy
|
|
248
|
+
|
|
249
|
+
Your files are sent to Converter Studio (converter.studio) to be converted; a link is downloaded
|
|
250
|
+
by Converter Studio. Uploaded files are deleted as soon as they are converted, and results 6 hours
|
|
251
|
+
later. Each request also says which client sent it (`X-Converter-Studio-Client: haystack/<version>`),
|
|
252
|
+
for usage statistics only. See the [privacy policy](https://converter.studio/privacy) and the
|
|
253
|
+
[terms](https://converter.studio/terms).
|
|
254
|
+
|
|
255
|
+
## Links
|
|
256
|
+
|
|
257
|
+
- Converter Studio: <https://converter.studio>
|
|
258
|
+
- API documentation: <https://converter.studio/developers>
|
|
259
|
+
- Pricing: <https://converter.studio/pricing>
|
|
260
|
+
- Python client: <https://pypi.org/project/converter-studio/>
|
|
261
|
+
- Source code and issues: <https://github.com/tariksucu/converter-studio-haystack>
|
|
262
|
+
- Changelog: <https://github.com/tariksucu/converter-studio-haystack/blob/main/CHANGELOG.md>
|
|
263
|
+
- Support: <support@converter.studio>
|
|
264
|
+
|
|
265
|
+
MIT licensed. © Converter Studio.
|