File size: 8,360 Bytes
006e5e9 | 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 | from __future__ import annotations
import base64
import io
import json
import os
from pathlib import Path
from typing import Dict, List, Optional
from openai import OpenAI
from PIL import Image
TORCH_IMPORT_ERROR: Optional[str] = None
try:
import torch
import torch.nn.functional as F
from torchvision import models, transforms
except Exception as exc: # pragma: no cover - defensive import guard
torch = None
F = None
models = None
transforms = None
TORCH_IMPORT_ERROR = str(exc)
TRANSFORMERS_IMPORT_ERROR: Optional[str] = None
try:
from transformers import CLIPModel, CLIPProcessor
except Exception as exc: # pragma: no cover - defensive import guard
CLIPModel = None
CLIPProcessor = None
TRANSFORMERS_IMPORT_ERROR = str(exc)
class CustomModelPredictor:
def __init__(self, checkpoint_path: str = "models/custom_resnet18.pth") -> None:
self.checkpoint_path = Path(checkpoint_path)
self.error: Optional[str] = None
self.model = None
self.labels: List[str] = []
self.image_size = 224
self.eval_transform = None
if torch is None or transforms is None:
self.device = None
self.error = (
"Custom model unavailable because torch/torchvision is missing. "
f"Import error: {TORCH_IMPORT_ERROR}"
)
return
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
self.eval_transform = transforms.Compose(
[
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
]
)
if self.checkpoint_path.exists():
self._load()
def _load(self) -> None:
checkpoint = torch.load(self.checkpoint_path, map_location=self.device)
self.labels = checkpoint["labels"]
self.image_size = int(checkpoint.get("image_size", 224))
model = models.resnet18(weights=None)
model.fc = torch.nn.Linear(model.fc.in_features, len(self.labels))
model.load_state_dict(checkpoint["state_dict"])
model.to(self.device)
model.eval()
self.model = model
def available(self) -> bool:
return self.model is not None and self.error is None
def predict(self, image: Image.Image, top_k: int = 3) -> Dict[str, object]:
if not self.available():
return {
"model": "custom-transfer-learning",
"available": False,
"error": self.error or f"Model not found at {self.checkpoint_path}",
}
image = image.convert("RGB")
tensor = self.eval_transform(image).unsqueeze(0).to(self.device)
with torch.no_grad():
logits = self.model(tensor)
probs = F.softmax(logits, dim=1).squeeze(0)
top_probs, top_idx = torch.topk(probs, k=min(top_k, len(self.labels)))
predictions = [
{"label": self.labels[idx], "confidence": float(prob)}
for prob, idx in zip(top_probs.cpu().tolist(), top_idx.cpu().tolist())
]
return {
"model": "custom-transfer-learning",
"available": True,
"top_prediction": predictions[0],
"predictions": predictions,
}
class ClipPredictor:
def __init__(self, labels: List[str], model_name: str = "openai/clip-vit-base-patch32") -> None:
self.labels = labels
self.model_name = model_name
self.error: Optional[str] = None
self.device = torch.device("cuda" if torch is not None and torch.cuda.is_available() else "cpu") if torch is not None else None
self.available_flag = False
self.processor = None
self.model = None
if torch is None or CLIPModel is None or CLIPProcessor is None:
self.error = (
"CLIP unavailable because required dependencies are missing. "
f"torch error: {TORCH_IMPORT_ERROR}; transformers error: {TRANSFORMERS_IMPORT_ERROR}"
)
return
if labels:
self._load()
def _load(self) -> None:
try:
self.processor = CLIPProcessor.from_pretrained(self.model_name)
self.model = CLIPModel.from_pretrained(self.model_name).to(self.device)
self.model.eval()
self.available_flag = True
except Exception:
self.available_flag = False
def available(self) -> bool:
return self.available_flag
def predict(self, image: Image.Image, top_k: int = 3) -> Dict[str, object]:
if not self.available():
return {
"model": "clip-open-source",
"available": False,
"error": self.error or "CLIP model could not be loaded.",
}
prompts = [f"a sprite or photo of a pokemon named {label}" for label in self.labels]
inputs = self.processor(text=prompts, images=image.convert("RGB"), return_tensors="pt", padding=True)
inputs = {k: v.to(self.device) for k, v in inputs.items()}
with torch.no_grad():
outputs = self.model(**inputs)
logits_per_image = outputs.logits_per_image
probs = logits_per_image.softmax(dim=1).squeeze(0)
top_probs, top_idx = torch.topk(probs, k=min(top_k, len(self.labels)))
predictions = [
{"label": self.labels[idx], "confidence": float(prob)}
for prob, idx in zip(top_probs.cpu().tolist(), top_idx.cpu().tolist())
]
return {
"model": "clip-open-source",
"available": True,
"top_prediction": predictions[0],
"predictions": predictions,
}
class OpenAIVisionPredictor:
def __init__(self, labels: List[str], model_name: str = "gpt-4.1-mini") -> None:
self.labels = labels
self.model_name = model_name
self.api_key = os.getenv("OPENAI_API_KEY", "")
self.client: Optional[OpenAI] = None
if self.api_key:
self.client = OpenAI(api_key=self.api_key)
def available(self) -> bool:
return self.client is not None
def predict(self, image: Image.Image) -> Dict[str, object]:
if not self.available():
return {
"model": "openai-vision",
"available": False,
"error": "OPENAI_API_KEY is not set.",
}
buffered = io.BytesIO()
image.convert("RGB").save(buffered, format="JPEG")
b64_image = base64.b64encode(buffered.getvalue()).decode("utf-8")
prompt = (
"You are an image classifier. "
f"Choose exactly one label from this list: {', '.join(self.labels)}. "
"Return strict JSON with keys: label, confidence, reason. "
"label must be one of the provided labels. confidence must be in [0,1]."
)
response = self.client.responses.create(
model=self.model_name,
input=[
{
"role": "user",
"content": [
{"type": "input_text", "text": prompt},
{"type": "input_image", "image_url": f"data:image/jpeg;base64,{b64_image}"},
],
}
],
temperature=0,
)
text = response.output_text.strip()
parsed = self._safe_parse(text)
return {
"model": "openai-vision",
"available": True,
"top_prediction": {
"label": parsed.get("label", "unknown"),
"confidence": float(parsed.get("confidence", 0.0)),
},
"raw_response": parsed,
}
@staticmethod
def _safe_parse(text: str) -> Dict[str, object]:
try:
return json.loads(text)
except json.JSONDecodeError:
return {"label": "unknown", "confidence": 0.0, "reason": text}
|