Edupy's picture
Arquitectura modular con funciones get_x/y reales
7954766
raw
history blame contribute delete
742 Bytes
import gradio as gr
from fastai.vision.all import *
import __main__
# 1. Definir las funciones que el modelo "busca" al abrirse.
# No necesitan hacer nada real, solo existir con el mismo nombre.
def get_x(i): return None
def get_y(i): return None
# 2. Inyectarlas en el módulo principal (esto es el truco clave)
__main__.get_x = get_x
__main__.get_y = get_y
# 3. Ahora sí, cargar el modelo
learn = load_learner('modelo_pokemon.pkl')
labels = learn.dls.vocab
def predict(img):
img = PILImage.create(img)
pred, pred_idx, probs = learn.predict(img)
return {labels[i]: float(probs[i]) for i in range(len(labels))}
# Interfaz de Gradio
gr.Interface(fn=predict, inputs=gr.Image(), outputs=gr.Label(num_top_classes=3)).launch()