Weather_App / app.py
ranjeetak780's picture
Rename Weather_App_Streamlit_Enhanced.py to app.py
525e0d4 verified
import streamlit as st
import json
import requests
from openai import AzureOpenAI
from dotenv import load_dotenv
import os
import urllib3
# Suppress the warnings for unverified HTTPS requests
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
load_dotenv()
AZURE_OPENAI_API_KEY = os.getenv("AZURE_OPENAI_API_KEY2")
AZURE_ENDPOINT = os.getenv("AZURE_ENDPOINT2")
chat_model = 'chatmodel'
if not AZURE_OPENAI_API_KEY or not AZURE_ENDPOINT:
raise ValueError("Missing Azure OpenAI API key or endpoint")
# Function to interact with Azure OpenAI
def chat_with_openai(query):
client = AzureOpenAI(
azure_endpoint=AZURE_ENDPOINT,
api_key=AZURE_OPENAI_API_KEY,
api_version="2024-02-01"
)
functions = [
{
"name": "getWeather",
"description": "Retrieve real-time weather information/data about a particular location/place",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "the exact location whose real-time weather is to be determined",
},
},
"required": ["location"]
},
}
]
response = client.chat.completions.create(
model=chat_model,
messages=[
{"role": "system", "content": "you are an assistant that helps people retrieve real-time weather data/info"},
{"role": "user", "content": query}
],
functions=functions
)
# Accessing the function call correctly
function_call = response.choices[0].message.function_call
function_argument = json.loads(function_call.arguments)
location = function_argument['location'] if 'location' in function_argument else None
return location
# Function to get weather details from OpenWeatherMap API
def get_weather(location):
api_key = "9254fb38dcbdf7bff64fef588a66583b"
url = f"https://api.openweathermap.org/data/2.5/weather?q={location}&appid={api_key}"
response = requests.get(url, verify=False)
weather_data = response.json()
if 'coord' not in weather_data:
st.error("Error: Unable to retrieve weather data for the location.")
return None
latitude = weather_data['coord']['lat']
longitude = weather_data['coord']['lon']
st.write(f"Latitude: {latitude}")
st.write(f"Longitude: {longitude}")
url_final = f"https://api.openweathermap.org/data/2.5/weather?lat={latitude}&lon={longitude}&appid={api_key}"
final_response = requests.get(url_final, verify=False)
final_response_json = final_response.json()
if 'weather' not in final_response_json:
st.error("Error: Unable to retrieve weather condition.")
return None
weather = final_response_json['weather'][0]['main']
temp = round((final_response_json['main']['temp'] - 273.15), 2)
temp_min = round((final_response_json['main']['temp_min'] - 273.15), 2)
temp_max = round((final_response_json['main']['temp_max'] - 273.15), 2)
pressure = final_response_json['main']['pressure']
humidity = final_response_json['main']['humidity']
st.write(f"Weather Condition: {weather}")
st.write(f"Temperature: {temp} °C")
st.write(f"Minimum Temperature: {temp_min} °C")
st.write(f"Maximum Temperature: {temp_max} °C")
st.write(f"Pressure: {pressure} hPa")
st.write(f"Humidity: {humidity}%")
# Streamlit UI
def main():
st.title("Weather Information")
# Input for city name
query = st.text_input("Enter your query", "How is the weather in Delhi?")
if st.button("Get Weather"):
location = chat_with_openai(query)
if location:
get_weather(location)
if __name__ == "__main__":
main()