import gradio as gr import tensorflow as tf import numpy as np import gdown from PIL import Image input_shape = (32, 32, 3) resized_shape = (224, 224, 3) num_classes = 10 labels = { 0: "plane", 1: "car", 2: "bird", 3: "cat", 4: "deer", 5: "dog", 6: "frog", 7: "horse", 8: "ship", 9: "truck", } # Download the model file def download_model(): url = "https://drive.google.com/uc?id=12700bE-pomYKoVQ214VrpBoJ7akXcTpL" output = "modelV2Lmixed.keras" gdown.download(url, output, quiet=False) return output model_file = download_model() # Load the model model = tf.keras.models.load_model(model_file) # Perform image classification def predict_class(image): img = tf.cast(image, tf.float32) img = tf.image.resize(img, [input_shape[0], input_shape[1]]) img = np.expand_dims(img, axis=0) prediction = model.predict(img) return prediction[0] # UI Design def classify_image(image): pred = predict_class(image) class_names = [ "plane", "car", "bird", "cat", "deer", "dog", "frog", "horse", "ship", "truck", ] probabilities = tf.nn.softmax(pred) top_indices = tf.argsort(probabilities, direction='DESCENDING') top_classes = [class_names[idx] for idx in top_indices] top_probs = [probabilities[idx] for idx in top_indices] output = "

Top 3 Predictions:

" for i in range(3): output += f"

{top_classes[i]}: {top_probs[i]*100:.2f}%

" return output inputs = gr.inputs.Image(label="Upload an image") outputs = gr.outputs.HTML() title = "

Image Classifier

" description = "Upload an image and get the top 3 predictions." gr.Interface(fn=classify_image, inputs=inputs, outputs=outputs, title=title, description=description).launch()