Spaces:
Runtime error
Runtime error
nurindahpratiwi
commited on
Commit
·
926ab8b
1
Parent(s):
8e8fa12
update
Browse files
app.py
CHANGED
@@ -1,13 +1,17 @@
|
|
1 |
import pandas as pd
|
2 |
-
from transformers import pipeline
|
3 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
import datetime
|
5 |
from huggingface_hub import hf_hub_download
|
6 |
-
import joblib
|
7 |
-
import json
|
8 |
|
9 |
-
REPO_ID = "
|
10 |
-
FILENAME = "sklearn_model.joblib"
|
11 |
|
12 |
num_imputer = joblib.load(
|
13 |
hf_hub_download(repo_id=REPO_ID, filename="numerical_imputer.joblib")
|
@@ -25,69 +29,105 @@ scaler = joblib.load(
|
|
25 |
hf_hub_download(repo_id=REPO_ID, filename="scaler.joblib")
|
26 |
)
|
27 |
|
28 |
-
|
29 |
hf_hub_download(repo_id=REPO_ID, filename="Final_model.joblib")
|
30 |
)
|
31 |
|
32 |
-
|
33 |
-
|
|
|
|
|
|
|
|
|
34 |
|
35 |
-
|
36 |
-
|
|
|
37 |
|
|
|
38 |
|
39 |
-
|
|
|
|
|
|
|
40 |
input_data = {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
input_data["Partner"] = st.radio('Do you have Partner', ('Yes', 'No'))
|
47 |
-
input_data["Dependents"] = st.selectbox('Do you have any Dependents?', ('No', 'Yes'))
|
48 |
-
input_data["tenure"] = st.number_input('Lenght of tenure (no. of months with Telco)', min_value=0, max_value=90, value=1, step=1)
|
49 |
-
input_data["PhoneService"] = st.radio('Do you have PhoneService? ', ('No', 'Yes'))
|
50 |
-
input_data["MultipleLines"] = st.radio('Do you have MultipleLines', ('No', 'Yes'))
|
51 |
-
input_data["InternetService"] = st.radio('Do you have InternetService', ('DSL', 'Fiber optic', 'No'))
|
52 |
-
input_data["OnlineSecurity"] = st.radio('Do you have OnlineSecurity?', ('No', 'Yes'))
|
53 |
-
input_data["OnlineBackup"] = st.radio('Do you have OnlineBackup?', ('No', 'Yes'))
|
54 |
-
input_data["DeviceProtection"] = st.radio('Do you have DeviceProtection?', ('No', 'Yes'))
|
55 |
-
input_data["TechSupport"] = st.radio('Do you have TechSupport?', ('No', 'Yes'))
|
56 |
-
input_data["StreamingTV"] = st.radio('Do you have StreamingTV?', ('No', 'Yes'))
|
57 |
-
input_data["StreamingMovies"] = st.radio('Do you have StreamingMovies?', ('No', 'Yes'))
|
58 |
-
input_data["Contract"] = st.selectbox('which Contract do you use?', ('Month-to-month', 'One year', 'Two year'))
|
59 |
-
input_data["PaperlessBilling"] = st.radio('Do you prefer PaperlessBilling?', ('Yes', 'No'))
|
60 |
-
input_data["PaymentMethod"] = st.selectbox('Which PaymentMethod do you prefer?', ('Electronic check', 'Mailed check', 'Bank transfer (automatic)',
|
61 |
-
'Credit card (automatic)'))
|
62 |
-
input_data["MonthlyCharges"] = st.number_input("Enter monthly charges (the range should between 0-120)")
|
63 |
-
input_data["TotalCharges"] = st.number_input("Enter total charges (the range should between 0-10.000)")
|
64 |
-
st.form_submit_button('Predict', on_click=click_button)
|
65 |
-
|
66 |
-
if st.session_state.clicked:
|
67 |
input_df = pd.DataFrame([input_data])
|
68 |
-
|
69 |
-
|
|
|
70 |
cat_columns = [col for col in input_df.columns if input_df[col].dtype == 'object']
|
71 |
num_columns = [col for col in input_df.columns if input_df[col].dtype != 'object']
|
72 |
-
|
73 |
-
|
|
|
74 |
input_df_imputed_cat = cat_imputer.transform(input_df[cat_columns])
|
75 |
input_df_imputed_num = num_imputer.transform(input_df[num_columns])
|
76 |
-
|
77 |
-
|
|
|
78 |
input_encoded_df = pd.DataFrame(encoder.transform(input_df_imputed_cat).toarray(),
|
79 |
columns=encoder.get_feature_names(cat_columns))
|
80 |
-
|
81 |
-
|
82 |
input_df_scaled = scaler.transform(input_df_imputed_num)
|
83 |
input_scaled_df = pd.DataFrame(input_df_scaled , columns = num_columns)
|
84 |
-
|
85 |
-
|
86 |
final_df = pd.concat([input_encoded_df, input_scaled_df], axis=1)
|
87 |
-
|
88 |
-
|
89 |
-
prediction =
|
90 |
-
|
91 |
-
|
|
|
92 |
st.write(f"The predicted sales are: {prediction}.")
|
93 |
-
st.table(input_df)
|
|
|
1 |
import pandas as pd
|
|
|
2 |
import streamlit as st
|
3 |
+
import numpy as np
|
4 |
+
from matplotlib import pyplot as plt
|
5 |
+
import pickle
|
6 |
+
import sklearn
|
7 |
+
import joblib
|
8 |
+
from PIL import Image
|
9 |
+
import base64
|
10 |
+
from transformers import pipeline
|
11 |
import datetime
|
12 |
from huggingface_hub import hf_hub_download
|
|
|
|
|
13 |
|
14 |
+
REPO_ID = "Abubakari/Sales_Prediction"
|
|
|
15 |
|
16 |
num_imputer = joblib.load(
|
17 |
hf_hub_download(repo_id=REPO_ID, filename="numerical_imputer.joblib")
|
|
|
29 |
hf_hub_download(repo_id=REPO_ID, filename="scaler.joblib")
|
30 |
)
|
31 |
|
32 |
+
dt_model = joblib.load(
|
33 |
hf_hub_download(repo_id=REPO_ID, filename="Final_model.joblib")
|
34 |
)
|
35 |
|
36 |
+
# Add a title and subtitle
|
37 |
+
st.write("<center><h1>Sales Prediction App</h1></center>", unsafe_allow_html=True)
|
38 |
+
|
39 |
+
# Set up the layout
|
40 |
+
col1, col2, col3 = st.columns([1, 3, 3])
|
41 |
+
|
42 |
|
43 |
+
#st.image("https://www.example.com/logo.png", width=200)
|
44 |
+
# Add a subtitle or description
|
45 |
+
st.write("This app uses machine learning to predict sales based on certain input parameters. Simply enter the required information and click 'Predict' to get a sales prediction!")
|
46 |
|
47 |
+
st.subheader("Enter the details to predict sales")
|
48 |
|
49 |
+
# Add some text
|
50 |
+
#st.write("Enter some data for Prediction.")
|
51 |
+
|
52 |
+
# Create the input fields
|
53 |
input_data = {}
|
54 |
+
col1,col2 = st.columns(2)
|
55 |
+
with col1:
|
56 |
+
input_data['store_nbr'] = st.slider("store_nbr",0,54)
|
57 |
+
input_data['products'] = st.selectbox("products", ['AUTOMOTIVE', 'CLEANING', 'BEAUTY', 'FOODS', 'STATIONERY',
|
58 |
+
'CELEBRATION', 'GROCERY', 'HARDWARE', 'HOME', 'LADIESWEAR',
|
59 |
+
'LAWN AND GARDEN', 'CLOTHING', 'LIQUOR,WINE,BEER', 'PET SUPPLIES'])
|
60 |
+
input_data['onpromotion'] =st.number_input("onpromotion",step=1)
|
61 |
+
input_data['state'] = st.selectbox("state", ['Pichincha', 'Cotopaxi', 'Chimborazo', 'Imbabura',
|
62 |
+
'Santo Domingo de los Tsachilas', 'Bolivar', 'Pastaza',
|
63 |
+
'Tungurahua', 'Guayas', 'Santa Elena', 'Los Rios', 'Azuay', 'Loja',
|
64 |
+
'El Oro', 'Esmeraldas', 'Manabi'])
|
65 |
+
input_data['store_type'] = st.selectbox("store_type",['D', 'C', 'B', 'E', 'A'])
|
66 |
+
input_data['cluster'] = st.number_input("cluster",step=1)
|
67 |
+
|
68 |
+
with col2:
|
69 |
+
input_data['dcoilwtico'] = st.number_input("dcoilwtico",step=1)
|
70 |
+
input_data['year'] = st.number_input("year",step=1)
|
71 |
+
input_data['month'] = st.slider("month",1,12)
|
72 |
+
input_data['day'] = st.slider("day",1,31)
|
73 |
+
input_data['dayofweek'] = st.number_input("dayofweek,0=Sun and 6=Sat",step=1)
|
74 |
+
input_data['end_month'] = st.selectbox("end_month",['True','False'])
|
75 |
+
|
76 |
+
|
77 |
+
# Define CSS style for the download button
|
78 |
+
# Define the custom CSS
|
79 |
+
predict_button_css = """
|
80 |
+
<style>
|
81 |
+
.predict-button {
|
82 |
+
background-color: #C4C4C4;
|
83 |
+
color: gray;
|
84 |
+
padding: 0.75rem 2rem;
|
85 |
+
border-radius: 0.5rem;
|
86 |
+
border: none;
|
87 |
+
font-size: 1.1rem;
|
88 |
+
font-weight: bold;
|
89 |
+
text-align: center;
|
90 |
+
margin-top: 2rem;
|
91 |
+
}
|
92 |
+
</style>
|
93 |
+
"""
|
94 |
+
|
95 |
+
# Display the custom CSS
|
96 |
+
st.markdown(predict_button_css, unsafe_allow_html=True)
|
97 |
+
|
98 |
|
99 |
+
# Create a button to make a prediction
|
100 |
+
|
101 |
+
if st.button("Predict", key="predict_button", help="Click to make a prediction."):
|
102 |
+
# Convert the input data to a pandas DataFrame
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
103 |
input_df = pd.DataFrame([input_data])
|
104 |
+
|
105 |
+
|
106 |
+
# Selecting categorical and numerical columns separately
|
107 |
cat_columns = [col for col in input_df.columns if input_df[col].dtype == 'object']
|
108 |
num_columns = [col for col in input_df.columns if input_df[col].dtype != 'object']
|
109 |
+
|
110 |
+
|
111 |
+
# Apply the imputers
|
112 |
input_df_imputed_cat = cat_imputer.transform(input_df[cat_columns])
|
113 |
input_df_imputed_num = num_imputer.transform(input_df[num_columns])
|
114 |
+
|
115 |
+
|
116 |
+
# Encode the categorical columns
|
117 |
input_encoded_df = pd.DataFrame(encoder.transform(input_df_imputed_cat).toarray(),
|
118 |
columns=encoder.get_feature_names(cat_columns))
|
119 |
+
|
120 |
+
# Scale the numerical columns
|
121 |
input_df_scaled = scaler.transform(input_df_imputed_num)
|
122 |
input_scaled_df = pd.DataFrame(input_df_scaled , columns = num_columns)
|
123 |
+
|
124 |
+
#joining the cat encoded and num scaled
|
125 |
final_df = pd.concat([input_encoded_df, input_scaled_df], axis=1)
|
126 |
+
|
127 |
+
# Make a prediction
|
128 |
+
prediction = dt_model.predict(final_df)[0]
|
129 |
+
|
130 |
+
|
131 |
+
# Display the prediction
|
132 |
st.write(f"The predicted sales are: {prediction}.")
|
133 |
+
st.table(input_df)
|