Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,15 +13,24 @@ import pandas as pd
|
|
| 13 |
from PIL import Image, ImageDraw, ImageFont
|
| 14 |
|
| 15 |
from fastai.learner import load_learner
|
|
|
|
| 16 |
|
| 17 |
-
#
|
| 18 |
-
# Load model once
|
| 19 |
-
# -----------------------
|
| 20 |
learn = load_learner("model.pkl")
|
| 21 |
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
def _safe_filename(name: str) -> str:
|
| 24 |
-
return "".join(c if c.isalnum() or c in ("-", "_", ".", " ") else "_" for c in name)
|
| 25 |
|
| 26 |
|
| 27 |
def _annotate_image(pil_img: Image.Image, text: str) -> Image.Image:
|
|
@@ -68,17 +77,18 @@ def predict_batch(files, display_mode):
|
|
| 68 |
os.makedirs(ann_dir, exist_ok=True)
|
| 69 |
|
| 70 |
for f in files:
|
| 71 |
-
path =
|
| 72 |
-
fname = os.path.basename(path)
|
| 73 |
safe_fname = _safe_filename(fname)
|
| 74 |
|
| 75 |
try:
|
| 76 |
-
|
|
|
|
| 77 |
with torch.inference_mode():
|
| 78 |
-
|
|
|
|
| 79 |
|
| 80 |
pred_str = str(pred)
|
| 81 |
-
|
| 82 |
pil_img = Image.open(path).convert("RGB")
|
| 83 |
|
| 84 |
if display_mode == "image":
|
|
@@ -106,7 +116,7 @@ def predict_batch(files, display_mode):
|
|
| 106 |
ann_img.save(out_path, quality=92)
|
| 107 |
|
| 108 |
except Exception as e:
|
| 109 |
-
print(f"ERROR processing {fname}")
|
| 110 |
traceback.print_exc()
|
| 111 |
|
| 112 |
rows.append({
|
|
|
|
| 13 |
from PIL import Image, ImageDraw, ImageFont
|
| 14 |
|
| 15 |
from fastai.learner import load_learner
|
| 16 |
+
from fastai.vision.core import PILImage
|
| 17 |
|
| 18 |
+
# Load model
|
|
|
|
|
|
|
| 19 |
learn = load_learner("model.pkl")
|
| 20 |
|
| 21 |
|
| 22 |
+
def _file_to_path(f):
|
| 23 |
+
"""
|
| 24 |
+
Gradio with type='filepath' usually returns a string path.
|
| 25 |
+
This helper safely converts any incoming file object to a path string.
|
| 26 |
+
"""
|
| 27 |
+
if isinstance(f, str):
|
| 28 |
+
return f
|
| 29 |
+
return getattr(f, "name", str(f))
|
| 30 |
+
|
| 31 |
+
|
| 32 |
def _safe_filename(name: str) -> str:
|
| 33 |
+
return "".join(c if c.isalnum() or c in ("-", "_", ".", " ") else "_" for c in name).strip()
|
| 34 |
|
| 35 |
|
| 36 |
def _annotate_image(pil_img: Image.Image, text: str) -> Image.Image:
|
|
|
|
| 77 |
os.makedirs(ann_dir, exist_ok=True)
|
| 78 |
|
| 79 |
for f in files:
|
| 80 |
+
path = _file_to_path(f)
|
| 81 |
+
fname = os.path.basename(str(path))
|
| 82 |
safe_fname = _safe_filename(fname)
|
| 83 |
|
| 84 |
try:
|
| 85 |
+
img = PILImage.create(path)
|
| 86 |
+
|
| 87 |
with torch.inference_mode():
|
| 88 |
+
with learn.no_bar(), learn.no_logging():
|
| 89 |
+
pred, _, _ = learn.predict(img)
|
| 90 |
|
| 91 |
pred_str = str(pred)
|
|
|
|
| 92 |
pil_img = Image.open(path).convert("RGB")
|
| 93 |
|
| 94 |
if display_mode == "image":
|
|
|
|
| 116 |
ann_img.save(out_path, quality=92)
|
| 117 |
|
| 118 |
except Exception as e:
|
| 119 |
+
print(f"ERROR processing file: {fname}")
|
| 120 |
traceback.print_exc()
|
| 121 |
|
| 122 |
rows.append({
|