File size: 1,414 Bytes
c20bc42
 
 
 
1ddae70
 
c20bc42
 
 
 
 
1ddae70
c20bc42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import numpy as np
import tensorflow as tf
import gradio as gr
from tensorflow.keras.optimizers import Adam
from huggingface_hub import from_pretrained_keras

reloaded_model = from_pretrained_keras('ShaharAdar/best-model-try')
reloaded_model.compile(optimizer=Adam(0.00001),
                       loss='categorical_crossentropy',
                       metrics=['accuracy']
)

def classify_image(image):
    # Resize the image to 224x224 as expected by your model
    image = tf.image.resize(image, (224, 224))

    # Add a batch dimension and make prediction
    image = tf.expand_dims(image, 0)  # model expects a batch of images
    preds = reloaded_model.predict(image)

    # Assuming the output is a softmax layer, get the predicted class index
    predicted_class = tf.argmax(preds, axis=1).numpy()[0]

    # Optionally, convert class index to label if you have a mapping
    labels = ['Clams', 'Corals', 'Crabs', 'Dolphin', 'Eel', 'Fish',
              'Jelly Fish', 'Lobster', 'Nudibranchs', 'Octopus', 'Otter',
              'Penguin', 'Puffers', 'Sea Rays', 'Sea Urchins', 'Seahorse',
              'Seal', 'Sharks', 'Shrimp', 'Squid', 'Starfish',
              'Turtle_Tortoise', 'Whale']    # example labels
    return labels[predicted_class]

import gradio as gr

# Define the interface
iface = gr.Interface(fn=classify_image, inputs="image", outputs="text")

# Launch the application
iface.launch()