AI-POWERED-FISH-PREDICTION / streamlit_app.py
1Codephoenix's picture
Update streamlit_app.py
0254525 verified
# βœ… 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}")