File size: 3,510 Bytes
08ae01a
 
 
 
 
 
 
 
 
 
456a22b
08ae01a
 
 
 
 
 
 
 
 
 
 
 
 
0e577d5
cc768a9
 
 
08ae01a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
456a22b
 
08ae01a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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

# Helper function to load different file types
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)
        # PDF parsing logic here (customized for table extraction)
        raise NotImplementedError("PDF parsing is not implemented.")
    else:
        raise ValueError("Unsupported file format. Please upload CSV, Excel, or PDF.")

# Train the model if it doesn't already exist
def train_model(data):
    data['Hour'] = pd.to_datetime(data['Timestamp']).dt.hour
    #data['Hour'] = pd.to_datetime(data['Timestamp']).dt.hour
    X = data[['Hour', 'Temperature', 'CloudCover']]
    #,'DATE_TIME','PLANT_ID','SOURCE_KEY','DC_POWER','AC_POWER','DAILY_YIELD','TOTAL_YIELD'
    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

# Load the trained model
def load_model():
    try:
        with open('solar_model.pkl', 'rb') as f:
            return pickle.load(f)
    except FileNotFoundError:
        return None

# Initialize the text generation pipeline
generator = pipeline('text-generation', model='gpt2', device=-1)

# Streamlit app
st.title("Smart Home Energy Advisor")

# File uploader
uploaded_file = st.file_uploader("Upload your data file (CSV, Excel, or PDF)", type=['csv', 'xlsx', 'pdf'])
#uploaded_file = "/solar_data.csv"
if uploaded_file:
    # Load and preprocess data
    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:
        # Load or train the model
        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!")

        # Preprocess data
        data['Hour'] = pd.to_datetime(data['Timestamp']).dt.hour
        data['PredictedSolarOutput'] = model.predict(data[['Hour', 'Temperature', 'CloudCover']])

        # Determine the best hour for maximum solar energy
        best_hour = data.loc[data['PredictedSolarOutput'].idxmax(), 'Hour']

        # Generate advice
        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']

        # Display predictions and advice
        st.subheader("Predictions")
        st.write(data)
        st.subheader("Recommendation")
        st.write(advice)

# Chatbot feature
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)