Update handler.py
Browse files- handler.py +10 -3
handler.py
CHANGED
|
@@ -6,13 +6,16 @@ from io import BytesIO
|
|
| 6 |
import base64
|
| 7 |
import ssl
|
| 8 |
import urllib3
|
|
|
|
|
|
|
| 9 |
urllib3.disable_warnings()
|
| 10 |
ssl._create_default_https_context = ssl._create_unverified_context
|
| 11 |
|
| 12 |
class EndpointHandler:
|
| 13 |
-
def __init__(self):
|
| 14 |
self.model = None
|
| 15 |
self.tokenizer = None
|
|
|
|
| 16 |
|
| 17 |
def load_model(self):
|
| 18 |
model_name = "openbmb/MiniCPM-V-2_6"
|
|
@@ -43,17 +46,21 @@ class EndpointHandler:
|
|
| 43 |
|
| 44 |
try:
|
| 45 |
if image_input.startswith("http"):
|
|
|
|
| 46 |
response = requests.get(image_input, verify=False)
|
| 47 |
image = Image.open(BytesIO(response.content)).convert("RGB")
|
| 48 |
else:
|
|
|
|
| 49 |
image = Image.open(BytesIO(base64.b64decode(image_input))).convert("RGB")
|
| 50 |
except Exception as e:
|
| 51 |
return {"error": f"Failed to load image: {e}"}
|
| 52 |
|
|
|
|
| 53 |
msgs = [{"role": "user", "content": [image, question]}]
|
| 54 |
|
| 55 |
try:
|
| 56 |
if stream:
|
|
|
|
| 57 |
generated_text = ""
|
| 58 |
for new_text in self.model.chat(
|
| 59 |
image=None,
|
|
@@ -65,6 +72,7 @@ class EndpointHandler:
|
|
| 65 |
generated_text += new_text
|
| 66 |
return {"output": generated_text}
|
| 67 |
else:
|
|
|
|
| 68 |
output = self.model.chat(
|
| 69 |
image=None,
|
| 70 |
msgs=msgs,
|
|
@@ -77,8 +85,7 @@ class EndpointHandler:
|
|
| 77 |
|
| 78 |
# Test block (optional, remove in production)
|
| 79 |
if __name__ == "__main__":
|
| 80 |
-
handler =
|
| 81 |
-
handler.load_model()
|
| 82 |
result = handler.predict({
|
| 83 |
"image": "https://upload.wikimedia.org/wikipedia/commons/9/9e/Ours_brun_parcanimalierpyrenees_1.jpg",
|
| 84 |
"question": "What animal is this?"
|
|
|
|
| 6 |
import base64
|
| 7 |
import ssl
|
| 8 |
import urllib3
|
| 9 |
+
|
| 10 |
+
# Disable SSL warnings and errors for custom models
|
| 11 |
urllib3.disable_warnings()
|
| 12 |
ssl._create_default_https_context = ssl._create_unverified_context
|
| 13 |
|
| 14 |
class EndpointHandler:
|
| 15 |
+
def __init__(self, model_dir=None):
|
| 16 |
self.model = None
|
| 17 |
self.tokenizer = None
|
| 18 |
+
self.load_model()
|
| 19 |
|
| 20 |
def load_model(self):
|
| 21 |
model_name = "openbmb/MiniCPM-V-2_6"
|
|
|
|
| 46 |
|
| 47 |
try:
|
| 48 |
if image_input.startswith("http"):
|
| 49 |
+
# Load image from URL
|
| 50 |
response = requests.get(image_input, verify=False)
|
| 51 |
image = Image.open(BytesIO(response.content)).convert("RGB")
|
| 52 |
else:
|
| 53 |
+
# Load image from base64 string
|
| 54 |
image = Image.open(BytesIO(base64.b64decode(image_input))).convert("RGB")
|
| 55 |
except Exception as e:
|
| 56 |
return {"error": f"Failed to load image: {e}"}
|
| 57 |
|
| 58 |
+
# Prepare message for the model
|
| 59 |
msgs = [{"role": "user", "content": [image, question]}]
|
| 60 |
|
| 61 |
try:
|
| 62 |
if stream:
|
| 63 |
+
# If streaming is enabled, collect the output incrementally
|
| 64 |
generated_text = ""
|
| 65 |
for new_text in self.model.chat(
|
| 66 |
image=None,
|
|
|
|
| 72 |
generated_text += new_text
|
| 73 |
return {"output": generated_text}
|
| 74 |
else:
|
| 75 |
+
# If streaming is not enabled, get the complete output
|
| 76 |
output = self.model.chat(
|
| 77 |
image=None,
|
| 78 |
msgs=msgs,
|
|
|
|
| 85 |
|
| 86 |
# Test block (optional, remove in production)
|
| 87 |
if __name__ == "__main__":
|
| 88 |
+
handler = EndpointHandler()
|
|
|
|
| 89 |
result = handler.predict({
|
| 90 |
"image": "https://upload.wikimedia.org/wikipedia/commons/9/9e/Ours_brun_parcanimalierpyrenees_1.jpg",
|
| 91 |
"question": "What animal is this?"
|