haxerwddle commited on
Commit
c78c56e
·
1 Parent(s): 7b9e04f

Add model

Browse files
Files changed (1) hide show
  1. app.py +62 -4
app.py CHANGED
@@ -1,7 +1,65 @@
1
  import gradio as gr
 
 
2
 
3
- def greet(name):
4
- return "Hello " + name + "!!"
 
 
5
 
6
- demo = gr.Interface(fn=greet, inputs="text", outputs="text")
7
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ import numpy as np
3
+ from PIL import Image
4
 
5
+ from tensorflow.keras.applications.resnet50 import preprocess_input
6
+ import tensorflow as tf
7
+ from huggingface_hub import hf_hub_download
8
+ from datasets import load_dataset
9
 
10
+ ds = load_dataset("dvk65/TrashTypes")
11
+ class_names = ds["train"].features["label"].names
12
+
13
+ REPO_ID = "dvk65/trash-classifier-resnet50"
14
+ FILENAME = "trashclassify_13.keras"
15
+
16
+ model_path = hf_hub_download(repo_id=REPO_ID, filename=FILENAME)
17
+
18
+ model = tf.keras.models.load_model(
19
+ model_path,
20
+ custom_objects={"preprocess_input": preprocess_input}
21
+ )
22
+
23
+
24
+ # --- PREPROCESSING ---
25
+ def preprocess(image):
26
+ image = image.resize((224, 224)) # depends on your model
27
+ image = np.array(image) / 255.0 # normalize
28
+ image = np.expand_dims(image, axis=0) # add batch dimension
29
+ return image
30
+
31
+
32
+ # --- PREDICTION FUNCTION ---
33
+ def predict(img):
34
+ img = preprocess(img)
35
+ preds = model.predict(img)[0] # shape: (num_classes,)
36
+
37
+ class_names = [
38
+ "apples",
39
+ "bananas",
40
+ "bottles",
41
+ "cans",
42
+ "cardboard",
43
+ "cups",
44
+ "eggshells",
45
+ "generalcompost", # mixed leftover food
46
+ "mixers", # wooden coffee stirrers
47
+ "peels", # oranges
48
+ "platicbags", # typo in original dataset? keep as-is
49
+ "plastics", # plastic wrappers
50
+ "tissue papers"
51
+ ]
52
+
53
+ result = {class_names[i]: float(preds[i]) for i in range(len(preds))}
54
+ return result
55
+
56
+ # --- GRADIO UI ---
57
+ demo = gr.Interface(
58
+ fn=predict,
59
+ inputs=gr.Image(type="pil"),
60
+ outputs=gr.Label(num_top_classes=3),
61
+ title="Trash Classifier (ResNet50)",
62
+ description="Upload an image of trash and get the predicted type."
63
+ )
64
+
65
+ demo.launch()