Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
from datetime import datetime, timedelta
|
| 5 |
+
import faiss
|
| 6 |
+
from groq import Groq
|
| 7 |
+
import os
|
| 8 |
+
from dotenv import load_dotenv # Import dotenv
|
| 9 |
+
|
| 10 |
+
# Load environment variables from .env file
|
| 11 |
+
load_dotenv()
|
| 12 |
+
|
| 13 |
+
# Fetch the API key from the environment variable
|
| 14 |
+
api_key = os.getenv("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")
|
| 15 |
+
|
| 16 |
+
# Debugging line to verify the API key is loaded
|
| 17 |
+
st.write("Loaded API Key:", api_key)
|
| 18 |
+
|
| 19 |
+
if api_key:
|
| 20 |
+
st.success("Groq API key loaded successfully.")
|
| 21 |
+
client = Groq(api_key=api_key)
|
| 22 |
+
else:
|
| 23 |
+
st.warning("Groq API key is not set. Features requiring Groq API will be disabled.")
|
| 24 |
+
client = None
|
| 25 |
+
|
| 26 |
+
# App title
|
| 27 |
+
st.title("Optimized EV Charging Scheduling in Microgrids")
|
| 28 |
+
|
| 29 |
+
# Upload Excel file
|
| 30 |
+
st.sidebar.header("Upload Data File")
|
| 31 |
+
uploaded_file = st.sidebar.file_uploader("Upload Excel File", type=["xlsx", "xls"])
|
| 32 |
+
|
| 33 |
+
if uploaded_file:
|
| 34 |
+
data = pd.ExcelFile(uploaded_file)
|
| 35 |
+
|
| 36 |
+
try:
|
| 37 |
+
# Load data from sheets
|
| 38 |
+
historical_data = data.parse("Historical Data")
|
| 39 |
+
renewable_forecast = data.parse("Renewable Energy Forecast")
|
| 40 |
+
ev_profiles = data.parse("EV Charging Profiles")
|
| 41 |
+
|
| 42 |
+
# Standardize column names
|
| 43 |
+
historical_data.columns = historical_data.columns.str.strip()
|
| 44 |
+
renewable_forecast.columns = renewable_forecast.columns.str.strip()
|
| 45 |
+
ev_profiles.columns = ev_profiles.columns.str.strip()
|
| 46 |
+
|
| 47 |
+
# Rename specific columns if necessary
|
| 48 |
+
historical_data.rename(columns={"DATE": "Date", "date": "Date"}, inplace=True)
|
| 49 |
+
renewable_forecast.rename(columns={"DATE": "Date", "date": "Date"}, inplace=True)
|
| 50 |
+
ev_profiles.rename(columns={"DATE": "Date", "date": "Date"}, inplace=True)
|
| 51 |
+
|
| 52 |
+
# Parameters
|
| 53 |
+
rated_grid_capacity = 3500 # kW
|
| 54 |
+
disconnect_threshold = 3200 # kW
|
| 55 |
+
|
| 56 |
+
# Generate optimized schedule
|
| 57 |
+
def optimize_schedule(historical, renewable, profiles):
|
| 58 |
+
combined_data = pd.merge(
|
| 59 |
+
historical, renewable, on=["Date", "Time"], how="inner"
|
| 60 |
+
)
|
| 61 |
+
combined_data = pd.merge(
|
| 62 |
+
combined_data, profiles, on=["Date"], how="inner"
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
# Initialize schedule
|
| 66 |
+
schedule = []
|
| 67 |
+
total_load = 0
|
| 68 |
+
|
| 69 |
+
for _, row in combined_data.iterrows():
|
| 70 |
+
ev_load = row["State of Charge (kWh)"]
|
| 71 |
+
solar_load = row["Solar Load (kW)"]
|
| 72 |
+
total_load += ev_load - solar_load
|
| 73 |
+
|
| 74 |
+
if total_load > disconnect_threshold:
|
| 75 |
+
schedule.append("DISCONNECT")
|
| 76 |
+
elif total_load > rated_grid_capacity:
|
| 77 |
+
schedule.append("V2G ENABLED")
|
| 78 |
+
else:
|
| 79 |
+
schedule.append("ALLOW")
|
| 80 |
+
|
| 81 |
+
combined_data["Charging Action"] = schedule
|
| 82 |
+
return combined_data
|
| 83 |
+
|
| 84 |
+
# Generate schedule
|
| 85 |
+
st.subheader("Optimized EV Charging Schedule")
|
| 86 |
+
schedule_df = optimize_schedule(historical_data, renewable_forecast, ev_profiles)
|
| 87 |
+
st.write(schedule_df)
|
| 88 |
+
|
| 89 |
+
# Download optimized schedule
|
| 90 |
+
st.download_button(
|
| 91 |
+
"Download Schedule",
|
| 92 |
+
data=schedule_df.to_csv(index=False),
|
| 93 |
+
file_name="optimized_schedule.csv",
|
| 94 |
+
mime="text/csv",
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
+
# Simulate Groq API
|
| 98 |
+
if client:
|
| 99 |
+
st.subheader("Groq AI Insights")
|
| 100 |
+
prompt = st.text_input("Ask Groq for insights (e.g., grid stability)")
|
| 101 |
+
if prompt:
|
| 102 |
+
try:
|
| 103 |
+
response = client.chat.completions.create(
|
| 104 |
+
messages=[{"role": "user", "content": prompt}],
|
| 105 |
+
model="llama-3.3-70b-versatile",
|
| 106 |
+
)
|
| 107 |
+
st.write(response.choices[0].message.content)
|
| 108 |
+
except Exception as e:
|
| 109 |
+
st.error(f"Error communicating with Groq API: {e}")
|
| 110 |
+
else:
|
| 111 |
+
st.warning("Groq API key is missing. Insights feature is disabled.")
|
| 112 |
+
|
| 113 |
+
except Exception as e:
|
| 114 |
+
st.error(f"Error parsing file: {e}")
|
| 115 |
+
|
| 116 |
+
else:
|
| 117 |
+
st.warning("Please upload a file to proceed.")
|