File size: 2,643 Bytes
0254525
e991448
0254525
 
e991448
c3e500b
15c40d8
c3e500b
 
15c40d8
c3e500b
 
0254525
c3e500b
15c40d8
 
0254525
15c40d8
 
 
c3e500b
15c40d8
c3e500b
0254525
c3e500b
 
 
 
0254525
 
 
c3e500b
 
0254525
 
 
 
c3e500b
 
e991448
0254525
 
 
c3e500b
 
 
0254525
e991448
 
 
c3e500b
0254525
c3e500b
15c40d8
c3e500b
0254525
c3e500b
e991448
c3e500b
e991448
c3e500b
0254525
c3e500b
 
 
15c40d8
c3e500b
0254525
15c40d8
0254525
c3e500b
 
0254525
 
 
 
c3e500b
 
e991448
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# βœ… Fix Hugging Face Space permission error
import os
os.environ["STREAMLIT_HOME"] = "/tmp"
os.environ["STREAMLIT_BROWSER_GATHER_USAGE_STATS"] = "false"

import streamlit as st
from huggingface_hub import hf_hub_download
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing import image
import numpy as np
from PIL import Image

# βœ… Load model from Hugging Face Model Hub
@st.cache_resource
def load_model_from_hf():
    model_path = hf_hub_download(
        repo_id="1Codephoenix/fish-freshness-model",
        filename="fish_freshness_model_retrained_final.keras"
    )
    return load_model(model_path)

model = load_model_from_hf()

# βœ… Class labels and messages
class_names = ['Fresh', 'Moderately Fresh', 'Spoiled']
custom_messages = {
    'Fresh': (
        "βœ… **Fresh Fish Detected**\n"
        "- Age: Less than 1 day\n"
        "- Bright eyes, red gills, firm flesh\n"
        "- Safe to eat raw or cooked"
    ),
    'Moderately Fresh': (
        "⚠️ **Moderately Fresh**\n"
        "- Age: 2–3 days\n"
        "- Slight odor, softer texture\n"
        "- Should be cooked thoroughly"
    ),
    'Spoiled': (
        "🚫 **Spoiled or Unsafe Fish**\n"
        "- Age: 4+ days or preserved\n"
        "- Strong odor, dull eyes, mushy flesh\n"
        "- Unsafe to consume"
    )
}

# βœ… Streamlit UI
st.set_page_config(page_title="Fish Freshness Classifier", page_icon="🐟")
st.title("🐟 AI-Powered Fish Freshness Classifier")
st.subheader("Upload a fish image to predict its freshness level")

uploaded_file = st.file_uploader("Choose a fish image", type=["jpg", "jpeg", "png"])

if uploaded_file:
    try:
        # Preprocess image
        img = Image.open(uploaded_file).convert("RGB")
        img_resized = img.resize((224, 224))
        img_array = image.img_to_array(img_resized)
        img_array = np.expand_dims(img_array / 255.0, axis=0)

        # Predict
        prediction = model.predict(img_array)
        predicted_index = np.argmax(prediction)
        predicted_class = class_names[predicted_index]
        confidence = prediction[0][predicted_index]

        # Display
        st.image(img, caption="Uploaded Fish Image", use_column_width=True)
        st.markdown(f"### 🎯 Prediction: **{predicted_class}** ({confidence * 100:.2f}%)")
        st.markdown(custom_messages[predicted_class])

        # Probabilities
        st.subheader("πŸ“Š Prediction Confidence:")
        for i, label in enumerate(class_names):
            st.write(f"- {label}: {prediction[0][i] * 100:.2f}%")

    except Exception as e:
        st.error(f"❌ Error processing image: {e}")