Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,31 +3,42 @@ import tensorflow as tf
|
|
3 |
from tensorflow.keras.preprocessing import image
|
4 |
import numpy as np
|
5 |
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
prediction = model.predict(img_array)
|
19 |
if prediction[0] < 0.5:
|
20 |
return "Cat"
|
21 |
else:
|
22 |
return "Dog"
|
23 |
|
24 |
-
# Create the Gradio interface
|
25 |
|
26 |
interface = gr.Interface(fn=predict_image,
|
27 |
-
inputs=gr.Image(type="pil"),
|
28 |
outputs="text",
|
29 |
title="Cat and Dog Classifier",
|
30 |
-
description="Upload an
|
|
|
31 |
|
32 |
-
|
33 |
-
interface.launch(share=True)
|
|
|
3 |
from tensorflow.keras.preprocessing import image
|
4 |
import numpy as np
|
5 |
|
6 |
+
|
7 |
+
def load_models():
|
8 |
+
models = {}
|
9 |
+
models['SimpleNN_model'] = tf.keras.models.load_model("SimpleNN_model.h5")
|
10 |
+
models['VGG16'] = tf.keras.models.load_model("vgg16.h5")
|
11 |
+
return models
|
12 |
+
|
13 |
+
|
14 |
+
models = load_models()
|
15 |
+
|
16 |
+
|
17 |
+
def predict_image(img, model_name):
|
18 |
+
model = models[model_name]
|
19 |
+
|
20 |
+
if model_name == 'SimpleNN_model':
|
21 |
+
img = img.resize((256, 256))
|
22 |
+
elif model_name == 'VGG16':
|
23 |
+
img = img.resize((224, 224))
|
24 |
+
|
25 |
+
img_array = image.img_to_array(img)
|
26 |
+
img_array = np.expand_dims(img_array, axis=0)
|
27 |
+
img_array = img_array / 255.0
|
28 |
+
|
29 |
+
|
30 |
prediction = model.predict(img_array)
|
31 |
if prediction[0] < 0.5:
|
32 |
return "Cat"
|
33 |
else:
|
34 |
return "Dog"
|
35 |
|
|
|
36 |
|
37 |
interface = gr.Interface(fn=predict_image,
|
38 |
+
inputs=[gr.Image(type="pil"), gr.Dropdown(["SimpleNN_model", "VGG16"], label="Select Model")],
|
39 |
outputs="text",
|
40 |
title="Cat and Dog Classifier",
|
41 |
+
description="Upload an Image")
|
42 |
+
|
43 |
|
44 |
+
interface.launch()
|
|