Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +67 -23
src/streamlit_app.py
CHANGED
|
@@ -1,7 +1,8 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import numpy as np
|
| 3 |
import pandas as pd
|
| 4 |
-
from xgboost import Booster, DMatrix
|
|
|
|
| 5 |
|
| 6 |
# ---------------------------
|
| 7 |
# 1. Load Model
|
|
@@ -16,40 +17,83 @@ def load_model():
|
|
| 16 |
model = load_model()
|
| 17 |
|
| 18 |
# ---------------------------
|
| 19 |
-
#
|
| 20 |
# ---------------------------
|
| 21 |
|
| 22 |
-
st.set_page_config(page_title="Advertising Sales Prediction", layout="
|
| 23 |
|
| 24 |
-
|
| 25 |
-
st.
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
)
|
| 29 |
|
| 30 |
st.subheader("Enter Advertising Budgets")
|
| 31 |
|
|
|
|
| 32 |
col1, col2, col3 = st.columns(3)
|
| 33 |
|
| 34 |
with col1:
|
| 35 |
-
tv = st.number_input("TV
|
|
|
|
| 36 |
with col2:
|
| 37 |
-
radio = st.number_input("Radio
|
|
|
|
| 38 |
with col3:
|
| 39 |
-
newspaper = st.number_input("Newspaper
|
| 40 |
-
|
| 41 |
-
if st.button("Predict Sales"):
|
| 42 |
-
# DataFrame mit exakt den Feature-Namen wie im Training
|
| 43 |
-
input_df = pd.DataFrame(
|
| 44 |
-
{
|
| 45 |
-
"Newspaper": [newspaper],
|
| 46 |
-
"Radio": [radio],
|
| 47 |
-
"TV": [tv],
|
| 48 |
-
}
|
| 49 |
-
)
|
| 50 |
|
| 51 |
-
# DMatrix aus dem DataFrame โ Namen stimmen jetzt
|
| 52 |
-
dmatrix = DMatrix(input_df, feature_names=["Newspaper", "Radio", "TV"])
|
| 53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
prediction = float(model.predict(dmatrix)[0])
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import numpy as np
|
| 3 |
import pandas as pd
|
| 4 |
+
from xgboost import Booster, DMatrix
|
| 5 |
+
import plotly.express as px
|
| 6 |
|
| 7 |
# ---------------------------
|
| 8 |
# 1. Load Model
|
|
|
|
| 17 |
model = load_model()
|
| 18 |
|
| 19 |
# ---------------------------
|
| 20 |
+
# Streamlit UI โ Modern Version
|
| 21 |
# ---------------------------
|
| 22 |
|
| 23 |
+
st.set_page_config(page_title="Advertising Sales Prediction", layout="wide")
|
| 24 |
|
| 25 |
+
# Sidebar
|
| 26 |
+
st.sidebar.title("๐ Advertising Sales Prediction")
|
| 27 |
+
st.sidebar.write("This app uses a trained **XGBoost Regression Model** to predict product sales based on advertising budgets.")
|
| 28 |
+
|
| 29 |
+
if st.sidebar.button("Try Example Values"):
|
| 30 |
+
tv = 150
|
| 31 |
+
radio = 20
|
| 32 |
+
newspaper = 30
|
| 33 |
+
|
| 34 |
+
st.sidebar.markdown("---")
|
| 35 |
+
st.sidebar.write("๐ Model deployed on HuggingFace Spaces")
|
| 36 |
+
st.sidebar.write("๐ป Created by Enes")
|
| 37 |
+
|
| 38 |
+
# Main Title
|
| 39 |
+
st.markdown(
|
| 40 |
+
"""
|
| 41 |
+
<h1 style='text-align: center; color: #333;'>Advertising Sales Prediction</h1>
|
| 42 |
+
<p style='text-align: center; color: grey; font-size:18px;'>
|
| 43 |
+
Predict product sales based on <b>TV</b>, <b>Radio</b>, and <b>Newspaper</b> advertising budgets using a trained XGBoost model.
|
| 44 |
+
</p>
|
| 45 |
+
""",
|
| 46 |
+
unsafe_allow_html=True,
|
| 47 |
)
|
| 48 |
|
| 49 |
st.subheader("Enter Advertising Budgets")
|
| 50 |
|
| 51 |
+
# Input columns
|
| 52 |
col1, col2, col3 = st.columns(3)
|
| 53 |
|
| 54 |
with col1:
|
| 55 |
+
tv = st.number_input("๐บ TV Budget", min_value=0.0, max_value=400.0, value=150.0, step=1.0)
|
| 56 |
+
|
| 57 |
with col2:
|
| 58 |
+
radio = st.number_input("๐ป Radio Budget", min_value=0.0, max_value=60.0, value=20.0, step=0.5)
|
| 59 |
+
|
| 60 |
with col3:
|
| 61 |
+
newspaper = st.number_input("๐ฐ Newspaper Budget", min_value=0.0, max_value=120.0, value=30.0, step=0.5)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
|
|
|
|
|
|
| 63 |
|
| 64 |
+
# Prediction
|
| 65 |
+
if st.button("๐ฎ Predict Sales"):
|
| 66 |
+
input_df = pd.DataFrame({
|
| 67 |
+
"Newspaper": [newspaper],
|
| 68 |
+
"Radio": [radio],
|
| 69 |
+
"TV": [tv],
|
| 70 |
+
})
|
| 71 |
+
|
| 72 |
+
dmatrix = DMatrix(input_df, feature_names=["Newspaper", "Radio", "TV"])
|
| 73 |
prediction = float(model.predict(dmatrix)[0])
|
| 74 |
+
|
| 75 |
+
st.success(f"๐ **Predicted Sales: {prediction:.2f} units**")
|
| 76 |
+
|
| 77 |
+
# Plotly visualization
|
| 78 |
+
st.markdown("### ๐ Visualization")
|
| 79 |
+
|
| 80 |
+
# Create synthetic range to show TV impact
|
| 81 |
+
x_vals = np.linspace(0, 300, 50)
|
| 82 |
+
demo_df = pd.DataFrame({
|
| 83 |
+
"Newspaper": [newspaper] * 50,
|
| 84 |
+
"Radio": [radio] * 50,
|
| 85 |
+
"TV": x_vals,
|
| 86 |
+
})
|
| 87 |
+
|
| 88 |
+
demo_dmatrix = DMatrix(demo_df, feature_names=["Newspaper", "Radio", "TV"])
|
| 89 |
+
y_vals = model.predict(demo_dmatrix)
|
| 90 |
+
|
| 91 |
+
fig = px.line(
|
| 92 |
+
x=x_vals,
|
| 93 |
+
y=y_vals,
|
| 94 |
+
labels={"x": "TV Budget", "y": "Predicted Sales"},
|
| 95 |
+
title="Predicted Sales as TV Budget Changes",
|
| 96 |
+
line_shape="spline"
|
| 97 |
+
)
|
| 98 |
+
|
| 99 |
+
st.plotly_chart(fig, use_container_width=True)
|