forked from weaviate/i2v-pytorch-models
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvectorizer.py
More file actions
43 lines (33 loc) · 1.07 KB
/
vectorizer.py
File metadata and controls
43 lines (33 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import base64
import os
from pydantic import BaseModel
from image2vec_vit import Img2VecViT
class VectorImagePayload(BaseModel):
id: str
image: str
class ImageVectorizer:
img2vec: Img2VecViT
def __init__(self, cuda_support, cuda_core):
self.img2vec = Img2VecViT(cuda_support, cuda_core)
def vectorize(self, id: str, image: str):
try:
filepath = self.saveImage(id, image)
return self.img2vec.get_vec(filepath)
except (RuntimeError, TypeError, NameError, Exception) as e:
print("vectorize error:", e)
raise e
finally:
self.removeFile(filepath)
def saveImage(self, id: str, image: str):
try:
filepath = id
file_content = base64.b64decode(image)
with open(filepath, "wb") as f:
f.write(file_content)
return filepath
except Exception as e:
print(str(e))
return ""
def removeFile(self, filepath: str):
if os.path.exists(filepath):
os.remove(filepath)