SalesPrediction / src /streamlit_app.py
EnYa32's picture
Update src/streamlit_app.py
cdbc46e verified
Raw History Blame Contribute Delete
2.78 kB
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)