Jnior commited on
Commit
9b6af0c
1 Parent(s): 151610c

adding main

Browse files
Files changed (1) hide show
  1. app.py +97 -0
app.py ADDED
@@ -0,0 +1,97 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import matplotlib.pyplot as plt
3
+ import matplotlib
4
+ import json
5
+ import pickle as pck
6
+ import joblib
7
+ import pandas as pd
8
+
9
+ #reading the data base for market information
10
+ db = pd.read_csv('/content/database.csv')
11
+
12
+ #loading the feature list
13
+ with open('/content/features.json') as f:
14
+ features = json.load(f)
15
+
16
+ #loading our encoder
17
+ with open(r"/content/columnTransformer.pickle", "rb") as file:
18
+ encoder = pck.load(file)
19
+ model = joblib.load("/content/model.joblib")
20
+
21
+ 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}
22
+
23
+ def encode(region,division,market,category, commodity, month):
24
+ encoded = encoder.transform([[months[month], region, division,market,category,commodity]])
25
+ return encoded
26
+
27
+ def predict(inp, quantity):
28
+ pred = model.predict(inp.toarray())
29
+ pred = '{:.2f}'.format(abs(pred[0])*quantity)
30
+ return float(pred)
31
+
32
+
33
+ def plot_fig(compared,market):
34
+ #ploting figure for comarison
35
+ compared.sort(key=lambda tup: tup[1])
36
+ mk_name = [ m_name[0] for m_name in compared]
37
+ prices = [ price[1] for price in compared]
38
+ colors = ["red" if i == market else "blue" for i in mk_name]
39
+ fig = plt.figure(figsize=(15,9))
40
+ plt.bar(mk_name, prices,width=0.5, color=colors)
41
+ plt.title('Price compared to other markets')
42
+ #plt.xlabel('Markets')
43
+ plt.xticks(rotation=45)
44
+ plt.ylabel('Price (Fcfa)')
45
+ return fig
46
+
47
+ def compararator(region,division,market,category, commodity, month, quantity):
48
+ #compare prediction for other marlkets
49
+ db_cop = db[db.market!= market]
50
+ samples = db_cop.sample(5)
51
+ compared = []
52
+ #predict for the given input
53
+ inp = encode(region,division,market,category, commodity, month)
54
+ pred = predict(inp,quantity)
55
+ compared.append((market,pred))
56
+
57
+ #predict for 5 random markets for price comparison
58
+ for index, sample in samples.iterrows():
59
+ inp = encode(sample['region'],sample['division'],sample['market'],category, commodity, month)
60
+ ypred = predict(inp,quantity)
61
+ compared.append((sample['market'],ypred))
62
+ fig = plot_fig(compared, market)
63
+ return pred, fig
64
+
65
+
66
+
67
+ def evaluate(region,division,market,category, commodity, month, quantity):
68
+ ypred,fig = compararator(region,division,market,category, commodity, month, quantity)
69
+ out = str(ypred) + "Fcfa for " +str(quantity)+ " (KG/L/P)"
70
+ st = "Price of " + commodity + " in " + market + " is : " + out
71
+
72
+ return st, fig
73
+
74
+
75
+ inputs = [
76
+ gr.Dropdown(features['regions'], label="Region"),
77
+ gr.Dropdown(features['division'], label="Division"),
78
+ gr.Dropdown(features['market'], label="market"),
79
+ gr.Dropdown(features['categories'], label="category"),
80
+ gr.Dropdown(features['commodity'], label="commodity"),
81
+ gr.Dropdown(list(months.keys()), label="Month"),
82
+ gr.Slider(1, 100, value=1, step=0.5, label= "quantity (Kilogram / Liters)", interactive=True),
83
+ ]
84
+ outputs = ["label", gr.Plot(label="Prices in other markets").style(container=True)]
85
+ desc= "Machine learning for food price prediction in Various Cameroon markets (Retail Price)"
86
+ camfreg = gr.Interface(
87
+ fn=evaluate,
88
+ inputs=inputs,
89
+ outputs=outputs,
90
+ cache_examples=True,
91
+ article='KG = kilogram, L = Liter, P = piece',
92
+ title= 'Camfreg : Cameroon food prices Prediction',
93
+ description= desc
94
+ )
95
+
96
+ if __name__ == "__main__":
97
+ camfreg.launch(share=True)