Spaces:
Sleeping
Sleeping
File size: 6,421 Bytes
a55ae21 012e682 a55ae21 | 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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 | # load datasets
import pandas as pd
import gradio as gr
import pandas as pd
import random
import plotly.graph_objs as go
predictions = pd.read_csv("data/processed/predictions.csv")
optimal_prices = pd.read_csv("data/processed/optimal_prices.csv")
amazon = pd.read_csv("data/raw/Amazon_Sale_Report.csv")
pl = pd.read_csv("data/raw/PLMarch2021.csv")
predictions = predictions.merge(amazon[['SKU', 'Category']], on='SKU', how='left')
# rename sku column to match optimal prices
pl.rename(columns={'Sku': 'SKU'}, inplace=True)
# write and an underscore on whitespace for every column in pl dataframe
pl.columns = pl.columns.str.replace(' ', '_')
pl.Category.rename({'Kurta': 'kurta', 'Kurta Set':'kurta', 'Gown':'Ethnic Dress', 'Tops':'Set','Nill':'Top' }, inplace=True)
competitor_columns = ['Amazon_MRP', 'Flipkart_MRP', 'Ajio_MRP', 'Limeroad_MRP', 'Myntra_MRP', 'Paytm_MRP', 'Snapdeal_MRP']
from src.data.utils import Utils
# convert to int the competitor_columns in the pl dataframe
for col in competitor_columns:
pl[col] = pl[col].apply(Utils.convert_to_int)
unique_skus = sorted(predictions['SKU'].unique())
unique_category = sorted(pl['Category'].unique())
def plot_demand_curve_gradio(sku):
# Load the predictions dataframe
#predictions = pd.read_csv('../data/processed/predictions.csv')
# Select the predictions for the given SKU
sku_predictions = predictions[predictions['SKU'] == sku]
# Create a scatter plot of the predicted sales against the price points
fig = go.Figure()
fig.add_trace(go.Scatter(x=sku_predictions['price'], y=sku_predictions['predicted_sales_xgboost'], mode='lines+markers'))
# Set the plot title and axis labels
fig.update_layout(title=f'Demand Curve for SKU {sku}', xaxis_title='Price', yaxis_title='Predicted Sales')
fig.update_xaxes(title_font=dict(size=18), tickfont=dict(size=14))
fig.update_yaxes(title_font=dict(size=18), tickfont=dict(size=14))
# Set the legend font size and position
fig.update_layout(legend=dict(font=dict(size=14), yanchor='top', y=0.99, xanchor='left', x=0.01), legend_title_text='SKU')
# Set the plot background color and grid lines
fig.update_layout(plot_bgcolor='white', xaxis=dict(showgrid=True, gridcolor='lightgray'), yaxis=dict(showgrid=True, gridcolor='lightgray'))
# Return the plot as an HTML div
return gr.update(value=fig, visible=True)
def plot_optimal_prices_gradio(sku):
# Load the optimal prices dataframe
#optimal_prices = pd.read_csv('../data/processed/optimal_prices.csv')
# Select the optimal prices for the given SKU
sku_optimal_prices = optimal_prices[optimal_prices['SKU'] == sku]
# Create a bar chart of the optimal prices at different price points
fig = go.Figure()
fig.add_trace(go.Bar(x=['Base Price', '-10%', '-15%', '+10%', '+15%'], y=sku_optimal_prices['optimal_prices'], name=sku))
# Set the plot title and axis labels
fig.update_layout(title=f'Optimal Prices for SKU {sku}', xaxis_title='Price Point', yaxis_title='Optimal Price', font=dict(size=14))
fig.update_xaxes(title_font=dict(size=18), tickfont=dict(size=14))
fig.update_yaxes(title_font=dict(size=18), tickfont=dict(size=14))
# Set the plot background color and grid lines
fig.update_layout(plot_bgcolor='white', xaxis=dict(showgrid=True, gridcolor='lightgray'), yaxis=dict(showgrid=True, gridcolor='lightgray'))
# Return the plot as an HTML div
return gr.update(value=fig, visible=True)
def plot_competitors_prices_gradio(category):
sku_competitors_results = pl[pl['Category'] == category]
# Calculate average prices for each competitor within the selected category
competitor_columns = ['Amazon_MRP', 'Flipkart_MRP', 'Ajio_MRP', 'Limeroad_MRP', 'Myntra_MRP', 'Paytm_MRP', 'Snapdeal_MRP']
avg_prices = [sku_competitors_results[col].median() for col in competitor_columns]
# Create a bar chart of the average competitors prices
fig = go.Figure()
fig.add_trace(go.Bar(x=competitor_columns, y=avg_prices, name=category))
# set the plot title and axis labels
fig.update_layout(title=f'Competitors Prices for Category {category}', xaxis_title='Competitor', yaxis_title='Median Price', font=dict(size=14))
fig.update_xaxes(title_font=dict(size=18), tickfont=dict(size=14))
fig.update_yaxes(title_font=dict(size=18), tickfont=dict(size=14))
# Set the plot background color and grid lines
fig.update_layout(plot_bgcolor='white', xaxis=dict(showgrid=True, gridcolor='lightgray'), yaxis=dict(showgrid=True, gridcolor='lightgray'))
# return the plot as an HTML div
return gr.update(value=fig, visible=True)
with gr.Blocks() as demo:
gr.Markdown("""
# Relu Demand Curve & Optimal Pricing Dashboard 💰 #
Version: 1.0.0
Enter Values below then click Predict and Pricing Buttons to see results.
""")
with gr.Row(equal_height=True):
sku = gr.Dropdown(
label="Select SKU",
choices=unique_skus,
value=lambda: random.choice(unique_skus),
)
with gr.Row(equal_height=True):
with gr.Column():
plot = gr.Plot()
with gr.Row():
predict_btn = gr.Button(value="Predict")
predict_btn.click(
plot_demand_curve_gradio,
inputs=[sku],
outputs= plot
)
with gr.Column():
plot = gr.Plot()
with gr.Row():
optimal_price_btn = gr.Button(value="Optimal Price")
optimal_price_btn.click(
plot_optimal_prices_gradio,
inputs=[sku],
outputs=plot
)
# return plot_comeptitors_prices_gradio(sku) as a plot for every sku
with gr.Row(equal_height=True):
category = gr.Dropdown(
label="Select Category",
choices=unique_category,
value=lambda: random.choice(unique_category))
with gr.Column():
plot = gr.Plot()
with gr.Row():
competitor_btn = gr.Button(value="Competitors Prices")
competitor_btn.click(
plot_competitors_prices_gradio,
inputs=[category],
outputs=plot)
demo.launch(share=False)
|