Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,23 +1,21 @@
|
|
| 1 |
from huggingface_hub import hf_hub_download
|
| 2 |
import tensorflow as tf
|
| 3 |
from tensorflow.keras import layers, models, regularizers
|
| 4 |
-
import
|
|
|
|
| 5 |
from PIL import Image
|
| 6 |
-
import numpy as np
|
| 7 |
-
import os
|
| 8 |
import gradio as gr
|
| 9 |
|
| 10 |
-
# ---- Load config if present ----
|
| 11 |
-
cfg = None
|
| 12 |
-
if os.path.exists("config.json"):
|
| 13 |
-
with open("config.json") as f:
|
| 14 |
-
cfg = json.load(f)
|
| 15 |
-
|
| 16 |
# ---- Class labels ----
|
| 17 |
-
class_names = ['battery',
|
|
|
|
| 18 |
|
| 19 |
-
# ---- Build model
|
| 20 |
-
base_model = tf.keras.applications.EfficientNetB7(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
model = models.Sequential([
|
| 22 |
base_model,
|
| 23 |
layers.GlobalAveragePooling2D(),
|
|
@@ -25,18 +23,17 @@ model = models.Sequential([
|
|
| 25 |
layers.Dropout(0.3),
|
| 26 |
layers.Dense(128, activation='relu', kernel_regularizer=regularizers.l2(0.001)),
|
| 27 |
layers.Dropout(0.3),
|
| 28 |
-
layers.Dense(
|
| 29 |
])
|
| 30 |
|
| 31 |
# ---- Load trained weights (pointer-safe) ----
|
| 32 |
WEIGHTS_LOCAL = "model_weights.h5"
|
| 33 |
-
MODEL_REPO_ID = "BunkerMinecraft/neurecaiv2" #
|
| 34 |
|
| 35 |
def _is_pointer(path: str) -> bool:
|
| 36 |
if not os.path.exists(path) or os.path.getsize(path) < 1024:
|
| 37 |
try:
|
| 38 |
-
with open(path, "rb") as f:
|
| 39 |
-
head = f.read(200)
|
| 40 |
return (b"git-lfs" in head) or (b"xet" in head)
|
| 41 |
except Exception:
|
| 42 |
return True
|
|
@@ -48,41 +45,32 @@ if _is_pointer(WEIGHTS_LOCAL):
|
|
| 48 |
filename="model_weights.h5",
|
| 49 |
repo_type="model"
|
| 50 |
)
|
| 51 |
-
|
| 52 |
model.load_weights(WEIGHTS_LOCAL)
|
| 53 |
print(f"Loaded weights from: {WEIGHTS_LOCAL}")
|
| 54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
-
# ----
|
| 57 |
-
def preprocess(image: Image.Image):
|
| 58 |
-
image = image.resize((224, 224))
|
| 59 |
-
image = np.array(image) / 255.0
|
| 60 |
-
image = np.expand_dims(image, axis=0)
|
| 61 |
-
return image
|
| 62 |
-
|
| 63 |
-
# ---- Prediction function ----
|
| 64 |
def predict(image: Image.Image):
|
| 65 |
x = preprocess(image)
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
return
|
| 70 |
-
"class": class_names[class_idx],
|
| 71 |
-
"confidence": confidence
|
| 72 |
-
}
|
| 73 |
|
| 74 |
-
# ----
|
| 75 |
interface = gr.Interface(
|
| 76 |
fn=predict,
|
| 77 |
inputs=gr.Image(type="pil", label="Upload an image"),
|
| 78 |
-
outputs=[
|
| 79 |
-
gr.Label(num_top_classes=1, label="Predicted class"),
|
| 80 |
-
gr.Number(label="Confidence")
|
| 81 |
-
],
|
| 82 |
title="NeuRecAI - Material Classifier",
|
| 83 |
-
description="
|
| 84 |
)
|
| 85 |
|
| 86 |
-
# ---- Launch ----
|
| 87 |
if __name__ == "__main__":
|
| 88 |
-
interface.launch()
|
|
|
|
| 1 |
from huggingface_hub import hf_hub_download
|
| 2 |
import tensorflow as tf
|
| 3 |
from tensorflow.keras import layers, models, regularizers
|
| 4 |
+
from tensorflow.keras.applications.efficientnet import preprocess_input
|
| 5 |
+
import json, os, numpy as np
|
| 6 |
from PIL import Image
|
|
|
|
|
|
|
| 7 |
import gradio as gr
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
# ---- Class labels ----
|
| 10 |
+
class_names = ['battery','glass','metal','organic','paper','plastic']
|
| 11 |
+
num_classes = len(class_names)
|
| 12 |
|
| 13 |
+
# ---- Build model (match training) ----
|
| 14 |
+
base_model = tf.keras.applications.EfficientNetB7(
|
| 15 |
+
weights=None, # or "imagenet" if that’s how you trained
|
| 16 |
+
include_top=False,
|
| 17 |
+
input_shape=(224, 224, 3)
|
| 18 |
+
)
|
| 19 |
model = models.Sequential([
|
| 20 |
base_model,
|
| 21 |
layers.GlobalAveragePooling2D(),
|
|
|
|
| 23 |
layers.Dropout(0.3),
|
| 24 |
layers.Dense(128, activation='relu', kernel_regularizer=regularizers.l2(0.001)),
|
| 25 |
layers.Dropout(0.3),
|
| 26 |
+
layers.Dense(num_classes, activation='softmax')
|
| 27 |
])
|
| 28 |
|
| 29 |
# ---- Load trained weights (pointer-safe) ----
|
| 30 |
WEIGHTS_LOCAL = "model_weights.h5"
|
| 31 |
+
MODEL_REPO_ID = "BunkerMinecraft/neurecaiv2" # your model repo
|
| 32 |
|
| 33 |
def _is_pointer(path: str) -> bool:
|
| 34 |
if not os.path.exists(path) or os.path.getsize(path) < 1024:
|
| 35 |
try:
|
| 36 |
+
with open(path, "rb") as f: head = f.read(200)
|
|
|
|
| 37 |
return (b"git-lfs" in head) or (b"xet" in head)
|
| 38 |
except Exception:
|
| 39 |
return True
|
|
|
|
| 45 |
filename="model_weights.h5",
|
| 46 |
repo_type="model"
|
| 47 |
)
|
|
|
|
| 48 |
model.load_weights(WEIGHTS_LOCAL)
|
| 49 |
print(f"Loaded weights from: {WEIGHTS_LOCAL}")
|
| 50 |
|
| 51 |
+
# ---- Preprocess ----
|
| 52 |
+
def preprocess(img: Image.Image):
|
| 53 |
+
img = img.convert("RGB").resize((224, 224), Image.BILINEAR)
|
| 54 |
+
x = np.asarray(img, dtype=np.float32)
|
| 55 |
+
x = preprocess_input(x) # EfficientNet normalization
|
| 56 |
+
return np.expand_dims(x, 0)
|
| 57 |
|
| 58 |
+
# ---- Predict ----
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
def predict(image: Image.Image):
|
| 60 |
x = preprocess(image)
|
| 61 |
+
y = model.predict(x, verbose=0)[0]
|
| 62 |
+
idx = int(np.argmax(y))
|
| 63 |
+
conf = float(y[idx])
|
| 64 |
+
return class_names[idx], conf
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
+
# ---- UI ----
|
| 67 |
interface = gr.Interface(
|
| 68 |
fn=predict,
|
| 69 |
inputs=gr.Image(type="pil", label="Upload an image"),
|
| 70 |
+
outputs=[gr.Label(label="Predicted class"), gr.Number(label="Confidence")],
|
|
|
|
|
|
|
|
|
|
| 71 |
title="NeuRecAI - Material Classifier",
|
| 72 |
+
description="Battery, glass, metal, organic, paper, or plastic."
|
| 73 |
)
|
| 74 |
|
|
|
|
| 75 |
if __name__ == "__main__":
|
| 76 |
+
interface.launch()
|