import streamlit as st import os from groq import Groq import pandas as pd import matplotlib.pyplot as plt # Set the API Key (ensure the key is properly stored as an environment variable) GROQ_API_KEY = "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" client = Groq(api_key=GROQ_API_KEY) # Helper function for energy demand calculation def calculate_energy(appliances): total_energy = 0 total_power = 0 for app, details in appliances.items(): total_energy += details['quantity'] * details['wattage'] * details['hours'] / 1000 # Energy in kWh total_power += details['quantity'] * details['wattage'] # Power in Watts return total_energy, total_power # Predefined appliances with their default wattage predefined_appliances = { "Select Appliance": 0, # Default placeholder "LED Light": 10, "Air Conditioner": 2000, "Refrigerator": 150, "Washing Machine": 500, "Fan": 75, "Laptop": 50, "Television": 100, "Microwave Oven": 1000, "Electric Water Heater": 1500, "Dishwasher": 1200, "Coffee Maker": 800, "Toaster": 1200, "Hair Dryer": 1500, "Electric Kettle": 1500, "Oven": 2000, "Vacuum Cleaner": 1000, "Iron": 1200, "Blender": 300, "Clothes Dryer": 1800, "Space Heater": 1500, "Dehumidifier": 700, "Freezer": 400, "Electric Grill": 1200, "Sewing Machine": 100, "Projector": 300, "Gaming Console": 200, "Electric Fan Heater": 1500 } # Streamlit App st.title("SustainaBot: Solar Energy Advisor") st.sidebar.header("User Input") # Inputs st.sidebar.subheader("Enter Appliance Details") if "appliances" not in st.session_state: st.session_state.appliances = {} # Function to reset appliance state def reset_appliances(): st.session_state.appliances = {} # Add appliance inputs dynamically appliance_count = len(st.session_state.appliances) for i in range(appliance_count): with st.sidebar.expander(f"Appliance {i+1} Details"): app_name = st.selectbox(f"Appliance {i+1} Name", options=list(predefined_appliances.keys()), key=f"app{i}_name") quantity = st.number_input(f"Quantity of {app_name}", min_value=1, step=1, key=f"app{i}_quantity", value=1) wattage = predefined_appliances.get(app_name, 0) # Default wattage hours = st.number_input(f"Hours used per day", min_value=1, step=1, key=f"app{i}_hours", value=1) power = quantity * wattage st.write(f"**Total Power (Watts):** {power} W") if app_name and app_name != "Select Appliance": st.session_state.appliances[app_name] = {"quantity": quantity, "wattage": wattage, "hours": hours} # Button to add more appliances if st.sidebar.button("Add Appliance"): st.session_state.appliances[f"Appliance {len(st.session_state.appliances) + 1}"] = {"quantity": 1, "wattage": 0, "hours": 0} # Button to reset appliances if st.sidebar.button("Reset Appliances"): reset_appliances() # Input for location and budget location = st.sidebar.text_input("Enter your Location (City, Country)") budget = st.sidebar.number_input("Enter your Budget for Solar System (in USD)", min_value=100, step=10) if st.sidebar.button("Submit"): # Calculate energy demand daily_energy, total_power = calculate_energy(st.session_state.appliances) st.write(f"### Total Daily Energy Demand: {daily_energy:.2f} kWh") st.write(f"### Total Power Demand: {total_power:.2f} W") # Interact with the Groq API prompt = f"Provide solar recommendations for a location: {location}, daily energy: {daily_energy:.2f} kWh, budget: {budget} USD." try: response = client.chat.completions.create( messages=[{"role": "user", "content": prompt}], model="llama-3.3-70b-versatile", ) answer = response.choices[0].message.content st.write("### Recommendations") st.write(answer) except Exception as e: st.error(f"Error with Groq API: {e}") # Display potential savings st.write("### Potential Savings") savings = daily_energy * 30 * 0.2 # Assuming $0.2/kWh savings st.write(f"By switching to solar, you could save approximately **${savings:.2f} per month!**") # Approximate Pricing Section st.write("### Solar Installation Prices") st.write(""" - **1 kW System:** Approx. PKR 120,000 - 150,000 - **3 kW System:** Approx. PKR 350,000 - 400,000 - **5 kW System:** Approx. PKR 600,000 - 700,000 - **10 kW System:** Approx. PKR 1,200,000 - 1,500,000 """) # Visualization st.write("### Appliance Breakdown") if st.session_state.appliances: appliance_data = pd.DataFrame([ {"Appliance": k, "Quantity": v['quantity'], "Daily Energy (kWh)": v['quantity'] * v['wattage'] * v['hours'] / 1000, "Power (W)": v['quantity'] * v['wattage']} for k, v in st.session_state.appliances.items() ]) st.table(appliance_data) # Bar chart for visualization st.write("### Energy Consumption by Appliance") fig, ax = plt.subplots() ax.bar(appliance_data["Appliance"], appliance_data["Daily Energy (kWh)"], color='skyblue') ax.set_xlabel("Appliances") ax.set_ylabel("Daily Energy (kWh)") ax.set_title("Energy Consumption Breakdown") plt.xticks(rotation=45, ha="right") st.pyplot(fig) # Highlight the most energy-consuming appliance max_energy_app = appliance_data.loc[appliance_data["Daily Energy (kWh)"].idxmax()] st.write(f"**Most Energy-Consuming Appliance:** {max_energy_app['Appliance']} consuming {max_energy_app['Daily Energy (kWh)']:.2f} kWh daily.")