| import joblib |
| import numpy as np |
| import pandas as pd |
|
|
| |
| model = joblib.load("rf_yield.pkl") |
| scaler = joblib.load("scaler.pkl") |
| ohe = joblib.load("ohe.pkl") |
| training_columns = joblib.load("training_columns.pkl") |
|
|
| def predict(inputs: dict): |
| """ |
| inputs example: |
| {"area": 50, "season": "Kharif"} |
| """ |
|
|
| |
| df = pd.DataFrame([inputs]) |
|
|
| |
| categorical_cols = ["season"] |
| encoded = ohe.transform(df[categorical_cols]).toarray() |
| encoded_df = pd.DataFrame(encoded, columns=ohe.get_feature_names_out(categorical_cols)) |
|
|
| |
| numeric_df = df.drop(columns=categorical_cols) |
|
|
| |
| full_df = pd.concat([numeric_df, encoded_df], axis=1) |
|
|
| |
| full_df = full_df.reindex(columns=training_columns, fill_value=0) |
|
|
| |
| scaled = scaler.transform(full_df) |
|
|
| |
| pred = model.predict(scaled) |
|
|
| return {"prediction": str(pred[0])} |
|
|