Update app.py
Browse files
app.py
CHANGED
|
@@ -1,32 +1,30 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import traceback
|
| 3 |
-
|
| 4 |
import gradio as gr
|
| 5 |
import torch
|
| 6 |
from huggingface_hub import snapshot_download
|
| 7 |
-
from fastai.vision.all import * # importante: registra clases/transforms de fastai
|
| 8 |
|
| 9 |
-
#
|
|
|
|
|
|
|
|
|
|
| 10 |
try:
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
LANCZOS = getattr(Image, "LANCZOS", Image.BICUBIC)
|
| 18 |
-
Image.Resampling = _Resampling
|
| 19 |
-
if not hasattr(Image, "ANTIALIAS") and hasattr(Image, "Resampling"):
|
| 20 |
-
Image.ANTIALIAS = Image.Resampling.LANCZOS
|
| 21 |
except Exception:
|
| 22 |
-
print("
|
|
|
|
| 23 |
|
|
|
|
| 24 |
def load_fastai_from_hub(repo_id: str, filename: str = "model.pkl"):
|
| 25 |
repo_dir = snapshot_download(repo_id)
|
| 26 |
pkl_path = os.path.join(repo_dir, filename)
|
| 27 |
print("Loading:", pkl_path, "exists:", os.path.exists(pkl_path), "size:", os.path.getsize(pkl_path))
|
| 28 |
|
| 29 |
-
#
|
| 30 |
learn = torch.load(pkl_path, map_location="cpu", weights_only=False)
|
| 31 |
learn.dls.cpu()
|
| 32 |
return learn
|
|
@@ -40,13 +38,9 @@ def predict(img):
|
|
| 40 |
pred, pred_idx, probs = learner.predict(img)
|
| 41 |
return {labels[i]: float(probs[i]) for i in range(len(labels))}
|
| 42 |
|
| 43 |
-
# evita romper si los ejemplos no existen en el repo del Space
|
| 44 |
-
example_files = [f for f in ["NormalT.jpeg", "AnormalT.jpeg"] if os.path.exists(f)]
|
| 45 |
-
|
| 46 |
demo = gr.Interface(
|
| 47 |
fn=predict,
|
| 48 |
inputs=gr.Image(type="pil"),
|
| 49 |
outputs=gr.Label(num_top_classes=3),
|
| 50 |
-
examples=example_files if example_files else None,
|
| 51 |
)
|
| 52 |
demo.launch()
|
|
|
|
| 1 |
+
import os, sys, types, traceback
|
|
|
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import torch
|
| 4 |
from huggingface_hub import snapshot_download
|
|
|
|
| 5 |
|
| 6 |
+
# Importa fastai (con Plum 2) -> fasttransform ya no revienta
|
| 7 |
+
from fastai.vision.all import PILImage
|
| 8 |
+
|
| 9 |
+
# --------- Alias para pickles antiguos que esperan plum.function ---------
|
| 10 |
try:
|
| 11 |
+
import plum
|
| 12 |
+
from plum import Function # en Plum 2 existe aquí
|
| 13 |
+
if "plum.function" not in sys.modules:
|
| 14 |
+
m = types.ModuleType("plum.function")
|
| 15 |
+
m.Function = Function
|
| 16 |
+
sys.modules["plum.function"] = m
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
except Exception:
|
| 18 |
+
print("Error preparando alias plum.function:\n", traceback.format_exc())
|
| 19 |
+
raise
|
| 20 |
|
| 21 |
+
# --------- Loader robusto ---------
|
| 22 |
def load_fastai_from_hub(repo_id: str, filename: str = "model.pkl"):
|
| 23 |
repo_dir = snapshot_download(repo_id)
|
| 24 |
pkl_path = os.path.join(repo_dir, filename)
|
| 25 |
print("Loading:", pkl_path, "exists:", os.path.exists(pkl_path), "size:", os.path.getsize(pkl_path))
|
| 26 |
|
| 27 |
+
# PyTorch >=2.6: hay que forzar weights_only=False para cargar objetos completos
|
| 28 |
learn = torch.load(pkl_path, map_location="cpu", weights_only=False)
|
| 29 |
learn.dls.cpu()
|
| 30 |
return learn
|
|
|
|
| 38 |
pred, pred_idx, probs = learner.predict(img)
|
| 39 |
return {labels[i]: float(probs[i]) for i in range(len(labels))}
|
| 40 |
|
|
|
|
|
|
|
|
|
|
| 41 |
demo = gr.Interface(
|
| 42 |
fn=predict,
|
| 43 |
inputs=gr.Image(type="pil"),
|
| 44 |
outputs=gr.Label(num_top_classes=3),
|
|
|
|
| 45 |
)
|
| 46 |
demo.launch()
|