Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,28 +1,41 @@
|
|
1 |
import gradio as gr
|
2 |
import tensorflow as tf
|
3 |
import numpy as np
|
|
|
4 |
|
5 |
-
|
6 |
-
model_path = "iris_mlp.keras"
|
7 |
model = tf.keras.models.load_model(model_path)
|
8 |
|
9 |
-
labels = ['Setosa', 'Versicolour', 'Virginica']
|
10 |
-
|
11 |
# Define the core prediction function
|
12 |
-
def
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
# Create the Gradio interface
|
|
|
21 |
iface = gr.Interface(
|
22 |
-
fn=
|
23 |
-
inputs=
|
24 |
outputs=gr.Label(),
|
25 |
-
examples=[
|
26 |
-
)
|
27 |
|
28 |
-
iface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import tensorflow as tf
|
3 |
import numpy as np
|
4 |
+
from PIL import Image
|
5 |
|
6 |
+
model_path = "transfer_learning_pk.keras"
|
|
|
7 |
model = tf.keras.models.load_model(model_path)
|
8 |
|
|
|
|
|
9 |
# Define the core prediction function
|
10 |
+
def predict_pokemon(image):
|
11 |
+
# Preprocess image
|
12 |
+
print(type(image))
|
13 |
+
image = Image.fromarray(image.astype('uint8')) # Convert numpy array to PIL image
|
14 |
+
image = image.resize((150, 150)) #resize the image to 28x28 and converts it to gray scale
|
15 |
+
image = np.array(image)
|
16 |
+
image = np.expand_dims(image, axis=0) # same as image[None, ...]
|
17 |
+
|
18 |
+
# Predict
|
19 |
+
prediction = model.predict(image)
|
20 |
+
|
21 |
+
# No need to apply sigmoid, as the output layer already uses softmax
|
22 |
+
# Convert the probabilities to rounded values
|
23 |
+
prediction = np.round(prediction, 2)
|
24 |
+
|
25 |
+
# Separate the probabilities for each class
|
26 |
+
p_gyarados = prediction[0][0] # Probability for class 'articuno'
|
27 |
+
p_metapod = prediction[0][1] # Probability for class 'moltres'
|
28 |
+
p_ponyta = prediction[0][2] # Probability for class 'zapdos'
|
29 |
+
|
30 |
+
return {'gyarados': p_gyarados, 'metapod': p_metapod, 'ponyta': p_ponyta}
|
31 |
|
32 |
# Create the Gradio interface
|
33 |
+
input_image = gr.Image()
|
34 |
iface = gr.Interface(
|
35 |
+
fn=predict_pokemon,
|
36 |
+
inputs=input_image,
|
37 |
outputs=gr.Label(),
|
38 |
+
examples=["img/gyarados1.jpg", "img/gyarados2.jpg", "img/gyarados3.jpg", "img/metapod1.jpg", "img/metapod2.jpg", "img/metapod3.jpg", "img/ponyta1.jpg", "img/ponyta2.jpg", "img/ponyta3.jpg"],
|
39 |
+
description="Pokemon")
|
40 |
|
41 |
+
iface.launch()
|