Update app.py
Browse files
app.py
CHANGED
|
@@ -1,133 +1,45 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
from fastapi import FastAPI
|
| 3 |
-
|
| 4 |
-
import numpy as np
|
| 5 |
-
import os
|
| 6 |
import joblib
|
| 7 |
-
import
|
| 8 |
-
|
| 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 |
-
#
|
| 29 |
-
|
| 30 |
-
'Albania', 'Algeria', 'Angola', 'Argentina', 'Armenia', 'Australia', 'Austria',
|
| 31 |
-
'Azerbaijan', 'Bahamas', 'Bahrain', 'Bangladesh', 'Belarus', 'Belgium', 'Botswana',
|
| 32 |
-
'Brazil', 'Bulgaria', 'Burkina Faso', 'Burundi', 'Cameroon', 'Canada',
|
| 33 |
-
'Central African Republic', 'Chile', 'Colombia', 'Croatia', 'Denmark',
|
| 34 |
-
'Dominican Republic', 'Ecuador', 'Egypt', 'El Salvador', 'Eritrea', 'Estonia',
|
| 35 |
-
'Finland', 'France', 'Germany', 'Ghana', 'Greece', 'Guatemala', 'Guinea',
|
| 36 |
-
'Guyana', 'Haiti', 'Honduras', 'Hungary', 'India', 'Indonesia', 'Iraq',
|
| 37 |
-
'Ireland', 'Italy', 'Jamaica', 'Japan', 'Kazakhstan', 'Kenya', 'Latvia',
|
| 38 |
-
'Lebanon', 'Lesotho', 'Libya', 'Lithuania', 'Madagascar', 'Malawi', 'Malaysia',
|
| 39 |
-
'Mali', 'Mauritania', 'Mauritius', 'Mexico', 'Montenegro', 'Morocco',
|
| 40 |
-
'Mozambique', 'Namibia', 'Nepal', 'Netherlands', 'New Zealand', 'Nicaragua',
|
| 41 |
-
'Niger', 'Norway', 'Pakistan', 'Papua New Guinea', 'Peru', 'Poland', 'Portugal',
|
| 42 |
-
'Qatar', 'Romania', 'Rwanda', 'Saudi Arabia', 'Senegal', 'Slovenia',
|
| 43 |
-
'South Africa', 'Spain', 'Sri Lanka', 'Sudan', 'Suriname', 'Sweden',
|
| 44 |
-
'Switzerland', 'Tajikistan', 'Thailand', 'Tunisia', 'Turkey', 'Uganda',
|
| 45 |
-
'Ukraine', 'United Kingdom', 'Uruguay', 'Zambia', 'Zimbabwe'
|
| 46 |
-
]
|
| 47 |
|
| 48 |
-
#
|
| 49 |
-
|
| 50 |
-
if model is None:
|
| 51 |
-
return f"β ERROR: {load_status}"
|
| 52 |
-
|
| 53 |
-
try:
|
| 54 |
-
year_val = int(year)
|
| 55 |
-
rainfall_val = float(rainfall)
|
| 56 |
-
pesticides_val = float(pesticides)
|
| 57 |
-
temperature_val = float(temperature)
|
| 58 |
-
|
| 59 |
-
input_data = {
|
| 60 |
-
'Area': [area],
|
| 61 |
-
'Item': [item],
|
| 62 |
-
'Year': [year_val],
|
| 63 |
-
'average_rain_fall_mm_per_year': [rainfall_val],
|
| 64 |
-
'pesticides_tonnes': [pesticides_val],
|
| 65 |
-
'avg_temp': [temperature_val]
|
| 66 |
-
}
|
| 67 |
-
|
| 68 |
-
input_df = pd.DataFrame(input_data)
|
| 69 |
-
prediction = model.predict(input_df)
|
| 70 |
-
predicted_yield = prediction[0]
|
| 71 |
-
predicted_yield_kg_ha = predicted_yield * 0.1
|
| 72 |
-
|
| 73 |
-
result_text = f"**πΎ Predicted Yield: {predicted_yield_kg_ha:,.0f} kg/ha**\n*({predicted_yield:,.0f} hg/ha)*"
|
| 74 |
-
|
| 75 |
-
return result_text
|
| 76 |
-
|
| 77 |
-
except Exception as e:
|
| 78 |
-
return f"β PREDICTION ERROR: {str(e)}"
|
| 79 |
|
| 80 |
-
#
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
area = gr.Dropdown(label="π Country/Area", choices=AVAILABLE_AREAS, value="India")
|
| 88 |
-
item = gr.Textbox(label="π± Crop Type", value="Maize")
|
| 89 |
-
year = gr.Number(label="π
Year", value=2023)
|
| 90 |
-
rainfall = gr.Textbox(label="π§ Average Rainfall (mm/year)", value="800.0")
|
| 91 |
-
pesticides = gr.Textbox(label="π§΄ Pesticides (tonnes)", value="5000.0")
|
| 92 |
-
temperature = gr.Textbox(label="π‘οΈ Average Temperature (Β°C)", value="20.0")
|
| 93 |
-
predict_btn = gr.Button("π Predict Yield", variant="primary")
|
| 94 |
-
|
| 95 |
-
with gr.Column():
|
| 96 |
-
output = gr.Markdown(label="Prediction Result")
|
| 97 |
-
|
| 98 |
-
predict_btn.click(
|
| 99 |
-
fn=predict_yield,
|
| 100 |
-
inputs=[area, item, year, rainfall, pesticides, temperature],
|
| 101 |
-
outputs=[output]
|
| 102 |
-
)
|
| 103 |
|
| 104 |
-
#
|
| 105 |
@app.get("/")
|
| 106 |
-
|
| 107 |
-
return {"message": "Crop Yield Predictor API
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
"temperature": temperature
|
| 130 |
-
}
|
| 131 |
-
}
|
| 132 |
-
|
| 133 |
-
app = gr.mount_gradio_app(app, demo, path="/")
|
|
|
|
|
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
+
from pydantic import BaseModel
|
|
|
|
|
|
|
| 3 |
import joblib
|
| 4 |
+
import pandas as pd
|
| 5 |
+
import uvicorn
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
# β
Initialize FastAPI app
|
| 8 |
+
app = FastAPI(title="Crop Yield Predictor API", version="1.0")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
# β
Load your trained model
|
| 11 |
+
model = joblib.load("CropYeildPredictor.pkl")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
# β
Define the expected input schema
|
| 14 |
+
class CropInput(BaseModel):
|
| 15 |
+
country: str
|
| 16 |
+
crop_type: str
|
| 17 |
+
year: int
|
| 18 |
+
avg_rainfall: float
|
| 19 |
+
pesticides: float
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
# β
Home route
|
| 22 |
@app.get("/")
|
| 23 |
+
def home():
|
| 24 |
+
return {"message": "Crop Yield Predictor API is running successfully!"}
|
| 25 |
+
|
| 26 |
+
# β
Prediction route
|
| 27 |
+
@app.post("/predict")
|
| 28 |
+
def predict_yield(data: CropInput):
|
| 29 |
+
# Convert input to DataFrame (must match training features order)
|
| 30 |
+
input_df = pd.DataFrame([{
|
| 31 |
+
"Country": data.country,
|
| 32 |
+
"Crop": data.crop_type,
|
| 33 |
+
"Year": data.year,
|
| 34 |
+
"average_rain_fall_mm_per_year": data.avg_rainfall,
|
| 35 |
+
"pesticides_tonnes": data.pesticides
|
| 36 |
+
}])
|
| 37 |
+
|
| 38 |
+
# Make prediction
|
| 39 |
+
prediction = model.predict(input_df)[0]
|
| 40 |
+
|
| 41 |
+
return {"predicted_yield": float(prediction)}
|
| 42 |
+
|
| 43 |
+
# β
For local testing (wonβt be used in HF)
|
| 44 |
+
if __name__ == "__main__":
|
| 45 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|