File size: 6,213 Bytes
504ab01
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eef8d64
504ab01
 
 
 
 
 
 
 
 
b747d26
504ab01
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a80cb88
504ab01
 
 
 
d88af70
6a23f01
d88af70
 
 
 
 
504ab01
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bac12f0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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()