File size: 2,457 Bytes
5d3adc5
3e001d0
 
 
 
a17bbf8
 
 
3e001d0
 
6563f85
67a6a0e
3e001d0
 
 
 
a17bbf8
3e001d0
 
 
 
 
 
 
 
 
 
 
 
 
 
a17bbf8
3e001d0
 
a17bbf8
 
 
 
 
 
3e001d0
a17bbf8
3e001d0
 
 
 
 
 
 
 
 
 
 
 
a17bbf8
3e001d0
 
 
 
a17bbf8
3e001d0
 
 
 
a17bbf8
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
import os
import streamlit as st
import openai
import requests

# Install missing dependencies in case requirements.txt fails
os.system('pip install openai')

# Function to fetch weather data
def get_weather(city):
    api_key = "84972785bb7170138af18aefec1a2706"  # Replace with your OpenWeather API key
    url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}&units=metric"
    response = requests.get(url)
    if response.status_code == 200:
        return response.json()
    else:
        st.error(f"Error fetching weather data: {response.text}")
        return None

# Function to generate user preference summary using NLP
def summarize_preferences(preferences):
    openai.api_key = "your_openai_api_key"  # Replace with your OpenAI API key
    prompt = f"Summarize the following HVAC preferences: {preferences}"
    response = openai.Completion.create(
        engine="text-davinci-003",
        prompt=prompt,
        max_tokens=100
    )
    return response.choices[0].text.strip()

# HVAC control logic
def adjust_hvac(occupancy, weather, preferences):
    if occupancy == "Occupied":
        temp = weather['main']['temp']
        if "20-22" in preferences:
            return "Heating ON. Adjusting to 22°C as per user preferences."
        elif "23-26" in preferences:
            return "Cooling ON. Adjusting to 24°C as per user preferences."
        elif "26-30" in preferences:
            return "Temperature is optimal. Maintaining between 26°C and 30°C as per user preferences."
        else:
            return "Custom preferences not recognized. Maintaining current settings."
    else:
        return "Room unoccupied. HVAC set to energy-saving mode."

# Streamlit app layout
st.title("Smart HVAC Control System")
st.header("Adjust your room's temperature intelligently!")

# User inputs
city = st.text_input("Enter your city for weather updates:")
occupancy = st.radio("Room Occupancy Status:", ["Occupied", "Unoccupied"])
preferences = st.text_area("Enter your HVAC preferences:")

# Adjust HVAC button logic
if st.button("Adjust HVAC"):
    if city and preferences:
        weather = get_weather(city)
        if weather:
            hvac_action = adjust_hvac(occupancy, weather, preferences)
            st.success(hvac_action)
        else:
            st.error("Could not fetch weather data. Please check the city name.")
    else:
        st.warning("Please provide both city and preferences.")