|
import streamlit as st |
|
import pandas as pd |
|
import pickle |
|
from sklearn.model_selection import train_test_split |
|
from sklearn.linear_model import LinearRegression |
|
from transformers import pipeline |
|
from PyPDF2 import PdfReader |
|
|
|
|
|
def load_file(uploaded_file): |
|
if uploaded_file.name.endswith('.csv'): |
|
return pd.read_csv(uploaded_file) |
|
elif uploaded_file.name.endswith('.xlsx'): |
|
return pd.read_excel(uploaded_file) |
|
elif uploaded_file.name.endswith('.pdf'): |
|
reader = PdfReader(uploaded_file) |
|
text = ''.join(page.extract_text() for page in reader.pages) |
|
|
|
raise NotImplementedError("PDF parsing is not implemented.") |
|
else: |
|
raise ValueError("Unsupported file format. Please upload CSV, Excel, or PDF.") |
|
|
|
|
|
def train_model(data): |
|
data['Hour'] = pd.to_datetime(data['Timestamp']).dt.hour |
|
|
|
X = data[['Hour', 'Temperature', 'CloudCover']] |
|
|
|
y = data['SolarOutput'] |
|
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) |
|
model = LinearRegression() |
|
model.fit(X_train, y_train) |
|
with open('solar_model.pkl', 'wb') as f: |
|
pickle.dump(model, f) |
|
return model |
|
|
|
|
|
def load_model(): |
|
try: |
|
with open('solar_model.pkl', 'rb') as f: |
|
return pickle.load(f) |
|
except FileNotFoundError: |
|
return None |
|
|
|
|
|
generator = pipeline('text-generation', model='gpt2', device=-1) |
|
|
|
|
|
st.title("Smart Home Energy Advisor") |
|
|
|
|
|
uploaded_file = st.file_uploader("Upload your data file (CSV, Excel, or PDF)", type=['csv', 'xlsx', 'pdf']) |
|
|
|
if uploaded_file: |
|
|
|
try: |
|
data = load_file(uploaded_file) |
|
st.write("Data Preview:", data.head()) |
|
except Exception as e: |
|
st.error(f"Error loading file: {e}") |
|
data = None |
|
|
|
if data is not None: |
|
|
|
model = load_model() |
|
if not model: |
|
st.warning("No pre-trained model found. Training a new model...") |
|
model = train_model(data) |
|
st.success("Model trained and saved successfully!") |
|
|
|
|
|
data['Hour'] = pd.to_datetime(data['Timestamp']).dt.hour |
|
data['PredictedSolarOutput'] = model.predict(data[['Hour', 'Temperature', 'CloudCover']]) |
|
|
|
|
|
best_hour = data.loc[data['PredictedSolarOutput'].idxmax(), 'Hour'] |
|
|
|
|
|
advice_prompt = f"The best time to use your appliances is between {best_hour}:00 and {best_hour + 2}:00." |
|
advice = generator(advice_prompt, max_length=50)[0]['generated_text'] |
|
|
|
|
|
st.subheader("Predictions") |
|
st.write(data) |
|
st.subheader("Recommendation") |
|
st.write(advice) |
|
|
|
|
|
st.subheader("Chat with the Advisor") |
|
user_query = st.text_input("Ask your question:") |
|
if user_query: |
|
chatbot_response = generator(f"User asked: {user_query}. Response:", max_length=50)[0]['generated_text'] |
|
st.write("Advisor Response:", chatbot_response) |