Spaces:
Sleeping
Sleeping
| import numpy as np | |
| import pandas as pd | |
| import seaborn as sns | |
| import matplotlib.pyplot as plt | |
| plt.style.use("fivethirtyeight") | |
| import sklearn | |
| from sklearn.tree import DecisionTreeRegressor | |
| from sklearn.ensemble import RandomForestRegressor, StackingRegressor | |
| from sklearn.preprocessing import FunctionTransformer, MinMaxScaler, StandardScaler, LabelEncoder, OrdinalEncoder | |
| from sklearn.model_selection import train_test_split | |
| import xgboost | |
| import tqdm | |
| from tqdm.auto import trange, tqdm | |
| import pickle | |
| import gradio as gr | |
| def run(): | |
| def make_predictions(domain_, year_, item_, unit_, flag_, flag_description_, element_, mean_temp_, total_temp_, mrh_, trh_, mrf_, trf_, mean_fert_, total_fert_, mean_pest_, total_pest_): | |
| X_train = pd.read_csv("xtrain.csv") | |
| X = X_train.copy() | |
| le = OrdinalEncoder(handle_unknown='use_encoded_value', unknown_value=-1) | |
| le.fit(X_train[["Domain", "Item", "Unit", "Flag Description", "Element"]]) | |
| X_train[["Domain", "Item", "Unit", "Flag Description", "Element"]] = le.transform(X_train[["Domain", "Item", "Unit", "Flag Description", "Element"]]) | |
| scaler_ = MinMaxScaler(feature_range=(0, 1)) | |
| scaler_.fit(X_train) | |
| X_train = scaler_.transform(X_train) | |
| X_train = pd.DataFrame(X_train, columns = X.columns) | |
| dataset_dict = {"Domain": [domain_], | |
| "Year": [float(year_)], | |
| "Item": [item_], | |
| "Unit": [unit_], | |
| "Flag": [flag_], | |
| "Flag Description": [flag_description_], | |
| "Element": [element_], | |
| "Mean temperature": [float(mean_temp_)], | |
| "Total temperature": [float(total_temp_)], | |
| "Mean relative humidity": [float(mrh_)], | |
| "Total relative humidity": [float(trh_)], | |
| "Mean Rainfall(mm)": [float(mrf_)], | |
| "Total Rainfall(mm)": [float(trf_)], | |
| "Mean Fertilizer": [float(mean_fert_)], | |
| "Total Fertilizer": [float(total_fert_)], | |
| "Mean Pesticide": [float(mean_pest_)], | |
| "Total Pesticide": [float(total_pest_)],} | |
| df = pd.DataFrame(data = dataset_dict) | |
| df = df[['Domain', 'Element', 'Item', 'Year', 'Unit', 'Flag Description', | |
| 'Mean temperature', 'Total temperature', 'Mean relative humidity', | |
| 'Total relative humidity', 'Mean Rainfall(mm)', 'Total Rainfall(mm)', | |
| 'Mean Fertilizer', 'Total Fertilizer', 'Mean Pesticide', | |
| 'Total Pesticide']] | |
| df[["Domain", "Item", "Unit", "Flag Description", "Element"]] = le.transform(df[["Domain", "Item", "Unit", "Flag Description", "Element"]]) | |
| X = scaler_.transform(df) | |
| df = pd.DataFrame(X, columns = X_train.columns) | |
| with open("model.pkl", "rb") as file_: | |
| model = pickle.load(file_) | |
| result = model.predict(df) | |
| file_.close() | |
| return f"Estimated Crop Yield based on Inputted Values is {abs(result[0])}" | |
| title = "Simple Gradio Interface for Estimating/Predicting crop Yield" | |
| description = " " | |
| cassava_yield = pd.read_csv("cassava_yield.csv") | |
| demo = gr.Interface(fn = make_predictions, inputs =[gr.Dropdown(choices = cassava_yield["Domain"].unique().tolist(), multiselect=False, label = "Domain"), | |
| gr.TextArea(lines = 1, label = "Year", placeholder = "Year"), | |
| gr.Dropdown(choices = cassava_yield["Item"].unique().tolist(), multiselect=False, label = "Item"), | |
| gr.Dropdown(choices = cassava_yield["Unit"].unique().tolist(), multiselect=False, label = "Unit"), | |
| gr.Dropdown(choices = cassava_yield["Flag"].unique().tolist(), multiselect=False, label = "Flag"), | |
| gr.Dropdown(choices = cassava_yield["Flag Description"].unique().tolist(), multiselect=False, label = "Flag Description"), | |
| gr.Dropdown(choices = cassava_yield["Element"].unique().tolist(), multiselect=False, label = "Element"), | |
| gr.TextArea(lines = 1, label = "Input Estimated Temperature(mean)", placeholder = "mean temp"), | |
| gr.TextArea(lines = 1, label = "Input Estimated Temperature(Total)", placeholder = "total temp"), | |
| gr.TextArea(lines = 1, label = "Input Estimated Relative Humidity(mean)", placeholder = "mean relative humidity"), | |
| gr.TextArea(lines = 1, label = "Input Estimated Relative Humidity(Total)", placeholder = "total relative humidity"), | |
| gr.TextArea(lines = 1, label = "Input Estimated Rainfall(mm)(mean)", placeholder = "mean rainfall"), | |
| gr.TextArea(lines = 1, label = "Input Estimated Rainfall(mm)(Total)", placeholder = "total rainfall"), | |
| gr.TextArea(lines = 1, label = "Input Estimated Fertilizer(mean)", placeholder = "mean fertilizer"), | |
| gr.TextArea(lines = 1, label = "Input Estimated Fertilizer(Total)", placeholder = "total fertilizer"), | |
| gr.TextArea(lines = 1, label = "Input Estimated Pesticide(mean)", placeholder = "mean pesticide"), | |
| gr.TextArea(lines = 1, label = "Input Estimated Pesticide(Total)", placeholder = "total pesticide")], | |
| outputs = gr.Textbox(label = "Result....."), | |
| title = title, | |
| description=description) | |
| demo.launch(share = False) | |
| if __name__ == "__main__": | |
| run() |