Zainulabedin1's picture
Update app.py
67a6a0e verified
Raw History Blame Contribute Delete
2.46 kB
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.")