yanmuyuan
commited on
Commit
·
a026586
1
Parent(s):
f0e3abd
fix
Browse files- 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.
|
| 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
|
| 31 |
inputs = data.pop("inputs", data)
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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)
|