leaf_disease_ai / src /app.py
BlasterBlade65's picture
Update src/app.py
b707f2a verified
import streamlit as st
import tensorflow as tf
from PIL import Image
import numpy as np
import os
import json
st.set_page_config(page_title="Plant Disease Expert System", layout="wide", page_icon="๐ŸŒฟ")
# PATH DEFINISI - Sesuaikan dengan folder src/model di repo Anda
BASE_MODEL_DIR = os.path.join("src", "model")
PATH_JSON = os.path.join(BASE_MODEL_DIR, "knowledge_base.json")
MODEL_PATHS = {
"MobileNetV2 (Best Fine-Tuned)": "model_skripsi_mobilenet_final_FT.h5",
"InceptionV3 (Fine-Tuned)": "model_skripsi_inception_finetuned.h5",
"Deep Custom CNN": "model_skripsi_cnn_deep.h5",
"MobileNetV2 (Standard)": "model_skripsi_mobilenet.h5",
"InceptionV3 (Standard)": "model_skripsi_inception.h5"
}
# Fungsi memuat model secara dinamis (Hanya 1 model di RAM)
@st.cache_resource
def load_selected_model(model_key):
try:
path = os.path.join(BASE_MODEL_DIR, MODEL_PATHS[model_key])
if os.path.exists(path):
return tf.keras.models.load_model(path)
st.error(f"File model tidak ditemukan: {path}")
return None
except Exception as e:
st.error(f"Gagal memuat model: {e}")
return None
@st.cache_data
def load_labels(path):
if os.path.exists(path):
with open(path, 'r') as f:
return json.load(f)
return {}
# UI Utama
st.sidebar.header("โš™๏ธ Kontrol Sistem")
model_choice = st.sidebar.selectbox("Pilih Arsitektur Model:", list(MODEL_PATHS.keys()))
KB_DATA = load_labels(PATH_JSON)
model = load_selected_model(model_choice)
if model and KB_DATA:
st.title("๐ŸŒฟ Identifikasi Penyakit Tanaman")
uploaded_file = st.file_uploader("Unggah foto daun...", type=["jpg", "jpeg", "png"])
if uploaded_file:
img = Image.open(uploaded_file).convert("RGB")
st.image(img, caption="Preview Foto", width=400)
if st.button("๐Ÿš€ Jalankan Diagnosa"):
# Preprocessing otomatis sesuai model
h, w = model.input_shape[1], model.input_shape[2]
img_res = img.resize((h, w))
img_arr = np.expand_dims(np.array(img_res) / 255.0, axis=0)
preds = model.predict(img_arr)
idx = np.argmax(preds)
conf = np.max(preds) * 100
res = KB_DATA.get(str(idx))
if res:
st.success(f"### {res['jenis']} ({conf:.2f}%)")
st.write(f"**Saran:** {res.get('deskripsi', 'N/A')}")