Update app.py
Browse files
app.py
CHANGED
|
@@ -1,26 +1,39 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
import joblib
|
| 3 |
import pandas as pd
|
| 4 |
from fastapi import FastAPI, HTTPException
|
| 5 |
from pydantic import BaseModel
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
model_path = os.path.join(script_dir, "crop_yield_pipeline.pkl")
|
| 10 |
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
model = None
|
|
|
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
class PredictionInput(BaseModel):
|
| 26 |
crop: str
|
|
@@ -36,12 +49,6 @@ class PredictionOutput(BaseModel):
|
|
| 36 |
prediction: str
|
| 37 |
insights: str
|
| 38 |
|
| 39 |
-
app = FastAPI(
|
| 40 |
-
title="Crop Yield Prediction API",
|
| 41 |
-
description="API for predicting crop yields based on agricultural parameters",
|
| 42 |
-
version="1.0.0"
|
| 43 |
-
)
|
| 44 |
-
|
| 45 |
@app.get("/health")
|
| 46 |
def health_check():
|
| 47 |
return {"status": "healthy", "model_loaded": model_loaded}
|
|
@@ -64,8 +71,7 @@ def predict(input_data: PredictionInput):
|
|
| 64 |
'Year': [input_data.year]
|
| 65 |
})
|
| 66 |
|
| 67 |
-
# Make prediction
|
| 68 |
-
import numpy as np
|
| 69 |
prediction = model.predict(df)[0]
|
| 70 |
|
| 71 |
# Format output
|
|
|
|
| 1 |
import os
|
| 2 |
+
import warnings
|
| 3 |
import joblib
|
| 4 |
import pandas as pd
|
| 5 |
from fastapi import FastAPI, HTTPException
|
| 6 |
from pydantic import BaseModel
|
| 7 |
|
| 8 |
+
# Suppress scikit-learn warnings globally (as backup; not needed with version match)
|
| 9 |
+
warnings.filterwarnings("ignore", category=UserWarning, module="sklearn")
|
|
|
|
| 10 |
|
| 11 |
+
app = FastAPI(
|
| 12 |
+
title="Crop Yield Prediction API",
|
| 13 |
+
description="API for predicting crop yields based on agricultural parameters",
|
| 14 |
+
version="1.0.0"
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
# Load model at app startup to ensure single load
|
| 18 |
model = None
|
| 19 |
+
model_loaded = False
|
| 20 |
|
| 21 |
+
@app.on_event("startup")
|
| 22 |
+
async def load_model():
|
| 23 |
+
global model, model_loaded
|
| 24 |
+
script_dir = os.path.dirname(os.path.abspath(__file__))
|
| 25 |
+
model_path = os.path.join(script_dir, "district_yield_pipeline.pkl")
|
| 26 |
+
|
| 27 |
+
if os.path.exists(model_path):
|
| 28 |
+
try:
|
| 29 |
+
model = joblib.load(model_path)
|
| 30 |
+
model_loaded = True
|
| 31 |
+
print("Model loaded successfully!")
|
| 32 |
+
except Exception as e:
|
| 33 |
+
print(f"Error loading model file: {e}")
|
| 34 |
+
else:
|
| 35 |
+
print(f"Model file not found at: {model_path}")
|
| 36 |
+
print(f"Available files: {os.listdir(script_dir)}")
|
| 37 |
|
| 38 |
class PredictionInput(BaseModel):
|
| 39 |
crop: str
|
|
|
|
| 49 |
prediction: str
|
| 50 |
insights: str
|
| 51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
@app.get("/health")
|
| 53 |
def health_check():
|
| 54 |
return {"status": "healthy", "model_loaded": model_loaded}
|
|
|
|
| 71 |
'Year': [input_data.year]
|
| 72 |
})
|
| 73 |
|
| 74 |
+
# Make prediction
|
|
|
|
| 75 |
prediction = model.predict(df)[0]
|
| 76 |
|
| 77 |
# Format output
|