File size: 5,167 Bytes
22712db 755be98 cd71796 755be98 cd71796 755be98 22712db 755be98 22712db 755be98 22712db 755be98 22712db 755be98 22712db 755be98 22712db 755be98 22712db 755be98 22712db 755be98 | 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 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 | import streamlit as st
import pandas as pd
import numpy as np
import joblib
from pricer import (
Product,
suggest_price,
freshness_factor,
format_sms,
simulate_7_days
)
# -----------------------------
# LOAD TRAINED ML MODEL
# -----------------------------
# ml_model = joblib.load("demand_model.pkl")
import joblib
from huggingface_hub import hf_hub_download
model_path = hf_hub_download(
repo_id="KB-Infinity-Tech/AIMSRICHackatonday1XGboostTrainedmodel",
filename="demand_model.pkl"
)
ml_model = joblib.load(model_path)
def ml_predict(price, age_hours, comp_avg, product_encoded):
X = pd.DataFrame([[
price,
age_hours,
comp_avg,
product_encoded
]], columns=["price", "age_hours", "comp_avg_price", "product_encoded"])
return ml_model.predict(X)[0]
# -----------------------------
# PAGE CONFIG
# -----------------------------
st.set_page_config(page_title="AI Hybrid Pricing Engine", layout="wide")
st.title("π§ AI Hybrid Retail Pricing System")
st.markdown("Rule-based + Machine Learning Dynamic Pricing Engine")
# -----------------------------
# SIDEBAR INPUTS
# -----------------------------
st.sidebar.header("π¦ Product Configuration")
sku = st.sidebar.text_input("Product SKU", "TOMATO-A")
cost = st.sidebar.number_input("Cost (RWF)", 500, 10000, 1000)
shelf_life = st.sidebar.number_input("Shelf Life (days)", 1, 14, 7)
p_ref = st.sidebar.number_input("Reference Price", 1000, 10000, 1800)
Q0 = st.sidebar.number_input("Base Demand (Q0)", 10, 500, 50)
alpha = st.sidebar.slider("Price Sensitivity (alpha)", 0.1, 5.0, 1.5)
age = st.sidebar.slider("Product Age (days)", 0.0, float(shelf_life), 3.0)
# Competitors
comp1 = st.sidebar.number_input("Competitor 1", 1000, 10000, 1600)
comp2 = st.sidebar.number_input("Competitor 2", 1000, 10000, 1700)
comp3 = st.sidebar.number_input("Competitor 3", 1000, 10000, 1900)
competitors = [comp1, comp2, comp3]
# -----------------------------
# PRODUCT OBJECT
# -----------------------------
product = Product(
sku=sku,
cost=cost,
shelf_life_days=shelf_life,
p_ref=p_ref,
Q0=Q0,
alpha=alpha
)
# -----------------------------
# RULE-BASED RESULT
# -----------------------------
result = suggest_price(product, age, competitors)
# -----------------------------
# DISPLAY METRICS
# -----------------------------
col1, col2, col3 = st.columns(3)
with col1:
st.metric("π§ Freshness", f"{result['freshness']:.3f}")
st.write(result["freshness_label"])
with col2:
st.metric("π° Rule Price", f"{result['suggested_price']:.0f} RWF")
with col3:
st.metric("π Margin %", f"{result['margin_pct']:.1f}%")
st.divider()
# -----------------------------
# SMS OUTPUT
# -----------------------------
st.subheader("π² SMS Output")
st.code(format_sms(result, "RWF"))
# -----------------------------
# FRESHNESS CURVE
# -----------------------------
st.subheader("π Freshness Decay Curve")
days = np.linspace(0, shelf_life, 50)
values = [freshness_factor(d, shelf_life) for d in days]
df_curve = pd.DataFrame({
"Day": days,
"Freshness": values
})
st.line_chart(df_curve.set_index("Day"))
# -----------------------------
# COMPETITOR VIEW
# -----------------------------
st.subheader("πͺ Market Comparison")
df_comp = pd.DataFrame({
"Entity": ["Competitor 1", "Competitor 2", "Competitor 3", "YOU (Rule)"],
"Price (RWF)": [comp1, comp2, comp3, result["suggested_price"]]
})
st.dataframe(df_comp)
# -----------------------------
# ML VS RULE COMPARISON
# -----------------------------
st.subheader("βοΈ Rule Engine vs ML Model")
comp_avg = np.mean(competitors)
product_encoded = 0
rule_profit = result["expected_daily_profit"]
ml_demand = ml_predict(
result["suggested_price"],
age * 24,
comp_avg,
product_encoded
)
ml_profit = (result["suggested_price"] - cost) * ml_demand
col1, col2 = st.columns(2)
with col1:
st.metric("π Rule Profit", f"{rule_profit:.2f} RWF")
with col2:
st.metric("π€ ML Profit", f"{ml_profit:.2f} RWF")
# -----------------------------
# PROFIT CURVE COMPARISON
# -----------------------------
st.subheader("π Profit Curve Comparison")
prices = np.linspace(500, 5000, 30)
ml_profits = []
rule_profits = []
for p in prices:
ml_d = ml_predict(p, age * 24, comp_avg, product_encoded)
ml_profits.append((p - cost) * ml_d)
rule_profits.append(result["expected_daily_profit"])
df_compare = pd.DataFrame({
"Price": prices,
"ML_Profit": ml_profits,
"Rule_Profit": rule_profits
})
st.line_chart(df_compare.set_index("Price"))
# -----------------------------
# 7-DAY SIMULATION
# -----------------------------
st.subheader("π 7-Day Simulation")
if st.button("Run Simulation"):
sim = simulate_7_days(product, competitors)
df_sim = pd.DataFrame(sim)
if "day" in df_sim.columns:
st.line_chart(df_sim.set_index("day")[["our_price", "demand"]])
st.dataframe(df_sim)
# -----------------------------
# FOOTER
# -----------------------------
st.markdown("---")
st.markdown("β‘ Hybrid AI Pricing System β Rule Engine + Machine Learning | AIMS-RIC Hackathon") |