haxerwddle commited on
Commit
606f48b
·
1 Parent(s): 1890393
Files changed (1) hide show
  1. app.py +8 -10
app.py CHANGED
@@ -1,29 +1,28 @@
1
  import gradio as gr
2
- # from transformers import pipeline
3
-
4
  import numpy as np
5
  from PIL import Image
6
 
7
  from datasets import load_dataset
8
  from huggingface_hub import hf_hub_download
9
  import tensorflow as tf
10
- import tensorflow as tf
11
 
12
- # --- LOAD CLASS LABELS FROM DATASET ---
13
  ds = load_dataset("dvk65/TrashTypes")
14
  class_names = ds["train"].features["label"].names
15
 
16
- # classifier = pipeline("image-classification", model="yangy50/garbage-classification")
17
-
18
-
19
  # --- LOAD MODEL ---
20
  REPO_ID = "dvk65/trash-classifier-resnet50"
21
  FILENAME = "trashclassify_13.keras"
22
- model_path = hf_hub_download(repo_id=REPO_ID, filename=FILENAME)
23
 
24
- model = tf.keras.models.load_model(model_path)
25
 
 
 
 
 
26
 
 
27
  def preprocess(image):
28
  image = image.resize((224, 224))
29
  image = np.array(image).astype("float32")
@@ -35,7 +34,6 @@ def predict(img):
35
  preds = model.predict(img)[0]
36
  return {class_names[i]: float(preds[i]) for i in range(len(preds))}
37
 
38
-
39
  demo = gr.Interface(
40
  fn=predict,
41
  inputs=gr.Image(type="pil"),
 
1
  import gradio as gr
 
 
2
  import numpy as np
3
  from PIL import Image
4
 
5
  from datasets import load_dataset
6
  from huggingface_hub import hf_hub_download
7
  import tensorflow as tf
8
+ from tensorflow.keras.applications.resnet50 import preprocess_input
9
 
10
+ # --- LOAD LABELS ---
11
  ds = load_dataset("dvk65/TrashTypes")
12
  class_names = ds["train"].features["label"].names
13
 
 
 
 
14
  # --- LOAD MODEL ---
15
  REPO_ID = "dvk65/trash-classifier-resnet50"
16
  FILENAME = "trashclassify_13.keras"
 
17
 
18
+ model_path = hf_hub_download(repo_id=REPO_ID, filename=FILENAME)
19
 
20
+ model = tf.keras.models.load_model(
21
+ model_path,
22
+ custom_objects={"preprocess_input": preprocess_input}
23
+ )
24
 
25
+ # --- PREPROCESS---
26
  def preprocess(image):
27
  image = image.resize((224, 224))
28
  image = np.array(image).astype("float32")
 
34
  preds = model.predict(img)[0]
35
  return {class_names[i]: float(preds[i]) for i in range(len(preds))}
36
 
 
37
  demo = gr.Interface(
38
  fn=predict,
39
  inputs=gr.Image(type="pil"),