Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,91 +1,93 @@
|
|
| 1 |
-
import numpy as np
|
| 2 |
-
import pandas as pd
|
| 3 |
-
import seaborn as sns
|
| 4 |
-
import matplotlib.pyplot as plt
|
| 5 |
-
plt.style.use("fivethirtyeight")
|
| 6 |
-
import sklearn
|
| 7 |
-
from sklearn.tree import DecisionTreeRegressor
|
| 8 |
-
from sklearn.ensemble import RandomForestRegressor, StackingRegressor
|
| 9 |
-
from sklearn.preprocessing import FunctionTransformer, MinMaxScaler, StandardScaler, LabelEncoder, OrdinalEncoder
|
| 10 |
-
from sklearn.model_selection import train_test_split
|
| 11 |
-
import xgboost
|
| 12 |
-
|
| 13 |
-
import tqdm
|
| 14 |
-
from tqdm.auto import trange, tqdm
|
| 15 |
-
import pickle
|
| 16 |
-
|
| 17 |
-
import gradio as gr
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
def run():
|
| 21 |
-
def make_predictions(domain_, item_, unit_, flag_, flag_description_, element_, mean_temp_, total_temp_, mrh_, trh_, mrf_, trf_, mean_fert_, total_fert_, mean_pest_, total_pest_):
|
| 22 |
-
X_train = pd.read_csv("xtrain.csv")
|
| 23 |
-
X = X_train.copy()
|
| 24 |
-
le = OrdinalEncoder()
|
| 25 |
-
le.fit(X_train[["Domain", "Item", "Unit", "Flag Description", "Element"]])
|
| 26 |
-
X_train[["Domain", "Item", "Unit", "Flag Description", "Element"]] = le.transform(X_train[["Domain", "Item", "Unit", "Flag Description", "Element"]])
|
| 27 |
-
|
| 28 |
-
scaler_ = MinMaxScaler(feature_range=(0, 1))
|
| 29 |
-
scaler_.fit(X_train)
|
| 30 |
-
X_train = scaler_.transform(X_train)
|
| 31 |
-
|
| 32 |
-
X_train = pd.DataFrame(X_train, columns = X.columns)
|
| 33 |
-
dataset_dict = {"Domain": [domain_],
|
| 34 |
-
"
|
| 35 |
-
"
|
| 36 |
-
"
|
| 37 |
-
"Flag
|
| 38 |
-
"
|
| 39 |
-
"
|
| 40 |
-
"
|
| 41 |
-
"
|
| 42 |
-
"
|
| 43 |
-
"
|
| 44 |
-
"
|
| 45 |
-
"
|
| 46 |
-
"
|
| 47 |
-
"
|
| 48 |
-
"
|
| 49 |
-
|
| 50 |
-
df =
|
| 51 |
-
|
| 52 |
-
'
|
| 53 |
-
'
|
| 54 |
-
'Total Pesticide'
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
gr.Dropdown(choices = cassava_yield["
|
| 70 |
-
gr.Dropdown(choices = cassava_yield["
|
| 71 |
-
gr.Dropdown(choices = cassava_yield["
|
| 72 |
-
gr.Dropdown(choices = cassava_yield["
|
| 73 |
-
gr.
|
| 74 |
-
gr.
|
| 75 |
-
gr.TextArea(lines = 1, label = "Input Estimated
|
| 76 |
-
gr.TextArea(lines = 1, label = "Input Estimated
|
| 77 |
-
gr.TextArea(lines = 1, label = "Input Estimated
|
| 78 |
-
gr.TextArea(lines = 1, label = "Input Estimated
|
| 79 |
-
gr.TextArea(lines = 1, label = "Input Estimated
|
| 80 |
-
gr.TextArea(lines = 1, label = "Input Estimated
|
| 81 |
-
gr.TextArea(lines = 1, label = "Input Estimated
|
| 82 |
-
gr.TextArea(lines = 1, label = "Input Estimated
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
|
|
|
|
|
|
| 91 |
run()
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import seaborn as sns
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
+
plt.style.use("fivethirtyeight")
|
| 6 |
+
import sklearn
|
| 7 |
+
from sklearn.tree import DecisionTreeRegressor
|
| 8 |
+
from sklearn.ensemble import RandomForestRegressor, StackingRegressor
|
| 9 |
+
from sklearn.preprocessing import FunctionTransformer, MinMaxScaler, StandardScaler, LabelEncoder, OrdinalEncoder
|
| 10 |
+
from sklearn.model_selection import train_test_split
|
| 11 |
+
import xgboost
|
| 12 |
+
|
| 13 |
+
import tqdm
|
| 14 |
+
from tqdm.auto import trange, tqdm
|
| 15 |
+
import pickle
|
| 16 |
+
|
| 17 |
+
import gradio as gr
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def run():
|
| 21 |
+
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_):
|
| 22 |
+
X_train = pd.read_csv("xtrain.csv")
|
| 23 |
+
X = X_train.copy()
|
| 24 |
+
le = OrdinalEncoder()
|
| 25 |
+
le.fit(X_train[["Domain", "Item", "Unit", "Flag Description", "Element"]])
|
| 26 |
+
X_train[["Domain", "Item", "Unit", "Flag Description", "Element"]] = le.transform(X_train[["Domain", "Item", "Unit", "Flag Description", "Element"]])
|
| 27 |
+
|
| 28 |
+
scaler_ = MinMaxScaler(feature_range=(0, 1))
|
| 29 |
+
scaler_.fit(X_train)
|
| 30 |
+
X_train = scaler_.transform(X_train)
|
| 31 |
+
|
| 32 |
+
X_train = pd.DataFrame(X_train, columns = X.columns)
|
| 33 |
+
dataset_dict = {"Domain": [domain_],
|
| 34 |
+
"Year": [year_],
|
| 35 |
+
"Item": [item_],
|
| 36 |
+
"Unit": [unit_],
|
| 37 |
+
"Flag": [flag_],
|
| 38 |
+
"Flag Description": [flag_description_],
|
| 39 |
+
"Element": [element_],
|
| 40 |
+
"Mean temperature": [float(mean_temp_)],
|
| 41 |
+
"Total temperature": [float(total_temp_)],
|
| 42 |
+
"Mean relative humidity": [float(mrh_)],
|
| 43 |
+
"Total relative humidity": [float(trh_)],
|
| 44 |
+
"Mean Rainfall(mm)": [float(mrf_)],
|
| 45 |
+
"Total Rainfall(mm)": [float(trf_)],
|
| 46 |
+
"Mean Fertilizer": [float(mean_fert_)],
|
| 47 |
+
"Total Fertilizer": [float(total_fert_)],
|
| 48 |
+
"Mean Pesticide": [float(mean_pest_)],
|
| 49 |
+
"Total Pesticide": [float(total_pest_)],}
|
| 50 |
+
df = pd.DataFrame(data = dataset_dict)
|
| 51 |
+
df = df[['Domain', 'Element', 'Item', 'Year', 'Unit', 'Flag Description',
|
| 52 |
+
'Mean temperature', 'Total temperature', 'Mean relative humidity',
|
| 53 |
+
'Total relative humidity', 'Mean Rainfall(mm)', 'Total Rainfall(mm)',
|
| 54 |
+
'Mean Fertilizer', 'Total Fertilizer', 'Mean Pesticide',
|
| 55 |
+
'Total Pesticide']]
|
| 56 |
+
df[["Domain", "Item", "Unit", "Flag Description", "Element"]] = le.transform(df[["Domain", "Item", "Unit", "Flag Description", "Element"]])
|
| 57 |
+
X = scaler_.transform(df)
|
| 58 |
+
df = pd.DataFrame(X, columns = X_train.columns)
|
| 59 |
+
with open("model.pkl", "rb") as file_:
|
| 60 |
+
model = pickle.load(file_)
|
| 61 |
+
result = model.predict(df)
|
| 62 |
+
file_.close()
|
| 63 |
+
return f"Estimated Crop Yield based on Inputted Values is {result[0]}"
|
| 64 |
+
|
| 65 |
+
title = "Simple Gradio Interface for Estimating/Predicting crop Yield"
|
| 66 |
+
description = " "
|
| 67 |
+
cassava_yield = pd.read_csv("cassava_yield.csv")
|
| 68 |
+
demo = gr.Interface(fn = make_predictions, inputs =[gr.Dropdown(choices = cassava_yield["Domain"].unique().tolist(), multiselect=False),
|
| 69 |
+
gr.Dropdown(choices = cassava_yield["Domain"].unique().tolist(), multiselect=False),
|
| 70 |
+
gr.Dropdown(choices = cassava_yield["Item"].unique().tolist(), multiselect=False),
|
| 71 |
+
gr.Dropdown(choices = cassava_yield["Unit"].unique().tolist(), multiselect=False),
|
| 72 |
+
gr.Dropdown(choices = cassava_yield["Flag"].unique().tolist(), multiselect=False),
|
| 73 |
+
gr.Dropdown(choices = cassava_yield["Flag Description"].unique().tolist(), multiselect=False),
|
| 74 |
+
gr.Dropdown(choices = cassava_yield["Element"].unique().tolist(), multiselect=False),
|
| 75 |
+
gr.TextArea(lines = 1, label = "Input Estimated Temperature(mean)", placeholder = "mean temp"),
|
| 76 |
+
gr.TextArea(lines = 1, label = "Input Estimated Temperature(Total)", placeholder = "total temp"),
|
| 77 |
+
gr.TextArea(lines = 1, label = "Input Estimated Relative Humidity(mean)", placeholder = "mean relative humidity"),
|
| 78 |
+
gr.TextArea(lines = 1, label = "Input Estimated Relative Humidity(Total)", placeholder = "total relative humidity"),
|
| 79 |
+
gr.TextArea(lines = 1, label = "Input Estimated Rainfall(mm)(mean)", placeholder = "mean rainfall"),
|
| 80 |
+
gr.TextArea(lines = 1, label = "Input Estimated Rainfall(mm)(Total)", placeholder = "total rainfall"),
|
| 81 |
+
gr.TextArea(lines = 1, label = "Input Estimated Fertilizer(mean)", placeholder = "mean fertilizer"),
|
| 82 |
+
gr.TextArea(lines = 1, label = "Input Estimated Fertilizer(Total)", placeholder = "total fertilizer"),
|
| 83 |
+
gr.TextArea(lines = 1, label = "Input Estimated Pesticide(mean)", placeholder = "mean pesticide"),
|
| 84 |
+
gr.TextArea(lines = 1, label = "Input Estimated Pesticide(Total)", placeholder = "total pesticide")],
|
| 85 |
+
outputs = gr.Textbox(label = "Result....."),
|
| 86 |
+
title = title,
|
| 87 |
+
description=description)
|
| 88 |
+
|
| 89 |
+
demo.launch(share = False)
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
if __name__ == "__main__":
|
| 93 |
run()
|