Apiarist Dev commited on
Commit Β·
9383bcd
1
Parent(s): ec6806d
revert: Qwen2.5-VL-7B crashes ZeroGPU container, use SmolVLM-Instruct 2.25B (known good)
Browse files
app.py
CHANGED
|
@@ -10,7 +10,7 @@ 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:
|
|
@@ -23,7 +23,11 @@ except ImportError:
|
|
| 23 |
return fn
|
| 24 |
|
| 25 |
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
_model = None
|
| 29 |
_processor = None
|
|
@@ -35,10 +39,7 @@ def get_model():
|
|
| 35 |
if _model is None:
|
| 36 |
print(f"Loading {MODEL_ID} ...")
|
| 37 |
_processor = AutoProcessor.from_pretrained(MODEL_ID)
|
| 38 |
-
_model =
|
| 39 |
-
MODEL_ID,
|
| 40 |
-
torch_dtype=torch.bfloat16,
|
| 41 |
-
)
|
| 42 |
_model.eval()
|
| 43 |
print("Model loaded.")
|
| 44 |
return _model, _processor
|
|
@@ -104,7 +105,7 @@ def build_narrative(r: dict, raw: str) -> str:
|
|
| 104 |
**Notes:** {r['notes']}
|
| 105 |
|
| 106 |
---
|
| 107 |
-
*Powered by
|
| 108 |
|
| 109 |
<details><summary>Raw model output</summary>
|
| 110 |
|
|
@@ -123,27 +124,24 @@ def analyze_frame(image: Image.Image, hive_name: str):
|
|
| 123 |
model, processor = get_model()
|
| 124 |
|
| 125 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 126 |
-
dtype = torch.
|
| 127 |
model = model.to(device=device, dtype=dtype)
|
| 128 |
|
| 129 |
messages = [
|
| 130 |
{
|
| 131 |
"role": "user",
|
| 132 |
"content": [
|
| 133 |
-
{"type": "image"
|
| 134 |
{"type": "text", "text": INSPECTION_PROMPT},
|
| 135 |
],
|
| 136 |
}
|
| 137 |
]
|
| 138 |
|
| 139 |
try:
|
| 140 |
-
|
| 141 |
-
messages,
|
| 142 |
-
tokenize=True,
|
| 143 |
-
add_generation_prompt=True,
|
| 144 |
-
return_dict=True,
|
| 145 |
-
return_tensors="pt",
|
| 146 |
)
|
|
|
|
| 147 |
inputs = inputs.to(device)
|
| 148 |
|
| 149 |
with torch.no_grad():
|
|
@@ -238,7 +236,7 @@ with gr.Blocks(title="Apiarist - Hive Frame Inspector") as app:
|
|
| 238 |
- π― Vision-language model fine-tuned for honeybees
|
| 239 |
- π Built in 10 days for the [Build Small Hackathon](https://huggingface.co/build-small-hackathon)
|
| 240 |
|
| 241 |
-
**Stack**:
|
| 242 |
"""
|
| 243 |
)
|
| 244 |
|
|
|
|
| 10 |
import json
|
| 11 |
import re
|
| 12 |
import torch
|
| 13 |
+
from transformers import AutoProcessor, AutoModelForVision2Seq
|
| 14 |
|
| 15 |
# ZeroGPU integration β no-op outside HF Spaces
|
| 16 |
try:
|
|
|
|
| 23 |
return fn
|
| 24 |
|
| 25 |
|
| 26 |
+
# SmolVLM-Instruct (2.25B). Bigger than the 500M we tested earlier β
|
| 27 |
+
# much better at fine-grained vision tasks like spotting queens.
|
| 28 |
+
# Qwen2.5-VL-7B kept crashing the ZeroGPU container at the OCI runtime
|
| 29 |
+
# layer; reverting to the SmolVLM family which is known to work here.
|
| 30 |
+
MODEL_ID = "HuggingFaceTB/SmolVLM-Instruct"
|
| 31 |
|
| 32 |
_model = None
|
| 33 |
_processor = None
|
|
|
|
| 39 |
if _model is None:
|
| 40 |
print(f"Loading {MODEL_ID} ...")
|
| 41 |
_processor = AutoProcessor.from_pretrained(MODEL_ID)
|
| 42 |
+
_model = AutoModelForVision2Seq.from_pretrained(MODEL_ID)
|
|
|
|
|
|
|
|
|
|
| 43 |
_model.eval()
|
| 44 |
print("Model loaded.")
|
| 45 |
return _model, _processor
|
|
|
|
| 105 |
**Notes:** {r['notes']}
|
| 106 |
|
| 107 |
---
|
| 108 |
+
*Powered by SmolVLM-Instruct (2.25B) on ZeroGPU. Fully local, no cloud APIs.*
|
| 109 |
|
| 110 |
<details><summary>Raw model output</summary>
|
| 111 |
|
|
|
|
| 124 |
model, processor = get_model()
|
| 125 |
|
| 126 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 127 |
+
dtype = torch.float16 if device == "cuda" else torch.float32
|
| 128 |
model = model.to(device=device, dtype=dtype)
|
| 129 |
|
| 130 |
messages = [
|
| 131 |
{
|
| 132 |
"role": "user",
|
| 133 |
"content": [
|
| 134 |
+
{"type": "image"},
|
| 135 |
{"type": "text", "text": INSPECTION_PROMPT},
|
| 136 |
],
|
| 137 |
}
|
| 138 |
]
|
| 139 |
|
| 140 |
try:
|
| 141 |
+
prompt = processor.apply_chat_template(
|
| 142 |
+
messages, add_generation_prompt=True
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
)
|
| 144 |
+
inputs = processor(text=prompt, images=[image], return_tensors="pt")
|
| 145 |
inputs = inputs.to(device)
|
| 146 |
|
| 147 |
with torch.no_grad():
|
|
|
|
| 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**: SmolVLM-Instruct (2.25B) on ZeroGPU, served via Gradio.
|
| 240 |
"""
|
| 241 |
)
|
| 242 |
|