Camfreg / app.py
Jnior's picture
up
6d60a3b
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()