File size: 1,562 Bytes
43f196e
 
 
 
 
639cd54
43f196e
 
 
 
 
 
 
 
 
 
 
3dd911a
1c2a810
a665b6d
1c2a810
a665b6d
1c2a810
823e3a4
1c2a810
 
 
 
 
 
 
 
 
 
43f196e
 
 
 
0a22c12
1c2a810
 
279f8b2
1c2a810
569f7e7
43f196e
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import gradio as gr
import tensorflow as tf
import numpy as np
from PIL import Image

model_path = 'model'
model = tf.saved_model.load(model_path)

labels = ['butterfly', 'cats', 'cow', 'dogs', 'elephant',
          'horse', 'monkey', 'sheep', 'spider', 'squirrel']

def predict_image(image):
  image_resized = image.resize((224, 224))
  image_array = np.array(image_resized).astype(np.float32) / 255.0
  image_array = np.expand_dims(image_array, axis=0)

  predictions = model.signatures['serving_default'](tf.convert_to_tensor(image_array, dtype=tf.float32))['output_0']
    
  # Highest prediction
  top_index = np.argmax(predictions.numpy(), axis=1)[0]
  top_label = labels[top_index]
  top_probability = predictions.numpy()[0][top_index]

  return {top_label:top_probability}  

# Example images
example_images = [
    ["exp_img/cat.jpg"],
    ["exp_img/cow.jpg"],
    ["exp_img/elephant.jpg"],
    ["exp_img/sheep.jpg"],
    ["exp_img/spider.jpg"],
    ["exp_img/squirrel.jpg"]
]

# Gradio Interface
interface = gr.Interface(
    fn=predict_image,
    inputs=gr.Image(type="pil"),
    outputs=gr.Label(num_top_classes=1, label="Prediction"),
    examples=example_images,
    title="Animals Classifier",
    description="Upload an image of an animal, and the model will predict it.\n\n**Disclaimer:** This model is trained only on specific animal classes (butterfly, cats, cow, dogs, elephant, horse, monkey, sheep, spider, squirrel) and may not accurately predict animals outside these classes.",
    allow_flagging="never"
)

interface.launch(share=True)