Spaces:
Sleeping
Sleeping
up
Browse files
app.py
CHANGED
|
@@ -1,25 +1,32 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from tensorflow.keras.models import load_model
|
| 3 |
-
from tensorflow.keras.preprocessing.image import img_to_array
|
| 4 |
-
from PIL import Image
|
| 5 |
import numpy as np
|
|
|
|
| 6 |
|
| 7 |
-
# Load your model
|
| 8 |
-
|
|
|
|
| 9 |
|
| 10 |
-
# Define prediction function
|
| 11 |
def predict(image):
|
| 12 |
-
#
|
| 13 |
-
img =
|
| 14 |
-
#
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
prediction
|
| 20 |
-
class_label = '
|
| 21 |
return class_label
|
| 22 |
|
| 23 |
# Create a Gradio interface
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from tensorflow.keras.models import load_model
|
| 3 |
+
from tensorflow.keras.preprocessing.image import img_to_array, load_img
|
|
|
|
| 4 |
import numpy as np
|
| 5 |
+
from PIL import Image
|
| 6 |
|
| 7 |
+
# Load your model (using a relative path)
|
| 8 |
+
model_path = "cats_and_dogs_classifier.h5"
|
| 9 |
+
model = load_model(model_path)
|
| 10 |
|
|
|
|
| 11 |
def predict(image):
|
| 12 |
+
# Resize image to match model's expected sizing
|
| 13 |
+
img = image.resize((150, 150))
|
| 14 |
+
# Convert image to array and expand dimensions to fit model input requirements
|
| 15 |
+
img_array = img_to_array(img)
|
| 16 |
+
img_array = np.expand_dims(img_array, axis=0)
|
| 17 |
+
# Predict using loaded model
|
| 18 |
+
prediction = model.predict(img_array)
|
| 19 |
+
# Convert prediction to label
|
| 20 |
+
class_label = 'Cat' if prediction[0][0] < 0.5 else 'Dog'
|
| 21 |
return class_label
|
| 22 |
|
| 23 |
# Create a Gradio interface
|
| 24 |
+
iface = gr.Interface(fn=predict,
|
| 25 |
+
inputs=gr.inputs.Image(type='pil', label="Upload a photo of a cat or dog"),
|
| 26 |
+
outputs="text",
|
| 27 |
+
title="Cat or Dog Classifier",
|
| 28 |
+
description="Upload an image of a cat or dog to classify it.")
|
| 29 |
+
|
| 30 |
+
if __name__ == "__main__":
|
| 31 |
+
iface.launch()
|
| 32 |
+
|