Update app.py
Browse files
app.py
CHANGED
|
@@ -1,115 +1,60 @@
|
|
| 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
|
| 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 |
-
# Initialize schedule
|
| 64 |
-
schedule = []
|
| 65 |
-
total_load = 0
|
| 66 |
-
|
| 67 |
-
for _, row in combined_data.iterrows():
|
| 68 |
-
ev_load = row["State of Charge (kWh)"]
|
| 69 |
-
solar_load = row["Solar Load (kW)"]
|
| 70 |
-
total_load += ev_load - solar_load
|
| 71 |
-
|
| 72 |
-
if total_load > disconnect_threshold:
|
| 73 |
-
schedule.append("DISCONNECT")
|
| 74 |
-
elif total_load > rated_grid_capacity:
|
| 75 |
-
schedule.append("V2G ENABLED")
|
| 76 |
-
else:
|
| 77 |
-
schedule.append("ALLOW")
|
| 78 |
-
|
| 79 |
-
combined_data["Charging Action"] = schedule
|
| 80 |
-
return combined_data
|
| 81 |
-
|
| 82 |
-
# Generate schedule
|
| 83 |
-
st.subheader("Optimized EV Charging Schedule")
|
| 84 |
-
schedule_df = optimize_schedule(historical_data, renewable_forecast, ev_profiles)
|
| 85 |
-
st.write(schedule_df)
|
| 86 |
-
|
| 87 |
-
# Download optimized schedule
|
| 88 |
-
st.download_button(
|
| 89 |
-
"Download Schedule",
|
| 90 |
-
data=schedule_df.to_csv(index=False),
|
| 91 |
-
file_name="optimized_schedule.csv",
|
| 92 |
-
mime="text/csv",
|
| 93 |
-
)
|
| 94 |
-
|
| 95 |
-
# Simulate Groq API
|
| 96 |
-
if client:
|
| 97 |
-
st.subheader("Groq AI Insights")
|
| 98 |
-
prompt = st.text_input("Ask Groq for insights (e.g., grid stability)")
|
| 99 |
-
if prompt:
|
| 100 |
-
try:
|
| 101 |
-
response = client.chat.completions.create(
|
| 102 |
-
messages=[{"role": "user", "content": prompt}],
|
| 103 |
-
model="llama-3.3-70b-versatile",
|
| 104 |
-
)
|
| 105 |
-
st.write(response.choices[0].message.content)
|
| 106 |
-
except Exception as e:
|
| 107 |
-
st.error(f"Error communicating with Groq API: {e}")
|
| 108 |
-
else:
|
| 109 |
-
st.warning("Groq API key is missing. Insights feature is disabled.")
|
| 110 |
-
|
| 111 |
-
except Exception as e:
|
| 112 |
-
st.error(f"Error parsing file: {e}")
|
| 113 |
-
|
| 114 |
-
else:
|
| 115 |
-
st.warning("Please upload a file to proceed.")
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
import numpy as np
|
|
|
|
| 4 |
import faiss
|
|
|
|
| 5 |
import os
|
| 6 |
+
from groq import Groq
|
| 7 |
+
from sklearn.preprocessing import StandardScaler
|
| 8 |
+
|
| 9 |
+
# Initialize Groq client
|
| 10 |
+
client = Groq(api_key=os.environ.get("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"))
|
| 11 |
+
|
| 12 |
+
# Function to upload and process the Excel file
|
| 13 |
+
def process_data(uploaded_file):
|
| 14 |
+
df = pd.read_excel(uploaded_file)
|
| 15 |
+
return df
|
| 16 |
+
|
| 17 |
+
# Function to generate embeddings and store in FAISS DB
|
| 18 |
+
def generate_embeddings(df):
|
| 19 |
+
# Placeholder: actual embedding generation would depend on the model you use
|
| 20 |
+
embeddings = np.random.rand(len(df), 128).astype('float32') # Random embeddings for illustration
|
| 21 |
+
index = faiss.IndexFlatL2(128)
|
| 22 |
+
index.add(embeddings)
|
| 23 |
+
return index
|
| 24 |
+
|
| 25 |
+
# Function to display charging schedules
|
| 26 |
+
def display_schedule():
|
| 27 |
+
st.title("Optimized EV Charging Schedule")
|
| 28 |
+
uploaded_file = st.file_uploader("Upload your Excel data", type=["xlsx"])
|
| 29 |
+
|
| 30 |
+
if uploaded_file:
|
| 31 |
+
# Process the data
|
| 32 |
+
df = process_data(uploaded_file)
|
| 33 |
+
st.write("Uploaded Data Preview", df.head())
|
| 34 |
+
|
| 35 |
+
# Generate embeddings and store in FAISS
|
| 36 |
+
index = generate_embeddings(df)
|
| 37 |
+
st.write("Embeddings generated and stored in FAISS.")
|
| 38 |
+
|
| 39 |
+
# Placeholder for optimized schedule
|
| 40 |
+
st.write("Here is your optimized EV charging schedule based on the historical data.")
|
| 41 |
+
|
| 42 |
+
# Displaying the RAG (Red, Amber, Green) status for grid stability
|
| 43 |
+
st.write("Current Grid Status: Green (Stable)")
|
| 44 |
+
|
| 45 |
+
# Sidebar for additional inputs from the user
|
| 46 |
+
def user_inputs():
|
| 47 |
+
with st.sidebar:
|
| 48 |
+
st.header("User Inputs")
|
| 49 |
+
arrival_time = st.time_input("Arrival Time at EV Charging Station")
|
| 50 |
+
state_of_charge = st.slider("State of Charge (SOC) in kWh", 0, 50, 7)
|
| 51 |
+
battery_capacity = st.number_input("EV Battery Capacity (kWh)", 30, 100, 50)
|
| 52 |
+
st.button("Generate Charging Schedule")
|
| 53 |
+
|
| 54 |
+
# Main function
|
| 55 |
+
def main():
|
| 56 |
+
user_inputs()
|
| 57 |
+
display_schedule()
|
| 58 |
+
|
| 59 |
+
if __name__ == "__main__":
|
| 60 |
+
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|