Update handler.py
Browse files- handler.py +9 -2
handler.py
CHANGED
|
@@ -43,13 +43,20 @@ class EndpointHandler:
|
|
| 43 |
# --- image embedding ---------------------------------
|
| 44 |
img = Image.open(io.BytesIO(base64.b64decode(img_b64))).convert("RGB")
|
| 45 |
img_in = self.processor(images=img, return_tensors="pt").to(self.device)
|
|
|
|
| 46 |
with torch.no_grad(), torch.cuda.amp.autocast():
|
| 47 |
img_feat = self.model.get_image_features(**img_in)
|
|
|
|
| 48 |
img_feat = img_feat / img_feat.norm(dim=-1, keepdim=True)
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
| 51 |
probs = (100 * img_feat @ txt_feat.T).softmax(dim=-1)[0].tolist()
|
| 52 |
|
|
|
|
|
|
|
| 53 |
return [
|
| 54 |
{"label": p, "score": float(s)}
|
| 55 |
for p, s in sorted(zip(prompts, probs), key=lambda x: x[1], reverse=True)
|
|
|
|
| 43 |
# --- image embedding ---------------------------------
|
| 44 |
img = Image.open(io.BytesIO(base64.b64decode(img_b64))).convert("RGB")
|
| 45 |
img_in = self.processor(images=img, return_tensors="pt").to(self.device)
|
| 46 |
+
|
| 47 |
with torch.no_grad(), torch.cuda.amp.autocast():
|
| 48 |
img_feat = self.model.get_image_features(**img_in)
|
| 49 |
+
|
| 50 |
img_feat = img_feat / img_feat.norm(dim=-1, keepdim=True)
|
| 51 |
+
txt_feat = txt_feat / txt_feat.norm(dim=-1, keepdim=True)
|
| 52 |
+
|
| 53 |
+
img_feat = img_feat.float() # ← add these two lines
|
| 54 |
+
txt_feat = txt_feat.float() # ←
|
| 55 |
+
|
| 56 |
probs = (100 * img_feat @ txt_feat.T).softmax(dim=-1)[0].tolist()
|
| 57 |
|
| 58 |
+
|
| 59 |
+
|
| 60 |
return [
|
| 61 |
{"label": p, "score": float(s)}
|
| 62 |
for p, s in sorted(zip(prompts, probs), key=lambda x: x[1], reverse=True)
|