-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvlm_interface.py
More file actions
299 lines (260 loc) · 10.8 KB
/
Copy pathvlm_interface.py
File metadata and controls
299 lines (260 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
"""
Small VLM & LLM Integration Module for Construction Safety AI.
Autonomous Edge Inference powered by AMD Lemonade SDK.
Supports local OpenAI-compatible endpoint at http://localhost:13305/v1
with automatic model discovery, streaming token delivery, and cloud benchmark comparison.
"""
import time
import base64
import requests
from io import BytesIO
from PIL import Image
from typing import Dict, Any, List, Optional, Generator
from config import (
LEMONADE_BASE_URL,
LEMONADE_FALLBACK_URL,
LEMONADE_API_KEY,
VLM_MODELS,
DEFAULT_VLM,
)
def check_lemonade_health(base_url: str = LEMONADE_BASE_URL) -> Dict[str, Any]:
"""
Check if the AMD Lemonade SDK server is active and reachable.
Returns:
Dict with 'is_healthy', 'url_used', 'models', and 'error_message'
"""
candidate_urls = [base_url, LEMONADE_FALLBACK_URL]
for url in candidate_urls:
try:
# Check models endpoint
models_endpoint = f"{url.rstrip('/')}/models"
resp = requests.get(models_endpoint, timeout=2.0)
if resp.status_code == 200:
data = resp.json()
model_list = [m.get("id") for m in data.get("data", [])] if "data" in data else []
return {
"is_healthy": True,
"url_used": url,
"models": model_list,
"error_message": None,
}
except Exception:
continue
return {
"is_healthy": False,
"url_used": base_url,
"models": [],
"error_message": f"Could not connect to Lemonade SDK at {base_url}. Ensure 'lemonade serve --port 13305' is running.",
}
class VLMInterface:
"""
Interface for Local Edge VLM / LLM inference via AMD Lemonade SDK,
with optional Azure OpenAI cloud benchmark support.
"""
def __init__(self, model_key: str = DEFAULT_VLM):
"""
Initialize the VLM interface.
Args:
model_key: Key from VLM_MODELS config dict
"""
if model_key not in VLM_MODELS:
# Check if it's a dynamic lemonade model name
self.model_key = model_key
self.model_config = {
"model_id": model_key,
"label": f"Lemonade Local ({model_key})",
"backend": "lemonade",
"description": "Locally hosted model via AMD Lemonade SDK.",
}
else:
self.model_key = model_key
self.model_config = VLM_MODELS[model_key]
self.backend = self.model_config.get("backend", "lemonade")
self.model_id = self.model_config.get("model_id", "auto")
self._client = None
self._device = "AMD Ryzen™ AI / Local Edge" if self.backend == "lemonade" else "api (cloud)"
self._is_loaded = False
self._resolved_model_id = self.model_id
def _load_model(self):
"""
Lazy-load the appropriate OpenAI-compatible client.
"""
if self._is_loaded and self._client is not None:
return
if self.backend == "lemonade":
try:
from openai import OpenAI
# Check health and resolve base url
health = check_lemonade_health(LEMONADE_BASE_URL)
base_url = health["url_used"] if health["is_healthy"] else LEMONADE_BASE_URL
self._client = OpenAI(
base_url=base_url,
api_key=LEMONADE_API_KEY,
)
# If model_id is 'auto', pick the first loaded model or default to qwen
if self.model_id == "auto":
if health["is_healthy"] and health["models"]:
self._resolved_model_id = health["models"][0]
else:
self._resolved_model_id = "qwen2.5-vl-7b-instruct"
else:
self._resolved_model_id = self.model_id
self._is_loaded = True
except Exception as e:
raise RuntimeError(
f"Failed to connect to local AMD Lemonade SDK at {LEMONADE_BASE_URL}.\n"
f"Error: {str(e)}\n"
f"Quick Fix: Start Lemonade with 'lemonade serve --port 13305' in your terminal."
)
else:
# Azure OpenAI fallback (for cloud latency benchmarking)
try:
from openai import AzureOpenAI
from dotenv import load_dotenv
import os
load_dotenv()
api_key = os.getenv("AZURE_OPENAI_API_KEY")
endpoint = os.getenv("AZURE_OPENAI_ENDPOINT")
api_version = os.getenv("AZURE_OPENAI_API_VERSION", "2024-12-01-preview")
self._resolved_model_id = os.getenv("AZURE_OPENAI_DEPLOYMENT", "gpt-4o")
if not api_key or not endpoint:
raise ValueError("AZURE_OPENAI_API_KEY or AZURE_OPENAI_ENDPOINT missing in .env")
self._client = AzureOpenAI(
api_key=api_key,
azure_endpoint=endpoint,
api_version=api_version,
)
self._is_loaded = True
except Exception as e:
raise RuntimeError(f"Failed to initialize Azure OpenAI client: {str(e)}")
def infer(
self,
image: Optional[Image.Image],
prompt: str,
max_new_tokens: int = 512,
temperature: float = 0.3,
system_prompt: Optional[str] = None,
) -> Dict[str, Any]:
"""
Run inference on prompt with optional image using the local Lemonade SDK or cloud baseline.
Returns:
Dictionary with:
- "raw_output": Full generated text
- "inference_time_ms": Latency in milliseconds
- "model_key": Model identifier
- "model_label": Human-readable label
- "device": Compute backend/device
- "backend": 'lemonade' or 'azure'
"""
self._load_model()
start_time = time.time()
raw_output = self._generate(image, prompt, max_new_tokens, temperature, system_prompt)
inference_time_ms = (time.time() - start_time) * 1000.0
return {
"raw_output": raw_output,
"inference_time_ms": round(inference_time_ms, 1),
"model_key": self.model_key,
"model_label": self.model_config.get("label", self.model_key),
"resolved_model_id": self._resolved_model_id,
"device": self._device,
"backend": self.backend,
}
def infer_stream(
self,
messages: List[Dict[str, Any]],
max_new_tokens: int = 512,
temperature: float = 0.3,
) -> Generator[str, None, None]:
"""
Stream generated tokens in real-time from the Lemonade SDK server.
"""
self._load_model()
response_stream = self._client.chat.completions.create(
model=self._resolved_model_id,
messages=messages,
max_tokens=max_new_tokens,
temperature=temperature,
stream=True,
)
for chunk in response_stream:
if chunk.choices and len(chunk.choices) > 0:
delta = chunk.choices[0].delta
if hasattr(delta, "content") and delta.content:
yield delta.content
def _generate(
self,
image: Optional[Image.Image],
prompt: str,
max_new_tokens: int,
temperature: float,
system_prompt: Optional[str] = None,
) -> str:
"""
Execute chat completion request with vision/text content.
"""
messages = []
if system_prompt:
messages.append({"role": "system", "content": system_prompt})
user_content = []
if image is not None:
# Convert PIL image to base64 data URL
buffered = BytesIO()
# Convert RGBA to RGB if needed
if image.mode in ("RGBA", "P"):
image = image.convert("RGB")
image.save(buffered, format="JPEG", quality=85)
img_base64 = base64.b64encode(buffered.getvalue()).decode("utf-8")
data_url = f"data:image/jpeg;base64,{img_base64}"
user_content.append({"type": "image_url", "image_url": {"url": data_url}})
user_content.append({"type": "text", "text": prompt})
messages.append({"role": "user", "content": user_content})
try:
response = self._client.chat.completions.create(
model=self._resolved_model_id,
messages=messages,
max_tokens=max_new_tokens,
temperature=temperature,
)
return response.choices[0].message.content.strip()
except Exception as e:
# If vision payload fails on text-only local models, fallback to text-only prompt
if image is not None and "image" in str(e).lower():
text_messages = []
if system_prompt:
text_messages.append({"role": "system", "content": system_prompt})
text_messages.append({"role": "user", "content": prompt})
response = self._client.chat.completions.create(
model=self._resolved_model_id,
messages=text_messages,
max_tokens=max_new_tokens,
temperature=temperature,
)
return response.choices[0].message.content.strip()
raise
def is_loaded(self) -> bool:
"""Check if client is ready."""
return self._is_loaded
def get_device(self) -> str:
"""Get the compute device descriptor."""
return self._device
def get_model_info(self) -> Dict[str, Any]:
"""Get model metadata."""
return {
"model_key": self.model_key,
"model_id": self.model_id,
"resolved_model_id": self._resolved_model_id,
"label": self.model_config.get("label", self.model_key),
"backend": self.backend,
"parameters": self.model_config.get("parameters", "Unknown"),
"paper_f1": self.model_config.get("paper_f1", "N/A"),
"paper_bertscore": self.model_config.get("paper_bertscore", "N/A"),
"description": self.model_config.get("description", ""),
"device": self._device,
"is_loaded": self._is_loaded,
}
def unload(self):
"""Reset client."""
if hasattr(self, "_client"):
del self._client
self._client = None
self._is_loaded = False