haxerwddle commited on
Commit
d150902
·
1 Parent(s): 33f731b

Change model

Browse files
Files changed (2) hide show
  1. app.py +29 -30
  2. requirements.txt +5 -4
app.py CHANGED
@@ -1,46 +1,45 @@
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
- # from tensorflow.keras.applications.resnet50 import preprocess_input
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
 
23
- # model_path = hf_hub_download(repo_id=REPO_ID, filename=FILENAME)
24
 
25
- # model = tf.keras.models.load_model(
26
- # model_path,
27
- # custom_objects={"preprocess_input": preprocess_input}
28
- # )
29
 
30
 
31
- # def preprocess(image):
32
- # image = image.resize((224, 224))
33
- # image = np.array(image)
34
- # image = preprocess_input(image)
35
- # image = np.expand_dims(image, axis=0)
36
- # return image
37
 
38
  def predict(img):
39
- predictions = classifier(img)
40
- return {p["label"]: p["score"] for p in predictions}
41
- # img = preprocess(img)
42
- # preds = model.predict(img)[0]
43
- # return {class_names[i]: float(preds[i]) for i in range(len(preds))}
44
 
45
 
46
  demo = gr.Interface(
 
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
+ from tensorflow.keras.applications.resnet50 import preprocess_input
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
 
23
+ model_path = hf_hub_download(repo_id=REPO_ID, filename=FILENAME)
24
 
25
+ model = tf.keras.models.load_model(
26
+ model_path,
27
+ custom_objects={"preprocess_input": preprocess_input}
28
+ )
29
 
30
 
31
+ def preprocess(image):
32
+ image = image.resize((224, 224))
33
+ image = np.array(image)
34
+ image = preprocess_input(image)
35
+ image = np.expand_dims(image, axis=0)
36
+ return image
37
 
38
  def predict(img):
39
+
40
+ img = preprocess(img)
41
+ preds = model.predict(img)[0]
42
+ return {class_names[i]: float(preds[i]) for i in range(len(preds))}
 
43
 
44
 
45
  demo = gr.Interface(
requirements.txt CHANGED
@@ -1,5 +1,6 @@
1
-
 
 
 
 
2
  Pillow
3
- transformers
4
- torch
5
- timm
 
1
+ tensorflow-cpu==2.16.1
2
+ huggingface_hub
3
+ datasets
4
+ gradio
5
+ numpy
6
  Pillow