-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.py
316 lines (273 loc) · 11.1 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
from fastapi import FastAPI, HTTPException, Depends
from fastapi.security import HTTPBasic, HTTPBasicCredentials
import requests
import os
import json
import requests_cache
from dotenv import load_dotenv
from pydantic import BaseModel
from models import WeatherData, Coordinates
from typing import List, Dict
from retry_requests import retry
import pandas as pd
from fastapi.middleware.cors import CORSMiddleware
from fastapi_cache import FastAPICache
from fastapi_cache.backends.inmemory import InMemoryBackend
from fastapi_cache.decorator import cache
from datetime import timedelta
from sklearn.preprocessing import LabelEncoder, MinMaxScaler
import joblib
import numpy as np
# Load environment variables from the .env file
app = FastAPI()
load_dotenv()
FastAPICache.init(InMemoryBackend(), prefix="fastapi-cache")
security = HTTPBasic()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# importing all api keys from the.env file
WEATHER_API_KEY = os.getenv("WEATHER_API_KEY")
WEATHER_API_URL = os.getenv("WEATHER_API_URL")
GEOCODING_API_URL = os.getenv("GEOCODING_API_URL")
FORECAST_API_URL = os.getenv("FORECAST_API_URL")
WEATHER_API_KEY2 = os.getenv("WEATHER_API_KEY2")
WEATHER_HISTORICAL_API_URL = os.getenv("WEATHER_HISTORICAL_API_URL")
CURRENT_AND_FORECAST_API_URL = os.getenv("CURRENT_AND_FORECAST_API_URL")
CURRENT_IP_ADDRESS = os.getenv("CURRENT_IP_ADDRESS")
# Load crop data from crops.json
with open("crops.json", "r") as f:
crop_data = json.load(f)
# Load model artifacts
model = joblib.load('crop_recommendation_model.pkl')
scaler = joblib.load('scaler.pkl')
label_encoder = joblib.load('label_encoder.pkl')
features = joblib.load('feature_names.pkl')
def get_weather(lat: float, lon: float):
params = {
'lat': lat,
'lon': lon,
'appid': WEATHER_API_KEY,
'units': 'metric'
}
response = requests.get(WEATHER_API_URL, params=params)
if response.status_code != 200:
raise HTTPException(status_code=404, detail="Weather data not found")
data = response.json()
return WeatherData(
temp=data['main']['temp'],
humidity=data['main']['humidity'],
wind_speed=data['wind']['speed'],
temp_min=data['main']['temp_min'],
temp_max=data['main']['temp_max'],
main=data['weather'][0]['main'],
description=data['weather'][0]['description'],
)
def get_coordinates(city: str):
params = {
'q': f"{city},NG",
'limit': 1,
'appid': WEATHER_API_KEY
}
response = requests.get(GEOCODING_API_URL, params=params)
if response.status_code != 200 or len(response.json()) == 0:
raise HTTPException(status_code=404, detail="City not found or not in Nigeria")
data = response.json()[0]
return data['lat'], data['lon']
def recommend_crops(weather_data, crop_data):
temp = weather_data['temp']
humidity = weather_data['humidity']
main = weather_data['main']
description = weather_data['description'].lower()
if temp > 20 and humidity < 50:
print("High temp, low humidity")
return crop_data.get("high_temp_low_humidity", [])
elif 20 >= temp > 15 and humidity >= 50:
print("Moderate temp, high humidity")
return crop_data.get("moderate_temp_high_humidity", [])
elif temp < 15:
print("Low temp")
return crop_data.get("low_temp", [])
elif 'rain' in description:
print("Rainy")
return crop_data.get("rainy", [])
else:
print("Default")
return crop_data.get("default", [])
def current_weather(lat: float, lon: float):
params = {
"latitude": lat,
"longitude": lon,
"current": ["temperature_2m", "apparent_temperature", "is_day", "precipitation", "rain", "cloud_cover", "surface_pressure", "wind_speed_10m", "wind_direction_10m", "wind_gusts_10m"],
"hourly": "temperature_2m",
"timezone": "Africa/Cairo"
}
responses = requests.get(WEATHER_API_KEY2, params=params)
if responses.status_code == 200:
data = responses.json()
print("Full API response:", data['current'])
# Extract specific variables from the current weather data
current_data = data['current']
return current_data
else:
# Handle the case where the API response is not successful
return {"error": "Failed to retrieve weather data"}
def historical_weather(lat:float, lon:float, start_date: str, end_date: str, ):
params = {
"latitude": 52.52,
"longitude": 13.41,
"start_date": start_date,
"end_date": end_date,
"daily": ["temperature_2m_max", "temperature_2m_mean", "sunrise", "sunset", "daylight_duration", "sunshine_duration", "precipitation_sum", "rain_sum", "snowfall_sum", "precipitation_hours"],
"timezone": "Africa/Cairo"
}
responses = requests.get(WEATHER_API_KEY2, params=params)
if responses.status_code != 200:
raise HTTPException(status_code=404, detail="Weather data not found")
data = responses.json()
return data
# Root endpoint
@app.get("/")
def read_root():
about="Api that recommends growable crops based on weather temperature for a given location and soil texture."
licenses = {'Full Name':"Samuel Peters", 'socials':{'github':"https://github.com/Petsamuel", "repository":"https://github.com/Petsamuel/weather-crop-API", "LinkedIn":"https:linkedIn.com/in/bieefilled"}, 'year':"2024"}
return {"message": about, "License":licenses }
# Get weather and recommendations for a city
@app.get("/weather/{city}", status_code=200)
def get_weather_only(city: str):
try:
lat, lon = get_coordinates(city)
weather_data = get_weather(lat, lon)
return {"status": "success", "data": weather_data.dict()}
except HTTPException as e:
return {"status": "error", "detail": e.detail}
# api for forecasting
@app.get("/weather/forecast/{city}", status_code=200)
def get_weather_forecast_and_crop_recommendations(city: str):
try:
lat, lon = get_coordinates(city)
weather_data = get_weather_forecast(lat, lon)
recommended_crops = recommend_crops(weather_data.dict(), crop_data)
return {"status": "success", "data": weather_data.dict(), "recommended_crops": recommended_crops}
except HTTPException as e:
return {"status": "error", "detail": e.detail}
def get_weather_forecast(lat: float, lon: float):
params = {
'lat': lat,
'lon': lon,
'appid': WEATHER_API_KEY,
'units': 'metric'
}
response = requests.get(WEATHER_API_URL, params=params)
if response.status_code != 200:
raise HTTPException(status_code=404, detail="Weather data not found")
data = response.json()
return WeatherData(
temp=data['main']['temp'],
humidity=data['main']['humidity'],
wind_speed=data['wind']['speed'],
temp_min=data['main']['temp_min'],
temp_max=data['main']['temp_max'],
main=data['weather'][0]['main'],
description=data['weather'][0]['description']
)
# Load model artifacts
model = joblib.load('best_crop_model.pkl')
scaler = joblib.load('scaler.pkl')
@app.get("/recommend-crops/{city}", status_code=200)
def recommend_crops_using_model(city: str):
try:
# Get weather data
lat, lon = get_coordinates(city)
weather_data = get_weather(lat, lon)
# Prepare features
input_data = pd.DataFrame([{
'N': 50, # Default values - adjust as needed
'P': 50,
'K': 50,
'temperature': weather_data.temp,
'humidity': weather_data.humidity,
'ph': 7.0, # Default value
'rainfall': 200 # Default value
}])
# Scale features
input_scaled = scaler.transform(input_data)
# Get predictions
prediction = model.predict(input_scaled)[0]
# Map prediction back to crop name
crop_dict_reverse = {v: k for k, v in CROP_DICT.items()}
recommended_crop = crop_dict_reverse[prediction]
return {
"status": "success",
"weather_data": weather_data.dict(),
"recommended_crop": recommended_crop
}
except Exception as e:
return {"status": "error", "detail": str(e)}
@app.get("/current/weather/{city}", status_code=200)
def get_current_weather(city: str):
lat, lon = get_coordinates(city)
current_data = current_weather(lat, lon)
return {"status": "success", "data": current_data}
@app.get("/weather/history/{city}/{start_date}/{end_date}")
def historical_weather_data(city: str, start_date: str, end_date: str):
lat, lon = get_coordinates(city)
historical_data = historical_weather(lat, lon, start_date, end_date)
return {"status": "success", "data": historical_data}
# New endpoint to recommend crops using the trained model
@app.get("/recommend-crops/{city}", status_code=200)
def recommend_crops_using_model(city: str):
try:
# Get weather data
lat, lon = get_coordinates(city)
weather_data = get_weather(lat, lon)
# Prepare features
input_data = pd.DataFrame([{
'temp_range': (weather_data.temp_max - weather_data.temp_min),
'humidity_range': weather_data.humidity,
'suitability_score': min(100, max(0,
(weather_data.temp - 15) * 5 +
(weather_data.humidity - 40) * 2))
}])
# Scale features
input_scaled = scaler.transform(input_data[features])
# Get predictions
prediction = model.predict(input_scaled)
crop_conditions = label_encoder.inverse_transform(prediction)
# Get recommended crops
recommended_crops = crop_data.get(crop_conditions[0], [])
return {
"status": "success",
"weather_condition": crop_conditions[0],
"recommended_crops": recommended_crops
}
except Exception as e:
return {"status": "error", "detail": str(e)}
# def recommend_crops_using_model(city: str):
# try:
# lat, lon = get_coordinates(city)
# weather_data = get_weather(lat, lon)
# # Preprocess the weather data to match the model's input format
# input_data = pd.DataFrame([{
# 'temp': weather_data.temp,
# 'humidity': weather_data.humidity,
# 'wind_speed': weather_data.wind_speed,
# 'temp_min': weather_data.temp_min,
# 'temp_max': weather_data.temp_max,
# 'main': weather_data.main,
# 'description': weather_data.description
# }])
# # Make predictions using the trained model
# predictions = model.predict(input_data)
# print(input_data)
# # Return the predictions
# return {"status": "success", "recommended_crops": predictions.tolist()}
# except HTTPException as e:
# return {"status": "error", "detail": e.detail}
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="localhost", port=5000)