Instructions to use dchen0/font_classifier_v4 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use dchen0/font_classifier_v4 with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-classification", model="dchen0/font_classifier_v4") pipe("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/hub/parrots.png")# Load model directly from transformers import AutoImageProcessor, AutoModelForImageClassification processor = AutoImageProcessor.from_pretrained("dchen0/font_classifier_v4") model = AutoModelForImageClassification.from_pretrained("dchen0/font_classifier_v4") - Notebooks
- Google Colab
- Kaggle
Add merged model + processor
Browse files- handler.py +14 -13
handler.py
CHANGED
|
@@ -94,16 +94,17 @@ class EndpointHandler:
|
|
| 94 |
pixel_values = self.transform(image).unsqueeze(0) # [1, C, H, W]
|
| 95 |
|
| 96 |
with torch.no_grad():
|
| 97 |
-
logits = self.model(pixel_values).logits
|
| 98 |
-
probs = logits.softmax(dim=-1)
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
"
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
|
|
|
|
|
| 94 |
pixel_values = self.transform(image).unsqueeze(0) # [1, C, H, W]
|
| 95 |
|
| 96 |
with torch.no_grad():
|
| 97 |
+
logits = self.model(pixel_values).logits[0] # tensor [num_labels]
|
| 98 |
+
probs = logits.softmax(dim=-1)
|
| 99 |
+
|
| 100 |
+
# convert to the required wire format (top‑k or all classes)
|
| 101 |
+
k = min(5, probs.numel()) # send top‑5
|
| 102 |
+
topk = torch.topk(probs, k)
|
| 103 |
+
|
| 104 |
+
response = [
|
| 105 |
+
{"label": self.id2label[idx.item()], "score": prob.item()}
|
| 106 |
+
for prob, idx in zip(topk.values, topk.indices)
|
| 107 |
+
]
|
| 108 |
+
|
| 109 |
+
return response # <‑‑ must be a *list* of dicts
|
| 110 |
+
|