pavun commited on
Commit ·
34806df
1
Parent(s): 104d0a3
Implement EndpointHandler class for image and text processing
Browse files- handler.py +71 -0
- requirements.txt +5 -0
handler.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import base64
|
| 2 |
+
import io
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
from transformers import Qwen2_5_VLForConditionalGeneration, AutoProcessor
|
| 7 |
+
from qwen_vl_utils import process_vision_info
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
class EndpointHandler:
|
| 11 |
+
|
| 12 |
+
def __init__(self, path=""):
|
| 13 |
+
|
| 14 |
+
self.model = Qwen2_5_VLForConditionalGeneration.from_pretrained(
|
| 15 |
+
path,
|
| 16 |
+
torch_dtype=torch.bfloat16,
|
| 17 |
+
device_map="auto",
|
| 18 |
+
trust_remote_code=True
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
self.processor = AutoProcessor.from_pretrained(
|
| 22 |
+
path,
|
| 23 |
+
trust_remote_code=True
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
def __call__(self, data):
|
| 27 |
+
|
| 28 |
+
image_b64 = data["inputs"]["image"]
|
| 29 |
+
prompt = data["inputs"]["text"]
|
| 30 |
+
|
| 31 |
+
image = Image.open(io.BytesIO(base64.b64decode(image_b64)))
|
| 32 |
+
|
| 33 |
+
messages = [
|
| 34 |
+
{
|
| 35 |
+
"role": "user",
|
| 36 |
+
"content": [
|
| 37 |
+
{"type": "image", "image": image},
|
| 38 |
+
{"type": "text", "text": prompt},
|
| 39 |
+
],
|
| 40 |
+
}
|
| 41 |
+
]
|
| 42 |
+
|
| 43 |
+
text = self.processor.apply_chat_template(
|
| 44 |
+
messages,
|
| 45 |
+
tokenize=False,
|
| 46 |
+
add_generation_prompt=True
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
image_inputs, video_inputs = process_vision_info(messages)
|
| 50 |
+
|
| 51 |
+
inputs = self.processor(
|
| 52 |
+
text=[text],
|
| 53 |
+
images=image_inputs,
|
| 54 |
+
videos=video_inputs,
|
| 55 |
+
padding=True,
|
| 56 |
+
return_tensors="pt",
|
| 57 |
+
).to(self.model.device)
|
| 58 |
+
|
| 59 |
+
outputs = self.model.generate(**inputs, max_new_tokens=512)
|
| 60 |
+
|
| 61 |
+
generated_ids_trimmed = [
|
| 62 |
+
out_ids[len(in_ids):]
|
| 63 |
+
for in_ids, out_ids in zip(inputs.input_ids, outputs)
|
| 64 |
+
]
|
| 65 |
+
|
| 66 |
+
decoded = self.processor.batch_decode(
|
| 67 |
+
generated_ids_trimmed,
|
| 68 |
+
skip_special_tokens=True
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
return decoded[0]
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch
|
| 2 |
+
transformers
|
| 3 |
+
accelerate
|
| 4 |
+
Pillow
|
| 5 |
+
qwen-vl-utils
|