Abrar20 commited on
Commit
83616b0
Β·
verified Β·
1 Parent(s): e2470a5

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +115 -0
app.py ADDED
@@ -0,0 +1,115 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import random
3
+ import time
4
+
5
+ # thresholds for soil moisture (in %)
6
+ PRIMARY_THRESHOLD = 30 # Below this, irrigation is necessary
7
+ SECONDARY_THRESHOLD = 20 # Below this, irrigation is critical
8
+
9
+ def calculate_irrigation_runtime(soil_moisture):
10
+ """Calculate irrigation runtime based on soil moisture."""
11
+ if soil_moisture < SECONDARY_THRESHOLD:
12
+ return 10 # Critical moisture, longer irrigation
13
+ elif soil_moisture < PRIMARY_THRESHOLD:
14
+ return 7 # Moderate moisture, shorter irrigation
15
+ else:
16
+ return 0 # No irrigation needed
17
+
18
+ def irrigation_decision(soil_moisture, rainfall_prediction):
19
+ if soil_moisture < SECONDARY_THRESHOLD:
20
+ if rainfall_prediction == "Yes":
21
+ return "Irrigation ON 🌧️🚿 (Critical soil moisture)"
22
+ else:
23
+ return "Irrigation ON 🚿 (Critical soil moisture)"
24
+ elif soil_moisture < PRIMARY_THRESHOLD:
25
+ if rainfall_prediction == "Yes":
26
+ return "Irrigation OFF β›” (Rainfall expected, conserving water)"
27
+ else:
28
+ return "Irrigation ON 🚿 (Low soil moisture)"
29
+ else:
30
+ return "Irrigation OFF β›” (Soil moisture sufficient)"
31
+
32
+ def simulate_values_and_runtime():
33
+ # Show loading message for data collection
34
+ yield "Collecting data from IoT device... πŸŒπŸ“‘", "", "", "", ""
35
+
36
+ time.sleep(2) # Simulate delay for fetching data
37
+
38
+ # Simulate IoT data
39
+ simulated_moisture = random.uniform(10, 50)
40
+ rainfall_prediction = random.choice(["Yes", "No"])
41
+ decision = irrigation_decision(simulated_moisture, rainfall_prediction)
42
+
43
+ # Display simulated data and initial decision
44
+ yield "", f"{simulated_moisture:.2f}", rainfall_prediction, decision, ""
45
+
46
+ # Simulate irrigation runtime if irrigation is ON
47
+ if "Irrigation ON" in decision:
48
+ runtime = calculate_irrigation_runtime(simulated_moisture)
49
+ if runtime > 0:
50
+ for minute in range(1, runtime + 1):
51
+ time.sleep(1) # Simulate a minute as 1 second for demonstration
52
+ status = f"Irrigation running... ⏳ ({minute}/{runtime} minutes)"
53
+ yield "", f"{simulated_moisture:.2f}", rainfall_prediction, decision, status
54
+
55
+ # Turn off irrigation after the duration
56
+ yield "", f"{simulated_moisture:.2f}", rainfall_prediction, decision, "Irrigation OFF β›” (Completed runtime)"
57
+ else:
58
+ yield "", f"{simulated_moisture:.2f}", rainfall_prediction, decision, "Irrigation remains OFF β›”"
59
+
60
+ def app(soil_moisture, rainfall_prediction):
61
+ decision = irrigation_decision(float(soil_moisture), rainfall_prediction)
62
+ return decision
63
+
64
+ # Gradio Interface
65
+ with gr.Blocks(title="Smart Irrigation System πŸŒ±πŸ’§") as demo:
66
+ gr.Markdown("# Smart Irrigation System πŸŒ±πŸ’§")
67
+ gr.Markdown(
68
+ """
69
+ This app helps farmers optimize water usage by predicting irrigation needs based on soil moisture and rainfall forecast for Dinajpur.
70
+ """
71
+ )
72
+
73
+ with gr.Row():
74
+ soil_moisture_input = gr.Slider(
75
+ minimum=0,
76
+ maximum=100,
77
+ step=1,
78
+ label="🌑️ Soil Moisture (%)",
79
+ value=30
80
+ )
81
+ rainfall_prediction_input = gr.Radio(
82
+ ["Yes", "No"], label="🌧️ Rainfall Prediction for Dinajpur", value="No"
83
+ )
84
+
85
+ decision_output = gr.Textbox(label="πŸ’‘ Irrigation Decision")
86
+
87
+ simulate_button = gr.Button("Simulate IoT Data & Runtime 🌐⏳")
88
+ collecting_data_output = gr.Textbox(label="Status")
89
+ simulate_output_moisture = gr.Textbox(label="Simulated Soil Moisture (%)")
90
+ simulate_output_rainfall = gr.Textbox(label="Simulated Rainfall Prediction")
91
+ simulate_decision_output = gr.Textbox(label="Simulated Irrigation Decision")
92
+ irrigation_runtime_output = gr.Textbox(label="Irrigation Runtime Status")
93
+
94
+ with gr.Row():
95
+ submit_button = gr.Button("Submit πŸš€")
96
+
97
+ # Function connections
98
+ submit_button.click(
99
+ app, inputs=[soil_moisture_input, rainfall_prediction_input], outputs=[decision_output]
100
+ )
101
+
102
+ simulate_button.click(
103
+ simulate_values_and_runtime,
104
+ inputs=[],
105
+ outputs=[
106
+ collecting_data_output,
107
+ simulate_output_moisture,
108
+ simulate_output_rainfall,
109
+ simulate_decision_output,
110
+ irrigation_runtime_output
111
+ ]
112
+ )
113
+
114
+ # Launch the app
115
+ demo.launch()