embedkit 0.1.0__py3-none-any.whl → 0.1.1__py3-none-any.whl

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.
embedkit/base.py CHANGED
@@ -2,7 +2,7 @@
2
2
  """Base classes for EmbedKit."""
3
3
 
4
4
  from abc import ABC, abstractmethod
5
- from typing import Union, List
5
+ from typing import Union, List, Optional
6
6
  from pathlib import Path
7
7
  import numpy as np
8
8
  from dataclasses import dataclass
@@ -14,6 +14,7 @@ class EmbeddingResult:
14
14
  model_name: str
15
15
  model_provider: str
16
16
  input_type: str
17
+ source_images_b64: Optional[List[str]] = None
17
18
 
18
19
  @property
19
20
  def shape(self) -> tuple:
@@ -6,7 +6,7 @@ from pathlib import Path
6
6
  import numpy as np
7
7
  from enum import Enum
8
8
 
9
- from ..utils import pdf_to_images
9
+ from ..utils import pdf_to_images, image_to_base64
10
10
  from ..base import EmbeddingProvider, EmbeddingError, EmbeddingResult
11
11
 
12
12
 
@@ -84,38 +84,11 @@ class CohereProvider(EmbeddingProvider):
84
84
  images = [images]
85
85
 
86
86
  try:
87
- import base64
88
-
89
87
  b64_images = []
90
88
  for image in images:
91
- if isinstance(image, (Path, str)):
92
- try:
93
- base64_only = base64.b64encode(Path(image).read_bytes()).decode(
94
- "utf-8"
95
- )
96
- except Exception as e:
97
- raise EmbeddingError(
98
- f"Failed to read image {image}: {e}"
99
- ) from e
100
-
101
- if isinstance(image, Path):
102
- image = str(image)
103
-
104
- if image.lower().endswith(".png"):
105
- content_type = "image/png"
106
- elif image.lower().endswith((".jpg", ".jpeg")):
107
- content_type = "image/jpeg"
108
- elif image.lower().endswith(".gif"):
109
- content_type = "image/gif"
110
- else:
111
- raise EmbeddingError(
112
- f"Unsupported image format for {image}; expected .png, .jpg, .jpeg, or .gif"
113
- )
114
- base64_image = f"data:{content_type};base64,{base64_only}"
115
- else:
116
- raise EmbeddingError(f"Unsupported image type: {type(image)}")
117
-
118
- b64_images.append(base64_image)
89
+ b64_image = image_to_base64(image)
90
+
91
+ b64_images.append(b64_image)
119
92
 
120
93
  response = client.embed(
121
94
  model=self.model_name,
@@ -129,6 +102,7 @@ class CohereProvider(EmbeddingProvider):
129
102
  model_name=self.model_name,
130
103
  model_provider=self.provider_name,
131
104
  input_type=input_type,
105
+ source_images_b64=b64_images,
132
106
  )
133
107
 
134
108
  except Exception as e:
@@ -8,7 +8,7 @@ import numpy as np
8
8
  import torch
9
9
  from PIL import Image
10
10
 
11
- from ..utils import pdf_to_images
11
+ from ..utils import pdf_to_images, image_to_base64
12
12
  from ..base import EmbeddingProvider, EmbeddingError, EmbeddingResult
13
13
 
14
14
  logger = logging.getLogger(__name__)
@@ -92,6 +92,7 @@ class ColPaliProvider(EmbeddingProvider):
92
92
 
93
93
  try:
94
94
  pil_images = []
95
+ b64_images = []
95
96
  for img_path in images:
96
97
  if not img_path.exists():
97
98
  raise EmbeddingError(f"Image not found: {img_path}")
@@ -99,8 +100,14 @@ class ColPaliProvider(EmbeddingProvider):
99
100
  with Image.open(img_path) as img:
100
101
  pil_images.append(img.convert("RGB"))
101
102
 
103
+ for image in images:
104
+ b64_image = image_to_base64(image)
105
+
106
+ b64_images.append(b64_image)
107
+
102
108
  processed = self._processor.process_images(pil_images).to(self.device)
103
109
 
110
+
104
111
  with torch.no_grad():
105
112
  embeddings = self._model(**processed)
106
113
 
@@ -109,6 +116,7 @@ class ColPaliProvider(EmbeddingProvider):
109
116
  model_name=self.model_name,
110
117
  model_provider=self.provider_name,
111
118
  input_type="image",
119
+ source_images_b64=b64_images,
112
120
  )
113
121
 
114
122
  except Exception as e:
embedkit/utils.py CHANGED
@@ -1,6 +1,7 @@
1
1
  from pdf2image import convert_from_path
2
2
  from pathlib import Path
3
3
  from .config import get_temp_dir
4
+ from typing import Union
4
5
 
5
6
 
6
7
  def pdf_to_images(pdf_path: Path) -> list[Path]:
@@ -19,3 +20,33 @@ def pdf_to_images(pdf_path: Path) -> list[Path]:
19
20
  image.save(output_path)
20
21
  image_paths.append(output_path)
