CoolFace
Apppublic

Eceismeier/MachineLearningCU

sourceHugging Facemitupdated 1y agoView on Hugging Face
0likes
app.py50 linesDownload Raw Back to root
1import gradio as gr2import tensorflow as tf3import numpy as np4 5# Load the trained model6model = tf.keras.models.load_model('model.h5')7print("Model loaded successfully!")8 9def preprocess_image(image):10    """Process the input image to match MNIST format"""11    # Convert to grayscale12    image = image.convert('L')13    # Resize to 28x2814    image = image.resize((28, 28))15    # Convert to numpy array and normalize16    image_array = np.array(image)17    image_array = image_array / 255.018    # Reshape to match model input19    image_array = np.expand_dims(image_array, axis=0)20    return image_array21 22def predict_digit(image):23    if image is None:24        return None25    26    # Preprocess the image27    processed_image = preprocess_image(image)28    29    # Make prediction30    predictions = model.predict(processed_image)31    pred_scores = tf.nn.softmax(predictions[0]).numpy()32    pred_class = np.argmax(pred_scores)33    34    # Create result string35    result = f"Prediction: {pred_class}"36    37    return result38 39# Create Gradio interface40demo = gr.Interface(41    fn=predict_digit,42    inputs=gr.Image(type="pil"),43    outputs=gr.Textbox(label="Result"),44    title="MNIST Digit Recognizer",45    description="Upload a digit from 0-9 and the model will predict which digit it is.",46    examples=None,47)48 49if __name__ == "__main__":50    demo.launch()