1Codephoenix commited on
Commit
e991448
Β·
verified Β·
1 Parent(s): 0892f1e

Update streamlit_app.py

Browse files
Files changed (1) hide show
  1. 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
- # Class labels and custom messages
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
- "- Characteristics: Bright eyes, red gills, firm flesh"
26
  ),
27
  'Moderately Fresh': (
28
- "⚠️ **Moderately Fresh**\n"
29
- "- Age: 2–3 days\n"
30
- "- Characteristics: Slightly dull, odor begins, flesh softens"
31
  ),
32
  'Spoiled': (
33
- "🚫 **Spoiled Fish**\n"
34
- "- Age: 4+ days or treated\n"
35
- "- Characteristics: Cloudy eyes, odor, unsafe to eat"
36
  )
37
  }
38
 
39
  # Streamlit UI
40
- st.title("🐟 Fish Freshness Classifier")
41
- st.subheader("Upload an image of a fish to predict its freshness level")
 
42
 
43
- uploaded_file = st.file_uploader("Upload Fish Image", type=["jpg", "jpeg", "png"])
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)) # Match model input
50
  img_array = image.img_to_array(img_resized)
51
- img_array = np.expand_dims(img_array / 255.0, axis=0) # Normalize
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
- # Show image and result
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("πŸ“Š Confidence Scores")
66
- for i, class_name in enumerate(class_names):
67
- st.write(f"- {class_name}: {prediction[0][i]*100:.2f}%")
68
 
69
  except Exception as e:
70
- st.error(f"⚠️ Error processing image: {e}")
 
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}")