expert_cotton / app.py
7jimmy's picture
Create app.py
7b8633c verified
import os
import requests
os.environ['TF_ENABLE_ONEDNN_OPTS'] = '0'
import tensorflow as tf
import numpy as np
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing import image
import gradio as gr
MODEL_PATH = 'desnet2.h5'
model = load_model(MODEL_PATH)
classes = ['American Bollworm on Cotton', 'Anthracnose on Cotton', 'Aphids', 'Army worm', 'Carpetweeds', 'Crabgrass', 'Eclipta',
'Flag Smut', 'Goosegrass', 'Healthy', 'Leaf Curl', 'Leaf smut', 'Morningglory', 'Mosaic sugarcane', 'Nutsedge', 'PalmerAmaranth',
'Powdery_Mildew', 'Prickly Sida', 'Purslane', 'Ragweed', 'RedRot sugarcane', 'RedRust sugarcane', 'Rice Blast', 'Sicklepod',
'SpottedSpurge', 'SpurredAnoda', 'Sugarcane Healthy', 'Swinecress', 'Target_spot', 'Tungro', 'Waterhemp', 'Wheat Brown leaf Rust',
'Wheat Stem fly', 'Wheat aphid', 'Wheat black rust', 'Wheat leaf blight', 'Wheat mite', 'Wheat powdery mildew', 'Wheat scab',
'Wheat___Yellow_Rust', 'Wilt', 'Yellow Rust Sugarcane', 'bacterial_blight in Cotton', 'bollrot on Cotton', 'bollworm on Cotton',
'cotton mealy bug', 'cotton whitefly', 'curl_virus', 'fussarium_wilt', 'maize ear rot', 'maize fall armyworm', 'maize stem borer',
'pink bollworm in cotton', 'red cotton bug', 'thirps on cotton']
def predict_disease(img):
img = img.resize((256, 256))
img = image.img_to_array(img)
img = img / 255
img = np.expand_dims(img, axis=0)
result = model.predict(img)
result = result.ravel()
max_index = np.argmax(result)
pred = str(classes[max_index])
return pred
def weather_forecasting(city):
api_key = "74fbb60fa9b326bf46b41f8db923ccaa"
url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}&units=metric"
response = requests.get(url)
data = response.json()
if data["cod"] != "404":
weather_data = {
"city": city,
"temperature": data["main"]["temp"],
"pressure": data["main"]["pressure"],
"humidity": data["main"]["humidity"],
"description": data["weather"][0]["description"],
}
return weather_data
else:
return "City not found."
def calculate_fertilizer(area, nitrogen, phosphorus, potassium):
total_nitrogen = area * nitrogen
total_phosphorus = area * phosphorus
total_potassium = area * potassium
return total_nitrogen, total_phosphorus, total_potassium
def fertilizer_advice(nitrogen, phosphorus, potassium):
user_input = f'Provide advice on using Nitrogen: {nitrogen} kg, Phosphorus: {phosphorus} kg, Potassium: {potassium} kg for cotton fields.'
# Mock response for the demonstration
return f"Advice based on Nitrogen: {nitrogen}, Phosphorus: {phosphorus}, Potassium: {potassium}"
with gr.Blocks() as demo:
gr.Markdown("# Cotton Disease Detection and Advisory System")
with gr.Tab("Disease Detection"):
gr.Markdown("## Upload an Image of the Cotton Plant")
img_input = gr.Image(type="pil")
disease_output = gr.Textbox(label="Predicted Disease")
gr.Button("Predict").click(predict_disease, inputs=img_input, outputs=disease_output)
with gr.Tab("Weather Forecasting"):
gr.Markdown("## Get Weather Data for Your City")
city_input = gr.Textbox(label="City Name")
weather_output = gr.JSON(label="Weather Data")
gr.Button("Get Weather").click(weather_forecasting, inputs=city_input, outputs=weather_output)
with gr.Tab("Fertilizer Calculator"):
gr.Markdown("## Calculate Fertilizer Needs")
area_input = gr.Number(label="Area (hectares)")
nitrogen_input = gr.Number(label="Nitrogen (kg/ha)")
phosphorus_input = gr.Number(label="Phosphorus (kg/ha)")
potassium_input = gr.Number(label="Potassium (kg/ha)")
fertilizer_output = gr.JSON(label="Total Fertilizer Needs")
gr.Button("Calculate").click(calculate_fertilizer,
inputs=[area_input, nitrogen_input, phosphorus_input, potassium_input],
outputs=fertilizer_output)
with gr.Tab("Fertilizer Advice"):
gr.Markdown("## Get Fertilizer Advice")
nitrogen_advice_input = gr.Number(label="Nitrogen")
phosphorus_advice_input = gr.Number(label="Phosphorus")
potassium_advice_input = gr.Number(label="Potassium")
advice_output = gr.Textbox(label="Advice")
gr.Button("Get Advice").click(fertilizer_advice,
inputs=[nitrogen_advice_input, phosphorus_advice_input, potassium_advice_input],
outputs=advice_output)
demo.launch(debug=True)