Spaces:
Runtime error
Runtime error
import gradio as gr | |
import tensorflow as tf | |
from PIL import Image | |
import numpy as np | |
labels = ['Haunter', 'Gengar', 'Ditto', 'Vulpix'] | |
def predict_pokemon_type(uploaded_file): | |
if uploaded_file is None: | |
return "No file uploaded.", None, "No prediction" | |
model = tf.keras.models.load_model('pokemon-model_2_transferlearning.keras') | |
# Load the image from the file path | |
with Image.open(uploaded_file) as img: | |
img = img.resize((150, 150)) | |
img_array = np.array(img) | |
prediction = model.predict(np.expand_dims(img_array, axis=0)) | |
confidences = {labels[i]: np.round(float(prediction[0][i]), 2) for i in range(len(labels))} | |
# Identify the most confident prediction | |
confidences = {labels[i]: np.round(float(prediction[0][i]), 2) for i in range(len(labels))} | |
return img, confidences | |
# Define the Gradio interface | |
iface = gr.Interface( | |
fn=predict_pokemon_type, # Function to process the input | |
inputs=gr.File(label="Upload File"), # File upload widget | |
outputs=["image", "text"], # Output types for image and text | |
title="Pokemon Classifier", # Title of the interface | |
description="Upload a picture of a Pokemon (preferably Cubone, Ditto, Psyduck, Snorlax, or Weedle) to see its type and confidence level. The trained model has an accuracy of 96%!" # Description of the interface | |
) | |
# Launch the interface | |
iface.launch() |