ShaharAdar commited on
Commit
f77de1e
·
verified ·
1 Parent(s): 8897615

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -63
app.py CHANGED
@@ -1,63 +1,38 @@
1
- import streamlit as st
2
- from transformers import pipeline
3
- from PIL import Image
4
-
5
- MODEL_1 = "ShaharAdar/best-model-try"
6
- MIN_ACEPTABLE_SCORE = 0.1
7
- MAX_N_LABELS = 5
8
- MODELS = [
9
- "ShaharAdar/best-model-try", #Classifição geral
10
- ]
11
-
12
- def classify(image, model):
13
- classifier = pipeline("image-classification", model=model)
14
- result= classifier(image)
15
- return result
16
-
17
- def save_result(result):
18
- st.write("In the future, this function will save the result in a database.")
19
-
20
- def print_result(result):
21
-
22
- comulative_discarded_score = 0
23
- for i in range(len(result)):
24
- if result[i]['score'] < MIN_ACEPTABLE_SCORE:
25
- comulative_discarded_score += result[i]['score']
26
- else:
27
- st.write(result[i]['label'])
28
- st.progress(result[i]['score'])
29
- st.write(result[i]['score'])
30
-
31
- st.write(f"comulative_discarded_score:")
32
- st.progress(comulative_discarded_score)
33
- st.write(comulative_discarded_score)
34
-
35
-
36
-
37
- def main():
38
- st.title("Image Classification")
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()