21
22
  return image_paths
23
+
24
+
25
+ def image_to_base64(image_path: Union[str, Path]):
26
+ import base64
27
+
28
+ try:
29
+ base64_only = base64.b64encode(Path(image_path).read_bytes()).decode(
30
+ "utf-8"
31
+ )
32
+ except Exception as e:
33
+ raise ValueError(
34
+ f"Failed to read image {image_path}: {e}"
35
+ ) from e
36
+
37
+ if isinstance(image_path, Path):
38
+ image_path_str = str(image_path)
39
+
40
+ if image_path_str.lower().endswith(".png"):
41
+ content_type = "image/png"
42
+ elif image_path_str.lower().endswith((".jpg", ".jpeg")):
43
+ content_type = "image/jpeg"
44
+ elif image_path_str.lower().endswith(".gif"):
45
+ content_type = "image/gif"
46
+ else:
47
+ raise ValueError(
48
+ f"Unsupported image format for {image_path}; expected .png, .jpg, .jpeg, or .gif"
49
+ )
50
+ base64_image = f"data:{content_type};base64,{base64_only}"
51
+
52
+ return base64_image
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: embedkit
3
- Version: 0.1.0
3
+ Version: 0.1.1
4
4
  Summary: A simple toolkit for generating vector embeddings across multiple providers and models
5
5
  Author-email: JP Hwang <me@jphwang.com>
6
6
  License: MIT
@@ -0,0 +1,12 @@
1
+ embedkit/__init__.py,sha256=vm_dF7i_EGQsNEgBn7WPq-Vbo1xTnqV2devUvY18Z5E,3862
2
+ embedkit/base.py,sha256=wB55ksH-LT5xkEj39FUE3mbJK14DSGyK2dUpwmMMEaE,1293
3
+ embedkit/config.py,sha256=EVGODSKxQAr46bU8dyORFunsfRuj6dnvtSqa4MxUZCo,138
4
+ embedkit/models.py,sha256=EBIYkyZeIhGaOPL-9bslHHdLaZ7qzOYLd0qxVZ7VX7w,226
5
+ embedkit/utils.py,sha256=LlwUq2KIiAl6J8WpVAxiAz5V6Gj1m1ItFjBeCHdBmy8,1616
6
+ embedkit/providers/__init__.py,sha256=HaS-HNQabvhn9xLNZCq3VUqPCb7rGG4pvgvpKP4AXcw,201
7
+ embedkit/providers/cohere.py,sha256=rDyZI2UWrwcX-AgJoDotLAmMk2mBmwC-NWiyyp5msek,3589
8
+ embedkit/providers/colpali.py,sha256=RDaGyYUCWv_QgkG7jVUgEgwJy1ReKaOY7iM_nxtvR60,4127
9
+ embedkit-0.1.1.dist-info/METADATA,sha256=dCC3gxaZFrDz0S2Ab9A_Sd2cGlyRQNVQp8Z7VBfyjSo,1893
10
+ embedkit-0.1.1.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
11
+ embedkit-0.1.1.dist-info/licenses/LICENSE,sha256=-g2Rad7b3rb2oVwOTwfMOIpscHT1zuaJoguamLRCBJs,1072
12
+ embedkit-0.1.1.dist-info/RECORD,,
@@ -1,12 +0,0 @@
1
- embedkit/__init__.py,sha256=vm_dF7i_EGQsNEgBn7WPq-Vbo1xTnqV2devUvY18Z5E,3862
2
- embedkit/base.py,sha256=ZwCeDnJXVsVVT5l7ybpP5wG2ZU9e19XgV3c9OJp9z2o,1233
3
- embedkit/config.py,sha256=EVGODSKxQAr46bU8dyORFunsfRuj6dnvtSqa4MxUZCo,138
4
- embedkit/models.py,sha256=EBIYkyZeIhGaOPL-9bslHHdLaZ7qzOYLd0qxVZ7VX7w,226
5
- embedkit/utils.py,sha256=TyFyDk6tMx-PaVotixSdJDx8U3JgrPi9nV2j-rW-clw,705
6
- embedkit/providers/__init__.py,sha256=HaS-HNQabvhn9xLNZCq3VUqPCb7rGG4pvgvpKP4AXcw,201
7
- embedkit/providers/cohere.py,sha256=u6zoAjXKkjaVfTZk1VgjwRqtQ7Bea1odlVBKWomB_1A,4737
8
- embedkit/providers/colpali.py,sha256=20YAEeTvkNoexax-KhU7lWjJBdWRHPzE4Zf-6XpP3v0,3896
9
- embedkit-0.1.0.dist-info/METADATA,sha256=18DAz2h--FOgMSO3VNgm9ZXENSXK9IsVkEYm-xb2a3c,1893
10
- embedkit-0.1.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
11
- embedkit-0.1.0.dist-info/licenses/LICENSE,sha256=-g2Rad7b3rb2oVwOTwfMOIpscHT1zuaJoguamLRCBJs,1072
12
- embedkit-0.1.0.dist-info/RECORD,,