Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,63 +1,38 @@
|
|
| 1 |
-
import
|
| 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 |
-
st.write("This is a simple web app to test and compare different image classifier models using Hugging Face's image-classification pipeline.")
|
| 40 |
-
st.write("From time to time more models will be added to the list. If you want to add a model, please open an issue on the GitHub repository.")
|
| 41 |
-
st.write("If you like this project, please consider liking it or buying me a coffee. It will help me to keep working on this and other projects. Thank you!")
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
input_image = st.file_uploader("Upload Image")
|
| 45 |
-
shosen_model = st.selectbox("Select the model to use", MODELS)
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
if input_image is not None:
|
| 49 |
-
image_to_classify = Image.open(input_image)
|
| 50 |
-
st.image(image_to_classify, caption="Uploaded Image")
|
| 51 |
-
if st.button("Classify"):
|
| 52 |
-
image_to_classify = Image.open(input_image)
|
| 53 |
-
classification_obj1 =[]
|
| 54 |
-
#avable_models = st.selectbox
|
| 55 |
-
|
| 56 |
-
classification_result = classify(image_to_classify, shosen_model)
|
| 57 |
-
classification_obj1.append(classification_result)
|
| 58 |
-
print_result(classification_result)
|
| 59 |
-
save_result(classification_result)
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
if __name__ == "__main__":
|
| 63 |
-
main()
|
|
|
|
| 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', return_dict=False)
|
| 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()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|