energy-redux / prompts.txt
jethrovic's picture
Add 3 files
daef116 verified
i want to make a beautiful dashboard for household savings using the api call to get the data and a visual to choose time range and the second file to clclualte teh most interesting points and crunch data before sending key points to open ai and a small caht bot on the side to ask more questions import streamlit as st import requests import pandas as pd import openai # Geocode an address using OpenStreetMap Nominatim def geocode_address(address): url = "https://nominatim.openstreetmap.org/search" params = {"q": address, "format": "json"} headers = {"User-Agent": "streamlit-app"} resp = requests.get(url, params=params, headers=headers) resp.raise_for_status() results = resp.json() if not results: return None, None lon = float(results[0]["lon"]) lat = float(results[0]["lat"]) return lon, lat # Call Palmetto BEM API with correct parameters def get_bem_data(palmetto_key, lon, lat): url = "https://ei.palmetto.com/api/v0/bem/calculate" payload = { "location": {"longitude": lon, "latitude": lat}, "parameters": { "from_datetime": "2025-03-19T00:00:00", "to_datetime": "2025-04-21T00:00:00", "interval_format": "long", "group_by": "hour", "clip_by": "inner", "variables": "all_non_zero" } } headers = { "accept": "application/json", "content-type": "application/json", "X-API-Key": palmetto_key } resp = requests.post(url, json=payload, headers=headers) resp.raise_for_status() return resp.json() # Analyze BEM output with OpenAI to recommend strategies def analyze_with_openai(openai_key, bem_json): openai.api_key = openai_key # Extract monthly consumption series intervals = bem_json.get("data", {}).get("intervals", []) if not intervals: return "No interval data to analyze." # Format prompt prompt = ( "You are an energy consultant. Given the following monthly energy consumption data for a residential building, " "provide list of appliances both present and absent and 5 concise, practical strategies to reduce energy usage.\n\n" f"Monthly Consumption:\n{intervals}\n\n" "Strategies:" ) # Use new OpenAI v1 interface for chat completions response = openai.chat.completions.create( model="gpt-4o", messages=[{"role": "user", "content": prompt}], max_tokens=3000 ) return response.choices[0].message.content.strip() # Streamlit UI def main(): st.title("🏠 Residential Energy Analysis & Recommendations") palmetto_key = st.text_input("Palmetto API Key", type="password") openai_key = st.text_input("OpenAI API Key", type="password") address = st.text_input("Enter your street address:") if st.button("Analyze Energy Data"): if not all([palmetto_key, openai_key, address]): st.error("Please provide your Palmetto key, OpenAI key, and an address.") return # Geocode try: lon, lat = geocode_address(address) except Exception as e: st.error(f"Geocoding error: {e}") return if lon is None: st.error("Could not geocode the provided address.") return st.success(f"Location: {address} β†’ Lon: {lon:.5f}, Lat: {lat:.5f}") # Fetch BEM data try: bem = get_bem_data(palmetto_key, lon, lat) except Exception as e: st.error(f"BEM API error: {e}") return # Display monthly consumption intervals = bem.get("data", {}).get("intervals", []) if intervals: df = pd.DataFrame(intervals) st.subheader("Monthly Energy Consumption (kWh)") st.dataframe(df) else: st.warning("No monthly data returned. Consider requesting additional variables.") # Generate AI-driven recommendations try: recommendations = analyze_with_openai(openai_key, bem) st.subheader("Recommended Energy-Saving Strategies") st.markdown(recommendations) except Exception as e: st.error(f"OpenAI analysis error: {e}") if __name__ == "__main__": main()import streamlit as st import pandas as pd import matplotlib.pyplot as plt import seaborn as sns import numpy as np import plotly.express as px import plotly.graph_objects as go from plotly.subplots import make_subplots # Set page configuration st.set_page_config( page_title="Energy Consumption Dashboard", page_icon="⚑", layout="wide" ) # Title and instructions st.title("⚑ Energy Consumption Dashboard") st.markdown("Upload your energy consumption CSV file to visualize and analyze your energy usage patterns.") # File uploader uploaded_file = st.file_uploader("Choose a CSV file", type="csv") # Function to process and analyze data def analyze_energy_data(df): # Basic data cleaning and preparation # Convert datetime strings to datetime objects df['from_datetime'] = pd.to_datetime(df['from_datetime']) df['to_datetime'] = pd.to_datetime(df['to_datetime']) # Create a date column for easier grouping (just the date part) df['date'] = df['from_datetime'].dt.date # Extract hour for time-of-day analysis df['hour'] = df['from_datetime'].dt.hour # Categorize variables all_vars = df['variable'].unique() electricity_vars = [var for var in all_vars if 'electricity' in var] fossil_fuel_vars = [var for var in all_vars if 'fossil_fuel' in var] thermal_vars = [var for var in all_vars if 'thermal' in var] grid_vars = [var for var in all_vars if 'grid' in var] cost_vars = [var for var in all_vars if 'costs' in var] emission_vars = [var for var in all_vars if 'emissions' in var] return df, electricity_vars, fossil_fuel_vars, thermal_vars, grid_vars, cost_vars, emission_vars # Function to display dashboard def create_dashboard(df, electricity_vars, fossil_fuel_vars, thermal_vars, grid_vars, cost_vars, emission_vars): # Data overview st.subheader("πŸ“Š Data Overview") # Key metrics in columns col1, col2, col3, col4 = st.columns(4) with col1: st.metric("Date Range", f"{df['from_datetime'].min().date()} to {df['to_datetime'].max().date()}") with col2: st.metric("Total Records", f"{len(df):,}") with col3: st.metric("Variables", f"{df['variable'].nunique()}") with col4: # Calculate total electricity consumption if 'consumption.electricity' in df['variable'].values: total_elec = df[df['variable'] == 'consumption.electricity']['value'].sum() st.metric("Total Electricity", f"{total_elec:.2f} kWh") else: st.metric("Total Electricity", "N/A") # Tabs for different analyses tab1, tab2, tab3, tab4 = st.tabs(["Consumption Overview", "Time Patterns", "Detailed Analysis", "Raw Data"]) ################################# # TAB 1: CONSUMPTION OVERVIEW ################################# with tab1: st.subheader("Energy Consumption by Category") # Total consumption by category category_totals = df.groupby('variable')['value'].sum().sort_values(ascending=False) # Filter for visualization (top categories) top_categories = category_totals.head(10) # Create bar chart using Plotly fig = px.bar( x=top_categories.values, y=top_categories.index, orientation='h', title="Top 10 Energy Consumption Categories", labels={'x': 'Total Consumption', 'y': 'Category'} ) st.plotly_chart(fig) # Component breakdown st.subheader("Electricity Consumption by Component") # Get specific electricity components elec_components = [var for var in electricity_vars if len(var.split('.')) > 2] if elec_components: component_data = df[df['variable'].isin(elec_components)].groupby('variable')['value'].sum() # Create pie chart fig = px.pie( values=component_data.values, names=component_data.index, title="Electricity Consumption by Component" ) st.plotly_chart(fig) else: st.info("No detailed electricity component data available") ################################# # TAB 2: TIME PATTERNS ################################# with tab2: st.subheader("Energy Consumption Over Time") # Daily consumption patterns main_categories = ['consumption.electricity', 'consumption.fossil_fuel'] available_categories = [cat for cat in main_categories if cat in df['variable'].values] if available_categories: daily_consumption = df[df['variable'].isin(available_categories)].groupby(['date', 'variable'])['value'].sum().reset_index() # Create line chart for daily patterns fig = px.line( daily_consumption, x='date', y='value', color='variable', title="Daily Energy Consumption", labels={'value': 'Consumption', 'date': 'Date', 'variable': 'Energy Type'} ) st.plotly_chart(fig) # Hourly patterns st.subheader("Hourly Energy Patterns") hourly_data = df[df['variable'].isin(available_categories)].groupby(['hour', 'variable'])['value'].mean().reset_index() # Create line chart for hourly patterns fig = px.line( hourly_data, x='hour', y='value', color='variable', title="Average Hourly Consumption", labels={'value': 'Average Consumption', 'hour': 'Hour of Day', 'variable': 'Energy Type'} ) fig.update_xaxes(tickvals=list(range(0, 24))) st.plotly_chart(fig) # Heatmap st.subheader("Electricity Consumption Heatmap") if 'consumption.electricity' in df['variable'].values: elec_hourly = df[df['variable'] == 'consumption.electricity'].copy() elec_pivot = elec_hourly.pivot_table(index='date', columns='hour', values='value', aggfunc='mean') # Create heatmap fig = px.imshow( elec_pivot, labels=dict(x="Hour of Day", y="Date", color="Consumption"), x=elec_pivot.columns, y=elec_pivot.index, color_continuous_scale='YlOrRd' ) st.plotly_chart(fig) else: st.info("No main category data available for time-based analysis") ################################# # TAB 3: DETAILED ANALYSIS ################################# with tab3: st.subheader("Statistical Summary") # Statistical summary for main consumption categories if available_categories: stats_cols = st.columns(len(available_categories)) for i, category in enumerate(available_categories): with stats_cols[i]: category_data = df[df['variable'] == category]['value'] st.subheader(f"{category.split('.')[-1].capitalize()}") st.metric("Mean", f"{category_data.mean():.3f}") st.metric("Median", f"{category_data.median():.3f}") st.metric("Min", f"{category_data.min():.3f}") st.metric("Max", f"{category_data.max():.3f}") st.metric("Std Dev", f"{category_data.std():.3f}") # Correlation analysis st.subheader("Correlation Analysis") if len(available_categories) > 1: # Prepare data for correlation pivot_data = df[df['variable'].isin(available_categories)].pivot_table( index=['from_datetime'], columns='variable', values='value' ).reset_index() correlation = pivot_data[available_categories].corr() # Create heatmap for correlation fig = px.imshow( correlation, text_auto=True, labels=dict(x="Category", y="Category", color="Correlation"), x=correlation.columns, y=correlation.index, color_continuous_scale='RdBu_r', range_color=[-1, 1] ) st.plotly_chart(fig) # Emissions and costs st.subheader("Emissions and Costs") metrics = ['costs', 'emissions'] available_metrics = [m for m in metrics if m in df['variable'].values] if available_metrics: metrics_data = df[df['variable'].isin(available_metrics)].groupby(['date', 'variable'])['value'].sum().reset_index() # Create bar chart fig = px.bar( metrics_data, x='date', y='value', color='variable', barmode='group', title="Daily Costs vs Emissions", labels={'value': 'Amount', 'date': 'Date', 'variable': 'Metric'} ) st.plotly_chart(fig) ################################# # TAB 4: RAW DATA ################################# with tab4: st.subheader("Raw Data") # Filter options st.markdown("### Filter Data") # Variable filter all_vars = sorted(df['variable'].unique()) selected_vars = st.multiselect("Select Variables", all_vars, default=all_vars[:5] if len(all_vars) > 5 else all_vars) # Date range filter date_range = st.date_input( "Select Date Range", value=(df['from_datetime'].min().date(), df['to_datetime'].max().date()), min_value=df['from_datetime'].min().date(), max_value=df['to_datetime'].max().date() ) # Apply filters filtered_df = df.copy() if selected_vars: filtered_df = filtered_df[filtered_df['variable'].isin(selected_vars)] if len(date_range) == 2: start_date, end_date = date_range filtered_df = filtered_df[(filtered_df['date'] >= start_date) & (filtered_df['date'] <= end_date)] # Display filtered data st.dataframe(filtered_df) # Download link for filtered data csv = filtered_df.to_csv(index=False) st.download_button( label="Download Filtered Data as CSV", data=csv, file_name="filtered_energy_data.csv", mime="text/csv" ) # Main logic if uploaded_file is not None: # Load and process the data df = pd.read_csv(uploaded_file) # Check if the file has the expected structure required_columns = ['from_datetime', 'to_datetime', 'variable', 'value'] if all(col in df.columns for col in required_columns): # Process the data df, electricity_vars, fossil_fuel_vars, thermal_vars, grid_vars, cost_vars, emission_vars = analyze_energy_data(df) # Create the dashboard create_dashboard(df, electricity_vars, fossil_fuel_vars, thermal_vars, grid_vars, cost_vars, emission_vars) else: st.error("The uploaded file doesn't have the required columns. Please ensure it has: from_datetime, to_datetime, variable, and value.") else: # Sample data message st.info("πŸ‘† Upload a CSV file to begin. Your file should contain columns: from_datetime, to_datetime, variable, and value.") # Example format st.markdown(""" ### Expected CSV Format: ``` ,from_datetime,to_datetime,variable,value 0,2025-04-19T00:00:00,2025-04-19T01:00:00,consumption.electricity.refrigerator,0.0474 1,2025-04-19T01:00:00,2025-04-19T02:00:00,consumption.electricity.refrigerator,0.0462 ... ``` """)
make it use search to find address ingore premium user and fake data api call veruy important