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
Files changed (1) hide show
  1. app.py +19 -14
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, AutoModelForVision2Seq
14
 
15
  # ZeroGPU integration β€” no-op outside HF Spaces
16
  try:
17
  import spaces
18
 
19
  def gpu(fn):
20
- return spaces.GPU(duration=60)(fn)
21
  except ImportError:
22
  def gpu(fn):
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,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 = AutoModelForVision2Seq.from_pretrained(MODEL_ID)
 
 
 
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 SmolVLM-Instruct (2.25B) on ZeroGPU. Fully local, no cloud APIs.*
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
- 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,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**: SmolVLM-Instruct (2.25B) on ZeroGPU, served via Gradio.
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