Spaces:
Sleeping
Sleeping
Download src/streamlit_app.py from EnYa32/SalesPrediction: direct link, hf CLI and curl.
- Browser
- Download file 2.78 kB
-
https://huggingface.co/spaces/EnYa32/SalesPrediction/resolve/main/src/streamlit_app.py
- Command line
-
hf download hf://spaces/EnYa32/SalesPrediction/src/streamlit_app.py
-
curl -L -o streamlit_app.py https://huggingface.co/spaces/EnYa32/SalesPrediction/resolve/main/src/streamlit_app.py
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 | |
| # --------------------------- | |
| 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) |