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( """
Predict product sales based on TV, Radio, and Newspaper advertising budgets using a trained XGBoost model.
""", 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)