Spaces:
Running
Running
File size: 4,593 Bytes
866cee0 36ff46c 866cee0 0ca6b56 866cee0 0571917 866cee0 36ff46c 866cee0 36ff46c 866cee0 36ff46c 866cee0 36ff46c 866cee0 36ff46c 866cee0 36ff46c 866cee0 36ff46c 866cee0 36ff46c 866cee0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
import streamlit as st
import json
import os
from utils import load_and_process_data, create_time_series_plot, display_statistics, call_api
if 'api_token' not in st.session_state:
st.session_state.api_token = os.getenv('NILM_API_TOKEN')
page_id = 2
if 'current_page' not in st.session_state:
st.session_state.current_page = page_id
elif st.session_state.current_page != page_id:
# Clear API response when switching to this page
if 'api_response' in st.session_state:
st.session_state.api_response = None
# Update current page
st.session_state.current_page = page_id
# Initialize session state variables
if 'current_file' not in st.session_state:
st.session_state.current_file = None
if 'json_data' not in st.session_state:
st.session_state.json_data = None
if 'api_response' not in st.session_state:
st.session_state.api_response = None
if 'using_default_file' not in st.session_state:
st.session_state.using_default_file = True
st.title("Long Term Energy Consumption Forecasting")
st.markdown("""
This service provides long-term forecasting of energy consumption patterns.
Upload your historical consumption data to generate predictions for extended periods.
### Features
- Hourly consumption forecasting
- Interactive visualizations
- Statistical analysis of predictions
""")
# Default file path
default_file_path = "samples/2_long_term_consumption.json" # Adjust this path to your default file
# File upload and processing
uploaded_file = st.file_uploader("Upload JSON file (or use default)", type=['json'])
# Load default file if no file is uploaded and using_default_file is True
if uploaded_file is None and st.session_state.using_default_file:
if os.path.exists(default_file_path):
st.info(f"Using default file: {default_file_path}")
with open(default_file_path, 'r') as f:
file_contents = f.read()
if st.session_state.current_file != file_contents:
st.session_state.current_file = file_contents
st.session_state.json_data = json.loads(file_contents)
else:
st.warning(f"Default file not found at: {default_file_path}")
st.session_state.using_default_file = False
# If a file is uploaded, process it
if uploaded_file:
st.session_state.using_default_file = False
try:
file_contents = uploaded_file.read()
st.session_state.current_file = file_contents
st.session_state.json_data = json.loads(file_contents)
except Exception as e:
st.error(f"Error processing file: {str(e)}")
# Process and display data if available
if st.session_state.json_data:
try:
dfs = load_and_process_data(st.session_state.json_data)
if dfs:
st.header("Input Data")
tabs = st.tabs(["Visualization", "Raw JSON", "Statistics"])
with tabs[0]:
for unit, df in dfs.items():
st.plotly_chart(create_time_series_plot(df, unit), use_container_width=True)
with tabs[1]:
st.json(st.session_state.json_data)
with tabs[2]:
display_statistics(dfs)
if st.button("Generate Long Term Forecast"):
if not st.session_state.api_token:
st.error("Please enter your API token in the sidebar first.")
else:
with st.spinner("Generating long-term forecast..."):
st.session_state.api_response = call_api(
st.session_state.current_file,
st.session_state.api_token,
"inference_consumption_long_term"
)
except Exception as e:
st.error(f"Error processing data: {str(e)}")
# Display API results
if st.session_state.api_response:
st.header("Forecast Results")
tabs = st.tabs(["Visualization", "Raw JSON", "Statistics"])
with tabs[0]:
response_dfs = load_and_process_data(
st.session_state.api_response,
input_data=st.session_state.json_data
)
if response_dfs:
if 'Celsius' in response_dfs:
del response_dfs['Celsius']
for unit, df in response_dfs.items():
st.plotly_chart(create_time_series_plot(df, unit), use_container_width=True)
with tabs[1]:
st.json(st.session_state.api_response)
with tabs[2]:
if response_dfs:
display_statistics(response_dfs) |