dazpye commited on
Commit
ec42764
·
verified ·
1 Parent(s): fe492f5

Update handler.py

Browse files
Files changed (1) hide show
  1. handler.py +15 -8
handler.py CHANGED
@@ -10,14 +10,21 @@ class EndpointHandler:
10
  self.model = CLIPModel.from_pretrained("dazpye/clip-image")
11
  self.processor = CLIPProcessor.from_pretrained("dazpye/clip-image")
12
 
13
- def _load_image(self, image_data):
14
- """Handles both URL-based and base64 image inputs."""
15
- if isinstance(image_data, str):
16
- if image_data.startswith("http"):
17
- return Image.open(requests.get(image_data, stream=True).raw)
18
- else: # Assume base64-encoded image
19
- return Image.open(io.BytesIO(base64.b64decode(image_data)))
20
- return None # Invalid image format
 
 
 
 
 
 
 
21
 
22
  def __call__(self, data):
23
  """Main inference function Hugging Face expects."""
 
10
  self.model = CLIPModel.from_pretrained("dazpye/clip-image")
11
  self.processor = CLIPProcessor.from_pretrained("dazpye/clip-image")
12
 
13
+ def _load_image(self, image_url):
14
+ """Fetches an image and ensures it is fully loaded."""
15
+ try:
16
+ print(f"Fetching image from: {image_url}")
17
+ response = requests.get(image_url, timeout=5)
18
+ print(f"HTTP Status Code: {response.status_code}")
19
+
20
+ if response.status_code == 200:
21
+ image_bytes = io.BytesIO(response.content) # Convert to bytes
22
+ return Image.open(image_bytes)
23
+ else:
24
+ print(f"❌ Failed to fetch image: HTTP {response.status_code}")
25
+ except Exception as e:
26
+ print(f"❌ Exception in image loading: {e}")
27
+ return None # Return None if image loading fails
28
 
29
  def __call__(self, data):
30
  """Main inference function Hugging Face expects."""