|
import gradio as gr
|
|
import tensorflow as tf
|
|
import numpy as np
|
|
|
|
|
|
model = tf.keras.models.load_model('model.h5')
|
|
print("Model loaded successfully!")
|
|
|
|
def preprocess_image(image):
|
|
"""Process the input image to match MNIST format"""
|
|
|
|
image = image.convert('L')
|
|
|
|
image = image.resize((28, 28))
|
|
|
|
image_array = np.array(image)
|
|
image_array = image_array / 255.0
|
|
|
|
image_array = np.expand_dims(image_array, axis=0)
|
|
return image_array
|
|
|
|
def predict_digit(image):
|
|
if image is None:
|
|
return None
|
|
|
|
|
|
processed_image = preprocess_image(image)
|
|
|
|
|
|
predictions = model.predict(processed_image)
|
|
pred_scores = tf.nn.softmax(predictions[0]).numpy()
|
|
pred_class = np.argmax(pred_scores)
|
|
|
|
|
|
result = f"Prediction: {pred_class}"
|
|
|
|
return result
|
|
|
|
|
|
demo = gr.Interface(
|
|
fn=predict_digit,
|
|
inputs=gr.Image(type="pil"),
|
|
outputs=gr.Textbox(label="Result"),
|
|
title="MNIST Digit Recognizer",
|
|
description="Upload a digit from 0-9 and the model will predict which digit it is.",
|
|
examples=None,
|
|
)
|
|
|
|
if __name__ == "__main__":
|
|
demo.launch() |