File size: 3,475 Bytes
9b6af0c
 
 
 
 
 
 
72b8c86
9b6af0c
 
83e3b88
9b6af0c
 
6423918
9b6af0c
 
 
6423918
9b6af0c
6423918
9b6af0c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
94
95
96
97
98
import gradio as gr
import matplotlib.pyplot as plt
import matplotlib
import json
import pickle as pck
import joblib
import pandas as pd
import sklearn

#reading the data base for market information
db = pd.read_csv('database.csv')

#loading the feature list
with open('features.json') as f:
   features = json.load(f)

#loading our encoder
with open(r"columnTransformer.pickle", "rb") as file:
    encoder = pck.load(file)
model = joblib.load("model.joblib")

months = {'January': 1.0, 'February': 2.0, 'March': 3.0, 'April': 4.0, 'May': 5.0, 'June': 6.0, 'July': 7.0, 'August': 8.0, 'September': 9.0, 'October': 10.0, 'November': 11.0, 'December': 12.0}

def encode(region,division,market,category, commodity,  month):
    encoded = encoder.transform([[months[month], region, division,market,category,commodity]])
    return encoded

def predict(inp, quantity):
    pred = model.predict(inp.toarray())
    pred = '{:.2f}'.format(abs(pred[0])*quantity)
    return float(pred)


def plot_fig(compared,market):
    #ploting figure for comarison
    compared.sort(key=lambda tup: tup[1])
    mk_name = [ m_name[0] for m_name in compared]
    prices =  [ price[1] for price in compared]
    colors = ["red" if i == market else "blue" for i in mk_name]
    fig = plt.figure(figsize=(15,9))
    plt.bar(mk_name, prices,width=0.5,  color=colors)
    plt.title('Price compared to other markets')
    #plt.xlabel('Markets')
    plt.xticks(rotation=45)
    plt.ylabel('Price (Fcfa)')
    return fig

def compararator(region,division,market,category, commodity,  month, quantity):
    #compare prediction for other marlkets
    db_cop = db[db.market!= market]
    samples = db_cop.sample(5)
    compared = []
    #predict for the given input
    inp = encode(region,division,market,category, commodity,  month)
    pred = predict(inp,quantity) 
    compared.append((market,pred))

    #predict for  5 random markets for price comparison
    for index, sample in samples.iterrows():
        inp = encode(sample['region'],sample['division'],sample['market'],category, commodity,  month)
        ypred = predict(inp,quantity) 
        compared.append((sample['market'],ypred))
    fig = plot_fig(compared, market)
    return pred, fig



def evaluate(region,division,market,category, commodity,  month, quantity):
    ypred,fig = compararator(region,division,market,category, commodity,  month, quantity)
    out = str(ypred) + "Fcfa for " +str(quantity)+ " (KG/L/P)"
    st = "Price of " + commodity + " in " + market + " is : " + out
    
    return st, fig 


inputs = [
    gr.Dropdown(features['regions'], label="Region"),
    gr.Dropdown(features['division'], label="Division"),
    gr.Dropdown(features['market'], label="market"),
    gr.Dropdown(features['categories'], label="category"),
    gr.Dropdown(features['commodity'], label="commodity"),
    gr.Dropdown(list(months.keys()), label="Month"),
    gr.Slider(1, 100, value=1, step=0.5, label= "quantity (Kilogram / Liters)", interactive=True),
]
outputs = ["label", gr.Plot(label="Prices in other markets").style(container=True)]
desc= "Machine learning for food price prediction in Various Cameroon markets (Retail Price)"		
camfreg = gr.Interface(
    fn=evaluate,
    inputs=inputs,
    outputs=outputs,
    cache_examples=True,
    article='KG = kilogram, L = Liter, P = piece',
     title= 'Camfreg : Cameroon food prices Prediction',
     description= desc 
)

if __name__ == "__main__":
    camfreg.launch(share=True)