Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,12 +11,13 @@ from dotenv import load_dotenv # Import dotenv
|
|
| 11 |
load_dotenv()
|
| 12 |
|
| 13 |
# Check for API key environment variable
|
| 14 |
-
api_key = os.getenv("
|
| 15 |
|
| 16 |
-
if
|
| 17 |
-
st.error("Groq API key is not set. Please configure it in your .env file.")
|
| 18 |
-
else:
|
| 19 |
client = Groq(api_key=api_key)
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
# App title
|
| 22 |
st.title("Optimized EV Charging Scheduling in Microgrids")
|
|
@@ -28,22 +29,21 @@ uploaded_file = st.sidebar.file_uploader("Upload Excel File", type=["xlsx", "xls
|
|
| 28 |
if uploaded_file:
|
| 29 |
data = pd.ExcelFile(uploaded_file)
|
| 30 |
|
| 31 |
-
# Load data from sheets
|
| 32 |
try:
|
|
|
|
| 33 |
historical_data = data.parse("Historical Data")
|
| 34 |
renewable_forecast = data.parse("Renewable Energy Forecast")
|
| 35 |
ev_profiles = data.parse("EV Charging Profiles")
|
| 36 |
|
| 37 |
-
#
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
st.write(ev_profiles)
|
| 47 |
|
| 48 |
# Parameters
|
| 49 |
rated_grid_capacity = 3500 # kW
|
|
@@ -91,14 +91,20 @@ if uploaded_file:
|
|
| 91 |
)
|
| 92 |
|
| 93 |
# Simulate Groq API
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
|
| 103 |
except Exception as e:
|
| 104 |
st.error(f"Error parsing file: {e}")
|
|
|
|
| 11 |
load_dotenv()
|
| 12 |
|
| 13 |
# Check for API key environment variable
|
| 14 |
+
api_key = os.getenv("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")
|
| 15 |
|
| 16 |
+
if api_key:
|
|
|
|
|
|
|
| 17 |
client = Groq(api_key=api_key)
|
| 18 |
+
else:
|
| 19 |
+
client = None # Set client to None if API key is not available
|
| 20 |
+
st.warning("Groq API key is not set. Features requiring Groq API will be disabled.")
|
| 21 |
|
| 22 |
# App title
|
| 23 |
st.title("Optimized EV Charging Scheduling in Microgrids")
|
|
|
|
| 29 |
if uploaded_file:
|
| 30 |
data = pd.ExcelFile(uploaded_file)
|
| 31 |
|
|
|
|
| 32 |
try:
|
| 33 |
+
# Load data from sheets
|
| 34 |
historical_data = data.parse("Historical Data")
|
| 35 |
renewable_forecast = data.parse("Renewable Energy Forecast")
|
| 36 |
ev_profiles = data.parse("EV Charging Profiles")
|
| 37 |
|
| 38 |
+
# Standardize column names
|
| 39 |
+
historical_data.columns = historical_data.columns.str.strip()
|
| 40 |
+
renewable_forecast.columns = renewable_forecast.columns.str.strip()
|
| 41 |
+
ev_profiles.columns = ev_profiles.columns.str.strip()
|
| 42 |
|
| 43 |
+
# Rename specific columns if necessary
|
| 44 |
+
historical_data.rename(columns={"DATE": "Date", "date": "Date"}, inplace=True)
|
| 45 |
+
renewable_forecast.rename(columns={"DATE": "Date", "date": "Date"}, inplace=True)
|
| 46 |
+
ev_profiles.rename(columns={"DATE": "Date", "date": "Date"}, inplace=True)
|
|
|
|
| 47 |
|
| 48 |
# Parameters
|
| 49 |
rated_grid_capacity = 3500 # kW
|
|
|
|
| 91 |
)
|
| 92 |
|
| 93 |
# Simulate Groq API
|
| 94 |
+
if client:
|
| 95 |
+
st.subheader("Groq AI Insights")
|
| 96 |
+
prompt = st.text_input("Ask Groq for insights (e.g., grid stability)")
|
| 97 |
+
if prompt:
|
| 98 |
+
try:
|
| 99 |
+
response = client.chat.completions.create(
|
| 100 |
+
messages=[{"role": "user", "content": prompt}],
|
| 101 |
+
model="llama-3.3-70b-versatile",
|
| 102 |
+
)
|
| 103 |
+
st.write(response.choices[0].message.content)
|
| 104 |
+
except Exception as e:
|
| 105 |
+
st.error(f"Error communicating with Groq API: {e}")
|
| 106 |
+
else:
|
| 107 |
+
st.warning("Groq API key is missing. Insights feature is disabled.")
|
| 108 |
|
| 109 |
except Exception as e:
|
| 110 |
st.error(f"Error parsing file: {e}")
|