realmanise commited on
Commit
0ec4110
·
verified ·
1 Parent(s): 5294e30

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +1 -46
app.py CHANGED
@@ -1,46 +1 @@
1
- import gradio as gr
2
- import numpy as np
3
- import tensorflow as tf
4
- import keras
5
-
6
- # 1. Define a dummy or pass-through function to satisfy the Lambda layer's configuration name
7
- def preprocess_input(x):
8
- return x
9
-
10
- # 2. Register the custom object explicitly so Keras can find the name during deserialization
11
- def load_garbage_model():
12
- model = keras.models.load_model(
13
- 'garbage_classifier.keras',
14
- custom_objects={'preprocess_input': preprocess_input},
15
- safe_mode=False
16
- )
17
- return model
18
-
19
- print("Loading Keras 3 model into Hugging Face memory...")
20
- model_loaded = load_garbage_model()
21
-
22
- lookup = {0: "Cardboard", 1: "Glass", 2: "Metal", 3: "Paper", 4: "Plastic", 5: "Trash"}
23
-
24
- def predict_image(img):
25
- if img is None:
26
- return "No image provided"
27
-
28
- # Resize the image array to match model requirements
29
- img_resized = tf.image.resize(img, [224, 224])
30
- img_tensor = tf.expand_dims(img_resized, axis=0)
31
-
32
- # Run the prediction through both input layers of your custom network
33
- predictions = model_loaded.predict({'resnet_input': img_tensor, 'vgg_input': img_tensor})
34
- class_idx = int(np.argmax(predictions[0]))
35
-
36
- return lookup.get(class_idx, "Unknown")
37
-
38
- # Create the Gradio interface
39
- demo = gr.Interface(
40
- fn=predict_image,
41
- inputs=gr.Image(type="numpy"),
42
- outputs="text",
43
- api_name="predict"
44
- )
45
-
46
- demo.launch()
 
1
+ lajskdlf;k