Create an application that has a text field to enter a URL (default value: /kaggle/input/electric-power-consumption-data-set). The app should read the CSV file (and cache it) from the URL and display a DataFrame. give me a app code for this dataset and which i give input like killowatt/hour and the give me predicted killowatt/hour next year electricity consumption prediction.in this dataset the target column is Global active power(kwh)
Drop files here
or click to upload
import streamlit as st
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from datetime import datetime, timedelta
st.title("Power Consumption Prediction")
# Input URL
url = st.text_input(
"Enter CSV URL",
value="/kaggle/input/electric-power-consumption-data-set",
key="url_input"
)
@st.cache_data
def load_data(url):
try:
# Add error handling for the URL
if url.startswith("/kaggle"):
st.error("Please provide a valid URL. The Kaggle path is not accessible directly.")
return None
df = pd.read_csv(url, sep=';', parse_dates=['Date Time'])
return df
except Exception as e:
st.error(f"Error loading data: {str(e)}")
return None
# Load and prepare data
if url:
df = load_data(url)
if df is not None:
st.subheader("Data Preview")
st.dataframe(df.head())
# Prepare data for prediction
df['Hour'] = df['Date Time'].dt.hour
df['DayOfWeek'] = df['Date Time'].dt.dayofweek
df['Month'] = df['Date Time'].dt.month
# Features for prediction
features = ['Hour', 'DayOfWeek', 'Month']
X = df[features]
y = df['Global_active_power']
# Train model
model = LinearRegression()
model.fit(X, y)
# Input for prediction
st.subheader("Enter Current Power Consumption Details")
current_hour = st.number_input("Hour (0-23)", min_value=0, max_value=23, value=datetime.now().hour)
current_day = st.number_input("Day of Week (0=Monday, 6=Sunday)", min_value=0, max_value=6, value=datetime.now().weekday())
current_month = st.number_input("Month (1-12)", min_value=1, max_value=12, value=datetime.now().month)
if st.button("Predict Next Year's Consumption"):
# Create input data for prediction
input_data = pd.DataFrame({
'Hour': [current_hour],
'DayOfWeek': [current_day],
'Month': [current_month]
})
# Make prediction
prediction = model.predict(input_data)[0]
st.success(f"Predicted power consumption for next year at this time: {prediction:.2f} kilowatts")
# Show additional insights
st.subheader("Historical Statistics")
stats_df = df.groupby('Month')['Global_active_power'].agg(['mean', 'min', 'max']).round(2)
stats_df.columns = ['Average kW', 'Minimum kW', 'Maximum kW']
st.write("Monthly Power Consumption Statistics:")
st.table(stats_df)
# Plot monthly trends
st.subheader("Monthly Trends")
monthly_avg = df.groupby('Month')['Global_active_power'].mean()
st.line_chart(monthly_avg)
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?