Update handler.py
Browse files- handler.py +24 -24
handler.py
CHANGED
@@ -1,37 +1,37 @@
|
|
1 |
-
from
|
2 |
from PIL import Image
|
|
|
3 |
from io import BytesIO
|
4 |
import base64
|
5 |
-
import
|
6 |
-
import open_clip
|
7 |
|
8 |
class EndpointHandler():
|
9 |
def __init__(self, path=""):
|
10 |
-
|
11 |
-
self.
|
|
|
12 |
|
13 |
-
|
14 |
-
|
15 |
-
parameters = data.get("parameters", None)
|
16 |
-
if image_base64 is None or parameters is None:
|
17 |
-
raise ValueError("Input data or parameters not provided")
|
18 |
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
|
23 |
-
|
24 |
-
|
25 |
-
text = self.tokenizer(candidate_labels)
|
26 |
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
|
|
|
|
|
|
32 |
|
33 |
-
|
|
|
34 |
|
35 |
-
|
36 |
return results
|
37 |
-
|
|
|
1 |
+
from transformers import CLIPProcessor, CLIPModel, pipeline
|
2 |
from PIL import Image
|
3 |
+
import requests
|
4 |
from io import BytesIO
|
5 |
import base64
|
6 |
+
from typing import Dict, List, Any
|
|
|
7 |
|
8 |
class EndpointHandler():
|
9 |
def __init__(self, path=""):
|
10 |
+
# Carrega el model i el processor espec铆fics de CLIP
|
11 |
+
self.model = CLIPModel.from_pretrained(path)
|
12 |
+
self.processor = CLIPProcessor.from_pretrained(path)
|
13 |
|
14 |
+
# Crea la pipeline de classificaci贸 d'imatges zero-shot
|
15 |
+
self.classifier = pipeline("zero-shot-image-classification", model=self.model, tokenizer=self.processor)
|
|
|
|
|
|
|
16 |
|
17 |
+
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
|
18 |
+
image_input = data.get("inputs", None)
|
19 |
+
candidate_labels = data.get("candidate_labels", None)
|
20 |
|
21 |
+
if image_input is None or candidate_labels is None:
|
22 |
+
raise ValueError("Image input or candidate labels not provided")
|
|
|
23 |
|
24 |
+
# Determina si l'input 茅s una URL o una cadena base64
|
25 |
+
if image_input.startswith("http"):
|
26 |
+
response = requests.get(image_input)
|
27 |
+
image = Image.open(BytesIO(response.content))
|
28 |
+
else:
|
29 |
+
# Suposa que l'input 茅s base64 i decodifica-la
|
30 |
+
image_data = base64.b64decode(image_input)
|
31 |
+
image = Image.open(BytesIO(image_data))
|
32 |
|
33 |
+
# Realitza la classificaci贸 zero-shot
|
34 |
+
results = self.classifier(images=image, candidate_labels=candidate_labels)
|
35 |
|
36 |
+
# Torna els resultats processats
|
37 |
return results
|
|