Rick
commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,82 +3,26 @@ from fastapi import FastAPI
|
|
| 3 |
import pandas as pd
|
| 4 |
import numpy as np
|
| 5 |
import os
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
from sklearn.compose import ColumnTransformer
|
| 10 |
-
from sklearn.preprocessing import OneHotEncoder
|
| 11 |
|
| 12 |
app = FastAPI(title="Crop Yield Predictor API")
|
| 13 |
|
| 14 |
-
# ========
|
| 15 |
-
def create_and_train_model():
|
| 16 |
-
"""Create a simple model that will definitely work"""
|
| 17 |
-
try:
|
| 18 |
-
# Create sample training data with the same features
|
| 19 |
-
sample_data = {
|
| 20 |
-
'Area': ['India', 'USA', 'China', 'Brazil', 'India', 'USA'],
|
| 21 |
-
'Item': ['Maize', 'Wheat', 'Rice', 'Soybean', 'Wheat', 'Maize'],
|
| 22 |
-
'Year': [2020, 2021, 2022, 2020, 2021, 2022],
|
| 23 |
-
'average_rain_fall_mm_per_year': [800, 900, 1200, 1100, 850, 950],
|
| 24 |
-
'pesticides_tonnes': [5000, 6000, 7000, 5500, 5200, 5800],
|
| 25 |
-
'avg_temp': [20, 18, 22, 25, 19, 21]
|
| 26 |
-
}
|
| 27 |
-
|
| 28 |
-
# Sample target (yield in hg/ha)
|
| 29 |
-
sample_target = [25000, 30000, 35000, 28000, 32000, 27000]
|
| 30 |
-
|
| 31 |
-
df = pd.DataFrame(sample_data)
|
| 32 |
-
|
| 33 |
-
# Define preprocessing
|
| 34 |
-
numeric_features = ['Year', 'average_rain_fall_mm_per_year', 'pesticides_tonnes', 'avg_temp']
|
| 35 |
-
categorical_features = ['Area', 'Item']
|
| 36 |
-
|
| 37 |
-
preprocessor = ColumnTransformer(
|
| 38 |
-
transformers=[
|
| 39 |
-
('num', StandardScaler(), numeric_features),
|
| 40 |
-
('cat', OneHotEncoder(handle_unknown='ignore'), categorical_features)
|
| 41 |
-
])
|
| 42 |
-
|
| 43 |
-
# Create simple pipeline
|
| 44 |
-
model = Pipeline(steps=[
|
| 45 |
-
('preprocessor', preprocessor),
|
| 46 |
-
('regressor', RandomForestRegressor(n_estimators=10, random_state=42))
|
| 47 |
-
])
|
| 48 |
-
|
| 49 |
-
# Train on sample data
|
| 50 |
-
model.fit(df, sample_target)
|
| 51 |
-
|
| 52 |
-
return model, "✅ New model created and trained successfully!"
|
| 53 |
-
|
| 54 |
-
except Exception as e:
|
| 55 |
-
return None, f"❌ Model creation failed: {str(e)}"
|
| 56 |
-
|
| 57 |
-
# ======== LOAD OR CREATE MODEL ========
|
| 58 |
def load_model_properly():
|
| 59 |
-
|
| 60 |
-
model_path = 'CropYieldPredictor.pkl'
|
| 61 |
|
| 62 |
-
if os.path.exists(model_path):
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
return create_and_train_model()
|
| 71 |
-
else:
|
| 72 |
-
# No model file, create new one
|
| 73 |
-
return create_and_train_model()
|
| 74 |
-
|
| 75 |
-
# Try to load pickle if needed
|
| 76 |
-
try:
|
| 77 |
-
import pickle
|
| 78 |
-
model, load_status = load_model_properly()
|
| 79 |
-
except:
|
| 80 |
-
model, load_status = create_and_train_model()
|
| 81 |
|
|
|
|
| 82 |
print(load_status)
|
| 83 |
|
| 84 |
# ======== AVAILABLE AREAS ========
|
|
|
|
| 3 |
import pandas as pd
|
| 4 |
import numpy as np
|
| 5 |
import os
|
| 6 |
+
import joblib
|
| 7 |
+
import warnings
|
| 8 |
+
warnings.filterwarnings('ignore')
|
|
|
|
|
|
|
| 9 |
|
| 10 |
app = FastAPI(title="Crop Yield Predictor API")
|
| 11 |
|
| 12 |
+
# ======== MODEL LOADING WITH JOBLIB ========
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
def load_model_properly():
|
| 14 |
+
model_path = 'CropYieldPredictor_COMPATIBLE.joblib'
|
|
|
|
| 15 |
|
| 16 |
+
if not os.path.exists(model_path):
|
| 17 |
+
return None, f"❌ Model file '{model_path}' not found!"
|
| 18 |
+
|
| 19 |
+
try:
|
| 20 |
+
model = joblib.load(model_path)
|
| 21 |
+
return model, "✅ Model loaded successfully with joblib!"
|
| 22 |
+
except Exception as e:
|
| 23 |
+
return None, f"❌ Loading failed: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
+
model, load_status = load_model_properly()
|
| 26 |
print(load_status)
|
| 27 |
|
| 28 |
# ======== AVAILABLE AREAS ========
|