|
import gradio as gr |
|
import random |
|
import time |
|
|
|
|
|
PRIMARY_THRESHOLD = 30 |
|
SECONDARY_THRESHOLD = 20 |
|
|
|
def calculate_irrigation_runtime(soil_moisture): |
|
"""Calculate irrigation runtime based on soil moisture.""" |
|
if soil_moisture < SECONDARY_THRESHOLD: |
|
return 10 |
|
elif soil_moisture < PRIMARY_THRESHOLD: |
|
return 7 |
|
else: |
|
return 0 |
|
|
|
def irrigation_decision_with_runtime(soil_moisture, rainfall_prediction): |
|
"""Make irrigation decision and simulate runtime.""" |
|
|
|
decision = irrigation_decision(soil_moisture, rainfall_prediction) |
|
yield decision, "" |
|
|
|
|
|
if "Irrigation ON" in decision: |
|
runtime = calculate_irrigation_runtime(soil_moisture) |
|
if runtime > 0: |
|
for minute in range(1, runtime + 1): |
|
time.sleep(1) |
|
status = f"Irrigation running... β³ ({minute}/{runtime} minutes)" |
|
yield decision, status |
|
|
|
|
|
yield decision, "Irrigation OFF β (Completed runtime)" |
|
else: |
|
yield decision, "Irrigation remains OFF β" |
|
|
|
def irrigation_decision(soil_moisture, rainfall_prediction): |
|
"""Decide whether to turn irrigation ON or OFF.""" |
|
if soil_moisture < SECONDARY_THRESHOLD: |
|
if rainfall_prediction == "Yes": |
|
return "Irrigation ON π§οΈπΏ (Critical soil moisture)" |
|
else: |
|
return "Irrigation ON πΏ (Critical soil moisture)" |
|
elif soil_moisture < PRIMARY_THRESHOLD: |
|
if rainfall_prediction == "Yes": |
|
return "Irrigation OFF β (Rainfall expected, conserving water)" |
|
else: |
|
return "Irrigation ON πΏ (Low soil moisture)" |
|
else: |
|
return "Irrigation OFF β (Soil moisture sufficient)" |
|
|
|
def simulate_values_and_runtime(): |
|
"""Simulate IoT data collection and runtime.""" |
|
|
|
yield "Collecting data from IoT device... ππ‘", "", "", "", "" |
|
|
|
time.sleep(2) |
|
|
|
|
|
simulated_moisture = random.uniform(10, 50) |
|
rainfall_prediction = random.choice(["Yes", "No"]) |
|
decision = irrigation_decision(simulated_moisture, rainfall_prediction) |
|
|
|
|
|
yield "", f"{simulated_moisture:.2f}", rainfall_prediction, decision, "" |
|
|
|
|
|
if "Irrigation ON" in decision: |
|
runtime = calculate_irrigation_runtime(simulated_moisture) |
|
if runtime > 0: |
|
for minute in range(1, runtime + 1): |
|
time.sleep(1) |
|
status = f"Irrigation running... β³ ({minute}/{runtime} minutes)" |
|
yield "", f"{simulated_moisture:.2f}", rainfall_prediction, decision, status |
|
|
|
|
|
yield "", f"{simulated_moisture:.2f}", rainfall_prediction, decision, "Irrigation OFF β (Completed runtime)" |
|
else: |
|
yield "", f"{simulated_moisture:.2f}", rainfall_prediction, decision, "Irrigation remains OFF β" |
|
|
|
|
|
with gr.Blocks(title="Smart Irrigation System π±π§") as demo: |
|
gr.Markdown("# Smart Irrigation System π±π§") |
|
gr.Markdown( |
|
""" |
|
This app helps farmers optimize water usage by predicting irrigation needs based on soil moisture and rainfall forecast for Dinajpur. |
|
""" |
|
) |
|
|
|
with gr.Row(): |
|
soil_moisture_input = gr.Slider( |
|
minimum=0, |
|
maximum=100, |
|
step=1, |
|
label="π‘οΈ Soil Moisture (%)", |
|
value=30 |
|
) |
|
rainfall_prediction_input = gr.Radio( |
|
["Yes", "No"], label="π§οΈ Rainfall Prediction in the Next 24 Hour", value="No" |
|
) |
|
|
|
decision_output = gr.Textbox(label="π‘ Irrigation Decision") |
|
runtime_status_output = gr.Textbox(label="β³ Irrigation Runtime Status") |
|
|
|
with gr.Row(): |
|
submit_button = gr.Button("Submit & Simulate Runtime π") |
|
|
|
with gr.Row(): |
|
simulate_button = gr.Button("Simulate IoT Data & Runtime πβ³") |
|
collecting_data_output = gr.Textbox(label="Status") |
|
|
|
simulate_output_moisture = gr.Textbox(label="Simulated Soil Moisture (%)") |
|
simulate_output_rainfall = gr.Textbox(label="Simulated Rainfall Prediction") |
|
simulate_decision_output = gr.Textbox(label="Simulated Irrigation Decision") |
|
simulate_runtime_output = gr.Textbox(label="Irrigation Runtime Status") |
|
|
|
|
|
submit_button.click( |
|
irrigation_decision_with_runtime, |
|
inputs=[soil_moisture_input, rainfall_prediction_input], |
|
outputs=[decision_output, runtime_status_output] |
|
) |
|
|
|
simulate_button.click( |
|
simulate_values_and_runtime, |
|
inputs=[], |
|
outputs=[ |
|
collecting_data_output, |
|
simulate_output_moisture, |
|
simulate_output_rainfall, |
|
simulate_decision_output, |
|
simulate_runtime_output |
|
] |
|
) |
|
|
|
|
|
demo.launch() |
|
|