1+ !pip install Flask requests beautifulsoup4
2+
13from flask import Flask , render_template , jsonify
24import random
35import requests
6+ from bs4 import BeautifulSoup
47
58app = Flask (__name__ )
69
@@ -23,47 +26,67 @@ def get_sensor_data():
2326}
2427
2528def generate_expert_recommendations (sensor_data ):
26- """
27- Generates AI-driven recommendations based on sensor data.
28- """
2929 recommendations = []
30-
30+
3131 if sensor_data ["soil_moisture" ] < 20 :
3232 recommendations .append ({"title" : "Soil Moisture Alert" , "details" : AGRICULTURE_KNOWLEDGE_BASE ["low_moisture" ]})
3333 elif sensor_data ["soil_moisture" ] > 50 :
3434 recommendations .append ({"title" : "Soil Overwatering Risk" , "details" : AGRICULTURE_KNOWLEDGE_BASE ["high_moisture" ]})
35-
35+
3636 if sensor_data ["temperature" ] > 35 :
3737 recommendations .append ({"title" : "Heat Stress Alert" , "details" : AGRICULTURE_KNOWLEDGE_BASE ["high_temperature" ]})
3838 elif sensor_data ["temperature" ] < 20 :
3939 recommendations .append ({"title" : "Cold Stress Alert" , "details" : AGRICULTURE_KNOWLEDGE_BASE ["low_temperature" ]})
40-
40+
4141 if sensor_data ["humidity" ] > 80 :
4242 recommendations .append ({"title" : "High Humidity Warning" , "details" : AGRICULTURE_KNOWLEDGE_BASE ["high_humidity" ]})
4343 elif sensor_data ["humidity" ] < 40 :
4444 recommendations .append ({"title" : "Low Humidity Concern" , "details" : AGRICULTURE_KNOWLEDGE_BASE ["low_humidity" ]})
4545
4646 return recommendations if recommendations else [{"title" : "Optimal Conditions" , "details" : "No immediate actions required." }]
4747
48+ FAO_URLS = [
49+ "https://www.fao.org/climate-smart-agriculture/en/" ,
50+ "https://www.fao.org/sustainable-agriculture/en/" ,
51+ "https://www.fao.org/soils-portal/soil-management/en/" ,
52+ "https://www.fao.org/water/en/" ,
53+ "https://www.fao.org/digital-agriculture/en/"
54+ ]
55+
4856def 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 )}]
57+ best_practices = []
58+ headers = {"User-Agent" : "Mozilla/5.0" }
59+
60+ for url in FAO_URLS :
61+ try :
62+ response = requests .get (url , headers = headers , timeout = 10 )
63+ response .raise_for_status ()
64+ soup = BeautifulSoup (response .text , "html.parser" )
65+
66+ titles = soup .find_all (["h2" , "h3" ], limit = 5 ) or []
67+ paragraphs = soup .find_all ("p" , limit = 5 ) or []
68+
69+ for title , paragraph in zip (titles , paragraphs ):
70+ best_practices .append ({
71+ "title" : title .get_text (strip = True ),
72+ "details" : paragraph .get_text (strip = True ),
73+ "source" : url
74+ })
75+
76+ except requests .exceptions .RequestException as e :
77+ best_practices .append ({
78+ "title" : "Error Fetching Data" ,
79+ "details" : str (e ),
80+ "source" : url
81+ })
82+
83+ return best_practices or [{"title" : "No Data" , "details" : "Could not retrieve best practices." , "source" : "N/A" }]
6084
6185@app .route ("/api/dashboard" )
6286def api_dashboard ():
6387 sensor_data = get_sensor_data ()
6488 recommendations = generate_expert_recommendations (sensor_data )
6589 web_best_practices = fetch_real_time_best_practices ()
66-
6790 return jsonify ({
6891 "sensor_data" : sensor_data ,
6992 "recommendations" : recommendations ,
@@ -75,8 +98,10 @@ def home():
7598 sensor_data = get_sensor_data ()
7699 recommendations = generate_expert_recommendations (sensor_data )
77100 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 )
101+ return render_template ("index.html" ,
102+ sensor_data = sensor_data ,
103+ recommendations = recommendations ,
104+ web_best_practices = web_best_practices )
80105
81106if __name__ == "__main__" :
82- app .run (host = "20.48.204.5 " , port = 8000 , debug = True )
107+ app .run (host = "0.0.0.0 " , port = 8000 , debug = True )
0 commit comments