Instructions to use aipanjab/digit-recognition with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Keras
How to use aipanjab/digit-recognition with Keras:
# Available backend options are: "jax", "torch", "tensorflow". import os os.environ["KERAS_BACKEND"] = "jax" import keras model = keras.saving.load_model("hf://aipanjab/digit-recognition") - Notebooks
- Google Colab
- Kaggle
Commit ·
08ab5ff
1
Parent(s): 90dc368
endpoint handler
Browse files- handler.py +42 -0
- requirements.txt +1 -0
handler.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from PIL import Image
|
| 2 |
+
from io import BytesIO
|
| 3 |
+
import base64
|
| 4 |
+
import tensorflow as tf
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
IMAGE_HEIGHT = 28
|
| 8 |
+
IMAGE_WIDTH = 28
|
| 9 |
+
LABELS = ["੦", "੧", "੨", "੩", "੪", "੫", "੬", "੭", "੮", "੯"]
|
| 10 |
+
DEFAULT_TOP_K = 3
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
class EndpointHandler():
|
| 14 |
+
def __init__(self, path=""):
|
| 15 |
+
self.model = tf.keras.models.load_model(path)
|
| 16 |
+
|
| 17 |
+
def __call__(self, data):
|
| 18 |
+
image = Image.open(BytesIO(base64.b64decode(data.pop("inputs").pop("image"))))
|
| 19 |
+
tensors = self.to_tensors(image)
|
| 20 |
+
predictions = self.model.predict(tensors)
|
| 21 |
+
top_k_scores, top_k_label_ids = tf.nn.top_k(predictions, k=data.pop("parameters", {}).pop("topK", DEFAULT_TOP_K))
|
| 22 |
+
|
| 23 |
+
return [
|
| 24 |
+
{
|
| 25 |
+
"label": LABELS[(int(label_id))],
|
| 26 |
+
"score": float(score),
|
| 27 |
+
}
|
| 28 |
+
for label_id, score in zip(top_k_label_ids[0], top_k_scores[0])
|
| 29 |
+
]
|
| 30 |
+
|
| 31 |
+
@staticmethod
|
| 32 |
+
def to_tensors(image):
|
| 33 |
+
if image.mode == "RGBA":
|
| 34 |
+
img = Image.new("RGB", image.size, (255, 255, 255))
|
| 35 |
+
img.paste(image, mask=image.split()[3])
|
| 36 |
+
image = img
|
| 37 |
+
elif image.mode != "RGB":
|
| 38 |
+
image = image.convert("RGB")
|
| 39 |
+
|
| 40 |
+
image = tf.image.resize(image, size=(IMAGE_HEIGHT, IMAGE_WIDTH), antialias=True)
|
| 41 |
+
image = tf.image.rgb_to_grayscale(image)
|
| 42 |
+
return tf.reshape(image, shape=(1, IMAGE_HEIGHT, IMAGE_WIDTH))
|
requirements.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
tensorflow==2.13.0
|