Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +183 -184
src/streamlit_app.py
CHANGED
|
@@ -1,185 +1,184 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
|
| 3 |
-
import
|
| 4 |
-
|
| 5 |
-
import
|
| 6 |
-
import
|
| 7 |
-
import
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
#
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
#
|
| 16 |
-
#
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
#
|
| 25 |
-
#
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
#
|
| 33 |
-
#
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
-
|
| 41 |
-
-
|
| 42 |
-
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
"
|
| 51 |
-
"
|
| 52 |
-
"
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
"
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
"
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
#
|
| 77 |
-
#
|
| 78 |
-
|
| 79 |
-
st.
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
#
|
| 97 |
-
#
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
img_cv =
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
0
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
img_display =
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
st.
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
st.
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
#
|
| 177 |
-
#
|
| 178 |
-
|
| 179 |
-
st.markdown(
|
| 180 |
-
|
| 181 |
-
"
|
| 182 |
-
"
|
| 183 |
-
|
| 184 |
-
unsafe_allow_html=True
|
| 185 |
)
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import tempfile
|
| 5 |
+
import os
|
| 6 |
+
import cv2
|
| 7 |
+
import numpy as np
|
| 8 |
+
|
| 9 |
+
# -----------------------------
|
| 10 |
+
# PAGE CONFIG
|
| 11 |
+
# -----------------------------
|
| 12 |
+
st.set_page_config(page_title="Cassava Disease Detection", layout="centered")
|
| 13 |
+
|
| 14 |
+
# -----------------------------
|
| 15 |
+
# CONFIGURATION
|
| 16 |
+
# -----------------------------
|
| 17 |
+
ROBOFLOW_API_KEY = st.secrets["ROBOFLOW_API_KEY"]
|
| 18 |
+
OPENROUTER_API_KEY = st.secrets["OPENROUTER_API_KEY"]
|
| 19 |
+
|
| 20 |
+
MODEL_ID = "cassavadisease/1"
|
| 21 |
+
ROBOFLOW_API_URL = "https://serverless.roboflow.com"
|
| 22 |
+
|
| 23 |
+
# -----------------------------
|
| 24 |
+
# INITIALIZE ROBOFLOW CLIENT
|
| 25 |
+
# -----------------------------
|
| 26 |
+
CLIENT = InferenceHTTPClient(
|
| 27 |
+
api_url=ROBOFLOW_API_URL,
|
| 28 |
+
api_key=ROBOFLOW_API_KEY
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
# -----------------------------
|
| 32 |
+
# FUNCTION: AI EXPLANATION
|
| 33 |
+
# -----------------------------
|
| 34 |
+
def get_ai_explanation(disease_name):
|
| 35 |
+
|
| 36 |
+
prompt = f"""
|
| 37 |
+
Explain briefly the cassava disease: {disease_name}.
|
| 38 |
+
Include:
|
| 39 |
+
- Cause
|
| 40 |
+
- Main Symptoms
|
| 41 |
+
- Prevention
|
| 42 |
+
- Treatment
|
| 43 |
+
Keep answer short.
|
| 44 |
+
"""
|
| 45 |
+
|
| 46 |
+
response = requests.post(
|
| 47 |
+
"https://openrouter.ai/api/v1/chat/completions",
|
| 48 |
+
headers={
|
| 49 |
+
"Authorization": f"Bearer {OPENROUTER_API_KEY}",
|
| 50 |
+
"Content-Type": "application/json",
|
| 51 |
+
"HTTP-Referer": "http://localhost:8501",
|
| 52 |
+
"X-Title": "Cassava Disease Detection App"
|
| 53 |
+
},
|
| 54 |
+
json={
|
| 55 |
+
"model": "minimax/minimax-m2.5",
|
| 56 |
+
"messages": [
|
| 57 |
+
{"role": "user", "content": prompt}
|
| 58 |
+
],
|
| 59 |
+
"max_tokens": 800,
|
| 60 |
+
"temperature": 0.3
|
| 61 |
+
}
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
if response.status_code != 200:
|
| 65 |
+
return f"OpenRouter API Error:\n{response.text}"
|
| 66 |
+
|
| 67 |
+
result = response.json()
|
| 68 |
+
|
| 69 |
+
if "choices" not in result:
|
| 70 |
+
return f"Unexpected API Response:\n{result}"
|
| 71 |
+
|
| 72 |
+
return result["choices"][0]["message"]["content"]
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
# -----------------------------
|
| 76 |
+
# UI
|
| 77 |
+
# -----------------------------
|
| 78 |
+
st.title("Cassava Disease Detection Web App")
|
| 79 |
+
st.write("Upload or capture a cassava leaf image for disease detection.")
|
| 80 |
+
|
| 81 |
+
source = st.radio("Select Image Source:", ["Upload Image", "Use Camera"])
|
| 82 |
+
|
| 83 |
+
image = None
|
| 84 |
+
|
| 85 |
+
if source == "Upload Image":
|
| 86 |
+
uploaded_file = st.file_uploader("Upload Image", type=["jpg", "jpeg", "png"])
|
| 87 |
+
if uploaded_file:
|
| 88 |
+
image = Image.open(uploaded_file)
|
| 89 |
+
|
| 90 |
+
elif source == "Use Camera":
|
| 91 |
+
camera_photo = st.camera_input("Take a picture of the cassava leaf")
|
| 92 |
+
if camera_photo:
|
| 93 |
+
image = Image.open(camera_photo)
|
| 94 |
+
|
| 95 |
+
# -----------------------------
|
| 96 |
+
# MAIN PROCESS
|
| 97 |
+
# -----------------------------
|
| 98 |
+
if image is not None:
|
| 99 |
+
|
| 100 |
+
st.image(image, caption="Captured Image", use_container_width=True)
|
| 101 |
+
|
| 102 |
+
# Save temp image
|
| 103 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as tmp:
|
| 104 |
+
image.save(tmp.name)
|
| 105 |
+
temp_path = tmp.name
|
| 106 |
+
|
| 107 |
+
# Roboflow inference
|
| 108 |
+
with st.spinner("Analyzing image..."):
|
| 109 |
+
result = CLIENT.infer(temp_path, model_id=MODEL_ID)
|
| 110 |
+
|
| 111 |
+
os.remove(temp_path)
|
| 112 |
+
|
| 113 |
+
predictions = result.get("predictions", [])
|
| 114 |
+
|
| 115 |
+
if predictions:
|
| 116 |
+
|
| 117 |
+
img_cv = np.array(image)
|
| 118 |
+
img_cv = cv2.cvtColor(img_cv, cv2.COLOR_RGB2BGR)
|
| 119 |
+
|
| 120 |
+
for pred in predictions:
|
| 121 |
+
|
| 122 |
+
x = pred["x"]
|
| 123 |
+
y = pred["y"]
|
| 124 |
+
w = pred["width"]
|
| 125 |
+
h = pred["height"]
|
| 126 |
+
label = pred["class"]
|
| 127 |
+
confidence = round(pred["confidence"] * 100, 2)
|
| 128 |
+
|
| 129 |
+
# Convert center to corner format
|
| 130 |
+
x1 = int(x - w / 2)
|
| 131 |
+
y1 = int(y - h / 2)
|
| 132 |
+
x2 = int(x + w / 2)
|
| 133 |
+
y2 = int(y + h / 2)
|
| 134 |
+
|
| 135 |
+
# Draw bounding box
|
| 136 |
+
cv2.rectangle(img_cv, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
| 137 |
+
|
| 138 |
+
# Label background
|
| 139 |
+
cv2.rectangle(img_cv, (x1, y1 - 30), (x1 + 250, y1), (0, 255, 0), -1)
|
| 140 |
+
|
| 141 |
+
# Label text
|
| 142 |
+
cv2.putText(
|
| 143 |
+
img_cv,
|
| 144 |
+
f"{label} ({confidence}%)",
|
| 145 |
+
(x1 + 5, y1 - 10),
|
| 146 |
+
cv2.FONT_HERSHEY_SIMPLEX,
|
| 147 |
+
0.6,
|
| 148 |
+
(0, 0, 0),
|
| 149 |
+
2
|
| 150 |
+
)
|
| 151 |
+
|
| 152 |
+
# Convert back to RGB
|
| 153 |
+
img_display = cv2.cvtColor(img_cv, cv2.COLOR_BGR2RGB)
|
| 154 |
+
st.image(img_display, caption="Detected & Labeled Image", use_container_width=True)
|
| 155 |
+
|
| 156 |
+
# Get highest confidence prediction
|
| 157 |
+
top_prediction = max(predictions, key=lambda x: x["confidence"])
|
| 158 |
+
disease_name = top_prediction["class"]
|
| 159 |
+
confidence = round(top_prediction["confidence"] * 100, 2)
|
| 160 |
+
|
| 161 |
+
st.success(f"Detected: **{disease_name}**")
|
| 162 |
+
st.info(f"Confidence: {confidence}%")
|
| 163 |
+
|
| 164 |
+
# AI Explanation
|
| 165 |
+
with st.spinner("Generating disease explanation..."):
|
| 166 |
+
explanation = get_ai_explanation(disease_name)
|
| 167 |
+
|
| 168 |
+
st.markdown("## 📘 Disease Information")
|
| 169 |
+
st.write(explanation)
|
| 170 |
+
|
| 171 |
+
else:
|
| 172 |
+
st.warning("No cassava leaf detected.")
|
| 173 |
+
|
| 174 |
+
|
| 175 |
+
# -----------------------------
|
| 176 |
+
# FOOTER
|
| 177 |
+
# -----------------------------
|
| 178 |
+
st.markdown("---")
|
| 179 |
+
st.markdown(
|
| 180 |
+
"<div style='text-align: center; font-size: 14px;'>"
|
| 181 |
+
"Developed by <b>Edcel Bogay</b>"
|
| 182 |
+
"</div>",
|
| 183 |
+
unsafe_allow_html=True
|
|
|
|
| 184 |
)
|