Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +85 -38
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,87 @@
|
|
| 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 |
+
from utils import predict, get_class_probabilities
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import os
|
| 5 |
+
import json
|
| 6 |
+
import numpy as np
|
| 7 |
+
import matplotlib.pyplot as plt
|
| 8 |
+
import seaborn as sns
|
| 9 |
+
|
| 10 |
+
st.set_page_config(page_title="Rose Disease Detection", layout="centered")
|
| 11 |
+
st.title("🌹 Rose Disease Detection")
|
| 12 |
+
st.write("Upload a rose leaf image to detect diseases")
|
| 13 |
+
|
| 14 |
+
# Add description of possible classes
|
| 15 |
+
st.markdown("""
|
| 16 |
+
### Possible Classifications:
|
| 17 |
+
- **Healthy Leaf Rose**: Healthy rose leaves
|
| 18 |
+
- **Rose Rust**: Rose leaves affected by rust disease
|
| 19 |
+
- **Rose Sawfly/Rose Slug**: Rose leaves affected by sawfly or slug damage
|
| 20 |
+
""")
|
| 21 |
+
|
| 22 |
+
uploaded_file = st.file_uploader("Upload a Rose Leaf Image", type=["jpg", "png", "jpeg"])
|
| 23 |
+
|
| 24 |
+
if uploaded_file is not None:
|
| 25 |
+
image = Image.open(uploaded_file)
|
| 26 |
+
st.image(image, caption="Uploaded Image", use_container_width=True)
|
| 27 |
+
|
| 28 |
+
if st.button("Detect Disease"):
|
| 29 |
+
model_path = "models/rose_model.h5"
|
| 30 |
+
try:
|
| 31 |
+
label, confidence = predict(model_path, uploaded_file)
|
| 32 |
+
|
| 33 |
+
# Customize the output based on the prediction
|
| 34 |
+
if "Healthy" in label:
|
| 35 |
+
st.success(f"**Prediction**: {label} ({confidence*100:.2f}% confidence)")
|
| 36 |
+
else:
|
| 37 |
+
st.warning(f"**Prediction**: {label} ({confidence*100:.2f}% confidence)")
|
| 38 |
+
st.info("⚠️ This leaf appears to be affected by a disease. Please take appropriate measures.")
|
| 39 |
+
|
| 40 |
+
# Display probability distribution
|
| 41 |
+
st.subheader("Probability Distribution")
|
| 42 |
+
probabilities = get_class_probabilities(model_path, uploaded_file)
|
| 43 |
+
|
| 44 |
+
# Create a bar chart of probabilities
|
| 45 |
+
plt.figure(figsize=(10, 6))
|
| 46 |
+
classes = list(probabilities.keys())
|
| 47 |
+
probs = list(probabilities.values())
|
| 48 |
+
plt.bar(classes, probs)
|
| 49 |
+
plt.xticks(rotation=45, ha='right')
|
| 50 |
+
plt.ylabel('Probability')
|
| 51 |
+
plt.title('Class Probabilities')
|
| 52 |
+
plt.tight_layout()
|
| 53 |
+
st.pyplot(plt)
|
| 54 |
+
|
| 55 |
+
except Exception as e:
|
| 56 |
+
st.error(f"Error during prediction: {str(e)}")
|
| 57 |
+
st.info("Please make sure the model is trained and available in the models directory.")
|
| 58 |
+
|
| 59 |
+
# Display metrics if available
|
| 60 |
+
metrics_path = f"models/{model_option.lower()}_metrics.json"
|
| 61 |
+
if os.path.exists(metrics_path):
|
| 62 |
+
with open(metrics_path, "r") as f:
|
| 63 |
+
metrics = json.load(f)
|
| 64 |
+
st.subheader("Model Performance Metrics")
|
| 65 |
+
col1, col2, col3 = st.columns(3)
|
| 66 |
+
with col1:
|
| 67 |
+
st.metric("Accuracy", f"{metrics['accuracy']:.2%}")
|
| 68 |
+
with col2:
|
| 69 |
+
st.metric("Precision", f"{metrics['precision']:.2%}")
|
| 70 |
+
with col3:
|
| 71 |
+
st.metric("Recall", f"{metrics['recall']:.2%}")
|
| 72 |
+
|
| 73 |
+
# Display confusion matrix if available
|
| 74 |
+
cm_path = f"models/{model_option.lower()}_confusion_matrix.json"
|
| 75 |
+
if os.path.exists(cm_path):
|
| 76 |
+
with open(cm_path, "r") as f:
|
| 77 |
+
cm = np.array(json.load(f))
|
| 78 |
+
plt.figure(figsize=(10, 8))
|
| 79 |
+
sns.heatmap(cm, annot=True, fmt='d', cmap='Blues')
|
| 80 |
+
plt.title('Confusion Matrix')
|
| 81 |
+
plt.ylabel('True Label')
|
| 82 |
+
plt.xlabel('Predicted Label')
|
| 83 |
+
st.pyplot(plt)
|
| 84 |
+
|
| 85 |
+
with open("models/class_names.json", "r") as f:
|
| 86 |
+
class_names = json.load(f)
|
| 87 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|