Spaces:
Runtime error
Runtime error
AdityaAdaki
commited on
Commit
·
f2531f3
1
Parent(s):
ed4e2ee
a1
Browse files
app.py
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import tensorflow as tf
|
| 3 |
import tensorflow_hub as hub
|
|
@@ -7,7 +11,6 @@ import requests
|
|
| 7 |
from googletrans import Translator
|
| 8 |
import asyncio
|
| 9 |
import nest_asyncio
|
| 10 |
-
import os
|
| 11 |
|
| 12 |
# Allow nested event loops in Streamlit
|
| 13 |
nest_asyncio.apply()
|
|
@@ -19,7 +22,7 @@ st.set_page_config(
|
|
| 19 |
initial_sidebar_state="expanded",
|
| 20 |
)
|
| 21 |
|
| 22 |
-
# Custom CSS styling
|
| 23 |
custom_css = """
|
| 24 |
<style>
|
| 25 |
body { background-color: #f8f9fa; }
|
|
@@ -30,6 +33,7 @@ h1, h2, h3, h4 { color: #2c3e50; font-family: 'Segoe UI', Tahoma, Geneva, Verdan
|
|
| 30 |
"""
|
| 31 |
st.markdown(custom_css, unsafe_allow_html=True)
|
| 32 |
|
|
|
|
| 33 |
pesticide_recommendations = {
|
| 34 |
'Bacterial Blight': 'Copper-based fungicides, Streptomycin',
|
| 35 |
'Red Rot': 'Fungicides containing Mancozeb or Copper',
|
|
@@ -52,12 +56,17 @@ def recommend_pesticide(predicted_class):
|
|
| 52 |
return 'No need for any pesticide, plant is healthy'
|
| 53 |
return pesticide_recommendations.get(predicted_class, "No recommendation available")
|
| 54 |
|
| 55 |
-
#
|
| 56 |
@st.cache_resource(show_spinner=False)
|
| 57 |
def load_h5_model(model_path):
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
-
# Define models dictionary
|
| 61 |
models = {
|
| 62 |
'sugarcane': load_h5_model("models/sugercane_model.h5"),
|
| 63 |
'maize': load_h5_model("models/maize_model.h5"),
|
|
@@ -66,7 +75,7 @@ models = {
|
|
| 66 |
'wheat': load_h5_model("models/wheat_model.h5"),
|
| 67 |
}
|
| 68 |
|
| 69 |
-
# Class names for each model (
|
| 70 |
class_names = {
|
| 71 |
'sugarcane': ['Bacterial Blight', 'Healthy', 'Red Rot'],
|
| 72 |
'maize': ['Blight', 'Common_Rust', 'Gray_Leaf_Spot,Healthy'],
|
|
@@ -75,6 +84,7 @@ class_names = {
|
|
| 75 |
'wheat': ['Healthy', 'septoria', 'strip_rust'],
|
| 76 |
}
|
| 77 |
|
|
|
|
| 78 |
def preprocess_image(image_file):
|
| 79 |
try:
|
| 80 |
image = Image.open(image_file).convert("RGB")
|
|
@@ -85,6 +95,7 @@ def preprocess_image(image_file):
|
|
| 85 |
st.error("Error processing image. Please upload a valid image file.")
|
| 86 |
return None
|
| 87 |
|
|
|
|
| 88 |
def classify_image(model_name, image_file):
|
| 89 |
input_image = preprocess_image(image_file)
|
| 90 |
if input_image is None:
|
|
@@ -95,6 +106,7 @@ def classify_image(model_name, image_file):
|
|
| 95 |
recommended_pesticide = recommend_pesticide(predicted_class)
|
| 96 |
return predicted_class, recommended_pesticide
|
| 97 |
|
|
|
|
| 98 |
def get_plant_info(disease, plant_type="Unknown"):
|
| 99 |
prompt = f"""
|
| 100 |
Disease Name: {disease}
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
# Force the use of legacy Keras (Keras 2 behavior) so that hub.KerasLayer is recognized properly.
|
| 3 |
+
os.environ["TF_USE_LEGACY_KERAS"] = "1"
|
| 4 |
+
|
| 5 |
import streamlit as st
|
| 6 |
import tensorflow as tf
|
| 7 |
import tensorflow_hub as hub
|
|
|
|
| 11 |
from googletrans import Translator
|
| 12 |
import asyncio
|
| 13 |
import nest_asyncio
|
|
|
|
| 14 |
|
| 15 |
# Allow nested event loops in Streamlit
|
| 16 |
nest_asyncio.apply()
|
|
|
|
| 22 |
initial_sidebar_state="expanded",
|
| 23 |
)
|
| 24 |
|
| 25 |
+
# Custom CSS for styling
|
| 26 |
custom_css = """
|
| 27 |
<style>
|
| 28 |
body { background-color: #f8f9fa; }
|
|
|
|
| 33 |
"""
|
| 34 |
st.markdown(custom_css, unsafe_allow_html=True)
|
| 35 |
|
| 36 |
+
# Dictionary mapping diseases to recommended pesticides
|
| 37 |
pesticide_recommendations = {
|
| 38 |
'Bacterial Blight': 'Copper-based fungicides, Streptomycin',
|
| 39 |
'Red Rot': 'Fungicides containing Mancozeb or Copper',
|
|
|
|
| 56 |
return 'No need for any pesticide, plant is healthy'
|
| 57 |
return pesticide_recommendations.get(predicted_class, "No recommendation available")
|
| 58 |
|
| 59 |
+
# Use st.cache_resource to load H5 models.
|
| 60 |
@st.cache_resource(show_spinner=False)
|
| 61 |
def load_h5_model(model_path):
|
| 62 |
+
# This will load your H5 Keras model (which contains hub.KerasLayer)
|
| 63 |
+
return tf.keras.models.load_model(
|
| 64 |
+
model_path,
|
| 65 |
+
custom_objects={"KerasLayer": hub.KerasLayer},
|
| 66 |
+
compile=False
|
| 67 |
+
)
|
| 68 |
|
| 69 |
+
# Define your models dictionary (update paths as needed)
|
| 70 |
models = {
|
| 71 |
'sugarcane': load_h5_model("models/sugercane_model.h5"),
|
| 72 |
'maize': load_h5_model("models/maize_model.h5"),
|
|
|
|
| 75 |
'wheat': load_h5_model("models/wheat_model.h5"),
|
| 76 |
}
|
| 77 |
|
| 78 |
+
# Class names for each model (ensure these match the order of your model outputs)
|
| 79 |
class_names = {
|
| 80 |
'sugarcane': ['Bacterial Blight', 'Healthy', 'Red Rot'],
|
| 81 |
'maize': ['Blight', 'Common_Rust', 'Gray_Leaf_Spot,Healthy'],
|
|
|
|
| 84 |
'wheat': ['Healthy', 'septoria', 'strip_rust'],
|
| 85 |
}
|
| 86 |
|
| 87 |
+
# Preprocess the uploaded image
|
| 88 |
def preprocess_image(image_file):
|
| 89 |
try:
|
| 90 |
image = Image.open(image_file).convert("RGB")
|
|
|
|
| 95 |
st.error("Error processing image. Please upload a valid image file.")
|
| 96 |
return None
|
| 97 |
|
| 98 |
+
# Classify the image using the appropriate model
|
| 99 |
def classify_image(model_name, image_file):
|
| 100 |
input_image = preprocess_image(image_file)
|
| 101 |
if input_image is None:
|
|
|
|
| 106 |
recommended_pesticide = recommend_pesticide(predicted_class)
|
| 107 |
return predicted_class, recommended_pesticide
|
| 108 |
|
| 109 |
+
# (Optional) Retrieve detailed plant information from LM Studio
|
| 110 |
def get_plant_info(disease, plant_type="Unknown"):
|
| 111 |
prompt = f"""
|
| 112 |
Disease Name: {disease}
|