yanmuyuan commited on
Commit
a026586
·
1 Parent(s): f0e3abd
Files changed (1) hide show
  1. handler.py +8 -3
handler.py CHANGED
@@ -21,15 +21,20 @@ class EndpointHandler:
21
  This method is called for every API request.
22
 
23
  Args:
24
- data (Dict): The input data dictionary. Expects "inputs" key with image bytes.
25
 
26
  Returns:
27
  Dict[str, str]: A dictionary with a single key "generated_text",
28
  containing a JSON string of the results.
29
  """
30
- # Get image bytes from the request
31
  inputs = data.pop("inputs", data)
32
- image = Image.open(io.BytesIO(inputs))
 
 
 
 
 
33
 
34
  # Pass the image to the pipeline
35
  prediction = self.pipe(image)
 
21
  This method is called for every API request.
22
 
23
  Args:
24
+ data (Dict): The input data dictionary. Can be PIL Image or bytes.
25
 
26
  Returns:
27
  Dict[str, str]: A dictionary with a single key "generated_text",
28
  containing a JSON string of the results.
29
  """
30
+ # Get image from the request
31
  inputs = data.pop("inputs", data)
32
+
33
+ # Handle both PIL Image objects (from image content-type) and bytes (from JSON)
34
+ if isinstance(inputs, Image.Image):
35
+ image = inputs
36
+ else:
37
+ image = Image.open(io.BytesIO(inputs))
38
 
39
  # Pass the image to the pipeline
40
  prediction = self.pipe(image)