Edupy commited on
Commit
7954766
·
1 Parent(s): 6dbb309

Arquitectura modular con funciones get_x/y reales

Browse files
Files changed (1) hide show
  1. app.py +12 -11
app.py CHANGED
@@ -2,24 +2,25 @@
2
  import gradio as gr
3
  from fastai.vision.all import *
4
  import __main__
5
- from utils import get_x, get_y
6
 
7
- # Inyectamos las funciones en el módulo principal para que load_learner las encuentre
 
 
 
 
 
8
  __main__.get_x = get_x
9
  __main__.get_y = get_y
10
 
11
- # Cargamos el archivo .pkl
12
  learn = load_learner('modelo_pokemon.pkl')
13
 
 
 
14
  def predict(img):
15
  img = PILImage.create(img)
16
- # Fastai usará el preprocesamiento guardado en el .pkl automáticamente
17
  pred, pred_idx, probs = learn.predict(img)
18
- return {learn.dls.vocab[i]: float(probs[i]) for i in range(len(learn.dls.vocab))}
19
 
20
- gr.Interface(
21
- fn=predict,
22
- inputs=gr.Image(),
23
- outputs=gr.Label(num_top_classes=3),
24
- title="Detector de Tipos Pokémon"
25
- ).launch()
 
2
  import gradio as gr
3
  from fastai.vision.all import *
4
  import __main__
 
5
 
6
+ # 1. Definir las funciones que el modelo "busca" al abrirse.
7
+ # No necesitan hacer nada real, solo existir con el mismo nombre.
8
+ def get_x(i): return None
9
+ def get_y(i): return None
10
+
11
+ # 2. Inyectarlas en el módulo principal (esto es el truco clave)
12
  __main__.get_x = get_x
13
  __main__.get_y = get_y
14
 
15
+ # 3. Ahora sí, cargar el modelo
16
  learn = load_learner('modelo_pokemon.pkl')
17
 
18
+ labels = learn.dls.vocab
19
+
20
  def predict(img):
21
  img = PILImage.create(img)
 
22
  pred, pred_idx, probs = learn.predict(img)
23
+ return {labels[i]: float(probs[i]) for i in range(len(labels))}
24
 
25
+ # Interfaz de Gradio
26
+ gr.Interface(fn=predict, inputs=gr.Image(), outputs=gr.Label(num_top_classes=3)).launch()