Spaces:
Build error
Build error
File size: 4,130 Bytes
4ad29fa 7e2021f 4ad29fa feb88c3 95fe281 f8624bb 5c3958a 95fe281 689acbb 5c3958a f8624bb 689acbb 95fe281 4ad29fa 689acbb 4ad29fa 689acbb 4ad29fa 689acbb 4ad29fa 689acbb 4ad29fa |
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 |
import streamlit as st
import pandas as pd
import numpy as np
from datetime import datetime, timedelta
import faiss
from groq import Groq
import os
from dotenv import load_dotenv # Import dotenv
# Load environment variables from .env file
load_dotenv()
# Fetch the API key from the environment variable
api_key = os.getenv("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")
# Debugging line to verify the API key is loaded
st.write("Loaded API Key:", api_key)
if api_key:
st.success("Groq API key loaded successfully.")
client = Groq(api_key=api_key)
else:
st.warning("Groq API key is not set. Features requiring Groq API will be disabled.")
client = None
# App title
st.title("Optimized EV Charging Scheduling in Microgrids")
# Upload Excel file
st.sidebar.header("Upload Data File")
uploaded_file = st.sidebar.file_uploader("Upload Excel File", type=["xlsx", "xls"])
if uploaded_file:
data = pd.ExcelFile(uploaded_file)
try:
# Load data from sheets
historical_data = data.parse("Historical Data")
renewable_forecast = data.parse("Renewable Energy Forecast")
ev_profiles = data.parse("EV Charging Profiles")
# Standardize column names
historical_data.columns = historical_data.columns.str.strip()
renewable_forecast.columns = renewable_forecast.columns.str.strip()
ev_profiles.columns = ev_profiles.columns.str.strip()
# Rename specific columns if necessary
historical_data.rename(columns={"DATE": "Date", "date": "Date"}, inplace=True)
renewable_forecast.rename(columns={"DATE": "Date", "date": "Date"}, inplace=True)
ev_profiles.rename(columns={"DATE": "Date", "date": "Date"}, inplace=True)
# Parameters
rated_grid_capacity = 3500 # kW
disconnect_threshold = 3200 # kW
# Generate optimized schedule
def optimize_schedule(historical, renewable, profiles):
combined_data = pd.merge(
historical, renewable, on=["Date", "Time"], how="inner"
)
combined_data = pd.merge(
combined_data, profiles, on=["Date"], how="inner"
)
# Initialize schedule
schedule = []
total_load = 0
for _, row in combined_data.iterrows():
ev_load = row["State of Charge (kWh)"]
solar_load = row["Solar Load (kW)"]
total_load += ev_load - solar_load
if total_load > disconnect_threshold:
schedule.append("DISCONNECT")
elif total_load > rated_grid_capacity:
schedule.append("V2G ENABLED")
else:
schedule.append("ALLOW")
combined_data["Charging Action"] = schedule
return combined_data
# Generate schedule
st.subheader("Optimized EV Charging Schedule")
schedule_df = optimize_schedule(historical_data, renewable_forecast, ev_profiles)
st.write(schedule_df)
# Download optimized schedule
st.download_button(
"Download Schedule",
data=schedule_df.to_csv(index=False),
file_name="optimized_schedule.csv",
mime="text/csv",
)
# Simulate Groq API
if client:
st.subheader("Groq AI Insights")
prompt = st.text_input("Ask Groq for insights (e.g., grid stability)")
if prompt:
try:
response = client.chat.completions.create(
messages=[{"role": "user", "content": prompt}],
model="llama-3.3-70b-versatile",
)
st.write(response.choices[0].message.content)
except Exception as e:
st.error(f"Error communicating with Groq API: {e}")
else:
st.warning("Groq API key is missing. Insights feature is disabled.")
except Exception as e:
st.error(f"Error parsing file: {e}")
else:
st.warning("Please upload a file to proceed.")
|