Apiarist Dev commited on
Commit Β·
be1617f
1
Parent(s): 9383bcd
swap to Qwen2.5-VL-3B (sweet spot: better vision than SmolVLM, fits ZeroGPU)
Browse files
app.py
CHANGED
|
@@ -10,24 +10,23 @@ from PIL import Image
|
|
| 10 |
import json
|
| 11 |
import re
|
| 12 |
import torch
|
| 13 |
-
from transformers import AutoProcessor,
|
| 14 |
|
| 15 |
# ZeroGPU integration β no-op outside HF Spaces
|
| 16 |
try:
|
| 17 |
import spaces
|
| 18 |
|
| 19 |
def gpu(fn):
|
| 20 |
-
return spaces.GPU(duration=
|
| 21 |
except ImportError:
|
| 22 |
def gpu(fn):
|
| 23 |
return fn
|
| 24 |
|
| 25 |
|
| 26 |
-
#
|
| 27 |
-
#
|
| 28 |
-
#
|
| 29 |
-
|
| 30 |
-
MODEL_ID = "HuggingFaceTB/SmolVLM-Instruct"
|
| 31 |
|
| 32 |
_model = None
|
| 33 |
_processor = None
|
|
@@ -39,7 +38,10 @@ def get_model():
|
|
| 39 |
if _model is None:
|
| 40 |
print(f"Loading {MODEL_ID} ...")
|
| 41 |
_processor = AutoProcessor.from_pretrained(MODEL_ID)
|
| 42 |
-
_model =
|
|
|
|
|
|
|
|
|
|
| 43 |
_model.eval()
|
| 44 |
print("Model loaded.")
|
| 45 |
return _model, _processor
|
|
@@ -105,7 +107,7 @@ def build_narrative(r: dict, raw: str) -> str:
|
|
| 105 |
**Notes:** {r['notes']}
|
| 106 |
|
| 107 |
---
|
| 108 |
-
*Powered by
|
| 109 |
|
| 110 |
<details><summary>Raw model output</summary>
|
| 111 |
|
|
@@ -131,17 +133,20 @@ def analyze_frame(image: Image.Image, hive_name: str):
|
|
| 131 |
{
|
| 132 |
"role": "user",
|
| 133 |
"content": [
|
| 134 |
-
{"type": "image"},
|
| 135 |
{"type": "text", "text": INSPECTION_PROMPT},
|
| 136 |
],
|
| 137 |
}
|
| 138 |
]
|
| 139 |
|
| 140 |
try:
|
| 141 |
-
|
| 142 |
-
messages,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
)
|
| 144 |
-
inputs = processor(text=prompt, images=[image], return_tensors="pt")
|
| 145 |
inputs = inputs.to(device)
|
| 146 |
|
| 147 |
with torch.no_grad():
|
|
@@ -236,7 +241,7 @@ with gr.Blocks(title="Apiarist - Hive Frame Inspector") as app:
|
|
| 236 |
- π― Vision-language model fine-tuned for honeybees
|
| 237 |
- π Built in 10 days for the [Build Small Hackathon](https://huggingface.co/build-small-hackathon)
|
| 238 |
|
| 239 |
-
**Stack**:
|
| 240 |
"""
|
| 241 |
)
|
| 242 |
|
|
|
|
| 10 |
import json
|
| 11 |
import re
|
| 12 |
import torch
|
| 13 |
+
from transformers import AutoProcessor, AutoModelForImageTextToText
|
| 14 |
|
| 15 |
# ZeroGPU integration β no-op outside HF Spaces
|
| 16 |
try:
|
| 17 |
import spaces
|
| 18 |
|
| 19 |
def gpu(fn):
|
| 20 |
+
return spaces.GPU(duration=90)(fn)
|
| 21 |
except ImportError:
|
| 22 |
def gpu(fn):
|
| 23 |
return fn
|
| 24 |
|
| 25 |
|
| 26 |
+
# Qwen2.5-VL-3B. State-of-the-art vision-language model at a size that
|
| 27 |
+
# comfortably fits on ZeroGPU. SmolVLM-2.25B hallucinated on bee anatomy;
|
| 28 |
+
# Qwen-7B crashed the container; this is the sweet spot.
|
| 29 |
+
MODEL_ID = "Qwen/Qwen2.5-VL-3B-Instruct"
|
|
|
|
| 30 |
|
| 31 |
_model = None
|
| 32 |
_processor = None
|
|
|
|
| 38 |
if _model is None:
|
| 39 |
print(f"Loading {MODEL_ID} ...")
|
| 40 |
_processor = AutoProcessor.from_pretrained(MODEL_ID)
|
| 41 |
+
_model = AutoModelForImageTextToText.from_pretrained(
|
| 42 |
+
MODEL_ID,
|
| 43 |
+
torch_dtype=torch.float16,
|
| 44 |
+
)
|
| 45 |
_model.eval()
|
| 46 |
print("Model loaded.")
|
| 47 |
return _model, _processor
|
|
|
|
| 107 |
**Notes:** {r['notes']}
|
| 108 |
|
| 109 |
---
|
| 110 |
+
*Powered by Qwen2.5-VL-3B on ZeroGPU. Fully local, no cloud APIs.*
|
| 111 |
|
| 112 |
<details><summary>Raw model output</summary>
|
| 113 |
|
|
|
|
| 133 |
{
|
| 134 |
"role": "user",
|
| 135 |
"content": [
|
| 136 |
+
{"type": "image", "image": image},
|
| 137 |
{"type": "text", "text": INSPECTION_PROMPT},
|
| 138 |
],
|
| 139 |
}
|
| 140 |
]
|
| 141 |
|
| 142 |
try:
|
| 143 |
+
inputs = processor.apply_chat_template(
|
| 144 |
+
messages,
|
| 145 |
+
tokenize=True,
|
| 146 |
+
add_generation_prompt=True,
|
| 147 |
+
return_dict=True,
|
| 148 |
+
return_tensors="pt",
|
| 149 |
)
|
|
|
|
| 150 |
inputs = inputs.to(device)
|
| 151 |
|
| 152 |
with torch.no_grad():
|
|
|
|
| 241 |
- π― Vision-language model fine-tuned for honeybees
|
| 242 |
- π Built in 10 days for the [Build Small Hackathon](https://huggingface.co/build-small-hackathon)
|
| 243 |
|
| 244 |
+
**Stack**: Qwen2.5-VL-3B on ZeroGPU, served via Gradio.
|
| 245 |
"""
|
| 246 |
)
|
| 247 |
|