attachments 0.2.1__tar.gz → 0.2.2__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.
- attachments-0.2.2/PKG-INFO +132 -0
- attachments-0.2.2/README.md +93 -0
- {attachments-0.2.1 → attachments-0.2.2}/pyproject.toml +1 -1
- attachments-0.2.2/src/attachments.egg-info/PKG-INFO +132 -0
- attachments-0.2.1/PKG-INFO +0 -284
- attachments-0.2.1/README.md +0 -245
- attachments-0.2.1/src/attachments.egg-info/PKG-INFO +0 -284
- {attachments-0.2.1 → attachments-0.2.2}/LICENSE +0 -0
- {attachments-0.2.1 → attachments-0.2.2}/setup.cfg +0 -0
- {attachments-0.2.1 → attachments-0.2.2}/src/attachments/__init__.py +0 -0
- {attachments-0.2.1 → attachments-0.2.2}/src/attachments/audio_processing.py +0 -0
- {attachments-0.2.1 → attachments-0.2.2}/src/attachments/config.py +0 -0
- {attachments-0.2.1 → attachments-0.2.2}/src/attachments/core.py +0 -0
- {attachments-0.2.1 → attachments-0.2.2}/src/attachments/detectors.py +0 -0
- {attachments-0.2.1 → attachments-0.2.2}/src/attachments/exceptions.py +0 -0
- {attachments-0.2.1 → attachments-0.2.2}/src/attachments/image_processing.py +0 -0
- {attachments-0.2.1 → attachments-0.2.2}/src/attachments/office_contact_sheet.py +0 -0
- {attachments-0.2.1 → attachments-0.2.2}/src/attachments/parsers.py +0 -0
- {attachments-0.2.1 → attachments-0.2.2}/src/attachments/renderers.py +0 -0
- {attachments-0.2.1 → attachments-0.2.2}/src/attachments/utils.py +0 -0
- {attachments-0.2.1 → attachments-0.2.2}/src/attachments.egg-info/SOURCES.txt +0 -0
- {attachments-0.2.1 → attachments-0.2.2}/src/attachments.egg-info/dependency_links.txt +0 -0
- {attachments-0.2.1 → attachments-0.2.2}/src/attachments.egg-info/requires.txt +0 -0
- {attachments-0.2.1 → attachments-0.2.2}/src/attachments.egg-info/top_level.txt +0 -0
- {attachments-0.2.1 → attachments-0.2.2}/tests/test_audio_processing.py +0 -0
- {attachments-0.2.1 → attachments-0.2.2}/tests/test_base.py +0 -0
- {attachments-0.2.1 → attachments-0.2.2}/tests/test_core_functionality.py +0 -0
- {attachments-0.2.1 → attachments-0.2.2}/tests/test_document_parsing.py +0 -0
- {attachments-0.2.1 → attachments-0.2.2}/tests/test_html_parsing.py +0 -0
- {attachments-0.2.1 → attachments-0.2.2}/tests/test_image_processing.py +0 -0
- {attachments-0.2.1 → attachments-0.2.2}/tests/test_indexing_slicing.py +0 -0
- {attachments-0.2.1 → attachments-0.2.2}/tests/test_markdown_rendering.py +0 -0
- {attachments-0.2.1 → attachments-0.2.2}/tests/test_office_contact_sheet.py +0 -0
- {attachments-0.2.1 → attachments-0.2.2}/tests/test_pdf_parsing.py +0 -0
- {attachments-0.2.1 → attachments-0.2.2}/tests/test_pptx_parsing.py +0 -0
- {attachments-0.2.1 → attachments-0.2.2}/tests/test_simple.py +0 -0
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: attachments
|
|
3
|
+
Version: 0.2.2
|
|
4
|
+
Summary: A Python library to handle various file types for LLMs.
|
|
5
|
+
Author-email: Maxim Rivest <mrive052@gmail.com>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/yourusername/attachments
|
|
8
|
+
Project-URL: Bug Tracker, https://github.com/yourusername/attachments/issues
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Requires-Python: >=3.10
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
Requires-Dist: Pillow
|
|
15
|
+
Requires-Dist: python-dotenv
|
|
16
|
+
Requires-Dist: pytest
|
|
17
|
+
Requires-Dist: beautifulsoup4
|
|
18
|
+
Requires-Dist: html2text
|
|
19
|
+
Requires-Dist: markdownify
|
|
20
|
+
Requires-Dist: lxml
|
|
21
|
+
Requires-Dist: python-magic
|
|
22
|
+
Requires-Dist: mammoth
|
|
23
|
+
Requires-Dist: PyMuPDF
|
|
24
|
+
Requires-Dist: python-pptx
|
|
25
|
+
Requires-Dist: python-docx
|
|
26
|
+
Requires-Dist: odfpy
|
|
27
|
+
Requires-Dist: pillow-heif
|
|
28
|
+
Requires-Dist: pydub
|
|
29
|
+
Requires-Dist: SpeechRecognition
|
|
30
|
+
Requires-Dist: markitdown[all]>=0.1.1
|
|
31
|
+
Requires-Dist: pypdfium2
|
|
32
|
+
Provides-Extra: dev
|
|
33
|
+
Requires-Dist: pytest; extra == "dev"
|
|
34
|
+
Requires-Dist: flake8; extra == "dev"
|
|
35
|
+
Requires-Dist: mypy; extra == "dev"
|
|
36
|
+
Requires-Dist: black; extra == "dev"
|
|
37
|
+
Requires-Dist: filelock; extra == "dev"
|
|
38
|
+
Dynamic: license-file
|
|
39
|
+
|
|
40
|
+
# Attachments
|
|
41
|
+
## Easiest way to give context to LLMs
|
|
42
|
+
|
|
43
|
+
Attachments has the ambition to be the general funnel for any files to be transformed into images+text for large language models context. For this to be true we, has a community, have to stop implementing this funnel (file -> images+text) each on our own. If you have a detector + parser + renderer contibute it! This will help you and us. Don't let your code alone in a dark corner of you computer.
|
|
44
|
+
|
|
45
|
+
## Very quickstart
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
> pip install attachments
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Most users will not have to learn anything more then that: `Attachments("path/to/file.pdf")`
|
|
52
|
+
|
|
53
|
+
```python
|
|
54
|
+
from attachments import Attachments
|
|
55
|
+
|
|
56
|
+
a = Attachment("/the/path/or/url/to/your/to/the/content/you/want/to/give/the/llm.xlsx", "another.pdf", "another.pptx"...)
|
|
57
|
+
|
|
58
|
+
prompt_ready_context = str(a)
|
|
59
|
+
images_ready_for_llm = a.images
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
That is really just it!
|
|
63
|
+
|
|
64
|
+
You can print it `print(a)`, you can interpolate it `f"the content is {a}"` you can string it `str(a)`. This will give you something very good to give the llm so that the AI can consider you content.
|
|
65
|
+
|
|
66
|
+
Nowadays, most genAI models comes with the ability to see images too. So you also have all attachments in images forms by using `a.images`.
|
|
67
|
+
This is a list of base64 encoded images, this is the fundamental format the most llm provider accept.
|
|
68
|
+
|
|
69
|
+
The simplest way to use and think about attachments is that if you want to put you best foot forward and up you chances that the llm grok your content you should pass all of the text in `a` to the prompt and all of the images in `a.images` in the image input. We aim for making those two as 'prompt engineered' as possible. *Attachments* is young but already very powerful and used in production. The api will not change. Maybe advanced feature and syntax will be added but the core will stay the same. Mostly we will support more file types and we will have better rendering for better performance and with less extreneous tokens.
|
|
70
|
+
|
|
71
|
+
## How to give attachments to openai llms?
|
|
72
|
+
|
|
73
|
+
```python
|
|
74
|
+
from openai import OpenAI
|
|
75
|
+
from attachments import Attachments
|
|
76
|
+
|
|
77
|
+
pdf_attachment = Attachments("https://github.com/microsoft/markitdown/raw/refs/heads/main/packages/markitdown/tests/test_files/test.pdf")
|
|
78
|
+
|
|
79
|
+
prompt = f"""
|
|
80
|
+
Analyze the following documents:
|
|
81
|
+
{pdf_attachment}
|
|
82
|
+
"""
|
|
83
|
+
|
|
84
|
+
content = [{"type": "input_image","image_url": image} for image in pdf_attachment.images] + \
|
|
85
|
+
[{"type": "input_text", "text": prompt}]
|
|
86
|
+
|
|
87
|
+
response = OpenAI().responses.create(model="gpt-4.1-nano", input=[{"role": "user", "content": content}])
|
|
88
|
+
response.output_text
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## Usage
|
|
92
|
+
|
|
93
|
+
### Basic Initialization
|
|
94
|
+
Create an `Attachments` object by passing one or more local file paths or URLs. Image processing commands can be appended to image paths.
|
|
95
|
+
|
|
96
|
+
```python
|
|
97
|
+
from attachments import Attachments
|
|
98
|
+
|
|
99
|
+
# Initialize with various local files, URLs, and image processing commands
|
|
100
|
+
a = Attachments(
|
|
101
|
+
"docs/report.pdf",
|
|
102
|
+
"images/diagram.png[resize:400xauto]",
|
|
103
|
+
"https://www.example.com/article.html",
|
|
104
|
+
"photos/vacation.heic[rotate:90,format:jpeg,quality:80]"
|
|
105
|
+
)
|
|
106
|
+
|
|
107
|
+
# The library will download URLs, process files, and extract content.
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
### Indexing Attachments
|
|
111
|
+
You can get a new `Attachments` object containing a subset of the original attachments using integer or slice indexing:
|
|
112
|
+
```python
|
|
113
|
+
first_attachment = a[0]
|
|
114
|
+
first_two_attachments = a[0:2]
|
|
115
|
+
|
|
116
|
+
print(f"Selected attachment: {first_attachment}")
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
### Page/Slide Selection
|
|
120
|
+
Specify pages for PDFs or slides for PPTX files using bracket notation in the path string:
|
|
121
|
+
```python
|
|
122
|
+
# Process only page 1 and pages 3 through 5 of a PDF
|
|
123
|
+
specific_pages_pdf = Attachments("long_document.pdf[1,3-5]")
|
|
124
|
+
|
|
125
|
+
# Process the first three slides and the last slide of a presentation
|
|
126
|
+
specific_slides_pptx = Attachments("presentation.pptx[:3,N]")
|
|
127
|
+
# 'N' refers to the last page/slide. Negative indexing like [-1:] also works.
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
## Supported File Types
|
|
131
|
+
* **Documents**: PDF (`.pdf`), PowerPoint (`.pptx`), Excel (`.xlsx`), Word (`.docx`), much more to come!
|
|
132
|
+
* **Images**: JPEG (`.jpg`, `.jpeg`), PNG (`.png`), GIF (`.gif`), BMP (`.bmp`), WEBP (`.webp`), TIFF (`.tiff`), HEIC (`.heic`), HEIF (`.heif`)
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
# Attachments
|
|
2
|
+
## Easiest way to give context to LLMs
|
|
3
|
+
|
|
4
|
+
Attachments has the ambition to be the general funnel for any files to be transformed into images+text for large language models context. For this to be true we, has a community, have to stop implementing this funnel (file -> images+text) each on our own. If you have a detector + parser + renderer contibute it! This will help you and us. Don't let your code alone in a dark corner of you computer.
|
|
5
|
+
|
|
6
|
+
## Very quickstart
|
|
7
|
+
|
|
8
|
+
```bash
|
|
9
|
+
> pip install attachments
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
Most users will not have to learn anything more then that: `Attachments("path/to/file.pdf")`
|
|
13
|
+
|
|
14
|
+
```python
|
|
15
|
+
from attachments import Attachments
|
|
16
|
+
|
|
17
|
+
a = Attachment("/the/path/or/url/to/your/to/the/content/you/want/to/give/the/llm.xlsx", "another.pdf", "another.pptx"...)
|
|
18
|
+
|
|
19
|
+
prompt_ready_context = str(a)
|
|
20
|
+
images_ready_for_llm = a.images
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
That is really just it!
|
|
24
|
+
|
|
25
|
+
You can print it `print(a)`, you can interpolate it `f"the content is {a}"` you can string it `str(a)`. This will give you something very good to give the llm so that the AI can consider you content.
|
|
26
|
+
|
|
27
|
+
Nowadays, most genAI models comes with the ability to see images too. So you also have all attachments in images forms by using `a.images`.
|
|
28
|
+
This is a list of base64 encoded images, this is the fundamental format the most llm provider accept.
|
|
29
|
+
|
|
30
|
+
The simplest way to use and think about attachments is that if you want to put you best foot forward and up you chances that the llm grok your content you should pass all of the text in `a` to the prompt and all of the images in `a.images` in the image input. We aim for making those two as 'prompt engineered' as possible. *Attachments* is young but already very powerful and used in production. The api will not change. Maybe advanced feature and syntax will be added but the core will stay the same. Mostly we will support more file types and we will have better rendering for better performance and with less extreneous tokens.
|
|
31
|
+
|
|
32
|
+
## How to give attachments to openai llms?
|
|
33
|
+
|
|
34
|
+
```python
|
|
35
|
+
from openai import OpenAI
|
|
36
|
+
from attachments import Attachments
|
|
37
|
+
|
|
38
|
+
pdf_attachment = Attachments("https://github.com/microsoft/markitdown/raw/refs/heads/main/packages/markitdown/tests/test_files/test.pdf")
|
|
39
|
+
|
|
40
|
+
prompt = f"""
|
|
41
|
+
Analyze the following documents:
|
|
42
|
+
{pdf_attachment}
|
|
43
|
+
"""
|
|
44
|
+
|
|
45
|
+
content = [{"type": "input_image","image_url": image} for image in pdf_attachment.images] + \
|
|
46
|
+
[{"type": "input_text", "text": prompt}]
|
|
47
|
+
|
|
48
|
+
response = OpenAI().responses.create(model="gpt-4.1-nano", input=[{"role": "user", "content": content}])
|
|
49
|
+
response.output_text
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## Usage
|
|
53
|
+
|
|
54
|
+
### Basic Initialization
|
|
55
|
+
Create an `Attachments` object by passing one or more local file paths or URLs. Image processing commands can be appended to image paths.
|
|
56
|
+
|
|
57
|
+
```python
|
|
58
|
+
from attachments import Attachments
|
|
59
|
+
|
|
60
|
+
# Initialize with various local files, URLs, and image processing commands
|
|
61
|
+
a = Attachments(
|
|
62
|
+
"docs/report.pdf",
|
|
63
|
+
"images/diagram.png[resize:400xauto]",
|
|
64
|
+
"https://www.example.com/article.html",
|
|
65
|
+
"photos/vacation.heic[rotate:90,format:jpeg,quality:80]"
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
# The library will download URLs, process files, and extract content.
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### Indexing Attachments
|
|
72
|
+
You can get a new `Attachments` object containing a subset of the original attachments using integer or slice indexing:
|
|
73
|
+
```python
|
|
74
|
+
first_attachment = a[0]
|
|
75
|
+
first_two_attachments = a[0:2]
|
|
76
|
+
|
|
77
|
+
print(f"Selected attachment: {first_attachment}")
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### Page/Slide Selection
|
|
81
|
+
Specify pages for PDFs or slides for PPTX files using bracket notation in the path string:
|
|
82
|
+
```python
|
|
83
|
+
# Process only page 1 and pages 3 through 5 of a PDF
|
|
84
|
+
specific_pages_pdf = Attachments("long_document.pdf[1,3-5]")
|
|
85
|
+
|
|
86
|
+
# Process the first three slides and the last slide of a presentation
|
|
87
|
+
specific_slides_pptx = Attachments("presentation.pptx[:3,N]")
|
|
88
|
+
# 'N' refers to the last page/slide. Negative indexing like [-1:] also works.
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## Supported File Types
|
|
92
|
+
* **Documents**: PDF (`.pdf`), PowerPoint (`.pptx`), Excel (`.xlsx`), Word (`.docx`), much more to come!
|
|
93
|
+
* **Images**: JPEG (`.jpg`, `.jpeg`), PNG (`.png`), GIF (`.gif`), BMP (`.bmp`), WEBP (`.webp`), TIFF (`.tiff`), HEIC (`.heic`), HEIF (`.heif`)
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: attachments
|
|
3
|
+
Version: 0.2.2
|
|
4
|
+
Summary: A Python library to handle various file types for LLMs.
|
|
5
|
+
Author-email: Maxim Rivest <mrive052@gmail.com>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/yourusername/attachments
|
|
8
|
+
Project-URL: Bug Tracker, https://github.com/yourusername/attachments/issues
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Requires-Python: >=3.10
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
Requires-Dist: Pillow
|
|
15
|
+
Requires-Dist: python-dotenv
|
|
16
|
+
Requires-Dist: pytest
|
|
17
|
+
Requires-Dist: beautifulsoup4
|
|
18
|
+
Requires-Dist: html2text
|
|
19
|
+
Requires-Dist: markdownify
|
|
20
|
+
Requires-Dist: lxml
|
|
21
|
+
Requires-Dist: python-magic
|
|
22
|
+
Requires-Dist: mammoth
|
|
23
|
+
Requires-Dist: PyMuPDF
|
|
24
|
+
Requires-Dist: python-pptx
|
|
25
|
+
Requires-Dist: python-docx
|
|
26
|
+
Requires-Dist: odfpy
|
|
27
|
+
Requires-Dist: pillow-heif
|
|
28
|
+
Requires-Dist: pydub
|
|
29
|
+
Requires-Dist: SpeechRecognition
|
|
30
|
+
Requires-Dist: markitdown[all]>=0.1.1
|
|
31
|
+
Requires-Dist: pypdfium2
|
|
32
|
+
Provides-Extra: dev
|
|
33
|
+
Requires-Dist: pytest; extra == "dev"
|
|
34
|
+
Requires-Dist: flake8; extra == "dev"
|
|
35
|
+
Requires-Dist: mypy; extra == "dev"
|
|
36
|
+
Requires-Dist: black; extra == "dev"
|
|
37
|
+
Requires-Dist: filelock; extra == "dev"
|
|
38
|
+
Dynamic: license-file
|
|
39
|
+
|
|
40
|
+
# Attachments
|
|
41
|
+
## Easiest way to give context to LLMs
|
|
42
|
+
|
|
43
|
+
Attachments has the ambition to be the general funnel for any files to be transformed into images+text for large language models context. For this to be true we, has a community, have to stop implementing this funnel (file -> images+text) each on our own. If you have a detector + parser + renderer contibute it! This will help you and us. Don't let your code alone in a dark corner of you computer.
|
|
44
|
+
|
|
45
|
+
## Very quickstart
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
> pip install attachments
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Most users will not have to learn anything more then that: `Attachments("path/to/file.pdf")`
|
|
52
|
+
|
|
53
|
+
```python
|
|
54
|
+
from attachments import Attachments
|
|
55
|
+
|
|
56
|
+
a = Attachment("/the/path/or/url/to/your/to/the/content/you/want/to/give/the/llm.xlsx", "another.pdf", "another.pptx"...)
|
|
57
|
+
|
|
58
|
+
prompt_ready_context = str(a)
|
|
59
|
+
images_ready_for_llm = a.images
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
That is really just it!
|
|
63
|
+
|
|
64
|
+
You can print it `print(a)`, you can interpolate it `f"the content is {a}"` you can string it `str(a)`. This will give you something very good to give the llm so that the AI can consider you content.
|
|
65
|
+
|
|
66
|
+
Nowadays, most genAI models comes with the ability to see images too. So you also have all attachments in images forms by using `a.images`.
|
|
67
|
+
This is a list of base64 encoded images, this is the fundamental format the most llm provider accept.
|
|
68
|
+
|
|
69
|
+
The simplest way to use and think about attachments is that if you want to put you best foot forward and up you chances that the llm grok your content you should pass all of the text in `a` to the prompt and all of the images in `a.images` in the image input. We aim for making those two as 'prompt engineered' as possible. *Attachments* is young but already very powerful and used in production. The api will not change. Maybe advanced feature and syntax will be added but the core will stay the same. Mostly we will support more file types and we will have better rendering for better performance and with less extreneous tokens.
|
|
70
|
+
|
|
71
|
+
## How to give attachments to openai llms?
|
|
72
|
+
|
|
73
|
+
```python
|
|
74
|
+
from openai import OpenAI
|
|
75
|
+
from attachments import Attachments
|
|
76
|
+
|
|
77
|
+
pdf_attachment = Attachments("https://github.com/microsoft/markitdown/raw/refs/heads/main/packages/markitdown/tests/test_files/test.pdf")
|
|
78
|
+
|
|
79
|
+
prompt = f"""
|
|
80
|
+
Analyze the following documents:
|
|
81
|
+
{pdf_attachment}
|
|
82
|
+
"""
|
|
83
|
+
|
|
84
|
+
content = [{"type": "input_image","image_url": image} for image in pdf_attachment.images] + \
|
|
85
|
+
[{"type": "input_text", "text": prompt}]
|
|
86
|
+
|
|
87
|
+
response = OpenAI().responses.create(model="gpt-4.1-nano", input=[{"role": "user", "content": content}])
|
|
88
|
+
response.output_text
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## Usage
|
|
92
|
+
|
|
93
|
+
### Basic Initialization
|
|
94
|
+
Create an `Attachments` object by passing one or more local file paths or URLs. Image processing commands can be appended to image paths.
|
|
95
|
+
|
|
96
|
+
```python
|
|
97
|
+
from attachments import Attachments
|
|
98
|
+
|
|
99
|
+
# Initialize with various local files, URLs, and image processing commands
|
|
100
|
+
a = Attachments(
|
|
101
|
+
"docs/report.pdf",
|
|
102
|
+
"images/diagram.png[resize:400xauto]",
|
|
103
|
+
"https://www.example.com/article.html",
|
|
104
|
+
"photos/vacation.heic[rotate:90,format:jpeg,quality:80]"
|
|
105
|
+
)
|
|
106
|
+
|
|
107
|
+
# The library will download URLs, process files, and extract content.
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
### Indexing Attachments
|
|
111
|
+
You can get a new `Attachments` object containing a subset of the original attachments using integer or slice indexing:
|
|
112
|
+
```python
|
|
113
|
+
first_attachment = a[0]
|
|
114
|
+
first_two_attachments = a[0:2]
|
|
115
|
+
|
|
116
|
+
print(f"Selected attachment: {first_attachment}")
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
### Page/Slide Selection
|
|
120
|
+
Specify pages for PDFs or slides for PPTX files using bracket notation in the path string:
|
|
121
|
+
```python
|
|
122
|
+
# Process only page 1 and pages 3 through 5 of a PDF
|
|
123
|
+
specific_pages_pdf = Attachments("long_document.pdf[1,3-5]")
|
|
124
|
+
|
|
125
|
+
# Process the first three slides and the last slide of a presentation
|
|
126
|
+
specific_slides_pptx = Attachments("presentation.pptx[:3,N]")
|
|
127
|
+
# 'N' refers to the last page/slide. Negative indexing like [-1:] also works.
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
## Supported File Types
|
|
131
|
+
* **Documents**: PDF (`.pdf`), PowerPoint (`.pptx`), Excel (`.xlsx`), Word (`.docx`), much more to come!
|
|
132
|
+
* **Images**: JPEG (`.jpg`, `.jpeg`), PNG (`.png`), GIF (`.gif`), BMP (`.bmp`), WEBP (`.webp`), TIFF (`.tiff`), HEIC (`.heic`), HEIF (`.heif`)
|
attachments-0.2.1/PKG-INFO
DELETED
|
@@ -1,284 +0,0 @@
|
|
|
1
|
-
Metadata-Version: 2.4
|
|
2
|
-
Name: attachments
|
|
3
|
-
Version: 0.2.1
|
|
4
|
-
Summary: A Python library to handle various file types for LLMs.
|
|
5
|
-
Author-email: Maxim Rivest <mrive052@gmail.com>
|
|
6
|
-
License-Expression: MIT
|
|
7
|
-
Project-URL: Homepage, https://github.com/yourusername/attachments
|
|
8
|
-
Project-URL: Bug Tracker, https://github.com/yourusername/attachments/issues
|
|
9
|
-
Classifier: Programming Language :: Python :: 3
|
|
10
|
-
Classifier: Operating System :: OS Independent
|
|
11
|
-
Requires-Python: >=3.10
|
|
12
|
-
Description-Content-Type: text/markdown
|
|
13
|
-
License-File: LICENSE
|
|
14
|
-
Requires-Dist: Pillow
|
|
15
|
-
Requires-Dist: python-dotenv
|
|
16
|
-
Requires-Dist: pytest
|
|
17
|
-
Requires-Dist: beautifulsoup4
|
|
18
|
-
Requires-Dist: html2text
|
|
19
|
-
Requires-Dist: markdownify
|
|
20
|
-
Requires-Dist: lxml
|
|
21
|
-
Requires-Dist: python-magic
|
|
22
|
-
Requires-Dist: mammoth
|
|
23
|
-
Requires-Dist: PyMuPDF
|
|
24
|
-
Requires-Dist: python-pptx
|
|
25
|
-
Requires-Dist: python-docx
|
|
26
|
-
Requires-Dist: odfpy
|
|
27
|
-
Requires-Dist: pillow-heif
|
|
28
|
-
Requires-Dist: pydub
|
|
29
|
-
Requires-Dist: SpeechRecognition
|
|
30
|
-
Requires-Dist: markitdown[all]>=0.1.1
|
|
31
|
-
Requires-Dist: pypdfium2
|
|
32
|
-
Provides-Extra: dev
|
|
33
|
-
Requires-Dist: pytest; extra == "dev"
|
|
34
|
-
Requires-Dist: flake8; extra == "dev"
|
|
35
|
-
Requires-Dist: mypy; extra == "dev"
|
|
36
|
-
Requires-Dist: black; extra == "dev"
|
|
37
|
-
Requires-Dist: filelock; extra == "dev"
|
|
38
|
-
Dynamic: license-file
|
|
39
|
-
|
|
40
|
-
# Attachments
|
|
41
|
-
## Easiest way to give context to LLMs
|
|
42
|
-
|
|
43
|
-
Attachments has the ambition to be the general funnel for any files to be transformed into images+text for large language models context. For this to be true we, has a community, have to stop implementing this funnel (file -> images+text) each on our own. If you have a detector + parser + renderer contibute it! This will help you and us. Don't let your code alone in a dark corner of you computer.
|
|
44
|
-
|
|
45
|
-
## Very quickstart
|
|
46
|
-
|
|
47
|
-
```python
|
|
48
|
-
from attachments import Attachments
|
|
49
|
-
|
|
50
|
-
a = Attachment("/the/path/or/url/to/your/to/the/content/you/want/to/give/the/llm.xlsx", "another.pdf", "another.pptx"...)
|
|
51
|
-
|
|
52
|
-
prompt_ready_context = str(a)
|
|
53
|
-
images_ready_for_llm = a.images
|
|
54
|
-
```
|
|
55
|
-
|
|
56
|
-
That is really just it!
|
|
57
|
-
|
|
58
|
-
You can print it `print(a)`, you can interpolate it `f"the content is {a}"` you can string it `str(a)`. This will give you something very good to give the llm so that the AI can consider you content.
|
|
59
|
-
|
|
60
|
-
Nowadays, most genAI models comes with the ability to see images too. So you also have all attachments in images forms by using `a.images`.
|
|
61
|
-
This is a list of base64 encoded images, this is the fundamental format the most llm provider accept.
|
|
62
|
-
|
|
63
|
-
The simplest way to use and think about attachments is that if you want to put you best foot forward and up you chances that the llm grok your content you should pass all of the text in `a` to the prompt and all of the images in `a.images` in the image input. We aim for making those two as 'prompt engineered' as possible. *Attachments* is young but already very powerful and used in production. The api will not change. Maybe advanced feature and syntax will be added but the core will stay the same. Mostly we will support more file types and we will have better rendering for better performance and with less extreneous tokens.
|
|
64
|
-
|
|
65
|
-
## How to give attachments to openai llms?
|
|
66
|
-
|
|
67
|
-
```python
|
|
68
|
-
from openai import OpenAI
|
|
69
|
-
from attachments import Attachments
|
|
70
|
-
|
|
71
|
-
pdf_attachment = Attachments("https://github.com/microsoft/markitdown/raw/refs/heads/main/packages/markitdown/tests/test_files/test.pdf")
|
|
72
|
-
|
|
73
|
-
prompt = f"""
|
|
74
|
-
Analyze the following documents:
|
|
75
|
-
{pdf_attachment}
|
|
76
|
-
"""
|
|
77
|
-
|
|
78
|
-
content = [{"type": "input_image","image_url": image} for image in pdf_attachment.images] + \
|
|
79
|
-
[{"type": "input_text", "text": prompt}]
|
|
80
|
-
|
|
81
|
-
response = OpenAI().responses.create(model="gpt-4.1-nano", input=[{"role": "user", "content": content}])
|
|
82
|
-
response.output_text
|
|
83
|
-
```
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
A Python library designed to seamlessly handle various file types (local or URLs), process them, and present them in formats suitable for Large Language Models (LLMs).
|
|
90
|
-
|
|
91
|
-
It is meant to be very minimal.
|
|
92
|
-
|
|
93
|
-
most user will not have to learn anything more then that: `Attachments("path/to/file.pdf")`
|
|
94
|
-
|
|
95
|
-
```bash
|
|
96
|
-
> pip install attachments
|
|
97
|
-
```
|
|
98
|
-
|
|
99
|
-
```python
|
|
100
|
-
from attachments import Attachments
|
|
101
|
-
|
|
102
|
-
a = Attachments("https://github.com/microsoft/markitdown/raw/refs/heads/main/packages/markitdown/tests/test_files/test.pdf",
|
|
103
|
-
"https://github.com/microsoft/markitdown/raw/refs/heads/main/packages/markitdown/tests/test_files/test.pptx",
|
|
104
|
-
"https://upload.wikimedia.org/wikipedia/commons/thumb/e/ea/BremenBotanikaZen.jpg/1280px-BremenBotanikaZen.jpg")
|
|
105
|
-
print(a)# will print a string representation of the pdf, pptx and image that can be used in a prompt
|
|
106
|
-
```
|
|
107
|
-
|
|
108
|
-
```xml
|
|
109
|
-
<attachments>
|
|
110
|
-
<pdf_attachment_1>
|
|
111
|
-
page 1:
|
|
112
|
-
Hello PDF!
|
|
113
|
-
</pdf_attachment_1>
|
|
114
|
-
<pptx_attachment_2>
|
|
115
|
-
--- Slide 1 ---
|
|
116
|
-
Slide 1 Title This is the first slide. Content for page 1.
|
|
117
|
-
|
|
118
|
-
--- Slide 2 ---
|
|
119
|
-
Slide 2 Title This is the second slide. Content for page 2.
|
|
120
|
-
|
|
121
|
-
--- Slide 3 ---
|
|
122
|
-
Slide 3 Title This is the third slide. Content for page 3.
|
|
123
|
-
</pptx_attachment_2>
|
|
124
|
-
</attachments>
|
|
125
|
-
```
|
|
126
|
-
|
|
127
|
-
The ambition of `attachments` is to provide a robust reader, processor, and renderer for a wide array of common file types, simplifying the process of providing complex, multi-modal context to LLMs. The power comes from doing a best effort media -> text + image prompt and thus most of the value of `attachments` is not visible in long tutorial and lots of things to learn but in rather in it's simplicity and ability to get out of your way and let you pass files/data/information to LLMs.
|
|
128
|
-
|
|
129
|
-
## Key Features
|
|
130
|
-
|
|
131
|
-
* **Versatile File Handling**: Process a variety of file types including PDFs, PPTX, HTML, common image formats, and audio files.
|
|
132
|
-
* **Local and URL Support**: Accepts local file paths and URLs as input.
|
|
133
|
-
* **Content Extraction**: Extracts text from documents and rich metadata from all supported types.
|
|
134
|
-
* **Advanced Image Processing**:
|
|
135
|
-
* On-the-fly transformations via path string commands: resizing (e.g., `image.jpg[resize:500x300]`, `image.png[resize:200xauto]`), rotation (`image.heic[rotate:90]`), and format conversion (`image.webp[format:png]`).
|
|
136
|
-
* Configurable output quality for JPEG/WEBP.
|
|
137
|
-
* **Rich Jupyter/IPython Display**: Automatic rich Markdown rendering when an `Attachments` object is the last item in a cell, featuring:
|
|
138
|
-
* A summary of all attachments with detailed metadata.
|
|
139
|
-
* A multi-column image gallery for visual previews of image attachments.
|
|
140
|
-
* **Powerful Indexing**:
|
|
141
|
-
* Select specific pages from PDFs or slides from PPTX files (e.g., `"file.pdf[1,3-5,N]"`, `"presentation.pptx[:3,-1:]"`).
|
|
142
|
-
* `Attachments` objects themselves are indexable and sliceable (e.g., `subset = attachments[0:2]`).
|
|
143
|
-
* **LLM-Ready Outputs**:
|
|
144
|
-
* Default XML rendering (`str(attachments)`) provides a structured representation suitable for LLM prompts, including detailed metadata and textual content.
|
|
145
|
-
* `.images` property: Conveniently access a list of base64-encoded image strings (e.g., `data:image/jpeg;base64,...`), ready for multi-modal LLM APIs.
|
|
146
|
-
* `.audios` property: Provides a list of audio file data, each with a filename, a `BytesIO` file object, and content type, suitable for audio-processing APIs (e.g., OpenAI Whisper).
|
|
147
|
-
* **Broad Image Format Support**: Handles JPEG, PNG, GIF, BMP, WEBP, TIFF, and modern formats like HEIC/HEIF (requires `libheif`).
|
|
148
|
-
|
|
149
|
-
## Installation
|
|
150
|
-
|
|
151
|
-
```bash
|
|
152
|
-
pip install attachments
|
|
153
|
-
```
|
|
154
|
-
For full HEIC/HEIF image support, you may need to install `libheif` on your system:
|
|
155
|
-
* macOS: `brew install libheif`
|
|
156
|
-
* Debian/Ubuntu: `sudo apt-get install libheif-examples`
|
|
157
|
-
|
|
158
|
-
## Usage
|
|
159
|
-
|
|
160
|
-
### Basic Initialization
|
|
161
|
-
Create an `Attachments` object by passing one or more local file paths or URLs. Image processing commands can be appended to image paths.
|
|
162
|
-
|
|
163
|
-
```python
|
|
164
|
-
from attachments import Attachments
|
|
165
|
-
|
|
166
|
-
# Initialize with various local files, URLs, and image processing commands
|
|
167
|
-
a = Attachments(
|
|
168
|
-
"docs/report.pdf",
|
|
169
|
-
"images/diagram.png[resize:400xauto]",
|
|
170
|
-
"https://www.example.com/article.html",
|
|
171
|
-
"photos/vacation.heic[rotate:90,format:jpeg,quality:80]"
|
|
172
|
-
)
|
|
173
|
-
|
|
174
|
-
# The library will download URLs, process files, and extract content.
|
|
175
|
-
```
|
|
176
|
-
|
|
177
|
-
### Default XML Output for LLMs
|
|
178
|
-
Simply converting an `Attachments` object to a string (or using it in an f-string) renders it as XML, which is useful for many LLM prompts.
|
|
179
|
-
|
|
180
|
-
```python
|
|
181
|
-
prompt = f"""
|
|
182
|
-
Analyze the following documents:
|
|
183
|
-
{a}
|
|
184
|
-
"""
|
|
185
|
-
print(prompt)
|
|
186
|
-
|
|
187
|
-
# Output (simplified):
|
|
188
|
-
# Analyze the following documents:
|
|
189
|
-
# <attachments>
|
|
190
|
-
# <attachment id="pdf1" type="pdf" original_path_str="docs/report.pdf" file_path="docs/report.pdf">
|
|
191
|
-
# <meta name="num_pages" value="10" />
|
|
192
|
-
# <meta name="indices_processed" value="[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]" />
|
|
193
|
-
# <content>
|
|
194
|
-
# ... extracted text from PDF ...
|
|
195
|
-
# </content>
|
|
196
|
-
# </attachment>
|
|
197
|
-
# <attachment id="png2" type="png" original_path_str="images/diagram.png[resize:400xauto]" file_path="images/diagram.png">
|
|
198
|
-
# <meta name="dimensions" value="400x..." />
|
|
199
|
-
# <meta name="original_format" value="PNG" />
|
|
200
|
-
# <meta name="applied_operations" value="{'resize': (400, None)}" />
|
|
201
|
-
# <meta name="output_format_target" value="jpeg" />
|
|
202
|
-
# <content>
|
|
203
|
-
# [Image: diagram.png (original: PNG ...) -> processed to 400x... for output as jpeg]
|
|
204
|
-
# </content>
|
|
205
|
-
# </attachment>
|
|
206
|
-
# ... other attachments ...
|
|
207
|
-
# </attachments>
|
|
208
|
-
```
|
|
209
|
-
|
|
210
|
-
### Rich Display in Jupyter/IPython
|
|
211
|
-
If an `Attachments` object is the last expression in a Jupyter Notebook or IPython console cell, it will automatically render a rich Markdown summary:
|
|
212
|
-
|
|
213
|
-
```python
|
|
214
|
-
# In a Jupyter cell:
|
|
215
|
-
from attachments import Attachments
|
|
216
|
-
a = Attachments("report.pdf", "image.png[resize:150x150]", "chart.jpg")
|
|
217
|
-
a # This will display the rich summary and image gallery
|
|
218
|
-
```
|
|
219
|
-
This output includes a main summary of all attachments (ID, type, source, extracted metadata/text snippets) and a separate "Image Previews" section with a multi-column gallery of image thumbnails.
|
|
220
|
-
|
|
221
|
-
### Accessing Processed Data and Images
|
|
222
|
-
|
|
223
|
-
**1. Parsed Data:**
|
|
224
|
-
Each processed attachment's data is stored in the `attachments_data` list:
|
|
225
|
-
```python
|
|
226
|
-
for item in a.attachments_data:
|
|
227
|
-
print(f"ID: {item['id']}, Type: {item['type']}")
|
|
228
|
-
if 'text' in item:
|
|
229
|
-
print(f" Text snippet: {item['text'][:100]}...")
|
|
230
|
-
if item['type'] in ['jpeg', 'png', 'heic']: # Image types
|
|
231
|
-
print(f" Dimensions: {item['width']}x{item['height']}")
|
|
232
|
-
print(f" Original Format: {item['original_format']}")
|
|
233
|
-
```
|
|
234
|
-
|
|
235
|
-
**2. Base64 Images for LLMs:**
|
|
236
|
-
The `.images` property provides a list of base64-encoded strings for all processed images, ready for use with multi-modal LLM APIs.
|
|
237
|
-
```python
|
|
238
|
-
base64_image_list = a.images
|
|
239
|
-
if base64_image_list:
|
|
240
|
-
print(f"First image data URI: {base64_image_list[0][:50]}...")
|
|
241
|
-
# Output: First image data URI: data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEASABIAAD...
|
|
242
|
-
```
|
|
243
|
-
|
|
244
|
-
**3. Audio Files for LLMs:**
|
|
245
|
-
The `.audios` property returns a list of dictionaries, each prepared for audio API submission (e.g., to OpenAI Whisper). Each dictionary contains:
|
|
246
|
-
* `'filename'`: The original filename (e.g., `'speech.mp3'`).
|
|
247
|
-
* `'file_object'`: An `io.BytesIO` object containing the raw audio data. Its `.name` attribute is set to the filename.
|
|
248
|
-
* `'content_type'`: The detected MIME type of the audio (e.g., `'audio/mpeg'`).
|
|
249
|
-
|
|
250
|
-
```python
|
|
251
|
-
audio_files_for_api = a.audios
|
|
252
|
-
if audio_files_for_api:
|
|
253
|
-
first_audio = audio_files_for_api[0]
|
|
254
|
-
print(f"Audio Filename: {first_audio['filename']}")
|
|
255
|
-
print(f"Audio Content-Type: {first_audio['content_type']}")
|
|
256
|
-
# The first_audio['file_object'] can be directly passed to APIs like openai.Audio.transcribe
|
|
257
|
-
# For example: transcript = openai.Audio.transcribe("whisper-1", first_audio['file_object'])
|
|
258
|
-
```
|
|
259
|
-
|
|
260
|
-
### Indexing Attachments
|
|
261
|
-
You can get a new `Attachments` object containing a subset of the original attachments using integer or slice indexing:
|
|
262
|
-
```python
|
|
263
|
-
first_attachment = a[0]
|
|
264
|
-
first_two_attachments = a[0:2]
|
|
265
|
-
|
|
266
|
-
print(f"Selected attachment: {first_attachment}")
|
|
267
|
-
```
|
|
268
|
-
|
|
269
|
-
### Page/Slide Selection
|
|
270
|
-
Specify pages for PDFs or slides for PPTX files using bracket notation in the path string:
|
|
271
|
-
```python
|
|
272
|
-
# Process only page 1 and pages 3 through 5 of a PDF
|
|
273
|
-
specific_pages_pdf = Attachments("long_document.pdf[1,3-5]")
|
|
274
|
-
|
|
275
|
-
# Process the first three slides and the last slide of a presentation
|
|
276
|
-
specific_slides_pptx = Attachments("presentation.pptx[:3,N]")
|
|
277
|
-
# 'N' refers to the last page/slide. Negative indexing like [-1:] also works.
|
|
278
|
-
```
|
|
279
|
-
|
|
280
|
-
## Supported File Types
|
|
281
|
-
* **Documents**: PDF (`.pdf`), PowerPoint (`.pptx`)
|
|
282
|
-
* **Web**: HTML (`.html`, URLs)
|
|
283
|
-
* **Images**: JPEG (`.jpg`, `.jpeg`), PNG (`.png`), GIF (`.gif`), BMP (`.bmp`), WEBP (`.webp`), TIFF (`.tiff`), HEIC (`.heic`), HEIF (`.heif`)
|
|
284
|
-
* **Audio**: FLAC (`.flac`), M4A (`.m4a`), MP3 (`.mp3`), MP4 audio (`.mp4`), MPEG audio (`.mpeg`, `.mpga`), Ogg audio (`.oga`, `.ogg`), WAV (`.wav`), WebM audio (`.webm`)
|
attachments-0.2.1/README.md
DELETED
|
@@ -1,245 +0,0 @@
|
|
|
1
|
-
# Attachments
|
|
2
|
-
## Easiest way to give context to LLMs
|
|
3
|
-
|
|
4
|
-
Attachments has the ambition to be the general funnel for any files to be transformed into images+text for large language models context. For this to be true we, has a community, have to stop implementing this funnel (file -> images+text) each on our own. If you have a detector + parser + renderer contibute it! This will help you and us. Don't let your code alone in a dark corner of you computer.
|
|
5
|
-
|
|
6
|
-
## Very quickstart
|
|
7
|
-
|
|
8
|
-
```python
|
|
9
|
-
from attachments import Attachments
|
|
10
|
-
|
|
11
|
-
a = Attachment("/the/path/or/url/to/your/to/the/content/you/want/to/give/the/llm.xlsx", "another.pdf", "another.pptx"...)
|
|
12
|
-
|
|
13
|
-
prompt_ready_context = str(a)
|
|
14
|
-
images_ready_for_llm = a.images
|
|
15
|
-
```
|
|
16
|
-
|
|
17
|
-
That is really just it!
|
|
18
|
-
|
|
19
|
-
You can print it `print(a)`, you can interpolate it `f"the content is {a}"` you can string it `str(a)`. This will give you something very good to give the llm so that the AI can consider you content.
|
|
20
|
-
|
|
21
|
-
Nowadays, most genAI models comes with the ability to see images too. So you also have all attachments in images forms by using `a.images`.
|
|
22
|
-
This is a list of base64 encoded images, this is the fundamental format the most llm provider accept.
|
|
23
|
-
|
|
24
|
-
The simplest way to use and think about attachments is that if you want to put you best foot forward and up you chances that the llm grok your content you should pass all of the text in `a` to the prompt and all of the images in `a.images` in the image input. We aim for making those two as 'prompt engineered' as possible. *Attachments* is young but already very powerful and used in production. The api will not change. Maybe advanced feature and syntax will be added but the core will stay the same. Mostly we will support more file types and we will have better rendering for better performance and with less extreneous tokens.
|
|
25
|
-
|
|
26
|
-
## How to give attachments to openai llms?
|
|
27
|
-
|
|
28
|
-
```python
|
|
29
|
-
from openai import OpenAI
|
|
30
|
-
from attachments import Attachments
|
|
31
|
-
|
|
32
|
-
pdf_attachment = Attachments("https://github.com/microsoft/markitdown/raw/refs/heads/main/packages/markitdown/tests/test_files/test.pdf")
|
|
33
|
-
|
|
34
|
-
prompt = f"""
|
|
35
|
-
Analyze the following documents:
|
|
36
|
-
{pdf_attachment}
|
|
37
|
-
"""
|
|
38
|
-
|
|
39
|
-
content = [{"type": "input_image","image_url": image} for image in pdf_attachment.images] + \
|
|
40
|
-
[{"type": "input_text", "text": prompt}]
|
|
41
|
-
|
|
42
|
-
response = OpenAI().responses.create(model="gpt-4.1-nano", input=[{"role": "user", "content": content}])
|
|
43
|
-
response.output_text
|
|
44
|
-
```
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
A Python library designed to seamlessly handle various file types (local or URLs), process them, and present them in formats suitable for Large Language Models (LLMs).
|
|
51
|
-
|
|
52
|
-
It is meant to be very minimal.
|
|
53
|
-
|
|
54
|
-
most user will not have to learn anything more then that: `Attachments("path/to/file.pdf")`
|
|
55
|
-
|
|
56
|
-
```bash
|
|
57
|
-
> pip install attachments
|
|
58
|
-
```
|
|
59
|
-
|
|
60
|
-
```python
|
|
61
|
-
from attachments import Attachments
|
|
62
|
-
|
|
63
|
-
a = Attachments("https://github.com/microsoft/markitdown/raw/refs/heads/main/packages/markitdown/tests/test_files/test.pdf",
|
|
64
|
-
"https://github.com/microsoft/markitdown/raw/refs/heads/main/packages/markitdown/tests/test_files/test.pptx",
|
|
65
|
-
"https://upload.wikimedia.org/wikipedia/commons/thumb/e/ea/BremenBotanikaZen.jpg/1280px-BremenBotanikaZen.jpg")
|
|
66
|
-
print(a)# will print a string representation of the pdf, pptx and image that can be used in a prompt
|
|
67
|
-
```
|
|
68
|
-
|
|
69
|
-
```xml
|
|
70
|
-
<attachments>
|
|
71
|
-
<pdf_attachment_1>
|
|
72
|
-
page 1:
|
|
73
|
-
Hello PDF!
|
|
74
|
-
</pdf_attachment_1>
|
|
75
|
-
<pptx_attachment_2>
|
|
76
|
-
--- Slide 1 ---
|
|
77
|
-
Slide 1 Title This is the first slide. Content for page 1.
|
|
78
|
-
|
|
79
|
-
--- Slide 2 ---
|
|
80
|
-
Slide 2 Title This is the second slide. Content for page 2.
|
|
81
|
-
|
|
82
|
-
--- Slide 3 ---
|
|
83
|
-
Slide 3 Title This is the third slide. Content for page 3.
|
|
84
|
-
</pptx_attachment_2>
|
|
85
|
-
</attachments>
|
|
86
|
-
```
|
|
87
|
-
|
|
88
|
-
The ambition of `attachments` is to provide a robust reader, processor, and renderer for a wide array of common file types, simplifying the process of providing complex, multi-modal context to LLMs. The power comes from doing a best effort media -> text + image prompt and thus most of the value of `attachments` is not visible in long tutorial and lots of things to learn but in rather in it's simplicity and ability to get out of your way and let you pass files/data/information to LLMs.
|
|
89
|
-
|
|
90
|
-
## Key Features
|
|
91
|
-
|
|
92
|
-
* **Versatile File Handling**: Process a variety of file types including PDFs, PPTX, HTML, common image formats, and audio files.
|
|
93
|
-
* **Local and URL Support**: Accepts local file paths and URLs as input.
|
|
94
|
-
* **Content Extraction**: Extracts text from documents and rich metadata from all supported types.
|
|
95
|
-
* **Advanced Image Processing**:
|
|
96
|
-
* On-the-fly transformations via path string commands: resizing (e.g., `image.jpg[resize:500x300]`, `image.png[resize:200xauto]`), rotation (`image.heic[rotate:90]`), and format conversion (`image.webp[format:png]`).
|
|
97
|
-
* Configurable output quality for JPEG/WEBP.
|
|
98
|
-
* **Rich Jupyter/IPython Display**: Automatic rich Markdown rendering when an `Attachments` object is the last item in a cell, featuring:
|
|
99
|
-
* A summary of all attachments with detailed metadata.
|
|
100
|
-
* A multi-column image gallery for visual previews of image attachments.
|
|
101
|
-
* **Powerful Indexing**:
|
|
102
|
-
* Select specific pages from PDFs or slides from PPTX files (e.g., `"file.pdf[1,3-5,N]"`, `"presentation.pptx[:3,-1:]"`).
|
|
103
|
-
* `Attachments` objects themselves are indexable and sliceable (e.g., `subset = attachments[0:2]`).
|
|
104
|
-
* **LLM-Ready Outputs**:
|
|
105
|
-
* Default XML rendering (`str(attachments)`) provides a structured representation suitable for LLM prompts, including detailed metadata and textual content.
|
|
106
|
-
* `.images` property: Conveniently access a list of base64-encoded image strings (e.g., `data:image/jpeg;base64,...`), ready for multi-modal LLM APIs.
|
|
107
|
-
* `.audios` property: Provides a list of audio file data, each with a filename, a `BytesIO` file object, and content type, suitable for audio-processing APIs (e.g., OpenAI Whisper).
|
|
108
|
-
* **Broad Image Format Support**: Handles JPEG, PNG, GIF, BMP, WEBP, TIFF, and modern formats like HEIC/HEIF (requires `libheif`).
|
|
109
|
-
|
|
110
|
-
## Installation
|
|
111
|
-
|
|
112
|
-
```bash
|
|
113
|
-
pip install attachments
|
|
114
|
-
```
|
|
115
|
-
For full HEIC/HEIF image support, you may need to install `libheif` on your system:
|
|
116
|
-
* macOS: `brew install libheif`
|
|
117
|
-
* Debian/Ubuntu: `sudo apt-get install libheif-examples`
|
|
118
|
-
|
|
119
|
-
## Usage
|
|
120
|
-
|
|
121
|
-
### Basic Initialization
|
|
122
|
-
Create an `Attachments` object by passing one or more local file paths or URLs. Image processing commands can be appended to image paths.
|
|
123
|
-
|
|
124
|
-
```python
|
|
125
|
-
from attachments import Attachments
|
|
126
|
-
|
|
127
|
-
# Initialize with various local files, URLs, and image processing commands
|
|
128
|
-
a = Attachments(
|
|
129
|
-
"docs/report.pdf",
|
|
130
|
-
"images/diagram.png[resize:400xauto]",
|
|
131
|
-
"https://www.example.com/article.html",
|
|
132
|
-
"photos/vacation.heic[rotate:90,format:jpeg,quality:80]"
|
|
133
|
-
)
|
|
134
|
-
|
|
135
|
-
# The library will download URLs, process files, and extract content.
|
|
136
|
-
```
|
|
137
|
-
|
|
138
|
-
### Default XML Output for LLMs
|
|
139
|
-
Simply converting an `Attachments` object to a string (or using it in an f-string) renders it as XML, which is useful for many LLM prompts.
|
|
140
|
-
|
|
141
|
-
```python
|
|
142
|
-
prompt = f"""
|
|
143
|
-
Analyze the following documents:
|
|
144
|
-
{a}
|
|
145
|
-
"""
|
|
146
|
-
print(prompt)
|
|
147
|
-
|
|
148
|
-
# Output (simplified):
|
|
149
|
-
# Analyze the following documents:
|
|
150
|
-
# <attachments>
|
|
151
|
-
# <attachment id="pdf1" type="pdf" original_path_str="docs/report.pdf" file_path="docs/report.pdf">
|
|
152
|
-
# <meta name="num_pages" value="10" />
|
|
153
|
-
# <meta name="indices_processed" value="[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]" />
|
|
154
|
-
# <content>
|
|
155
|
-
# ... extracted text from PDF ...
|
|
156
|
-
# </content>
|
|
157
|
-
# </attachment>
|
|
158
|
-
# <attachment id="png2" type="png" original_path_str="images/diagram.png[resize:400xauto]" file_path="images/diagram.png">
|
|
159
|
-
# <meta name="dimensions" value="400x..." />
|
|
160
|
-
# <meta name="original_format" value="PNG" />
|
|
161
|
-
# <meta name="applied_operations" value="{'resize': (400, None)}" />
|
|
162
|
-
# <meta name="output_format_target" value="jpeg" />
|
|
163
|
-
# <content>
|
|
164
|
-
# [Image: diagram.png (original: PNG ...) -> processed to 400x... for output as jpeg]
|
|
165
|
-
# </content>
|
|
166
|
-
# </attachment>
|
|
167
|
-
# ... other attachments ...
|
|
168
|
-
# </attachments>
|
|
169
|
-
```
|
|
170
|
-
|
|
171
|
-
### Rich Display in Jupyter/IPython
|
|
172
|
-
If an `Attachments` object is the last expression in a Jupyter Notebook or IPython console cell, it will automatically render a rich Markdown summary:
|
|
173
|
-
|
|
174
|
-
```python
|
|
175
|
-
# In a Jupyter cell:
|
|
176
|
-
from attachments import Attachments
|
|
177
|
-
a = Attachments("report.pdf", "image.png[resize:150x150]", "chart.jpg")
|
|
178
|
-
a # This will display the rich summary and image gallery
|
|
179
|
-
```
|
|
180
|
-
This output includes a main summary of all attachments (ID, type, source, extracted metadata/text snippets) and a separate "Image Previews" section with a multi-column gallery of image thumbnails.
|
|
181
|
-
|
|
182
|
-
### Accessing Processed Data and Images
|
|
183
|
-
|
|
184
|
-
**1. Parsed Data:**
|
|
185
|
-
Each processed attachment's data is stored in the `attachments_data` list:
|
|
186
|
-
```python
|
|
187
|
-
for item in a.attachments_data:
|
|
188
|
-
print(f"ID: {item['id']}, Type: {item['type']}")
|
|
189
|
-
if 'text' in item:
|
|
190
|
-
print(f" Text snippet: {item['text'][:100]}...")
|
|
191
|
-
if item['type'] in ['jpeg', 'png', 'heic']: # Image types
|
|
192
|
-
print(f" Dimensions: {item['width']}x{item['height']}")
|
|
193
|
-
print(f" Original Format: {item['original_format']}")
|
|
194
|
-
```
|
|
195
|
-
|
|
196
|
-
**2. Base64 Images for LLMs:**
|
|
197
|
-
The `.images` property provides a list of base64-encoded strings for all processed images, ready for use with multi-modal LLM APIs.
|
|
198
|
-
```python
|
|
199
|
-
base64_image_list = a.images
|
|
200
|
-
if base64_image_list:
|
|
201
|
-
print(f"First image data URI: {base64_image_list[0][:50]}...")
|
|
202
|
-
# Output: First image data URI: data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEASABIAAD...
|
|
203
|
-
```
|
|
204
|
-
|
|
205
|
-
**3. Audio Files for LLMs:**
|
|
206
|
-
The `.audios` property returns a list of dictionaries, each prepared for audio API submission (e.g., to OpenAI Whisper). Each dictionary contains:
|
|
207
|
-
* `'filename'`: The original filename (e.g., `'speech.mp3'`).
|
|
208
|
-
* `'file_object'`: An `io.BytesIO` object containing the raw audio data. Its `.name` attribute is set to the filename.
|
|
209
|
-
* `'content_type'`: The detected MIME type of the audio (e.g., `'audio/mpeg'`).
|
|
210
|
-
|
|
211
|
-
```python
|
|
212
|
-
audio_files_for_api = a.audios
|
|
213
|
-
if audio_files_for_api:
|
|
214
|
-
first_audio = audio_files_for_api[0]
|
|
215
|
-
print(f"Audio Filename: {first_audio['filename']}")
|
|
216
|
-
print(f"Audio Content-Type: {first_audio['content_type']}")
|
|
217
|
-
# The first_audio['file_object'] can be directly passed to APIs like openai.Audio.transcribe
|
|
218
|
-
# For example: transcript = openai.Audio.transcribe("whisper-1", first_audio['file_object'])
|
|
219
|
-
```
|
|
220
|
-
|
|
221
|
-
### Indexing Attachments
|
|
222
|
-
You can get a new `Attachments` object containing a subset of the original attachments using integer or slice indexing:
|
|
223
|
-
```python
|
|
224
|
-
first_attachment = a[0]
|
|
225
|
-
first_two_attachments = a[0:2]
|
|
226
|
-
|
|
227
|
-
print(f"Selected attachment: {first_attachment}")
|
|
228
|
-
```
|
|
229
|
-
|
|
230
|
-
### Page/Slide Selection
|
|
231
|
-
Specify pages for PDFs or slides for PPTX files using bracket notation in the path string:
|
|
232
|
-
```python
|
|
233
|
-
# Process only page 1 and pages 3 through 5 of a PDF
|
|
234
|
-
specific_pages_pdf = Attachments("long_document.pdf[1,3-5]")
|
|
235
|
-
|
|
236
|
-
# Process the first three slides and the last slide of a presentation
|
|
237
|
-
specific_slides_pptx = Attachments("presentation.pptx[:3,N]")
|
|
238
|
-
# 'N' refers to the last page/slide. Negative indexing like [-1:] also works.
|
|
239
|
-
```
|
|
240
|
-
|
|
241
|
-
## Supported File Types
|
|
242
|
-
* **Documents**: PDF (`.pdf`), PowerPoint (`.pptx`)
|
|
243
|
-
* **Web**: HTML (`.html`, URLs)
|
|
244
|
-
* **Images**: JPEG (`.jpg`, `.jpeg`), PNG (`.png`), GIF (`.gif`), BMP (`.bmp`), WEBP (`.webp`), TIFF (`.tiff`), HEIC (`.heic`), HEIF (`.heif`)
|
|
245
|
-
* **Audio**: FLAC (`.flac`), M4A (`.m4a`), MP3 (`.mp3`), MP4 audio (`.mp4`), MPEG audio (`.mpeg`, `.mpga`), Ogg audio (`.oga`, `.ogg`), WAV (`.wav`), WebM audio (`.webm`)
|
|
@@ -1,284 +0,0 @@
|
|
|
1
|
-
Metadata-Version: 2.4
|
|
2
|
-
Name: attachments
|
|
3
|
-
Version: 0.2.1
|
|
4
|
-
Summary: A Python library to handle various file types for LLMs.
|
|
5
|
-
Author-email: Maxim Rivest <mrive052@gmail.com>
|
|
6
|
-
License-Expression: MIT
|
|
7
|
-
Project-URL: Homepage, https://github.com/yourusername/attachments
|
|
8
|
-
Project-URL: Bug Tracker, https://github.com/yourusername/attachments/issues
|
|
9
|
-
Classifier: Programming Language :: Python :: 3
|
|
10
|
-
Classifier: Operating System :: OS Independent
|
|
11
|
-
Requires-Python: >=3.10
|
|
12
|
-
Description-Content-Type: text/markdown
|
|
13
|
-
License-File: LICENSE
|
|
14
|
-
Requires-Dist: Pillow
|
|
15
|
-
Requires-Dist: python-dotenv
|
|
16
|
-
Requires-Dist: pytest
|
|
17
|
-
Requires-Dist: beautifulsoup4
|
|
18
|
-
Requires-Dist: html2text
|
|
19
|
-
Requires-Dist: markdownify
|
|
20
|
-
Requires-Dist: lxml
|
|
21
|
-
Requires-Dist: python-magic
|
|
22
|
-
Requires-Dist: mammoth
|
|
23
|
-
Requires-Dist: PyMuPDF
|
|
24
|
-
Requires-Dist: python-pptx
|
|
25
|
-
Requires-Dist: python-docx
|
|
26
|
-
Requires-Dist: odfpy
|
|
27
|
-
Requires-Dist: pillow-heif
|
|
28
|
-
Requires-Dist: pydub
|
|
29
|
-
Requires-Dist: SpeechRecognition
|
|
30
|
-
Requires-Dist: markitdown[all]>=0.1.1
|
|
31
|
-
Requires-Dist: pypdfium2
|
|
32
|
-
Provides-Extra: dev
|
|
33
|
-
Requires-Dist: pytest; extra == "dev"
|
|
34
|
-
Requires-Dist: flake8; extra == "dev"
|
|
35
|
-
Requires-Dist: mypy; extra == "dev"
|
|
36
|
-
Requires-Dist: black; extra == "dev"
|
|
37
|
-
Requires-Dist: filelock; extra == "dev"
|
|
38
|
-
Dynamic: license-file
|
|
39
|
-
|
|
40
|
-
# Attachments
|
|
41
|
-
## Easiest way to give context to LLMs
|
|
42
|
-
|
|
43
|
-
Attachments has the ambition to be the general funnel for any files to be transformed into images+text for large language models context. For this to be true we, has a community, have to stop implementing this funnel (file -> images+text) each on our own. If you have a detector + parser + renderer contibute it! This will help you and us. Don't let your code alone in a dark corner of you computer.
|
|
44
|
-
|
|
45
|
-
## Very quickstart
|
|
46
|
-
|
|
47
|
-
```python
|
|
48
|
-
from attachments import Attachments
|
|
49
|
-
|
|
50
|
-
a = Attachment("/the/path/or/url/to/your/to/the/content/you/want/to/give/the/llm.xlsx", "another.pdf", "another.pptx"...)
|
|
51
|
-
|
|
52
|
-
prompt_ready_context = str(a)
|
|
53
|
-
images_ready_for_llm = a.images
|
|
54
|
-
```
|
|
55
|
-
|
|
56
|
-
That is really just it!
|
|
57
|
-
|
|
58
|
-
You can print it `print(a)`, you can interpolate it `f"the content is {a}"` you can string it `str(a)`. This will give you something very good to give the llm so that the AI can consider you content.
|
|
59
|
-
|
|
60
|
-
Nowadays, most genAI models comes with the ability to see images too. So you also have all attachments in images forms by using `a.images`.
|
|
61
|
-
This is a list of base64 encoded images, this is the fundamental format the most llm provider accept.
|
|
62
|
-
|
|
63
|
-
The simplest way to use and think about attachments is that if you want to put you best foot forward and up you chances that the llm grok your content you should pass all of the text in `a` to the prompt and all of the images in `a.images` in the image input. We aim for making those two as 'prompt engineered' as possible. *Attachments* is young but already very powerful and used in production. The api will not change. Maybe advanced feature and syntax will be added but the core will stay the same. Mostly we will support more file types and we will have better rendering for better performance and with less extreneous tokens.
|
|
64
|
-
|
|
65
|
-
## How to give attachments to openai llms?
|
|
66
|
-
|
|
67
|
-
```python
|
|
68
|
-
from openai import OpenAI
|
|
69
|
-
from attachments import Attachments
|
|
70
|
-
|
|
71
|
-
pdf_attachment = Attachments("https://github.com/microsoft/markitdown/raw/refs/heads/main/packages/markitdown/tests/test_files/test.pdf")
|
|
72
|
-
|
|
73
|
-
prompt = f"""
|
|
74
|
-
Analyze the following documents:
|
|
75
|
-
{pdf_attachment}
|
|
76
|
-
"""
|
|
77
|
-
|
|
78
|
-
content = [{"type": "input_image","image_url": image} for image in pdf_attachment.images] + \
|
|
79
|
-
[{"type": "input_text", "text": prompt}]
|
|
80
|
-
|
|
81
|
-
response = OpenAI().responses.create(model="gpt-4.1-nano", input=[{"role": "user", "content": content}])
|
|
82
|
-
response.output_text
|
|
83
|
-
```
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
A Python library designed to seamlessly handle various file types (local or URLs), process them, and present them in formats suitable for Large Language Models (LLMs).
|
|
90
|
-
|
|
91
|
-
It is meant to be very minimal.
|
|
92
|
-
|
|
93
|
-
most user will not have to learn anything more then that: `Attachments("path/to/file.pdf")`
|
|
94
|
-
|
|
95
|
-
```bash
|
|
96
|
-
> pip install attachments
|
|
97
|
-
```
|
|
98
|
-
|
|
99
|
-
```python
|
|
100
|
-
from attachments import Attachments
|
|
101
|
-
|
|
102
|
-
a = Attachments("https://github.com/microsoft/markitdown/raw/refs/heads/main/packages/markitdown/tests/test_files/test.pdf",
|
|
103
|
-
"https://github.com/microsoft/markitdown/raw/refs/heads/main/packages/markitdown/tests/test_files/test.pptx",
|
|
104
|
-
"https://upload.wikimedia.org/wikipedia/commons/thumb/e/ea/BremenBotanikaZen.jpg/1280px-BremenBotanikaZen.jpg")
|
|
105
|
-
print(a)# will print a string representation of the pdf, pptx and image that can be used in a prompt
|
|
106
|
-
```
|
|
107
|
-
|
|
108
|
-
```xml
|
|
109
|
-
<attachments>
|
|
110
|
-
<pdf_attachment_1>
|
|
111
|
-
page 1:
|
|
112
|
-
Hello PDF!
|
|
113
|
-
</pdf_attachment_1>
|
|
114
|
-
<pptx_attachment_2>
|
|
115
|
-
--- Slide 1 ---
|
|
116
|
-
Slide 1 Title This is the first slide. Content for page 1.
|
|
117
|
-
|
|
118
|
-
--- Slide 2 ---
|
|
119
|
-
Slide 2 Title This is the second slide. Content for page 2.
|
|
120
|
-
|
|
121
|
-
--- Slide 3 ---
|
|
122
|
-
Slide 3 Title This is the third slide. Content for page 3.
|
|
123
|
-
</pptx_attachment_2>
|
|
124
|
-
</attachments>
|
|
125
|
-
```
|
|
126
|
-
|
|
127
|
-
The ambition of `attachments` is to provide a robust reader, processor, and renderer for a wide array of common file types, simplifying the process of providing complex, multi-modal context to LLMs. The power comes from doing a best effort media -> text + image prompt and thus most of the value of `attachments` is not visible in long tutorial and lots of things to learn but in rather in it's simplicity and ability to get out of your way and let you pass files/data/information to LLMs.
|
|
128
|
-
|
|
129
|
-
## Key Features
|
|
130
|
-
|
|
131
|
-
* **Versatile File Handling**: Process a variety of file types including PDFs, PPTX, HTML, common image formats, and audio files.
|
|
132
|
-
* **Local and URL Support**: Accepts local file paths and URLs as input.
|
|
133
|
-
* **Content Extraction**: Extracts text from documents and rich metadata from all supported types.
|
|
134
|
-
* **Advanced Image Processing**:
|
|
135
|
-
* On-the-fly transformations via path string commands: resizing (e.g., `image.jpg[resize:500x300]`, `image.png[resize:200xauto]`), rotation (`image.heic[rotate:90]`), and format conversion (`image.webp[format:png]`).
|
|
136
|
-
* Configurable output quality for JPEG/WEBP.
|
|
137
|
-
* **Rich Jupyter/IPython Display**: Automatic rich Markdown rendering when an `Attachments` object is the last item in a cell, featuring:
|
|
138
|
-
* A summary of all attachments with detailed metadata.
|
|
139
|
-
* A multi-column image gallery for visual previews of image attachments.
|
|
140
|
-
* **Powerful Indexing**:
|
|
141
|
-
* Select specific pages from PDFs or slides from PPTX files (e.g., `"file.pdf[1,3-5,N]"`, `"presentation.pptx[:3,-1:]"`).
|
|
142
|
-
* `Attachments` objects themselves are indexable and sliceable (e.g., `subset = attachments[0:2]`).
|
|
143
|
-
* **LLM-Ready Outputs**:
|
|
144
|
-
* Default XML rendering (`str(attachments)`) provides a structured representation suitable for LLM prompts, including detailed metadata and textual content.
|
|
145
|
-
* `.images` property: Conveniently access a list of base64-encoded image strings (e.g., `data:image/jpeg;base64,...`), ready for multi-modal LLM APIs.
|
|
146
|
-
* `.audios` property: Provides a list of audio file data, each with a filename, a `BytesIO` file object, and content type, suitable for audio-processing APIs (e.g., OpenAI Whisper).
|
|
147
|
-
* **Broad Image Format Support**: Handles JPEG, PNG, GIF, BMP, WEBP, TIFF, and modern formats like HEIC/HEIF (requires `libheif`).
|
|
148
|
-
|
|
149
|
-
## Installation
|
|
150
|
-
|
|
151
|
-
```bash
|
|
152
|
-
pip install attachments
|
|
153
|
-
```
|
|
154
|
-
For full HEIC/HEIF image support, you may need to install `libheif` on your system:
|
|
155
|
-
* macOS: `brew install libheif`
|
|
156
|
-
* Debian/Ubuntu: `sudo apt-get install libheif-examples`
|
|
157
|
-
|
|
158
|
-
## Usage
|
|
159
|
-
|
|
160
|
-
### Basic Initialization
|
|
161
|
-
Create an `Attachments` object by passing one or more local file paths or URLs. Image processing commands can be appended to image paths.
|
|
162
|
-
|
|
163
|
-
```python
|
|
164
|
-
from attachments import Attachments
|
|
165
|
-
|
|
166
|
-
# Initialize with various local files, URLs, and image processing commands
|
|
167
|
-
a = Attachments(
|
|
168
|
-
"docs/report.pdf",
|
|
169
|
-
"images/diagram.png[resize:400xauto]",
|
|
170
|
-
"https://www.example.com/article.html",
|
|
171
|
-
"photos/vacation.heic[rotate:90,format:jpeg,quality:80]"
|
|
172
|
-
)
|
|
173
|
-
|
|
174
|
-
# The library will download URLs, process files, and extract content.
|
|
175
|
-
```
|
|
176
|
-
|
|
177
|
-
### Default XML Output for LLMs
|
|
178
|
-
Simply converting an `Attachments` object to a string (or using it in an f-string) renders it as XML, which is useful for many LLM prompts.
|
|
179
|
-
|
|
180
|
-
```python
|
|
181
|
-
prompt = f"""
|
|
182
|
-
Analyze the following documents:
|
|
183
|
-
{a}
|
|
184
|
-
"""
|
|
185
|
-
print(prompt)
|
|
186
|
-
|
|
187
|
-
# Output (simplified):
|
|
188
|
-
# Analyze the following documents:
|
|
189
|
-
# <attachments>
|
|
190
|
-
# <attachment id="pdf1" type="pdf" original_path_str="docs/report.pdf" file_path="docs/report.pdf">
|
|
191
|
-
# <meta name="num_pages" value="10" />
|
|
192
|
-
# <meta name="indices_processed" value="[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]" />
|
|
193
|
-
# <content>
|
|
194
|
-
# ... extracted text from PDF ...
|
|
195
|
-
# </content>
|
|
196
|
-
# </attachment>
|
|
197
|
-
# <attachment id="png2" type="png" original_path_str="images/diagram.png[resize:400xauto]" file_path="images/diagram.png">
|
|
198
|
-
# <meta name="dimensions" value="400x..." />
|
|
199
|
-
# <meta name="original_format" value="PNG" />
|
|
200
|
-
# <meta name="applied_operations" value="{'resize': (400, None)}" />
|
|
201
|
-
# <meta name="output_format_target" value="jpeg" />
|
|
202
|
-
# <content>
|
|
203
|
-
# [Image: diagram.png (original: PNG ...) -> processed to 400x... for output as jpeg]
|
|
204
|
-
# </content>
|
|
205
|
-
# </attachment>
|
|
206
|
-
# ... other attachments ...
|
|
207
|
-
# </attachments>
|
|
208
|
-
```
|
|
209
|
-
|
|
210
|
-
### Rich Display in Jupyter/IPython
|
|
211
|
-
If an `Attachments` object is the last expression in a Jupyter Notebook or IPython console cell, it will automatically render a rich Markdown summary:
|
|
212
|
-
|
|
213
|
-
```python
|
|
214
|
-
# In a Jupyter cell:
|
|
215
|
-
from attachments import Attachments
|
|
216
|
-
a = Attachments("report.pdf", "image.png[resize:150x150]", "chart.jpg")
|
|
217
|
-
a # This will display the rich summary and image gallery
|
|
218
|
-
```
|
|
219
|
-
This output includes a main summary of all attachments (ID, type, source, extracted metadata/text snippets) and a separate "Image Previews" section with a multi-column gallery of image thumbnails.
|
|
220
|
-
|
|
221
|
-
### Accessing Processed Data and Images
|
|
222
|
-
|
|
223
|
-
**1. Parsed Data:**
|
|
224
|
-
Each processed attachment's data is stored in the `attachments_data` list:
|
|
225
|
-
```python
|
|
226
|
-
for item in a.attachments_data:
|
|
227
|
-
print(f"ID: {item['id']}, Type: {item['type']}")
|
|
228
|
-
if 'text' in item:
|
|
229
|
-
print(f" Text snippet: {item['text'][:100]}...")
|
|
230
|
-
if item['type'] in ['jpeg', 'png', 'heic']: # Image types
|
|
231
|
-
print(f" Dimensions: {item['width']}x{item['height']}")
|
|
232
|
-
print(f" Original Format: {item['original_format']}")
|
|
233
|
-
```
|
|
234
|
-
|
|
235
|
-
**2. Base64 Images for LLMs:**
|
|
236
|
-
The `.images` property provides a list of base64-encoded strings for all processed images, ready for use with multi-modal LLM APIs.
|
|
237
|
-
```python
|
|
238
|
-
base64_image_list = a.images
|
|
239
|
-
if base64_image_list:
|
|
240
|
-
print(f"First image data URI: {base64_image_list[0][:50]}...")
|
|
241
|
-
# Output: First image data URI: data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEASABIAAD...
|
|
242
|
-
```
|
|
243
|
-
|
|
244
|
-
**3. Audio Files for LLMs:**
|
|
245
|
-
The `.audios` property returns a list of dictionaries, each prepared for audio API submission (e.g., to OpenAI Whisper). Each dictionary contains:
|
|
246
|
-
* `'filename'`: The original filename (e.g., `'speech.mp3'`).
|
|
247
|
-
* `'file_object'`: An `io.BytesIO` object containing the raw audio data. Its `.name` attribute is set to the filename.
|
|
248
|
-
* `'content_type'`: The detected MIME type of the audio (e.g., `'audio/mpeg'`).
|
|
249
|
-
|
|
250
|
-
```python
|
|
251
|
-
audio_files_for_api = a.audios
|
|
252
|
-
if audio_files_for_api:
|
|
253
|
-
first_audio = audio_files_for_api[0]
|
|
254
|
-
print(f"Audio Filename: {first_audio['filename']}")
|
|
255
|
-
print(f"Audio Content-Type: {first_audio['content_type']}")
|
|
256
|
-
# The first_audio['file_object'] can be directly passed to APIs like openai.Audio.transcribe
|
|
257
|
-
# For example: transcript = openai.Audio.transcribe("whisper-1", first_audio['file_object'])
|
|
258
|
-
```
|
|
259
|
-
|
|
260
|
-
### Indexing Attachments
|
|
261
|
-
You can get a new `Attachments` object containing a subset of the original attachments using integer or slice indexing:
|
|
262
|
-
```python
|
|
263
|
-
first_attachment = a[0]
|
|
264
|
-
first_two_attachments = a[0:2]
|
|
265
|
-
|
|
266
|
-
print(f"Selected attachment: {first_attachment}")
|
|
267
|
-
```
|
|
268
|
-
|
|
269
|
-
### Page/Slide Selection
|
|
270
|
-
Specify pages for PDFs or slides for PPTX files using bracket notation in the path string:
|
|
271
|
-
```python
|
|
272
|
-
# Process only page 1 and pages 3 through 5 of a PDF
|
|
273
|
-
specific_pages_pdf = Attachments("long_document.pdf[1,3-5]")
|
|
274
|
-
|
|
275
|
-
# Process the first three slides and the last slide of a presentation
|
|
276
|
-
specific_slides_pptx = Attachments("presentation.pptx[:3,N]")
|
|
277
|
-
# 'N' refers to the last page/slide. Negative indexing like [-1:] also works.
|
|
278
|
-
```
|
|
279
|
-
|
|
280
|
-
## Supported File Types
|
|
281
|
-
* **Documents**: PDF (`.pdf`), PowerPoint (`.pptx`)
|
|
282
|
-
* **Web**: HTML (`.html`, URLs)
|
|
283
|
-
* **Images**: JPEG (`.jpg`, `.jpeg`), PNG (`.png`), GIF (`.gif`), BMP (`.bmp`), WEBP (`.webp`), TIFF (`.tiff`), HEIC (`.heic`), HEIF (`.heif`)
|
|
284
|
-
* **Audio**: FLAC (`.flac`), M4A (`.m4a`), MP3 (`.mp3`), MP4 audio (`.mp4`), MPEG audio (`.mpeg`, `.mpga`), Ogg audio (`.oga`, `.ogg`), WAV (`.wav`), WebM audio (`.webm`)
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|