11from flask import Flask , render_template , jsonify
22import random
3+ import requests
34
45app = Flask (__name__ )
56
6- # Agriculture Knowledge Base
7- AGRICULTURE_KNOWLEDGE_BASE = {
8- "recommendations" : [
9- "Implement precision irrigation scheduling" ,
10- "Adopt integrated pest management" ,
11- "Use soil moisture sensors" ,
12- "Apply crop rotation strategies" ,
13- "Monitor weather patterns for planting" ,
14- "Utilize organic fertilizers" ,
15- "Practice conservation tillage" ,
16- "Install windbreaks for soil protection"
17- ],
18- "details" : [
19- """Precision Irrigation Best Practices:
20- - Use soil moisture sensors for data-driven watering
21- - Implement drip irrigation systems
22- - Schedule irrigation during cooler hours
23- - Monitor evapotranspiration rates
24- - Adjust for crop growth stages""" ,
7+ # Simulated IoT Sensor Data for Farming
8+ def get_sensor_data ():
9+ return {
10+ "soil_moisture" : round (random .uniform (10 , 60 ), 2 ), # Percentage
11+ "temperature" : round (random .uniform (15 , 40 ), 2 ), # Celsius
12+ "humidity" : round (random .uniform (30 , 90 ), 2 ) # Percentage
13+ }
2514
26- """Integrated Pest Management:
27- - Regular field scouting
28- - Use biological control agents
29- - Implement trap cropping
30- - Apply targeted pesticides
31- - Maintain pest monitoring records"""
32- ]
15+ # AI-Driven Agriculture Knowledge Base
16+ AGRICULTURE_KNOWLEDGE_BASE = {
17+ "low_moisture" : "Increase irrigation frequency and consider soil amendments." ,
18+ "high_moisture" : "Reduce watering to prevent root rot and monitor drainage." ,
19+ "high_temperature" : "Implement shading techniques and adjust irrigation schedules." ,
20+ "low_temperature" : "Use row covers or greenhouses to maintain optimal warmth." ,
21+ "high_humidity" : "Increase ventilation and monitor for fungal diseases." ,
22+ "low_humidity" : "Use mulching techniques to retain soil moisture."
3323}
3424
35- def generate_recommendation_cards ( num_cards = 3 ):
25+ def generate_expert_recommendations ( sensor_data ):
3626 """
37- Generates recommendation dashboard cards for farmers .
27+ Generates AI-driven recommendations based on sensor data .
3828 """
39- recommendations = AGRICULTURE_KNOWLEDGE_BASE ["recommendations" ]
40- details = AGRICULTURE_KNOWLEDGE_BASE ["details" ]
41-
42- selected_recommendations = random .sample (recommendations , min (num_cards , len (recommendations )))
29+ recommendations = []
30+
31+ if sensor_data ["soil_moisture" ] < 20 :
32+ recommendations .append ({"title" : "Soil Moisture Alert" , "details" : AGRICULTURE_KNOWLEDGE_BASE ["low_moisture" ]})
33+ elif sensor_data ["soil_moisture" ] > 50 :
34+ recommendations .append ({"title" : "Soil Overwatering Risk" , "details" : AGRICULTURE_KNOWLEDGE_BASE ["high_moisture" ]})
35+
36+ if sensor_data ["temperature" ] > 35 :
37+ recommendations .append ({"title" : "Heat Stress Alert" , "details" : AGRICULTURE_KNOWLEDGE_BASE ["high_temperature" ]})
38+ elif sensor_data ["temperature" ] < 20 :
39+ recommendations .append ({"title" : "Cold Stress Alert" , "details" : AGRICULTURE_KNOWLEDGE_BASE ["low_temperature" ]})
40+
41+ if sensor_data ["humidity" ] > 80 :
42+ recommendations .append ({"title" : "High Humidity Warning" , "details" : AGRICULTURE_KNOWLEDGE_BASE ["high_humidity" ]})
43+ elif sensor_data ["humidity" ] < 40 :
44+ recommendations .append ({"title" : "Low Humidity Concern" , "details" : AGRICULTURE_KNOWLEDGE_BASE ["low_humidity" ]})
4345
44- recommendation_cards = []
45- for rec in selected_recommendations :
46- detail_index = recommendations .index (rec ) if recommendations .index (rec ) < len (details ) else None
47- recommendation_cards .append ({
48- "title" : rec ,
49- "details" : details [detail_index ] if detail_index is not None else "Additional information not available."
50- })
46+ return recommendations if recommendations else [{"title" : "Optimal Conditions" , "details" : "No immediate actions required." }]
5147
52- return recommendation_cards
48+ def fetch_real_time_best_practices ():
49+ """
50+ Fetches web-based best practices for farm management.
51+ """
52+ try :
53+ response = requests .get ("https://api.farmmanagement.best-practices.com/agriculture" )
54+ if response .status_code == 200 :
55+ return response .json ().get ("best_practices" , [])
56+ else :
57+ return [{"title" : "Web Data Unavailable" , "details" : "Real-time recommendations could not be retrieved." }]
58+ except Exception as e :
59+ return [{"title" : "Error Fetching Data" , "details" : str (e )}]
5360
54- @app .route ("/api/recommendations" )
55- def api_recommendations ():
56- return jsonify (generate_recommendation_cards ())
61+ @app .route ("/api/dashboard" )
62+ def api_dashboard ():
63+ sensor_data = get_sensor_data ()
64+ recommendations = generate_expert_recommendations (sensor_data )
65+ web_best_practices = fetch_real_time_best_practices ()
66+
67+ return jsonify ({
68+ "sensor_data" : sensor_data ,
69+ "recommendations" : recommendations ,
70+ "web_best_practices" : web_best_practices
71+ })
5772
5873@app .route ("/" )
5974def home ():
60- return render_template ( "index.html" , recommendations = generate_recommendation_cards () )
61-
62- @ app . route ( "/api/recommendations" )
63- def api_recommendations ():
64- return jsonify ( generate_recommendation_cards () )
75+ sensor_data = get_sensor_data ( )
76+ recommendations = generate_expert_recommendations ( sensor_data )
77+ web_best_practices = fetch_real_time_best_practices ( )
78+
79+ return render_template ( "index.html" , sensor_data = sensor_data , recommendations = recommendations , web_best_practices = web_best_practices )
6580
6681if __name__ == "__main__" :
67- app .run (host = "0.0.0.0" , port = 8000 )
82+ app .run (host = "0.0.0.0" , port = 8000 , debug = True )
0 commit comments