Apiarist Dev commited on
Commit
ec6806d
Β·
1 Parent(s): 3d9702c

upgrade SmolVLM-500M -> Qwen2.5-VL-7B for real vision quality on ZeroGPU

Browse files
Files changed (1) hide show
  1. app.py +26 -21
app.py CHANGED
@@ -1,9 +1,8 @@
1
  """
2
  Apiarist - Offline AI inspector for honeybee hive frames.
3
 
4
- Day 6: SmolVLM-500M + ZeroGPU (free on-demand GPU). The @spaces.GPU
5
- decorator tells HF to allocate a GPU just for the inference call,
6
- then release it.
7
  """
8
 
9
  import gradio as gr
@@ -11,9 +10,9 @@ from PIL import Image
11
  import json
12
  import re
13
  import torch
14
- from transformers import AutoProcessor, AutoModelForVision2Seq
15
 
16
- # ZeroGPU integration β€” gracefully degrades to a no-op outside HF Spaces
17
  try:
18
  import spaces
19
 
@@ -24,7 +23,7 @@ except ImportError:
24
  return fn
25
 
26
 
27
- MODEL_ID = "HuggingFaceTB/SmolVLM-500M-Instruct"
28
 
29
  _model = None
30
  _processor = None
@@ -36,7 +35,10 @@ def get_model():
36
  if _model is None:
37
  print(f"Loading {MODEL_ID} ...")
38
  _processor = AutoProcessor.from_pretrained(MODEL_ID)
39
- _model = AutoModelForVision2Seq.from_pretrained(MODEL_ID)
 
 
 
40
  _model.eval()
41
  print("Model loaded.")
42
  return _model, _processor
@@ -52,10 +54,11 @@ HEALTH: good, watch, or alarm
52
  NOTES: one short sentence describing what you see
53
 
54
  Definitions:
55
- - Queens are noticeably larger bees with long abdomens.
56
- - Varroa mites are small reddish-brown parasites on bees or comb cells.
57
- - Swarm cells are peanut-shaped cells hanging from the bottom edge of the comb.
58
- - Brood pattern is solid when cells are tightly packed and consistent, spotty when scattered."""
 
59
 
60
 
61
  def parse_response(text: str, hive_name: str) -> dict:
@@ -101,7 +104,7 @@ def build_narrative(r: dict, raw: str) -> str:
101
  **Notes:** {r['notes']}
102
 
103
  ---
104
- *Powered by SmolVLM-500M on ZeroGPU β€” lightweight stand-in. Qwen2.5-VL-7B + specialist YOLO detector are the planned final stack.*
105
 
106
  <details><summary>Raw model output</summary>
107
 
@@ -119,26 +122,28 @@ def analyze_frame(image: Image.Image, hive_name: str):
119
 
120
  model, processor = get_model()
121
 
122
- # ZeroGPU allocates a GPU only inside this @gpu-decorated call.
123
  device = "cuda" if torch.cuda.is_available() else "cpu"
124
- dtype = torch.float16 if device == "cuda" else torch.float32
125
  model = model.to(device=device, dtype=dtype)
126
 
127
  messages = [
128
  {
129
  "role": "user",
130
  "content": [
131
- {"type": "image"},
132
  {"type": "text", "text": INSPECTION_PROMPT},
133
  ],
134
  }
135
  ]
136
 
137
  try:
138
- prompt = processor.apply_chat_template(
139
- messages, add_generation_prompt=True
 
 
 
 
140
  )
141
- inputs = processor(text=prompt, images=[image], return_tensors="pt")
142
  inputs = inputs.to(device)
143
 
144
  with torch.no_grad():
@@ -229,11 +234,11 @@ with gr.Blocks(title="Apiarist - Hive Frame Inspector") as app:
229
  """
230
  **Apiarist** is a fully-offline vision AI for backyard beekeepers.
231
 
232
- - πŸ”Œ No cloud APIs β€” runs entirely on your laptop
233
- - 🎯 Fine-tuned for honeybees
234
  - πŸ““ Built in 10 days for the [Build Small Hackathon](https://huggingface.co/build-small-hackathon)
235
 
236
- **Stack**: SmolVLM-500M on ZeroGPU. Qwen2.5-VL-7B + YOLOv8 are the planned upgrades.
237
  """
238
  )
239
 
 
1
  """
2
  Apiarist - Offline AI inspector for honeybee hive frames.
3
 
4
+ Day 7: upgraded SmolVLM-500M -> Qwen2.5-VL-7B for real vision quality.
5
+ ZeroGPU gives us a Blackwell GPU with plenty of VRAM, so 7B is cheap.
 
6
  """
7
 
8
  import gradio as gr
 
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
 
 
23
  return fn
24
 
25
 
26
+ MODEL_ID = "Qwen/Qwen2.5-VL-7B-Instruct"
27
 
28
  _model = None
29
  _processor = None
 
35
  if _model is None:
36
  print(f"Loading {MODEL_ID} ...")
37
  _processor = AutoProcessor.from_pretrained(MODEL_ID)
38
+ _model = AutoModelForImageTextToText.from_pretrained(
39
+ MODEL_ID,
40
+ torch_dtype=torch.bfloat16,
41
+ )
42
  _model.eval()
43
  print("Model loaded.")
44
  return _model, _processor
 
54
  NOTES: one short sentence describing what you see
55
 
56
  Definitions:
57
+ - Queens are noticeably larger bees with elongated abdomens, often appearing distinct from worker bees.
58
+ - Varroa mites are small reddish-brown parasites visible on bees or comb cells.
59
+ - Swarm cells are peanut-shaped cells hanging from the bottom or edges of the comb.
60
+ - Brood pattern is solid when capped cells are tightly packed and consistent, spotty when scattered with empty cells.
61
+ - Be honest about uncertainty β€” only say "yes" when you can clearly see the feature."""
62
 
63
 
64
  def parse_response(text: str, hive_name: str) -> dict:
 
104
  **Notes:** {r['notes']}
105
 
106
  ---
107
+ *Powered by Qwen2.5-VL-7B on ZeroGPU. Fully local, no cloud APIs.*
108
 
109
  <details><summary>Raw model output</summary>
110
 
 
122
 
123
  model, processor = get_model()
124
 
 
125
  device = "cuda" if torch.cuda.is_available() else "cpu"
126
+ dtype = torch.bfloat16 if device == "cuda" else torch.float32
127
  model = model.to(device=device, dtype=dtype)
128
 
129
  messages = [
130
  {
131
  "role": "user",
132
  "content": [
133
+ {"type": "image", "image": image},
134
  {"type": "text", "text": INSPECTION_PROMPT},
135
  ],
136
  }
137
  ]
138
 
139
  try:
140
+ inputs = processor.apply_chat_template(
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():
 
234
  """
235
  **Apiarist** is a fully-offline vision AI for backyard beekeepers.
236
 
237
+ - πŸ”Œ No cloud APIs β€” runs entirely on the laptop
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**: Qwen2.5-VL-7B on ZeroGPU, served via Gradio.
242
  """
243
  )
244