Update app.py
Browse files
app.py
CHANGED
@@ -15,7 +15,28 @@ def calculate_irrigation_runtime(soil_moisture):
|
|
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)"
|
@@ -30,6 +51,7 @@ def irrigation_decision(soil_moisture, rainfall_prediction):
|
|
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 |
|
@@ -43,7 +65,7 @@ def simulate_values_and_runtime():
|
|
43 |
# Display simulated data and initial decision
|
44 |
yield "", f"{simulated_moisture:.2f}", rainfall_prediction, decision, ""
|
45 |
|
46 |
-
# Simulate irrigation runtime
|
47 |
if "Irrigation ON" in decision:
|
48 |
runtime = calculate_irrigation_runtime(simulated_moisture)
|
49 |
if runtime > 0:
|
@@ -57,10 +79,6 @@ def simulate_values_and_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 π±π§")
|
@@ -79,24 +97,29 @@ with gr.Blocks(title="Smart Irrigation System π±π§") as demo:
|
|
79 |
value=30
|
80 |
)
|
81 |
rainfall_prediction_input = gr.Radio(
|
82 |
-
["Yes", "No"], label="π§οΈ Rainfall Prediction
|
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 |
-
|
93 |
-
|
94 |
-
with gr.Row():
|
95 |
-
submit_button = gr.Button("Submit π")
|
96 |
|
97 |
# Function connections
|
98 |
submit_button.click(
|
99 |
-
|
|
|
|
|
100 |
)
|
101 |
|
102 |
simulate_button.click(
|
@@ -107,7 +130,7 @@ with gr.Blocks(title="Smart Irrigation System π±π§") as demo:
|
|
107 |
simulate_output_moisture,
|
108 |
simulate_output_rainfall,
|
109 |
simulate_decision_output,
|
110 |
-
|
111 |
]
|
112 |
)
|
113 |
|
|
|
15 |
else:
|
16 |
return 0 # No irrigation needed
|
17 |
|
18 |
+
def irrigation_decision_with_runtime(soil_moisture, rainfall_prediction):
|
19 |
+
"""Make irrigation decision and simulate runtime."""
|
20 |
+
# Initial irrigation decision
|
21 |
+
decision = irrigation_decision(soil_moisture, rainfall_prediction)
|
22 |
+
yield decision, ""
|
23 |
+
|
24 |
+
# Simulate irrigation runtime if irrigation is ON
|
25 |
+
if "Irrigation ON" in decision:
|
26 |
+
runtime = calculate_irrigation_runtime(soil_moisture)
|
27 |
+
if runtime > 0:
|
28 |
+
for minute in range(1, runtime + 1):
|
29 |
+
time.sleep(1) # Simulate a minute as 1 second for demonstration
|
30 |
+
status = f"Irrigation running... β³ ({minute}/{runtime} minutes)"
|
31 |
+
yield decision, status
|
32 |
+
|
33 |
+
# Turn off irrigation after the duration
|
34 |
+
yield decision, "Irrigation OFF β (Completed runtime)"
|
35 |
+
else:
|
36 |
+
yield decision, "Irrigation remains OFF β"
|
37 |
+
|
38 |
def irrigation_decision(soil_moisture, rainfall_prediction):
|
39 |
+
"""Decide whether to turn irrigation ON or OFF."""
|
40 |
if soil_moisture < SECONDARY_THRESHOLD:
|
41 |
if rainfall_prediction == "Yes":
|
42 |
return "Irrigation ON π§οΈπΏ (Critical soil moisture)"
|
|
|
51 |
return "Irrigation OFF β (Soil moisture sufficient)"
|
52 |
|
53 |
def simulate_values_and_runtime():
|
54 |
+
"""Simulate IoT data collection and runtime."""
|
55 |
# Show loading message for data collection
|
56 |
yield "Collecting data from IoT device... ππ‘", "", "", "", ""
|
57 |
|
|
|
65 |
# Display simulated data and initial decision
|
66 |
yield "", f"{simulated_moisture:.2f}", rainfall_prediction, decision, ""
|
67 |
|
68 |
+
# Simulate irrigation runtime
|
69 |
if "Irrigation ON" in decision:
|
70 |
runtime = calculate_irrigation_runtime(simulated_moisture)
|
71 |
if runtime > 0:
|
|
|
79 |
else:
|
80 |
yield "", f"{simulated_moisture:.2f}", rainfall_prediction, decision, "Irrigation remains OFF β"
|
81 |
|
|
|
|
|
|
|
|
|
82 |
# Gradio Interface
|
83 |
with gr.Blocks(title="Smart Irrigation System π±π§") as demo:
|
84 |
gr.Markdown("# Smart Irrigation System π±π§")
|
|
|
97 |
value=30
|
98 |
)
|
99 |
rainfall_prediction_input = gr.Radio(
|
100 |
+
["Yes", "No"], label="π§οΈ Rainfall Prediction in the Next 24 Hour", value="No"
|
101 |
)
|
102 |
|
103 |
decision_output = gr.Textbox(label="π‘ Irrigation Decision")
|
104 |
+
runtime_status_output = gr.Textbox(label="β³ Irrigation Runtime Status")
|
105 |
+
|
106 |
+
with gr.Row():
|
107 |
+
submit_button = gr.Button("Submit & Simulate Runtime π")
|
108 |
+
|
109 |
+
with gr.Row():
|
110 |
+
simulate_button = gr.Button("Simulate IoT Data & Runtime πβ³")
|
111 |
+
collecting_data_output = gr.Textbox(label="Status")
|
112 |
|
|
|
|
|
113 |
simulate_output_moisture = gr.Textbox(label="Simulated Soil Moisture (%)")
|
114 |
simulate_output_rainfall = gr.Textbox(label="Simulated Rainfall Prediction")
|
115 |
simulate_decision_output = gr.Textbox(label="Simulated Irrigation Decision")
|
116 |
+
simulate_runtime_output = gr.Textbox(label="Irrigation Runtime Status")
|
|
|
|
|
|
|
117 |
|
118 |
# Function connections
|
119 |
submit_button.click(
|
120 |
+
irrigation_decision_with_runtime,
|
121 |
+
inputs=[soil_moisture_input, rainfall_prediction_input],
|
122 |
+
outputs=[decision_output, runtime_status_output]
|
123 |
)
|
124 |
|
125 |
simulate_button.click(
|
|
|
130 |
simulate_output_moisture,
|
131 |
simulate_output_rainfall,
|
132 |
simulate_decision_output,
|
133 |
+
simulate_runtime_output
|
134 |
]
|
135 |
)
|
136 |
|