7jimmy commited on
Commit
7b8633c
·
verified ·
1 Parent(s): c5f6049

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +96 -0
app.py ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import requests
3
+ os.environ['TF_ENABLE_ONEDNN_OPTS'] = '0'
4
+ import tensorflow as tf
5
+ import numpy as np
6
+ from tensorflow.keras.models import load_model
7
+ from tensorflow.keras.preprocessing import image
8
+ import gradio as gr
9
+
10
+ MODEL_PATH = 'desnet2.h5'
11
+ model = load_model(MODEL_PATH)
12
+ classes = ['American Bollworm on Cotton', 'Anthracnose on Cotton', 'Aphids', 'Army worm', 'Carpetweeds', 'Crabgrass', 'Eclipta',
13
+ 'Flag Smut', 'Goosegrass', 'Healthy', 'Leaf Curl', 'Leaf smut', 'Morningglory', 'Mosaic sugarcane', 'Nutsedge', 'PalmerAmaranth',
14
+ 'Powdery_Mildew', 'Prickly Sida', 'Purslane', 'Ragweed', 'RedRot sugarcane', 'RedRust sugarcane', 'Rice Blast', 'Sicklepod',
15
+ 'SpottedSpurge', 'SpurredAnoda', 'Sugarcane Healthy', 'Swinecress', 'Target_spot', 'Tungro', 'Waterhemp', 'Wheat Brown leaf Rust',
16
+ 'Wheat Stem fly', 'Wheat aphid', 'Wheat black rust', 'Wheat leaf blight', 'Wheat mite', 'Wheat powdery mildew', 'Wheat scab',
17
+ 'Wheat___Yellow_Rust', 'Wilt', 'Yellow Rust Sugarcane', 'bacterial_blight in Cotton', 'bollrot on Cotton', 'bollworm on Cotton',
18
+ 'cotton mealy bug', 'cotton whitefly', 'curl_virus', 'fussarium_wilt', 'maize ear rot', 'maize fall armyworm', 'maize stem borer',
19
+ 'pink bollworm in cotton', 'red cotton bug', 'thirps on cotton']
20
+
21
+ def predict_disease(img):
22
+ img = img.resize((256, 256))
23
+ img = image.img_to_array(img)
24
+ img = img / 255
25
+ img = np.expand_dims(img, axis=0)
26
+ result = model.predict(img)
27
+ result = result.ravel()
28
+ max_index = np.argmax(result)
29
+ pred = str(classes[max_index])
30
+ return pred
31
+
32
+ def weather_forecasting(city):
33
+ api_key = "74fbb60fa9b326bf46b41f8db923ccaa"
34
+ url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}&units=metric"
35
+ response = requests.get(url)
36
+ data = response.json()
37
+ if data["cod"] != "404":
38
+ weather_data = {
39
+ "city": city,
40
+ "temperature": data["main"]["temp"],
41
+ "pressure": data["main"]["pressure"],
42
+ "humidity": data["main"]["humidity"],
43
+ "description": data["weather"][0]["description"],
44
+ }
45
+ return weather_data
46
+ else:
47
+ return "City not found."
48
+
49
+ def calculate_fertilizer(area, nitrogen, phosphorus, potassium):
50
+ total_nitrogen = area * nitrogen
51
+ total_phosphorus = area * phosphorus
52
+ total_potassium = area * potassium
53
+ return total_nitrogen, total_phosphorus, total_potassium
54
+
55
+ def fertilizer_advice(nitrogen, phosphorus, potassium):
56
+ user_input = f'Provide advice on using Nitrogen: {nitrogen} kg, Phosphorus: {phosphorus} kg, Potassium: {potassium} kg for cotton fields.'
57
+ # Mock response for the demonstration
58
+ return f"Advice based on Nitrogen: {nitrogen}, Phosphorus: {phosphorus}, Potassium: {potassium}"
59
+
60
+ with gr.Blocks() as demo:
61
+ gr.Markdown("# Cotton Disease Detection and Advisory System")
62
+
63
+ with gr.Tab("Disease Detection"):
64
+ gr.Markdown("## Upload an Image of the Cotton Plant")
65
+ img_input = gr.Image(type="pil")
66
+ disease_output = gr.Textbox(label="Predicted Disease")
67
+ gr.Button("Predict").click(predict_disease, inputs=img_input, outputs=disease_output)
68
+
69
+ with gr.Tab("Weather Forecasting"):
70
+ gr.Markdown("## Get Weather Data for Your City")
71
+ city_input = gr.Textbox(label="City Name")
72
+ weather_output = gr.JSON(label="Weather Data")
73
+ gr.Button("Get Weather").click(weather_forecasting, inputs=city_input, outputs=weather_output)
74
+
75
+ with gr.Tab("Fertilizer Calculator"):
76
+ gr.Markdown("## Calculate Fertilizer Needs")
77
+ area_input = gr.Number(label="Area (hectares)")
78
+ nitrogen_input = gr.Number(label="Nitrogen (kg/ha)")
79
+ phosphorus_input = gr.Number(label="Phosphorus (kg/ha)")
80
+ potassium_input = gr.Number(label="Potassium (kg/ha)")
81
+ fertilizer_output = gr.JSON(label="Total Fertilizer Needs")
82
+ gr.Button("Calculate").click(calculate_fertilizer,
83
+ inputs=[area_input, nitrogen_input, phosphorus_input, potassium_input],
84
+ outputs=fertilizer_output)
85
+
86
+ with gr.Tab("Fertilizer Advice"):
87
+ gr.Markdown("## Get Fertilizer Advice")
88
+ nitrogen_advice_input = gr.Number(label="Nitrogen")
89
+ phosphorus_advice_input = gr.Number(label="Phosphorus")
90
+ potassium_advice_input = gr.Number(label="Potassium")
91
+ advice_output = gr.Textbox(label="Advice")
92
+ gr.Button("Get Advice").click(fertilizer_advice,
93
+ inputs=[nitrogen_advice_input, phosphorus_advice_input, potassium_advice_input],
94
+ outputs=advice_output)
95
+
96
+ demo.launch(debug=True)