pediot commited on
Commit
fdf1598
·
1 Parent(s): 67ff13f
Files changed (3) hide show
  1. app.py +2 -24
  2. src/encoder.py +4 -4
  3. src/utils.py +17 -15
app.py CHANGED
@@ -1,18 +1,10 @@
1
- from fastapi import FastAPI #, HTTPException, status, Security
2
- # from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
3
- from decouple import config
4
 
5
  from src.encoder import FashionCLIPEncoder
6
  from src.models import TextRequest, ImageRequest, Response
7
 
8
 
9
- # security = HTTPBearer()
10
  encoder = FashionCLIPEncoder()
11
-
12
-
13
- API_TOKEN = config("API_TOKEN")
14
-
15
-
16
  app = FastAPI()
17
 
18
 
@@ -30,15 +22,8 @@ async def root():
30
 
31
  @app.post("/encode_texts")
32
  async def encode_texts(
33
- request: TextRequest,
34
- # credentials: HTTPAuthorizationCredentials = Security(security)
35
  ) -> Response:
36
- # if credentials.credentials != API_TOKEN:
37
- # raise HTTPException(
38
- # status_code=status.HTTP_401_UNAUTHORIZED,
39
- # detail="Invalid authentication token",
40
- # )
41
-
42
  embeddings = encoder.encode_text(request.texts)
43
  response = Response(embeddings=embeddings)
44
 
@@ -48,14 +33,7 @@ async def encode_texts(
48
  @app.post("/encode_images")
49
  async def encode_images(
50
  request: ImageRequest,
51
- # credentials: HTTPAuthorizationCredentials = Security(security),
52
  ) -> Response:
53
- # if credentials.credentials != API_TOKEN:
54
- # raise HTTPException(
55
- # status_code=status.HTTP_401_UNAUTHORIZED,
56
- # detail="Invalid authentication token",
57
- # )
58
-
59
  images = request.download()
60
  embeddings = encoder.encode_images(images)
61
  response = Response(embeddings=embeddings)
 
1
+ from fastapi import FastAPI
 
 
2
 
3
  from src.encoder import FashionCLIPEncoder
4
  from src.models import TextRequest, ImageRequest, Response
5
 
6
 
 
7
  encoder = FashionCLIPEncoder()
 
 
 
 
 
8
  app = FastAPI()
9
 
10
 
 
22
 
23
  @app.post("/encode_texts")
24
  async def encode_texts(
25
+ request: TextRequest,
 
26
  ) -> Response:
 
 
 
 
 
 
27
  embeddings = encoder.encode_text(request.texts)
28
  response = Response(embeddings=embeddings)
29
 
 
33
  @app.post("/encode_images")
34
  async def encode_images(
35
  request: ImageRequest,
 
36
  ) -> Response:
 
 
 
 
 
 
37
  images = request.download()
38
  embeddings = encoder.encode_images(images)
39
  response = Response(embeddings=embeddings)
src/encoder.py CHANGED
@@ -11,12 +11,12 @@ MODEL_NAME = "Marqo/marqo-fashionCLIP"
11
  class FashionCLIPEncoder:
12
  def __init__(self):
13
  self.device = torch.device("cpu")
14
-
15
  self.processor = AutoProcessor.from_pretrained(
16
- MODEL_NAME,
17
  trust_remote_code=True,
18
  )
19
-
20
  self.model = AutoModel.from_pretrained(
21
  MODEL_NAME,
22
  trust_remote_code=True,
@@ -51,4 +51,4 @@ class FashionCLIPEncoder:
51
  return self.model.get_text_features(**batch).detach().cpu().numpy().tolist()
52
 
53
  def _encode_images(self, batch: Dict) -> List[List[float]]:
54
- return self.model.get_image_features(**batch).detach().cpu().numpy().tolist()
 
11
  class FashionCLIPEncoder:
12
  def __init__(self):
13
  self.device = torch.device("cpu")
14
+
15
  self.processor = AutoProcessor.from_pretrained(
16
+ MODEL_NAME,
17
  trust_remote_code=True,
18
  )
19
+
20
  self.model = AutoModel.from_pretrained(
21
  MODEL_NAME,
22
  trust_remote_code=True,
 
51
  return self.model.get_text_features(**batch).detach().cpu().numpy().tolist()
52
 
53
  def _encode_images(self, batch: Dict) -> List[List[float]]:
54
+ return self.model.get_image_features(**batch).detach().cpu().numpy().tolist()
src/utils.py CHANGED
@@ -26,39 +26,41 @@ def analyze_model_parameters(model: torch.nn.Module) -> Dict:
26
  total_params = 0
27
  param_types = set()
28
  param_type_counts = {}
29
-
30
  for param in model.parameters():
31
  total_params += param.numel()
32
  dtype = param.dtype
33
  param_types.add(dtype)
34
  param_type_counts[dtype] = param_type_counts.get(dtype, 0) + param.numel()
35
-
36
  results = {
37
  "total_params": total_params,
38
  "param_types": {},
39
  "device_info": {
40
  "device": next(model.parameters()).device,
41
- "cuda_available": torch.cuda.is_available()
42
- }
43
  }
44
-
45
  for dtype in param_types:
46
  count = param_type_counts[dtype]
47
  percentage = (count / total_params) * 100
48
  memory_bytes = count * torch.finfo(dtype).bits // 8
49
  memory_mb = memory_bytes / (1024 * 1024)
50
-
51
  results["param_types"][str(dtype)] = {
52
  "count": count,
53
  "percentage": percentage,
54
- "memory_mb": memory_mb
55
  }
56
-
57
  if torch.cuda.is_available():
58
- results["device_info"].update({
59
- "cuda_device": torch.cuda.get_device_name(0),
60
- "cuda_memory_allocated_mb": torch.cuda.memory_allocated(0) / 1024**2,
61
- "cuda_memory_cached_mb": torch.cuda.memory_reserved(0) / 1024**2
62
- })
63
-
64
- return results
 
 
 
26
  total_params = 0
27
  param_types = set()
28
  param_type_counts = {}
29
+
30
  for param in model.parameters():
31
  total_params += param.numel()
32
  dtype = param.dtype
33
  param_types.add(dtype)
34
  param_type_counts[dtype] = param_type_counts.get(dtype, 0) + param.numel()
35
+
36
  results = {
37
  "total_params": total_params,
38
  "param_types": {},
39
  "device_info": {
40
  "device": next(model.parameters()).device,
41
+ "cuda_available": torch.cuda.is_available(),
42
+ },
43
  }
44
+
45
  for dtype in param_types:
46
  count = param_type_counts[dtype]
47
  percentage = (count / total_params) * 100
48
  memory_bytes = count * torch.finfo(dtype).bits // 8
49
  memory_mb = memory_bytes / (1024 * 1024)
50
+
51
  results["param_types"][str(dtype)] = {
52
  "count": count,
53
  "percentage": percentage,
54
+ "memory_mb": memory_mb,
55
  }
56
+
57
  if torch.cuda.is_available():
58
+ results["device_info"].update(
59
+ {
60
+ "cuda_device": torch.cuda.get_device_name(0),
61
+ "cuda_memory_allocated_mb": torch.cuda.memory_allocated(0) / 1024**2,
62
+ "cuda_memory_cached_mb": torch.cuda.memory_reserved(0) / 1024**2,
63
+ }
64
+ )
65
+
66
+ return results