Update app.py
Browse files
app.py
CHANGED
|
@@ -1,71 +1,87 @@
|
|
| 1 |
import os
|
| 2 |
-
import numpy as np
|
| 3 |
import pandas as pd
|
| 4 |
-
import
|
| 5 |
-
|
| 6 |
from groq import Groq
|
| 7 |
|
| 8 |
-
|
| 9 |
-
model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 10 |
|
| 11 |
# Initialize Groq API client
|
| 12 |
-
GROQ_API_KEY = "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"
|
| 13 |
client = Groq(api_key=GROQ_API_KEY)
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
def
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
embeddings = [generate_embeddings(row.to_string()) for _, row in data.iterrows()]
|
| 23 |
-
embeddings = np.array(embeddings).astype("float32")
|
| 24 |
-
index.add(embeddings)
|
| 25 |
-
return index, embeddings
|
| 26 |
|
| 27 |
-
#
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
# Generate a detailed report using Groq's generative model
|
| 35 |
-
def generate_report_with_groq(query, results):
|
| 36 |
-
input_text = f"Based on the query '{query}', the following insights are generated:\n\n{results.to_string(index=False)}"
|
| 37 |
-
response = client.chat.completions.create(
|
| 38 |
-
messages=[{"role": "user", "content": input_text}],
|
| 39 |
-
model="llama3-8b-8192",
|
| 40 |
-
stream=False
|
| 41 |
)
|
| 42 |
-
return response.choices[0].message.content
|
| 43 |
|
| 44 |
-
#
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
-
# User
|
| 58 |
-
|
| 59 |
-
print(f"User Query: {query}")
|
| 60 |
|
| 61 |
-
#
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
| 68 |
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
import streamlit as st
|
| 5 |
from groq import Groq
|
| 6 |
|
| 7 |
+
GROQ_API_KEY = "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"
|
|
|
|
| 8 |
|
| 9 |
# Initialize Groq API client
|
|
|
|
| 10 |
client = Groq(api_key=GROQ_API_KEY)
|
| 11 |
|
| 12 |
+
# Function to analyze energy usage
|
| 13 |
+
def analyze_energy_usage(data, household_id=None):
|
| 14 |
+
if household_id:
|
| 15 |
+
# Filter data for a specific household
|
| 16 |
+
household_data = data[data["Household ID"] == household_id]
|
| 17 |
+
else:
|
| 18 |
+
household_data = data
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
# Aggregate data
|
| 21 |
+
total_usage = household_data["Energy Usage (kWh)"].sum()
|
| 22 |
+
avg_cost = household_data["Cost"].mean()
|
| 23 |
+
peak_time_period = (
|
| 24 |
+
household_data.groupby("Time Period")["Energy Usage (kWh)"]
|
| 25 |
+
.sum()
|
| 26 |
+
.idxmax()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
)
|
|
|
|
| 28 |
|
| 29 |
+
# Generate a summary report
|
| 30 |
+
report_summary = f"""
|
| 31 |
+
Total Energy Usage: {total_usage} kWh
|
| 32 |
+
Average Cost: ${avg_cost:.2f}
|
| 33 |
+
Peak Usage Time Period: {peak_time_period}
|
| 34 |
+
"""
|
| 35 |
+
return report_summary, household_data
|
| 36 |
|
| 37 |
+
# Function to generate recommendations using Groq's API
|
| 38 |
+
def generate_recommendations(context):
|
| 39 |
+
try:
|
| 40 |
+
response = client.chat.completions.create(
|
| 41 |
+
messages=[
|
| 42 |
+
{
|
| 43 |
+
"role": "user",
|
| 44 |
+
"content": f"Based on the following data:\n{context}\nProvide energy-saving recommendations and insights."
|
| 45 |
+
}
|
| 46 |
+
],
|
| 47 |
+
model="llama3-8b-8192",
|
| 48 |
+
stream=False,
|
| 49 |
+
)
|
| 50 |
+
return response.choices[0].message.content
|
| 51 |
+
except Exception as e:
|
| 52 |
+
return f"An error occurred: {e}"
|
| 53 |
|
| 54 |
+
# Streamlit App
|
| 55 |
+
st.title("Energy Usage Analysis Report Generator")
|
| 56 |
+
|
| 57 |
+
# Upload Dataset
|
| 58 |
+
uploaded_file = st.file_uploader("Upload your energy usage dataset (CSV)", type="csv")
|
| 59 |
+
if uploaded_file:
|
| 60 |
+
# Load dataset
|
| 61 |
+
data = pd.read_csv(uploaded_file)
|
| 62 |
+
st.write("Dataset Preview:", data.head())
|
| 63 |
|
| 64 |
+
# User Input
|
| 65 |
+
household_id = st.text_input("Enter Household ID for specific analysis (optional)")
|
|
|
|
| 66 |
|
| 67 |
+
# Analyze Data
|
| 68 |
+
if st.button("Analyze Energy Usage"):
|
| 69 |
+
with st.spinner("Analyzing..."):
|
| 70 |
+
report_summary, filtered_data = analyze_energy_usage(data, household_id)
|
| 71 |
+
st.subheader("Energy Usage Summary")
|
| 72 |
+
st.text(report_summary)
|
| 73 |
|
| 74 |
+
# Generate recommendations
|
| 75 |
+
st.subheader("Recommendations")
|
| 76 |
+
context = filtered_data.to_string(index=False)
|
| 77 |
+
recommendations = generate_recommendations(context)
|
| 78 |
+
st.text(recommendations)
|
| 79 |
|
| 80 |
+
# Footer
|
| 81 |
+
st.sidebar.title("About")
|
| 82 |
+
st.sidebar.info(
|
| 83 |
+
"""
|
| 84 |
+
This app generates energy usage reports and recommendations based on uploaded data.
|
| 85 |
+
Built with Streamlit and powered by Groq's language model.
|
| 86 |
+
"""
|
| 87 |
+
)
|