BunkerMinecraft commited on
Commit
b82a801
·
verified ·
1 Parent(s): 37bac32

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -23
app.py CHANGED
@@ -1,51 +1,78 @@
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 = "BunkerMinecraft/neurecaiv2" # <-- change this
15
- MODEL_FILENAME = "model_final.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 ----
 
 
 
 
 
 
 
 
 
35
  def predict(image: Image.Image):
36
  x = preprocess(image)
37
- y = model.predict(x, verbose=0)[0]
38
- idx = int(np.argmax(y))
39
- conf = float(y[idx])
 
 
40
  return class_names[idx], conf
41
 
42
- # ---- UI ----
 
 
 
43
  interface = gr.Interface(
44
  fn=predict,
45
  inputs=gr.Image(type="pil", label="Upload an image"),
46
  outputs=[gr.Label(label="Predicted class"), gr.Number(label="Confidence")],
47
- title="NeuRecAI - Material Classifier",
48
- description="Battery, glass, metal, organic, paper, or plastic."
49
  )
50
 
51
  if __name__ == "__main__":
 
1
  from huggingface_hub import hf_hub_download
2
  import tensorflow as tf
 
 
3
  import numpy as np
4
+ from PIL import Image, ImageOps
5
  import gradio as gr
6
 
7
+ # -----------------------
8
+ # CONFIG
9
+ # -----------------------
 
 
10
  MODEL_REPO_ID = "BunkerMinecraft/neurecaiv2" # <-- change this
11
+ MODEL_FILENAME = "keras_model.h5"
12
+ LABELS_FILENAME = "labels.txt"
13
+ IMAGE_SIZE = (224, 224) # TM fixed size
14
 
15
+ # -----------------------
16
+ # DOWNLOAD MODEL + LABELS
17
+ # -----------------------
18
  model_path = hf_hub_download(
19
  repo_id=MODEL_REPO_ID,
20
  filename=MODEL_FILENAME,
21
  repo_type="model"
22
  )
23
+
24
+ labels_path = hf_hub_download(
25
+ repo_id=MODEL_REPO_ID,
26
+ filename=LABELS_FILENAME,
27
+ repo_type="model"
28
+ )
29
+
30
+ # Load the Teachable Machine Keras .h5 model
31
  model = tf.keras.models.load_model(model_path, compile=False)
32
+ print(f"Loaded model: {model_path}")
33
+
34
+ # Load labels
35
+ with open(labels_path, "r") as f:
36
+ class_names = [line.strip() for line in f.readlines()]
37
+ print("Loaded labels:", class_names)
38
 
39
 
40
+ # -----------------------
41
+ # PREPROCESS FUNCTION (matches TM)
42
+ # -----------------------
43
  def preprocess(img: Image.Image):
44
+ img = img.convert("RGB")
45
+ img = ImageOps.fit(img, IMAGE_SIZE, Image.Resampling.LANCZOS)
 
 
 
46
 
47
+ image_array = np.asarray(img).astype(np.float32)
48
+ # TM normalization:
49
+ normalized = (image_array / 127.5) - 1.0
50
+
51
+ return np.expand_dims(normalized, axis=0)
52
+
53
+
54
+ # -----------------------
55
+ # PREDICT FUNCTION
56
+ # -----------------------
57
  def predict(image: Image.Image):
58
  x = preprocess(image)
59
+ preds = model.predict(x, verbose=0)[0]
60
+
61
+ idx = int(np.argmax(preds))
62
+ conf = float(preds[idx])
63
+
64
  return class_names[idx], conf
65
 
66
+
67
+ # -----------------------
68
+ # UI (Gradio)
69
+ # -----------------------
70
  interface = gr.Interface(
71
  fn=predict,
72
  inputs=gr.Image(type="pil", label="Upload an image"),
73
  outputs=[gr.Label(label="Predicted class"), gr.Number(label="Confidence")],
74
+ title="Teachable Machine Classifier",
75
+ description="Upload an image to classify using your keras_Model.h5 Teachable Machine model."
76
  )
77
 
78
  if __name__ == "__main__":