BunkerMinecraft commited on
Commit
0933310
·
verified ·
1 Parent(s): e18a27e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -41
app.py CHANGED
@@ -1,58 +1,34 @@
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(),
22
- layers.Dense(256, activation='relu', kernel_regularizer=regularizers.l2(0.001)),
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(6, 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
40
- return False
41
 
42
- if _is_pointer(WEIGHTS_LOCAL):
43
- WEIGHTS_LOCAL = hf_hub_download(
44
- repo_id=MODEL_REPO_ID,
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 ----
 
1
  from huggingface_hub import hf_hub_download
2
  import tensorflow as tf
 
3
  from tensorflow.keras.applications.efficientnet import preprocess_input
4
+ from tensorflow.keras import backend as K # not required, but safe
5
+ import numpy as np
6
  from PIL import Image
7
  import gradio as gr
8
 
9
+ # ---- Labels ----
10
  class_names = ['battery','glass','metal','organic','paper','plastic']
 
11
 
12
+ # REPLACE YOUR OLD MODEL BUILD + load_weights WITH THIS BLOCK
13
+ # Download and load the FULL saved model (.keras) from your HF Model repo
14
+ MODEL_REPO_ID = "yourname/neurecai-b7" # <-- change this
15
+ MODEL_FILENAME = "model.keras"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
+ model_path = hf_hub_download(
18
+ repo_id=MODEL_REPO_ID,
19
+ filename=MODEL_FILENAME,
20
+ repo_type="model"
21
+ )
22
+ model = tf.keras.models.load_model(model_path, compile=False)
23
+ print(f"Loaded full model: {model_path}")
 
24
 
 
 
 
 
 
 
 
 
25
 
26
+ # ---- Preprocess (keep consistent with training) ----
27
  def preprocess(img: Image.Image):
28
  img = img.convert("RGB").resize((224, 224), Image.BILINEAR)
29
  x = np.asarray(img, dtype=np.float32)
30
+ # If you trained with x/255.0, replace next line with: x = x / 255.0
31
+ x = preprocess_input(x) # EfficientNet normalization
32
  return np.expand_dims(x, 0)
33
 
34
  # ---- Predict ----