Spaces:
Build error
Build error
Download app.py from Abrar20/Automated-Irrigation-System: direct link, hf CLI and curl.
- Browser
- Download file 5.41 kB
-
https://huggingface.co/spaces/Abrar20/Automated-Irrigation-System/resolve/main/app.py
- Command line
-
hf download hf://spaces/Abrar20/Automated-Irrigation-System/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Abrar20/Automated-Irrigation-System/resolve/main/app.py
5.41 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. | |
| """ | |
| ) | |
| 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() | |