Update handler.py
Browse files- handler.py +16 -6
handler.py
CHANGED
|
@@ -11,7 +11,7 @@ class EndpointHandler():
|
|
| 11 |
model_id = path
|
| 12 |
self.model = LlavaForConditionalGeneration.from_pretrained(
|
| 13 |
model_id,
|
| 14 |
-
torch_dtype=torch.float32
|
| 15 |
low_cpu_mem_usage=True,
|
| 16 |
load_in_4bit=True
|
| 17 |
)
|
|
@@ -23,9 +23,19 @@ class EndpointHandler():
|
|
| 23 |
|
| 24 |
outputs = []
|
| 25 |
for link in parameters:
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
return outputs
|
|
|
|
| 11 |
model_id = path
|
| 12 |
self.model = LlavaForConditionalGeneration.from_pretrained(
|
| 13 |
model_id,
|
| 14 |
+
torch_dtype=torch.float32,
|
| 15 |
low_cpu_mem_usage=True,
|
| 16 |
load_in_4bit=True
|
| 17 |
)
|
|
|
|
| 23 |
|
| 24 |
outputs = []
|
| 25 |
for link in parameters:
|
| 26 |
+
try:
|
| 27 |
+
# Fetch image from URL
|
| 28 |
+
response = requests.get(link, stream=True)
|
| 29 |
+
response.raise_for_status() # Raise an exception for 4xx or 5xx status codes
|
| 30 |
+
raw_image = Image.open(response.raw)
|
| 31 |
+
|
| 32 |
+
# Process image and generate output
|
| 33 |
+
inputs = self.processor(prompt, raw_image, return_tensors='pt').to(0, torch.float32)
|
| 34 |
+
output = self.model.generate(**inputs, max_new_tokens=200, do_sample=False)
|
| 35 |
+
readable = self.processor.decode(output[0][2:], skip_special_tokens=True)
|
| 36 |
+
outputs.append(readable)
|
| 37 |
+
except Exception as e:
|
| 38 |
+
# Handle any exceptions and log the error
|
| 39 |
+
outputs.append(f"Error processing image from {link}: {str(e)}")
|
| 40 |
+
|
| 41 |
return outputs
|