Osmanerendgn commited on
Commit
f5c45fe
·
verified ·
1 Parent(s): ba3e561

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. 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
- # Welcome to Streamlit!
 
 
8
 
9
- Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
10
- If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
11
- forums](https://discuss.streamlit.io).
12
 
13
- In the meantime, below is an example of what you can do with just a few lines of code:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  """
15
 
16
- num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
17
- num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
18
-
19
- indices = np.linspace(0, 1, num_points)
20
- theta = 2 * np.pi * num_turns * indices
21
- radius = indices
22
-
23
- x = radius * np.cos(theta)
24
- y = radius * np.sin(theta)
25
-
26
- df = pd.DataFrame({
27
- "x": x,
28
- "y": y,
29
- "idx": indices,
30
- "rand": np.random.randn(num_points),
31
- })
32
-
33
- st.altair_chart(alt.Chart(df, height=700, width=700)
34
- .mark_point(filled=True)
35
- .encode(
36
- x=alt.X("x", axis=None),
37
- y=alt.Y("y", axis=None),
38
- color=alt.Color("idx", legend=None, scale=alt.Scale()),
39
- size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
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
+