tamvi commited on
Commit
1cc8e4d
1 Parent(s): 51ece47

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -0
app.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import tensorflow as tf
3
+ import numpy as np
4
+ from PIL import Image
5
+
6
+ model_path = "transferlearning_pokemon.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 150x150
15
+ image = np.array(image)
16
+ image = np.expand_dims(image, axis=0) # Expand dimensions to match the model input shape
17
+
18
+ # Predict
19
+ prediction = model.predict(image)
20
+
21
+ # Print the shape of the prediction to debug
22
+ print(f"Prediction shape: {prediction.shape}")
23
+
24
+ # Assuming the output is already softmax probabilities
25
+ probabilities = prediction[0]
26
+
27
+ # Print the probabilities array to debug
28
+ print(f"Probabilities: {probabilities}")
29
+
30
+ # Assuming your model was trained with these class names
31
+ class_names = ['charmander', 'eevee', 'pikachuu'] # Replace 'another_pokemon' with your third class name
32
+
33
+ # Create a dictionary of class probabilities
34
+ result = {class_names[i]: float(probabilities[i]) for i in range(len(class_names))}
35
+
36
+ return result
37
+
38
+
39
+ # Create the Gradio interface
40
+ input_image = gr.Image()
41
+ iface = gr.Interface(
42
+ fn=predict_pokemon,
43
+ inputs=input_image,
44
+ outputs=gr.Label(),
45
+ examples=["pokemon_examples/charmander.png", "pokemon_examples/charmander1.jpg", "pokemon_examples/eevee.png", "pokemon_examples/eevee1.jpg", "pokemon_examples/pika.png", "pokemon_examples/pika1.jpg"],
46
+ description="A simple mlp classification model for image classification using the mnist dataset.")
47
+ iface.launch()