Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +61 -34
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,67 @@
|
|
| 1 |
-
import altair as alt
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
"""
|
| 7 |
-
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
|
| 11 |
-
forums](https://discuss.streamlit.io).
|
| 12 |
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
"""
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
st.
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import numpy as np
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import tensorflow as tf
|
| 5 |
+
from tensorflow.keras.models import load_model
|
| 6 |
|
| 7 |
+
st.set_page_config(page_title="Rice Classification", page_icon="🍚", layout="centered")
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
model_path = "rice_efficientnet_feature_extractor.keras"
|
| 11 |
|
| 12 |
+
CLASS_NAMES = ["Arborio", "Basmati", "Ipsala", "Jasmine", "Karacadag"]
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
@st.cache_resource
|
| 15 |
+
def load_cached_model():
|
| 16 |
+
return load_model(model_path)
|
| 17 |
+
|
| 18 |
+
model = load_cached_model()
|
| 19 |
+
|
| 20 |
+
"""@st.cache_resource
|
| 21 |
+
def load_model():
|
| 22 |
+
model = tf.keras.models.load_model(MODEL_PATH)
|
| 23 |
+
return model"""
|
| 24 |
+
|
| 25 |
+
def preprocess(File):
|
| 26 |
+
img = Image.open(File).convert("RGB")
|
| 27 |
+
img = img.resize((224,224))
|
| 28 |
+
x = np.array(img)
|
| 29 |
+
x = np.expand_dims(x,axis=0)
|
| 30 |
+
return img, x
|
| 31 |
+
|
| 32 |
+
"""def topk(prob, k=3):
|
| 33 |
+
idx = np.argsort(prob)[::-1][:k]
|
| 34 |
+
return idx, prob[idx]
|
| 35 |
"""
|
| 36 |
|
| 37 |
+
st.title("🍚 Rice Classification (Transfer Learning)")
|
| 38 |
+
st.write("Upload an image and get the predicted rice type.")
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
"""try:
|
| 42 |
+
model = load_model()
|
| 43 |
+
except Exception as e:
|
| 44 |
+
st.error(f"Model load failed. Check MODEL_PATH.\n\nError: {e}")
|
| 45 |
+
st.stop()"""
|
| 46 |
+
|
| 47 |
+
file = st.file_uploader("Upload an image", type=["jpg", "jpeg"])
|
| 48 |
+
|
| 49 |
+
if file:
|
| 50 |
+
pil_img, x = preprocess(file)
|
| 51 |
+
preds = model.predict(x, verbose=0)
|
| 52 |
+
preds = np.array(preds)
|
| 53 |
+
|
| 54 |
+
st.image(pil_img, caption="Uploaded image", use_container_width=True)
|
| 55 |
+
|
| 56 |
+
prob = preds[0]
|
| 57 |
+
|
| 58 |
+
best_idx = np.argmax(prob)
|
| 59 |
+
best_label = CLASS_NAMES[best_idx]
|
| 60 |
+
best_conf = prob[best_idx]
|
| 61 |
+
|
| 62 |
+
st.subheader("Prediction")
|
| 63 |
+
st.success(f"{best_label} | confidence: {best_conf}")
|
| 64 |
+
else:
|
| 65 |
+
st.caption("No image uploaded yet.")
|
| 66 |
+
|
| 67 |
+
|