Muhammad Abdiel Al Hafiz commited on
Commit
43f196e
ยท
1 Parent(s): af4fa21

add app file

Browse files
Files changed (1) hide show
  1. app.py +37 -0
app.py ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import tensorflow as tf
3
+ import numpy as np
4
+ from PIL import Image
5
+
6
+ model_path = '/workspaces/animals-classifier-demo/animals-classifier-demo/model'
7
+ model = tf.saved_model.load(model_path)
8
+
9
+ labels = ['butterfly', 'cats', 'cow', 'dogs', 'elephant',
10
+ 'horse', 'monkey', 'sheep', 'spider', 'squirrel']
11
+
12
+ def predict_image(image):
13
+ image_resized = image.resize((224, 224))
14
+ image_array = np.array(image_resized).astype(np.float32) / 255.0
15
+ image_array = np.expand_dims(image_array, axis=0)
16
+
17
+ predictions = model.signatures['serving_default'](tf.convert_to_tensor(image_array, dtype=tf.float32))['output_0']
18
+
19
+ # Top 3 classes
20
+ top_3_indices = np.argsort(predictions.numpy(), axis=1)[0][-3:][::-1]
21
+ top_3_labels = [labels[i] for i in top_3_indices]
22
+ top_3_probabilities = [predictions.numpy()[0][i] * 100 for i in top_3_indices]
23
+
24
+ output_string = "\n".join([f"{label}: {probability:.2f}%" for label, probability in zip(top_3_labels, top_3_probabilities)])
25
+
26
+ return image_resized, output_string
27
+
28
+ # Gradio Interface
29
+ interface = gr.Interface(
30
+ fn=predict_image,
31
+ inputs=gr.Image(type="pil"),
32
+ outputs=[gr.Image(type="pil"), "text"],
33
+ title="Animal Classifier",
34
+ description="Upload an image of an animal, and the model will predict it."
35
+ )
36
+
37
+ interface.launch(share=True)