SPP commited on
Commit ·
cd2ed90
1
Parent(s): 98ceb88
fix: ZeroGPU compatibility
Browse files- core/model.py +38 -35
core/model.py
CHANGED
|
@@ -11,7 +11,6 @@ model = AutoModelForImageTextToText.from_pretrained(
|
|
| 11 |
MODEL_ID,
|
| 12 |
torch_dtype=torch.bfloat16,
|
| 13 |
)
|
| 14 |
-
model = model.to("cuda")
|
| 15 |
model.eval()
|
| 16 |
|
| 17 |
|
|
@@ -38,43 +37,47 @@ def vision_infer(
|
|
| 38 |
if not isinstance(images, list):
|
| 39 |
images = [images]
|
| 40 |
|
| 41 |
-
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
|
| 52 |
-
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
downsample_mode="16x",
|
| 61 |
-
max_slice_nums=36,
|
| 62 |
-
).to(model.device)
|
| 63 |
-
|
| 64 |
-
with torch.no_grad():
|
| 65 |
-
generated_ids = model.generate(
|
| 66 |
-
**inputs,
|
| 67 |
downsample_mode="16x",
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
|
|
|
|
|
|
|
|
| 11 |
MODEL_ID,
|
| 12 |
torch_dtype=torch.bfloat16,
|
| 13 |
)
|
|
|
|
| 14 |
model.eval()
|
| 15 |
|
| 16 |
|
|
|
|
| 37 |
if not isinstance(images, list):
|
| 38 |
images = [images]
|
| 39 |
|
| 40 |
+
model.to("cuda")
|
| 41 |
+
try:
|
| 42 |
+
pil_images = [_to_pil(img) for img in images]
|
| 43 |
|
| 44 |
+
if json_mode:
|
| 45 |
+
instruction = (
|
| 46 |
+
instruction
|
| 47 |
+
+ "\n\nRespond with ONLY valid JSON. No markdown fences, no prose, no explanation."
|
| 48 |
+
)
|
| 49 |
|
| 50 |
+
content = [{"type": "image", "image": img} for img in pil_images]
|
| 51 |
+
content.append({"type": "text", "text": instruction})
|
| 52 |
|
| 53 |
+
messages = [{"role": "user", "content": content}]
|
| 54 |
|
| 55 |
+
inputs = processor.apply_chat_template(
|
| 56 |
+
messages,
|
| 57 |
+
tokenize=True,
|
| 58 |
+
add_generation_prompt=True,
|
| 59 |
+
return_dict=True,
|
| 60 |
+
return_tensors="pt",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
downsample_mode="16x",
|
| 62 |
+
max_slice_nums=36,
|
| 63 |
+
).to(model.device)
|
| 64 |
+
|
| 65 |
+
with torch.no_grad():
|
| 66 |
+
generated_ids = model.generate(
|
| 67 |
+
**inputs,
|
| 68 |
+
downsample_mode="16x",
|
| 69 |
+
max_new_tokens=max_tokens,
|
| 70 |
+
do_sample=False,
|
| 71 |
+
)
|
| 72 |
|
| 73 |
+
trimmed = [
|
| 74 |
+
out_ids[len(in_ids):]
|
| 75 |
+
for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
|
| 76 |
+
]
|
| 77 |
+
return processor.batch_decode(
|
| 78 |
+
trimmed,
|
| 79 |
+
skip_special_tokens=True,
|
| 80 |
+
clean_up_tokenization_spaces=False,
|
| 81 |
+
)[0]
|
| 82 |
+
finally:
|
| 83 |
+
model.to("cpu")
|