import numpy as np
import tensorflow as tf
import gradio as gr
from huggingface_hub import from_pretrained_keras
model = from_pretrained_keras("keras-io/conv_mixer_image_classification")
class_names = [
    "Airplane",
    "Automobile",
    "Bird",
    "Cat",
    "Deer",
    "Dog",
    "Frog",
    "Horse",
    "Ship",
    "Truck",
]
examples = [
    ['./aeroplane.png'],
    ['./horse.png'],
    ['./ship.png'],
    ['./truck.png']
] 
IMG_SIZE = 32
def infer(input_image):
    image_tensor = tf.convert_to_tensor(input_image)
    image_tensor.set_shape([None, None, 3])
    image_tensor = tf.image.resize(image_tensor, (IMG_SIZE, IMG_SIZE))
    predictions = model.predict(np.expand_dims((image_tensor), axis=0))
    predictions = np.squeeze(predictions)
    predictions = np.argmax(predictions)
    predicted_label = class_names[predictions.item()]
    return str(predicted_label)
    
input = gr.inputs.Image(shape=(IMG_SIZE, IMG_SIZE))
output = [gr.outputs.Label(label = "Model Output")]
title = "Image Classification using Conv Mixer Model"
description = "Upload an image or select from examples to classify it.
The allowed classes are - Airplane, Automobile, Bird, Cat, Deer, Dog, Frog, Horse, Ship, Truck.
Model Repo - https://huggingface.co/keras-io/conv_mixer_image_classification 
Keras Example - https://keras.io/examples/vision/convmixer//