Update streamlit_app.py
Browse files- streamlit_app.py +24 -20
streamlit_app.py
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from huggingface_hub import hf_hub_download
|
| 3 |
from tensorflow.keras.models import load_model
|
|
@@ -9,62 +12,63 @@ from PIL import Image
|
|
| 9 |
@st.cache_resource
|
| 10 |
def load_model_from_hf():
|
| 11 |
model_path = hf_hub_download(
|
| 12 |
-
repo_id="1Codephoenix/fish-freshness-model",
|
| 13 |
filename="fish_freshness_model_retrained_final.keras"
|
| 14 |
)
|
| 15 |
return load_model(model_path)
|
| 16 |
|
| 17 |
model = load_model_from_hf()
|
| 18 |
|
| 19 |
-
#
|
| 20 |
class_names = ['Fresh', 'Moderately Fresh', 'Spoiled']
|
| 21 |
custom_messages = {
|
| 22 |
'Fresh': (
|
| 23 |
"β
**Fresh Fish Detected**\n"
|
| 24 |
"- Age: Less than 1 day old\n"
|
| 25 |
-
"-
|
| 26 |
),
|
| 27 |
'Moderately Fresh': (
|
| 28 |
-
"β οΈ **Moderately Fresh**\n"
|
| 29 |
-
"- Age: 2β3 days\n"
|
| 30 |
-
"-
|
| 31 |
),
|
| 32 |
'Spoiled': (
|
| 33 |
-
"π« **Spoiled Fish**\n"
|
| 34 |
-
"- Age: 4+ days or
|
| 35 |
-
"-
|
| 36 |
)
|
| 37 |
}
|
| 38 |
|
| 39 |
# Streamlit UI
|
| 40 |
-
st.
|
| 41 |
-
st.
|
|
|
|
| 42 |
|
| 43 |
-
uploaded_file = st.file_uploader("
|
| 44 |
|
| 45 |
if uploaded_file:
|
| 46 |
try:
|
| 47 |
# Load and preprocess the image
|
| 48 |
img = Image.open(uploaded_file).convert("RGB")
|
| 49 |
-
img_resized = img.resize((224, 224))
|
| 50 |
img_array = image.img_to_array(img_resized)
|
| 51 |
-
img_array = np.expand_dims(img_array / 255.0, axis=0)
|
| 52 |
|
| 53 |
-
# Predict
|
| 54 |
prediction = model.predict(img_array)
|
| 55 |
predicted_index = np.argmax(prediction)
|
| 56 |
predicted_class = class_names[predicted_index]
|
| 57 |
confidence = prediction[0][predicted_index]
|
| 58 |
|
| 59 |
-
#
|
| 60 |
st.image(img, caption="Uploaded Fish Image", use_column_width=True)
|
| 61 |
st.markdown(f"### π― Prediction: **{predicted_class}** ({confidence * 100:.2f}% confidence)")
|
| 62 |
st.markdown(custom_messages[predicted_class])
|
| 63 |
|
| 64 |
# Show all class probabilities
|
| 65 |
-
st.subheader("π
|
| 66 |
-
for i,
|
| 67 |
-
st.write(f"- {
|
| 68 |
|
| 69 |
except Exception as e:
|
| 70 |
-
st.error(f"
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
os.environ["STREAMLIT_BROWSER_GATHER_USAGE_STATS"] = "false" # Prevent Hugging Face write errors
|
| 3 |
+
|
| 4 |
import streamlit as st
|
| 5 |
from huggingface_hub import hf_hub_download
|
| 6 |
from tensorflow.keras.models import load_model
|
|
|
|
| 12 |
@st.cache_resource
|
| 13 |
def load_model_from_hf():
|
| 14 |
model_path = hf_hub_download(
|
| 15 |
+
repo_id="1Codephoenix/fish-freshness-model", # Your model repo
|
| 16 |
filename="fish_freshness_model_retrained_final.keras"
|
| 17 |
)
|
| 18 |
return load_model(model_path)
|
| 19 |
|
| 20 |
model = load_model_from_hf()
|
| 21 |
|
| 22 |
+
# Define class names and interpretation messages
|
| 23 |
class_names = ['Fresh', 'Moderately Fresh', 'Spoiled']
|
| 24 |
custom_messages = {
|
| 25 |
'Fresh': (
|
| 26 |
"β
**Fresh Fish Detected**\n"
|
| 27 |
"- Age: Less than 1 day old\n"
|
| 28 |
+
"- Bright eyes, red gills, firm flesh"
|
| 29 |
),
|
| 30 |
'Moderately Fresh': (
|
| 31 |
+
"β οΈ **Moderately Fresh Fish**\n"
|
| 32 |
+
"- Age: Around 2β3 days\n"
|
| 33 |
+
"- Slight odor, some loss of firmness"
|
| 34 |
),
|
| 35 |
'Spoiled': (
|
| 36 |
+
"π« **Spoiled or Unsafe Fish**\n"
|
| 37 |
+
"- Age: 4+ days or chemically preserved\n"
|
| 38 |
+
"- Strong smell, dull eyes, unsafe to eat"
|
| 39 |
)
|
| 40 |
}
|
| 41 |
|
| 42 |
# Streamlit UI
|
| 43 |
+
st.set_page_config(page_title="Fish Freshness Classifier", page_icon="π")
|
| 44 |
+
st.title("π AI-Powered Fish Freshness Classifier")
|
| 45 |
+
st.subheader("Upload a fish image to predict its freshness level")
|
| 46 |
|
| 47 |
+
uploaded_file = st.file_uploader("Choose an image", type=["jpg", "jpeg", "png"])
|
| 48 |
|
| 49 |
if uploaded_file:
|
| 50 |
try:
|
| 51 |
# Load and preprocess the image
|
| 52 |
img = Image.open(uploaded_file).convert("RGB")
|
| 53 |
+
img_resized = img.resize((224, 224))
|
| 54 |
img_array = image.img_to_array(img_resized)
|
| 55 |
+
img_array = np.expand_dims(img_array / 255.0, axis=0)
|
| 56 |
|
| 57 |
+
# Predict using the model
|
| 58 |
prediction = model.predict(img_array)
|
| 59 |
predicted_index = np.argmax(prediction)
|
| 60 |
predicted_class = class_names[predicted_index]
|
| 61 |
confidence = prediction[0][predicted_index]
|
| 62 |
|
| 63 |
+
# Display results
|
| 64 |
st.image(img, caption="Uploaded Fish Image", use_column_width=True)
|
| 65 |
st.markdown(f"### π― Prediction: **{predicted_class}** ({confidence * 100:.2f}% confidence)")
|
| 66 |
st.markdown(custom_messages[predicted_class])
|
| 67 |
|
| 68 |
# Show all class probabilities
|
| 69 |
+
st.subheader("π Class Probabilities:")
|
| 70 |
+
for i, name in enumerate(class_names):
|
| 71 |
+
st.write(f"- {name}: {prediction[0][i] * 100:.2f}%")
|
| 72 |
|
| 73 |
except Exception as e:
|
| 74 |
+
st.error(f"β Error processing image: {e}")
|