Update app.py
Browse files
app.py
CHANGED
@@ -2,6 +2,7 @@ import streamlit as st
|
|
2 |
import os
|
3 |
from groq import Groq
|
4 |
import pandas as pd
|
|
|
5 |
|
6 |
# Set the API Key (ensure the key is properly stored as an environment variable)
|
7 |
GROQ_API_KEY = "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"
|
@@ -10,9 +11,11 @@ client = Groq(api_key=GROQ_API_KEY)
|
|
10 |
# Helper function for energy demand calculation
|
11 |
def calculate_energy(appliances):
|
12 |
total_energy = 0
|
|
|
13 |
for app, details in appliances.items():
|
14 |
-
total_energy += details['quantity'] * details['wattage'] * details['hours'] / 1000
|
15 |
-
|
|
|
16 |
|
17 |
# Predefined appliances with their default wattage
|
18 |
predefined_appliances = {
|
@@ -68,6 +71,8 @@ for i in range(appliance_count):
|
|
68 |
quantity = st.number_input(f"Quantity of {app_name}", min_value=1, step=1, key=f"app{i}_quantity", value=1)
|
69 |
wattage = predefined_appliances.get(app_name, 0) # Default wattage
|
70 |
hours = st.number_input(f"Hours used per day", min_value=1, step=1, key=f"app{i}_hours", value=1)
|
|
|
|
|
71 |
if app_name and app_name != "Select Appliance":
|
72 |
st.session_state.appliances[app_name] = {"quantity": quantity, "wattage": wattage, "hours": hours}
|
73 |
|
@@ -79,13 +84,15 @@ if st.sidebar.button("Add Appliance"):
|
|
79 |
if st.sidebar.button("Reset Appliances"):
|
80 |
reset_appliances()
|
81 |
|
82 |
-
|
83 |
-
|
|
|
84 |
|
85 |
if st.sidebar.button("Submit"):
|
86 |
# Calculate energy demand
|
87 |
-
daily_energy = calculate_energy(st.session_state.appliances)
|
88 |
st.write(f"### Total Daily Energy Demand: {daily_energy:.2f} kWh")
|
|
|
89 |
|
90 |
# Interact with the Groq API
|
91 |
prompt = f"Provide solar recommendations for a location: {location}, daily energy: {daily_energy:.2f} kWh, budget: {budget} USD."
|
@@ -96,7 +103,7 @@ if st.sidebar.button("Submit"):
|
|
96 |
)
|
97 |
answer = response.choices[0].message.content
|
98 |
st.write("### Recommendations")
|
99 |
-
st.
|
100 |
except Exception as e:
|
101 |
st.error(f"Error with Groq API: {e}")
|
102 |
|
@@ -114,11 +121,27 @@ if st.sidebar.button("Submit"):
|
|
114 |
- **10 kW System:** Approx. PKR 1,200,000 - 1,500,000
|
115 |
""")
|
116 |
|
117 |
-
# Visualization
|
118 |
-
st.write("### Appliance Breakdown")
|
119 |
-
if st.session_state.appliances:
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
import os
|
3 |
from groq import Groq
|
4 |
import pandas as pd
|
5 |
+
import matplotlib.pyplot as plt
|
6 |
|
7 |
# Set the API Key (ensure the key is properly stored as an environment variable)
|
8 |
GROQ_API_KEY = "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"
|
|
|
11 |
# Helper function for energy demand calculation
|
12 |
def calculate_energy(appliances):
|
13 |
total_energy = 0
|
14 |
+
total_power = 0
|
15 |
for app, details in appliances.items():
|
16 |
+
total_energy += details['quantity'] * details['wattage'] * details['hours'] / 1000 # Energy in kWh
|
17 |
+
total_power += details['quantity'] * details['wattage'] # Power in Watts
|
18 |
+
return total_energy, total_power
|
19 |
|
20 |
# Predefined appliances with their default wattage
|
21 |
predefined_appliances = {
|
|
|
71 |
quantity = st.number_input(f"Quantity of {app_name}", min_value=1, step=1, key=f"app{i}_quantity", value=1)
|
72 |
wattage = predefined_appliances.get(app_name, 0) # Default wattage
|
73 |
hours = st.number_input(f"Hours used per day", min_value=1, step=1, key=f"app{i}_hours", value=1)
|
74 |
+
power = quantity * wattage
|
75 |
+
st.write(f"**Total Power (Watts):** {power} W")
|
76 |
if app_name and app_name != "Select Appliance":
|
77 |
st.session_state.appliances[app_name] = {"quantity": quantity, "wattage": wattage, "hours": hours}
|
78 |
|
|
|
84 |
if st.sidebar.button("Reset Appliances"):
|
85 |
reset_appliances()
|
86 |
|
87 |
+
# Input for location and budget
|
88 |
+
location = st.sidebar.text_input("Enter your Location (City, Country)")
|
89 |
+
budget = st.sidebar.number_input("Enter your Budget for Solar System (in USD)", min_value=100, step=10)
|
90 |
|
91 |
if st.sidebar.button("Submit"):
|
92 |
# Calculate energy demand
|
93 |
+
daily_energy, total_power = calculate_energy(st.session_state.appliances)
|
94 |
st.write(f"### Total Daily Energy Demand: {daily_energy:.2f} kWh")
|
95 |
+
st.write(f"### Total Power Demand: {total_power:.2f} W")
|
96 |
|
97 |
# Interact with the Groq API
|
98 |
prompt = f"Provide solar recommendations for a location: {location}, daily energy: {daily_energy:.2f} kWh, budget: {budget} USD."
|
|
|
103 |
)
|
104 |
answer = response.choices[0].message.content
|
105 |
st.write("### Recommendations")
|
106 |
+
st.write(answer)
|
107 |
except Exception as e:
|
108 |
st.error(f"Error with Groq API: {e}")
|
109 |
|
|
|
121 |
- **10 kW System:** Approx. PKR 1,200,000 - 1,500,000
|
122 |
""")
|
123 |
|
124 |
+
# Visualization
|
125 |
+
st.write("### Appliance Breakdown")
|
126 |
+
if st.session_state.appliances:
|
127 |
+
appliance_data = pd.DataFrame([
|
128 |
+
{"Appliance": k, "Quantity": v['quantity'], "Daily Energy (kWh)": v['quantity'] * v['wattage'] * v['hours'] / 1000, "Power (W)": v['quantity'] * v['wattage']}
|
129 |
+
for k, v in st.session_state.appliances.items()
|
130 |
+
])
|
131 |
+
st.table(appliance_data)
|
132 |
+
|
133 |
+
# Bar chart for visualization
|
134 |
+
st.write("### Energy Consumption by Appliance")
|
135 |
+
fig, ax = plt.subplots()
|
136 |
+
ax.bar(appliance_data["Appliance"], appliance_data["Daily Energy (kWh)"], color='skyblue')
|
137 |
+
ax.set_xlabel("Appliances")
|
138 |
+
ax.set_ylabel("Daily Energy (kWh)")
|
139 |
+
ax.set_title("Energy Consumption Breakdown")
|
140 |
+
plt.xticks(rotation=45, ha="right")
|
141 |
+
st.pyplot(fig)
|
142 |
+
|
143 |
+
# Highlight the most energy-consuming appliance
|
144 |
+
max_energy_app = appliance_data.loc[appliance_data["Daily Energy (kWh)"].idxmax()]
|
145 |
+
st.write(f"**Most Energy-Consuming Appliance:** {max_energy_app['Appliance']} consuming {max_energy_app['Daily Energy (kWh)']:.2f} kWh daily.")
|
146 |
+
|
147 |
+
|