Abrar20's picture
Update app.py
afb0a8a verified
raw
history blame
5.43 kB
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_with_runtime(soil_moisture, rainfall_prediction):
"""Make irrigation decision and simulate runtime."""
# Initial irrigation decision
decision = irrigation_decision(soil_moisture, rainfall_prediction)
yield decision, ""
# Simulate irrigation runtime if irrigation is ON
if "Irrigation ON" in decision:
runtime = calculate_irrigation_runtime(soil_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 decision, status
# Turn off irrigation after the duration
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."""
# 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 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 β›”"
# 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 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")
# Function connections
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
]
)
# Launch the app
demo.launch()