Spaces:
Sleeping
Sleeping
import gradio as gr | |
import random | |
import time | |
# thresholds for soil moisture (in %) | |
PRIMARY_THRESHOLD = 30 # Below this, irrigation is necessary | |
SECONDARY_THRESHOLD = 20 # Below this, irrigation is critical | |
def calculate_irrigation_runtime(soil_moisture): | |
"""Calculate irrigation runtime based on soil moisture.""" | |
if soil_moisture < SECONDARY_THRESHOLD: | |
return 10 # Critical moisture, longer irrigation | |
elif soil_moisture < PRIMARY_THRESHOLD: | |
return 7 # Moderate moisture, shorter irrigation | |
else: | |
return 0 # No irrigation needed | |
def irrigation_decision(soil_moisture, rainfall_prediction): | |
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(): | |
# Show loading message for data collection | |
yield "Collecting data from IoT device... ππ‘", "", "", "", "" | |
time.sleep(2) # Simulate delay for fetching data | |
# Simulate IoT data | |
simulated_moisture = random.uniform(10, 50) | |
rainfall_prediction = random.choice(["Yes", "No"]) | |
decision = irrigation_decision(simulated_moisture, rainfall_prediction) | |
# Display simulated data and initial decision | |
yield "", f"{simulated_moisture:.2f}", rainfall_prediction, decision, "" | |
# Simulate irrigation runtime if irrigation is ON | |
if "Irrigation ON" in decision: | |
runtime = calculate_irrigation_runtime(simulated_moisture) | |
if runtime > 0: | |
for minute in range(1, runtime + 1): | |
time.sleep(1) # Simulate a minute as 1 second for demonstration | |
status = f"Irrigation running... β³ ({minute}/{runtime} minutes)" | |
yield "", f"{simulated_moisture:.2f}", rainfall_prediction, decision, status | |
# Turn off irrigation after the duration | |
yield "", f"{simulated_moisture:.2f}", rainfall_prediction, decision, "Irrigation OFF β (Completed runtime)" | |
else: | |
yield "", f"{simulated_moisture:.2f}", rainfall_prediction, decision, "Irrigation remains OFF β" | |
def app(soil_moisture, rainfall_prediction): | |
decision = irrigation_decision(float(soil_moisture), rainfall_prediction) | |
return decision | |
# Gradio Interface | |
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 for Dinajpur", value="No" | |
) | |
decision_output = gr.Textbox(label="π‘ Irrigation Decision") | |
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") | |
irrigation_runtime_output = gr.Textbox(label="Irrigation Runtime Status") | |
with gr.Row(): | |
submit_button = gr.Button("Submit π") | |
# Function connections | |
submit_button.click( | |
app, inputs=[soil_moisture_input, rainfall_prediction_input], outputs=[decision_output] | |
) | |
simulate_button.click( | |
simulate_values_and_runtime, | |
inputs=[], | |
outputs=[ | |
collecting_data_output, | |
simulate_output_moisture, | |
simulate_output_rainfall, | |
simulate_decision_output, | |
irrigation_runtime_output | |
] | |
) | |
# Launch the app | |
demo.launch() | |