Spaces:
Runtime error
Runtime error
Azie88
commited on
Commit
·
7bb59cd
1
Parent(s):
ea5b3f1
model
Browse files- app.py +137 -0
- toolkit/model.joblib +3 -0
app.py
ADDED
@@ -0,0 +1,137 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
import streamlit as st
|
3 |
+
import numpy as np
|
4 |
+
import sklearn
|
5 |
+
import joblib
|
6 |
+
import plotly.express as px
|
7 |
+
|
8 |
+
|
9 |
+
pipeline = joblib.load('toolkit/Pipeline.joblib')
|
10 |
+
model = joblib.load('toolkit/model.joblib')
|
11 |
+
|
12 |
+
df = pd.read_csv('Dataset/predicted_data_access_consumption_2015_2029.csv')
|
13 |
+
|
14 |
+
|
15 |
+
st.set_page_config(
|
16 |
+
page_title='Electricity Access Prediction App',
|
17 |
+
page_icon=':cityscape:'
|
18 |
+
)
|
19 |
+
|
20 |
+
|
21 |
+
|
22 |
+
# Add a title and subtitle
|
23 |
+
st.write("<center><h1>Electricity Access Prediction App ⚡</h1></center>", unsafe_allow_html=True)
|
24 |
+
|
25 |
+
# Sidebar navigation
|
26 |
+
page = st.sidebar.selectbox("Select Page", ["Predicted Dataset", "Prediction"])
|
27 |
+
|
28 |
+
|
29 |
+
|
30 |
+
if page == "Predicted Dataset":
|
31 |
+
# Add a section for displaying the preloaded DataFrame
|
32 |
+
st.subheader("Predicted Dataset")
|
33 |
+
|
34 |
+
# Display the DataFrame
|
35 |
+
st.write("Actual and predicted values of our dataset (2015-2029) from the IMF:")
|
36 |
+
st.dataframe(df)
|
37 |
+
|
38 |
+
# Add a selectbox to choose a country
|
39 |
+
country = st.selectbox("Select a Country", df['Country'].unique())
|
40 |
+
|
41 |
+
# Filter the DataFrame based on the selected country
|
42 |
+
filtered_df = df[df['Country'] == country]
|
43 |
+
|
44 |
+
# Display the filtered DataFrame
|
45 |
+
st.write(f"Data for {country}:")
|
46 |
+
st.dataframe(filtered_df)
|
47 |
+
|
48 |
+
st.subheader(f"Access to Electricity in {country} Over Time")
|
49 |
+
fig = px.line(filtered_df, x='Year', y='Predicted_Access_to_electricity', title=f'Access to Electricity in {country} Over Time', markers=True)
|
50 |
+
st.plotly_chart(fig)
|
51 |
+
|
52 |
+
|
53 |
+
|
54 |
+
|
55 |
+
|
56 |
+
elif page == "Prediction":
|
57 |
+
|
58 |
+
st.write("This app uses machine learning to predict access to electricity based on certain input parameters. Simply enter the required information and click 'Predict' to get a prediction!")
|
59 |
+
st.subheader("Enter the details to predict access to electricity by % of population")
|
60 |
+
|
61 |
+
st.sidebar.write("""
|
62 |
+
## Data Input Explanation
|
63 |
+
This app predicts access to electricity based on the following input parameters:
|
64 |
+
- **Year**: The year for which the prediction is being made.
|
65 |
+
- **Income Group**: The income classification of the country (Low income, Lower middle income, Upper middle income, High income).
|
66 |
+
- **Population**: The total population of the country.
|
67 |
+
- **GDP per capita (USD)**: The Gross Domestic Product per capita in current US dollars.
|
68 |
+
- **Inflation (annual %)**: The annual inflation rate.
|
69 |
+
- **Consumption (kWh per capita)**: The electricity consumption per capita in kilowatt-hours.
|
70 |
+
""")
|
71 |
+
|
72 |
+
# Set up the layout
|
73 |
+
col1, col2 = st.columns(2)
|
74 |
+
|
75 |
+
# Create the input fields
|
76 |
+
input_data = {}
|
77 |
+
with col1:
|
78 |
+
input_data['Year'] = st.number_input("Input Year", min_value=1990, max_value=2030, step=1)
|
79 |
+
input_data['IncomeGroup'] = st.radio("Pick an income group", ["Lower middle income", "Upper middle income", "High income", "Low income"])
|
80 |
+
input_data['Population'] = st.slider("Population of the country", min_value=9000, max_value=10000000000)
|
81 |
+
|
82 |
+
with col2:
|
83 |
+
input_data['GDP_per_capita_USD'] = st.slider("Enter GDP per capita (Current $)", min_value=20.0, max_value=250000.0, step=0.1)
|
84 |
+
input_data['Inflation_annual_percent'] = st.slider("Enter Consumer Price Index", min_value=-20.0, max_value=25000.0, step=0.1)
|
85 |
+
input_data['Consumption (kWh per capita)'] = st.slider("Enter Electricity Consumption", min_value=10.0, max_value=55000.0, step=0.1)
|
86 |
+
|
87 |
+
# Define the custom CSS
|
88 |
+
predict_button_css = """
|
89 |
+
<style>
|
90 |
+
.predict-button {
|
91 |
+
background-color: #C4C4C4;
|
92 |
+
color: gray;
|
93 |
+
padding: 0.75rem 2rem;
|
94 |
+
border-radius: 0.5rem;
|
95 |
+
border: none;
|
96 |
+
font-size: 1.1rem;
|
97 |
+
font-weight: bold;
|
98 |
+
text-align: center;
|
99 |
+
margin-top: 2rem;
|
100 |
+
}
|
101 |
+
</style>
|
102 |
+
"""
|
103 |
+
|
104 |
+
# Display the custom CSS
|
105 |
+
st.markdown(predict_button_css, unsafe_allow_html=True)
|
106 |
+
|
107 |
+
# Create a button to make a prediction
|
108 |
+
if st.button("Predict", key="predict_button", help="Click to make a prediction."):
|
109 |
+
# Convert the input data to a pandas DataFrame
|
110 |
+
input_df = pd.DataFrame([input_data])
|
111 |
+
|
112 |
+
# Selecting categorical and numerical columns separately
|
113 |
+
numerical = input_df.select_dtypes(include=[np.number]).columns.tolist()
|
114 |
+
categorical = input_df.select_dtypes(exclude=[np.number]).columns.tolist()
|
115 |
+
|
116 |
+
# Fit and transform the data
|
117 |
+
X_processed = pipeline.transform(input_df)
|
118 |
+
|
119 |
+
# Extracting feature names for numerical columns
|
120 |
+
num_feature_names = numerical
|
121 |
+
|
122 |
+
# Extracting feature names for categorical columns after one-hot encoding
|
123 |
+
cat_encoder = pipeline.named_steps['preprocessor'].named_transformers_['cat'].named_steps['onehot']
|
124 |
+
cat_feature_names = cat_encoder.get_feature_names_out(categorical)
|
125 |
+
|
126 |
+
# Concatenating numerical and categorical feature names
|
127 |
+
feature_names = num_feature_names + list(cat_feature_names)
|
128 |
+
|
129 |
+
# Convert X_processed to DataFrame
|
130 |
+
final_df = pd.DataFrame(X_processed, columns=feature_names)
|
131 |
+
|
132 |
+
# Make a prediction
|
133 |
+
prediction = model.predict(final_df)[0]
|
134 |
+
|
135 |
+
# Display the prediction
|
136 |
+
st.write(f"The predicted Access to electricity is: {prediction}.")
|
137 |
+
st.table(input_df)
|
toolkit/model.joblib
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:58bddbe34894d41a3350643f5ed79fe4f9b1959d65c507f4884d1ca4ae49df50
|
3 |
+
size 2552428
|