Spaces:
Runtime error
Runtime error
GiladtheFixer
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,8 +1,38 @@
|
|
|
|
|
|
|
|
|
|
1 |
from huggingface_hub import from_pretrained_keras
|
2 |
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
|
|
7 |
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import tensorflow as tf
|
3 |
+
import gradio as gr
|
4 |
+
from tensorflow.keras.optimizers import Adam
|
5 |
from huggingface_hub import from_pretrained_keras
|
6 |
|
7 |
+
reloaded_model = from_pretrained_keras('ShaharAdar/best-model-try')
|
8 |
+
reloaded_model.compile(optimizer=Adam(0.00001),
|
9 |
+
loss='categorical_crossentropy',
|
10 |
+
metrics=['accuracy']
|
11 |
+
)
|
12 |
|
13 |
+
def classify_image(image):
|
14 |
+
# Resize the image to 224x224 as expected by your model
|
15 |
+
image = tf.image.resize(image, (224, 224))
|
16 |
+
|
17 |
+
# Add a batch dimension and make prediction
|
18 |
+
image = tf.expand_dims(image, 0) # model expects a batch of images
|
19 |
+
preds = reloaded_model.predict(image)
|
20 |
+
|
21 |
+
# Assuming the output is a softmax layer, get the predicted class index
|
22 |
+
predicted_class = tf.argmax(preds, axis=1).numpy()[0]
|
23 |
+
|
24 |
+
# Optionally, convert class index to label if you have a mapping
|
25 |
+
labels = ['Clams', 'Corals', 'Crabs', 'Dolphin', 'Eel', 'Fish',
|
26 |
+
'Jelly Fish', 'Lobster', 'Nudibranchs', 'Octopus', 'Otter',
|
27 |
+
'Penguin', 'Puffers', 'Sea Rays', 'Sea Urchins', 'Seahorse',
|
28 |
+
'Seal', 'Sharks', 'Shrimp', 'Squid', 'Starfish',
|
29 |
+
'Turtle_Tortoise', 'Whale'] # example labels
|
30 |
+
return labels[predicted_class]
|
31 |
+
|
32 |
+
import gradio as gr
|
33 |
+
|
34 |
+
# Define the interface
|
35 |
+
iface = gr.Interface(fn=classify_image, inputs="image", outputs="text")
|
36 |
+
|
37 |
+
# Launch the application
|
38 |
+
iface.launch()
|