Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -11,12 +11,6 @@ api_key = os.getenv('GEMINI_API_KEY')
|
|
11 |
# Replace with your actual Gemini API key
|
12 |
client = genai.Client(api_key=api_key)
|
13 |
|
14 |
-
def validate_coordinates(lat, lon):
|
15 |
-
"""Validate and convert latitude and longitude to float."""
|
16 |
-
try:
|
17 |
-
return float(lat), float(lon)
|
18 |
-
except (TypeError, ValueError):
|
19 |
-
return None, None
|
20 |
|
21 |
@app.route('/')
|
22 |
def index():
|
@@ -88,9 +82,33 @@ def get_weather_data():
|
|
88 |
except Exception as e:
|
89 |
return jsonify({"error": str(e)}), 500
|
90 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
91 |
@app.route('/get_soil_properties', methods=['GET'])
|
92 |
def get_soil_properties():
|
93 |
-
"""Fetch soil properties using SoilGrids API and map to user-friendly names."""
|
94 |
lat = request.args.get('lat')
|
95 |
lon = request.args.get('lon')
|
96 |
lat, lon = validate_coordinates(lat, lon)
|
@@ -98,46 +116,39 @@ def get_soil_properties():
|
|
98 |
return jsonify({"error": "Invalid coordinates"}), 400
|
99 |
|
100 |
try:
|
|
|
101 |
prop_url = "https://rest.isric.org/soilgrids/v2.0/properties/query"
|
|
|
|
|
102 |
prop_params = {
|
103 |
-
"
|
104 |
-
"
|
105 |
-
"property":
|
106 |
-
"bdod", "cec", "cfvo", "clay", "nitrogen",
|
107 |
-
"ocd", "phh2o", "sand", "silt",
|
108 |
-
"soc", "wv0010", "wv0033", "wv1500"
|
109 |
-
],
|
110 |
"depth": "5-15cm",
|
111 |
"value": "mean"
|
112 |
}
|
113 |
headers = {"accept": "application/json"}
|
114 |
-
# Added timeout to prevent long hanging calls
|
115 |
response = requests.get(prop_url, params=prop_params, headers=headers, timeout=10)
|
116 |
response.raise_for_status()
|
117 |
prop_data = response.json()
|
118 |
|
|
|
|
|
|
|
119 |
table_data = []
|
120 |
-
|
121 |
-
"bdod": "Bulk Density",
|
122 |
-
"cec": "CEC",
|
123 |
-
"cfvo": "Field Capacity",
|
124 |
-
"clay": "Clay",
|
125 |
-
"nitrogen": "Nitrogen",
|
126 |
-
"ocd": "Organic Carbon Density",
|
127 |
-
"phh2o": "pH",
|
128 |
-
"sand": "Sand",
|
129 |
-
"silt": "Silt",
|
130 |
-
"soc": "Soil Organic Carbon",
|
131 |
-
"wv0010": "Volumetric Water Content (0-10cm)",
|
132 |
-
"wv0033": "Volumetric Water Content (10-33cm)",
|
133 |
-
"wv1500": "Volumetric Water Content (1500)"
|
134 |
-
}
|
135 |
for layer in prop_data.get('properties', {}).get('layers', []):
|
136 |
parameter = layer.get('name')
|
137 |
display_name = PARAMETER_NAMES.get(parameter, parameter)
|
138 |
-
|
|
|
|
|
|
|
|
|
|
|
139 |
if value is None:
|
140 |
continue
|
|
|
141 |
if parameter in ["wv0010", "wv0033", "wv1500"]:
|
142 |
final_value = value / 10.0
|
143 |
unit = layer.get('unit_measure', {}).get("target_units", "")
|
@@ -148,11 +159,11 @@ def get_soil_properties():
|
|
148 |
final_value = value
|
149 |
unit = layer.get('unit_measure', {}).get("mapped_units", "")
|
150 |
table_data.append([display_name, final_value, unit])
|
151 |
-
|
152 |
return jsonify({"soil_properties": table_data})
|
153 |
except Exception as e:
|
154 |
return jsonify({"error": str(e)}), 500
|
155 |
|
|
|
156 |
def call_gemini_api(input_data):
|
157 |
language = input_data.get('language', 'English')
|
158 |
prompt = f"""
|
@@ -336,4 +347,4 @@ def predict():
|
|
336 |
|
337 |
|
338 |
if __name__ == '__main__':
|
339 |
-
app.run(debug=True)
|
|
|
11 |
# Replace with your actual Gemini API key
|
12 |
client = genai.Client(api_key=api_key)
|
13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
@app.route('/')
|
16 |
def index():
|
|
|
82 |
except Exception as e:
|
83 |
return jsonify({"error": str(e)}), 500
|
84 |
|
85 |
+
|
86 |
+
def validate_coordinates(lat, lon):
|
87 |
+
"""Convert and validate latitude and longitude values."""
|
88 |
+
try:
|
89 |
+
return float(lat), float(lon)
|
90 |
+
except (TypeError, ValueError):
|
91 |
+
return None, None
|
92 |
+
|
93 |
+
# Mapping of soil property short codes to user-friendly names
|
94 |
+
PARAMETER_NAMES = {
|
95 |
+
"bdod": "Bulk Density",
|
96 |
+
"cec": "CEC",
|
97 |
+
"cfvo": "Field Capacity",
|
98 |
+
"clay": "Clay Content",
|
99 |
+
"nitrogen": "Nitrogen",
|
100 |
+
"ocd": "Organic Carbon Density",
|
101 |
+
"phh2o": "Soil pH",
|
102 |
+
"sand": "Sand Content",
|
103 |
+
"silt": "Silt Content",
|
104 |
+
"soc": "Soil Organic Carbon",
|
105 |
+
"wv0010": "Volumetric Water Content (0-10cm)",
|
106 |
+
"wv0033": "Volumetric Water Content (10-33cm)",
|
107 |
+
"wv1500": "Volumetric Water Content (1500kPa)"
|
108 |
+
}
|
109 |
+
|
110 |
@app.route('/get_soil_properties', methods=['GET'])
|
111 |
def get_soil_properties():
|
|
|
112 |
lat = request.args.get('lat')
|
113 |
lon = request.args.get('lon')
|
114 |
lat, lon = validate_coordinates(lat, lon)
|
|
|
116 |
return jsonify({"error": "Invalid coordinates"}), 400
|
117 |
|
118 |
try:
|
119 |
+
# Use the correct host; note that rest.isric.org is now used instead of rest.soilgrids.org
|
120 |
prop_url = "https://rest.isric.org/soilgrids/v2.0/properties/query"
|
121 |
+
# Provide the soil property codes as a comma-separated string
|
122 |
+
properties = "bdod,cec,cfvo,clay,nitrogen,ocd,phh2o,sand,silt,soc,wv0010,wv0033,wv1500"
|
123 |
prop_params = {
|
124 |
+
"lat": lat,
|
125 |
+
"lon": lon,
|
126 |
+
"property": properties,
|
|
|
|
|
|
|
|
|
127 |
"depth": "5-15cm",
|
128 |
"value": "mean"
|
129 |
}
|
130 |
headers = {"accept": "application/json"}
|
|
|
131 |
response = requests.get(prop_url, params=prop_params, headers=headers, timeout=10)
|
132 |
response.raise_for_status()
|
133 |
prop_data = response.json()
|
134 |
|
135 |
+
# Uncomment the following line to print the entire API response for debugging:
|
136 |
+
# print(json.dumps(prop_data, indent=2))
|
137 |
+
|
138 |
table_data = []
|
139 |
+
# Loop through each layer in the response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
140 |
for layer in prop_data.get('properties', {}).get('layers', []):
|
141 |
parameter = layer.get('name')
|
142 |
display_name = PARAMETER_NAMES.get(parameter, parameter)
|
143 |
+
depths = layer.get('depths', [])
|
144 |
+
if not depths:
|
145 |
+
continue
|
146 |
+
# Extract the mean value from the first depth entry
|
147 |
+
value_dict = depths[0].get('values', {})
|
148 |
+
value = value_dict.get('mean')
|
149 |
if value is None:
|
150 |
continue
|
151 |
+
# Convert values for specific parameters
|
152 |
if parameter in ["wv0010", "wv0033", "wv1500"]:
|
153 |
final_value = value / 10.0
|
154 |
unit = layer.get('unit_measure', {}).get("target_units", "")
|
|
|
159 |
final_value = value
|
160 |
unit = layer.get('unit_measure', {}).get("mapped_units", "")
|
161 |
table_data.append([display_name, final_value, unit])
|
|
|
162 |
return jsonify({"soil_properties": table_data})
|
163 |
except Exception as e:
|
164 |
return jsonify({"error": str(e)}), 500
|
165 |
|
166 |
+
|
167 |
def call_gemini_api(input_data):
|
168 |
language = input_data.get('language', 'English')
|
169 |
prompt = f"""
|
|
|
347 |
|
348 |
|
349 |
if __name__ == '__main__':
|
350 |
+
app.run(debug=True)
|