Refactor Dockerfile and encoder.py to improve environment variable handling and model initialization
Browse files- Dockerfile +0 -1
- src/encoder.py +22 -7
Dockerfile
CHANGED
@@ -3,7 +3,6 @@ FROM python:3.9
|
|
3 |
WORKDIR /app
|
4 |
|
5 |
ENV HF_HOME=/app/hf_cache
|
6 |
-
ENV HF_TOKEN=${HF_TOKEN}
|
7 |
RUN mkdir -p /app/hf_cache && chmod 777 /app/hf_cache
|
8 |
|
9 |
COPY requirements.txt .
|
|
|
3 |
WORKDIR /app
|
4 |
|
5 |
ENV HF_HOME=/app/hf_cache
|
|
|
6 |
RUN mkdir -p /app/hf_cache && chmod 777 /app/hf_cache
|
7 |
|
8 |
COPY requirements.txt .
|
src/encoder.py
CHANGED
@@ -1,23 +1,38 @@
|
|
1 |
from typing import List, Dict
|
2 |
from PIL.Image import Image
|
|
|
3 |
|
4 |
import torch
|
5 |
from transformers import AutoModel, AutoProcessor
|
6 |
|
7 |
|
8 |
MODEL_NAME = "Marqo/marqo-fashionCLIP"
|
|
|
9 |
|
10 |
|
11 |
class FashionCLIPEncoder:
|
12 |
def __init__(self):
|
|
|
|
|
13 |
self.processor = AutoProcessor.from_pretrained(
|
14 |
-
MODEL_NAME,
|
|
|
|
|
15 |
)
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
def encode_text(self, texts: List[str]) -> List[List[float]]:
|
23 |
kwargs = {
|
@@ -45,4 +60,4 @@ class FashionCLIPEncoder:
|
|
45 |
return self.model.get_text_features(**batch).detach().cpu().numpy().tolist()
|
46 |
|
47 |
def _encode_images(self, batch: Dict) -> List[List[float]]:
|
48 |
-
return self.model.get_image_features(**batch).detach().cpu().numpy().tolist()
|
|
|
1 |
from typing import List, Dict
|
2 |
from PIL.Image import Image
|
3 |
+
import os
|
4 |
|
5 |
import torch
|
6 |
from transformers import AutoModel, AutoProcessor
|
7 |
|
8 |
|
9 |
MODEL_NAME = "Marqo/marqo-fashionCLIP"
|
10 |
+
HF_TOKEN = os.environ.get("HF_TOKEN")
|
11 |
|
12 |
|
13 |
class FashionCLIPEncoder:
|
14 |
def __init__(self):
|
15 |
+
self.device = torch.device("cpu")
|
16 |
+
|
17 |
self.processor = AutoProcessor.from_pretrained(
|
18 |
+
MODEL_NAME,
|
19 |
+
trust_remote_code=True,
|
20 |
+
token=HF_TOKEN
|
21 |
)
|
22 |
+
|
23 |
+
try:
|
24 |
+
self.model = AutoModel.from_pretrained(
|
25 |
+
MODEL_NAME,
|
26 |
+
trust_remote_code=True,
|
27 |
+
device_map=None,
|
28 |
+
token=HF_TOKEN
|
29 |
+
)
|
30 |
+
self.model = self.model.to(self.device)
|
31 |
+
self.model.eval()
|
32 |
+
|
33 |
+
except Exception as e:
|
34 |
+
print(f"Error initializing model: {str(e)}")
|
35 |
+
raise
|
36 |
|
37 |
def encode_text(self, texts: List[str]) -> List[List[float]]:
|
38 |
kwargs = {
|
|
|
60 |
return self.model.get_text_features(**batch).detach().cpu().numpy().tolist()
|
61 |
|
62 |
def _encode_images(self, batch: Dict) -> List[List[float]]:
|
63 |
+
return self.model.get_image_features(**batch).detach().cpu().numpy().tolist()
|