Spaces:
Sleeping
Sleeping
File size: 2,778 Bytes
e984f45 9ba173e e07942d fcf8e28 9ba173e 56855e3 9ba173e fcf8e28 9ba173e fcf8e28 9ba173e fcf8e28 cdbc46e fcf8e28 9ba173e fcf8e28 9ba173e fcf8e28 9ba173e fcf8e28 9ba173e fcf8e28 e07942d fcf8e28 e07942d fcf8e28 1042b01 | 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 | import streamlit as st
import numpy as np
import pandas as pd
from xgboost import Booster, DMatrix
import plotly.express as px
# ---------------------------
# 1. Load Model
# ---------------------------
@st.cache_resource
def load_model():
booster = Booster()
booster.load_model("src/xgb_model.json")
return booster
model = load_model()
# ---------------------------
# Streamlit UI โ Modern Version
# ---------------------------
st.set_page_config(page_title="Advertising Sales Prediction", layout="wide")
# Sidebar
st.sidebar.title("๐ Advertising Sales Prediction")
st.sidebar.write("This app uses a trained **XGBoost Regression Model** to predict product sales based on advertising budgets.")
if st.sidebar.button("Try Example Values"):
tv = 150
radio = 20
newspaper = 30
st.sidebar.markdown("---")
st.sidebar.write("๐ Model deployed on HuggingFace Spaces")
st.sidebar.write("๐ป Created by Enes Yamac")
# Main Title
st.markdown(
"""
<h1 style='text-align: center; color: #333;'>Advertising Sales Prediction</h1>
<p style='text-align: center; color: grey; font-size:18px;'>
Predict product sales based on <b>TV</b>, <b>Radio</b>, and <b>Newspaper</b> advertising budgets using a trained XGBoost model.
</p>
""",
unsafe_allow_html=True,
)
st.subheader("Enter Advertising Budgets")
# Input columns
col1, col2, col3 = st.columns(3)
with col1:
tv = st.number_input("๐บ TV Budget", min_value=0.0, max_value=400.0, value=150.0, step=1.0)
with col2:
radio = st.number_input("๐ป Radio Budget", min_value=0.0, max_value=60.0, value=20.0, step=0.5)
with col3:
newspaper = st.number_input("๐ฐ Newspaper Budget", min_value=0.0, max_value=120.0, value=30.0, step=0.5)
# Prediction
if st.button("๐ฎ Predict Sales"):
input_df = pd.DataFrame({
"Newspaper": [newspaper],
"Radio": [radio],
"TV": [tv],
})
dmatrix = DMatrix(input_df, feature_names=["Newspaper", "Radio", "TV"])
prediction = float(model.predict(dmatrix)[0])
st.success(f"๐ **Predicted Sales: {prediction:.2f} units**")
# Plotly visualization
st.markdown("### ๐ Visualization")
# Create synthetic range to show TV impact
x_vals = np.linspace(0, 300, 50)
demo_df = pd.DataFrame({
"Newspaper": [newspaper] * 50,
"Radio": [radio] * 50,
"TV": x_vals,
})
demo_dmatrix = DMatrix(demo_df, feature_names=["Newspaper", "Radio", "TV"])
y_vals = model.predict(demo_dmatrix)
fig = px.line(
x=x_vals,
y=y_vals,
labels={"x": "TV Budget", "y": "Predicted Sales"},
title="Predicted Sales as TV Budget Changes",
line_shape="spline"
)
st.plotly_chart(fig, use_container_width=True) |