Spaces:
Sleeping
Sleeping
Download app.py from riyadahmadov/digit_classifier: direct link, hf CLI and curl.
- Browser
- Download file 1.31 kB
-
https://huggingface.co/spaces/riyadahmadov/digit_classifier/resolve/main/app.py
- Command line
-
hf download hf://spaces/riyadahmadov/digit_classifier/app.py
-
curl -L -o app.py https://huggingface.co/spaces/riyadahmadov/digit_classifier/resolve/main/app.py
1.31 kB
| import gradio as gr | |
| import numpy as np | |
| from tensorflow.keras.models import load_model | |
| from PIL import Image | |
| import os | |
| import json | |
| import tensorflow as tf | |
| # Disable oneDNN custom operations | |
| os.environ['TF_ENABLE_ONEDNN_OPTS'] = '0' | |
| # Disable GPU usage | |
| os.environ['CUDA_VISIBLE_DEVICES'] = '-1' | |
| # Let's load the model | |
| model = load_model("mnist_model.h5") | |
| def predict_digit(image): | |
| # Let's convert Gradio image to PIL Image | |
| img = Image.fromarray(image.astype('uint8'), 'RGB') | |
| # Let's resize the input image to match the expected shape (28, 28) | |
| img = img.resize((28, 28)) | |
| # Let's convert the image to grayscale | |
| img = img.convert('L') | |
| # Let's convert the image to numpy array and normalize pixel values | |
| img = np.array(img) / 255.0 | |
| # Let's reshape the image to add batch and channel dimensions | |
| img = np.expand_dims(np.expand_dims(img, axis=-1), axis=0) | |
| # Let's predict the digit | |
| prediction = model.predict(img) | |
| # Let's get the predicted digit | |
| predicted_digit = np.argmax(prediction) | |
| return str(predicted_digit) | |
| # Let's create Gradio interface | |
| inputs = gr.Image(type='numpy', label="Draw Digit") | |
| outputs = gr.Textbox(label="Predicted Digit") | |
| iface = gr.Interface(fn=predict_digit, inputs=inputs, outputs=outputs, title="Digit Recognition") | |
| iface.launch() |