Update handler.py
Browse files- handler.py +7 -7
handler.py
CHANGED
@@ -11,19 +11,20 @@ class EndpointHandler():
|
|
11 |
self.model = Qwen2VLForConditionalGeneration.from_pretrained(path)
|
12 |
|
13 |
def __call__(self, data: Any) -> Dict[str, Any]:
|
|
|
|
|
14 |
|
15 |
-
image_input
|
16 |
-
|
17 |
-
|
18 |
|
19 |
-
|
20 |
if image_input.startswith('http'):
|
21 |
image = Image.open(requests.get(image_input, stream=True).raw).convert('RGB')
|
22 |
else:
|
23 |
image_data = base64.b64decode(image_input)
|
24 |
image = Image.open(io.BytesIO(image_data)).convert('RGB')
|
25 |
-
|
26 |
-
return {"error": "
|
27 |
|
28 |
messages = [
|
29 |
{
|
@@ -49,4 +50,3 @@ class EndpointHandler():
|
|
49 |
)[0]
|
50 |
|
51 |
return {"generated_text": output_text}
|
52 |
-
|
|
|
11 |
self.model = Qwen2VLForConditionalGeneration.from_pretrained(path)
|
12 |
|
13 |
def __call__(self, data: Any) -> Dict[str, Any]:
|
14 |
+
image_input = data.get('image')
|
15 |
+
text_input = data.get('text', "Describe this image.")
|
16 |
|
17 |
+
if image_input is None:
|
18 |
+
return {"error": "No image provided."}
|
|
|
19 |
|
20 |
+
try:
|
21 |
if image_input.startswith('http'):
|
22 |
image = Image.open(requests.get(image_input, stream=True).raw).convert('RGB')
|
23 |
else:
|
24 |
image_data = base64.b64decode(image_input)
|
25 |
image = Image.open(io.BytesIO(image_data)).convert('RGB')
|
26 |
+
except Exception as e:
|
27 |
+
return {"error": f"Failed to process the image. Details: {str(e)}"}
|
28 |
|
29 |
messages = [
|
30 |
{
|
|
|
50 |
)[0]
|
51 |
|
52 |
return {"generated_text": output_text}
|
|