cifar10-ia / app.py
StalinAM's picture
Update app.py
79c5ba4 verified
import gradio as gr
from PIL import Image
import numpy as np
import tensorflow as tf
# Etiquetas en espa帽ol
cifar10_labels = np.array([
'avi贸n', 'autom贸vil', 'p谩jaro', 'gato', 'venado'
])
# Cargar el modelo al iniciar la app
model = tf.keras.models.load_model('my_model.h5')
def preprocess_image(image):
"""Preprocesa la imagen para el modelo"""
img = image.resize((32, 32)) # Redimensionar
img = np.array(img) # Convertir a numpy array
img = img.astype('float32') / 255 # Normalizar
return img.reshape(1, 32, 32, 3) # Reformatear para el modelo
def predict(image):
"""Realiza la predicci贸n y devuelve los resultados"""
processed_img = preprocess_image(image)
preds = model.predict(processed_img)[0]
return {cifar10_labels[i]: float(preds[i]) for i in range(5)}
# Configuraci贸n de la interfaz
title = "CIFAR-10"
description = "Sube una imagen"
# Crear la interfaz Gradio
interface = gr.Interface(
fn=predict,
inputs=gr.Image(type="pil", label="Imagen de entrada"),
outputs=gr.Label(num_top_classes=3, label="Predicciones"),
title=title,
description=description,
theme=gr.themes.Soft()
)
# Lanzar la aplicaci贸n
interface.launch()