AyoAgbaje commited on
Commit
504ab01
·
verified ·
1 Parent(s): bac12f0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +92 -90
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
- "Item": [item_],
35
- "Unit": [unit_],
36
- "Flag": [flag_],
37
- "Flag Description": [flag_description_],
38
- "Element": [element_],
39
- "Mean temperature": [float(mean_temp_)],
40
- "Total temperature": [float(total_temp_)],
41
- "Mean relative humidity": [float(mrh_)],
42
- "Total relative humidity": [float(trh_)],
43
- "Mean Rainfall(mm)": [float(mrf_)],
44
- "Total Rainfall(mm)": [float(trf_)],
45
- "Mean Fertilizer": [float(mean_fert_)],
46
- "Total Fertilizer": [float(total_fert_)],
47
- "Mean Pesticide": [float(mean_pest_)],
48
- "Total Pesticide": [float(total_pest_)],}
49
- df = pd.DataFrame(data = dataset_dict)
50
- df = df[['Domain', 'Element', 'Item', 'Year', 'Unit', 'Flag Description',
51
- 'Mean temperature', 'Total temperature', 'Mean relative humidity',
52
- 'Total relative humidity', 'Mean Rainfall(mm)', 'Total Rainfall(mm)',
53
- 'Mean Fertilizer', 'Total Fertilizer', 'Mean Pesticide',
54
- 'Total Pesticide']]
55
- df[["Domain", "Item", "Unit", "Flag Description", "Element"]] = le.transform(df[["Domain", "Item", "Unit", "Flag Description", "Element"]])
56
- X = scaler_.transform(df)
57
- df = pd.DataFrame(X, columns = X_train.columns)
58
- with open("model.pkl", "rb") as file_:
59
- model = pickle.load(file_)
60
- result = model.predict(df)
61
- file_.close()
62
- return f"Estimated Crop Yield based on Inputted Values is {result[0]}"
63
-
64
- title = "Simple Gradio Interface for Estimating/Predicting crop Yield"
65
- description = " "
66
- cassava_yield = pd.read_csv("cassava_yield.csv")
67
- demo = gr.Interface(fn = make_predictions, inputs = [gr.Dropdown(choices = cassava_yield["Domain"].unique().tolist(), multiselect=False),
68
- gr.Dropdown(choices = cassava_yield["Item"].unique().tolist(), multiselect=False),
69
- gr.Dropdown(choices = cassava_yield["Unit"].unique().tolist(), multiselect=False),
70
- gr.Dropdown(choices = cassava_yield["Flag"].unique().tolist(), multiselect=False),
71
- gr.Dropdown(choices = cassava_yield["Flag Description"].unique().tolist(), multiselect=False),
72
- gr.Dropdown(choices = cassava_yield["Element"].unique().tolist(), multiselect=False),
73
- gr.TextArea(lines = 1, label = "Input Estimated Temperature(mean)", placeholder = "mean temp"),
74
- gr.TextArea(lines = 1, label = "Input Estimated Temperature(Total)", placeholder = "total temp"),
75
- gr.TextArea(lines = 1, label = "Input Estimated Relative Humidity(mean)", placeholder = "mean relative humidity"),
76
- gr.TextArea(lines = 1, label = "Input Estimated Relative Humidity(Total)", placeholder = "total relative humidity"),
77
- gr.TextArea(lines = 1, label = "Input Estimated Rainfall(mm)(mean)", placeholder = "mean rainfall"),
78
- gr.TextArea(lines = 1, label = "Input Estimated Rainfall(mm)(Total)", placeholder = "total rainfall"),
79
- gr.TextArea(lines = 1, label = "Input Estimated Fertilizer(mean)", placeholder = "mean fertilizer"),
80
- gr.TextArea(lines = 1, label = "Input Estimated Fertilizer(Total)", placeholder = "total fertilizer"),
81
- gr.TextArea(lines = 1, label = "Input Estimated Pesticide(mean)", placeholder = "mean pesticide"),
82
- gr.TextArea(lines = 1, label = "Input Estimated Pesticide(Total)", placeholder = "total pesticide")],
83
- outputs = gr.Textbox(label = "Result....."),
84
- title = title,
85
- description=description)
86
-
87
- demo.launch(share = False)
88
-
89
-
90
- if __name__ == "__main__":
 
 
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()