Spaces:
Sleeping
Sleeping
ranjeetak780
commited on
Commit
•
5464460
1
Parent(s):
9fbcb0d
Upload Weather_App_Streamlit_Enhanced.py
Browse files
Weather_App_Streamlit_Enhanced.py
ADDED
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import json
|
3 |
+
import requests
|
4 |
+
from openai import AzureOpenAI
|
5 |
+
from dotenv import load_dotenv
|
6 |
+
import os
|
7 |
+
import urllib3
|
8 |
+
|
9 |
+
# Suppress the warnings for unverified HTTPS requests
|
10 |
+
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
|
11 |
+
|
12 |
+
load_dotenv()
|
13 |
+
AZURE_OPENAI_API_KEY = os.getenv("AZURE_OPENAI_API_KEY2")
|
14 |
+
AZURE_ENDPOINT = os.getenv("AZURE_ENDPOINT2")
|
15 |
+
chat_model = 'chatmodel'
|
16 |
+
|
17 |
+
if not AZURE_OPENAI_API_KEY or not AZURE_ENDPOINT:
|
18 |
+
raise ValueError("Missing Azure OpenAI API key or endpoint")
|
19 |
+
|
20 |
+
# Function to interact with Azure OpenAI
|
21 |
+
def chat_with_openai(query):
|
22 |
+
client = AzureOpenAI(
|
23 |
+
azure_endpoint=AZURE_ENDPOINT,
|
24 |
+
api_key=AZURE_OPENAI_API_KEY,
|
25 |
+
api_version="2024-02-01"
|
26 |
+
)
|
27 |
+
|
28 |
+
functions = [
|
29 |
+
{
|
30 |
+
"name": "getWeather",
|
31 |
+
"description": "Retrieve real-time weather information/data about a particular location/place",
|
32 |
+
"parameters": {
|
33 |
+
"type": "object",
|
34 |
+
"properties": {
|
35 |
+
"location": {
|
36 |
+
"type": "string",
|
37 |
+
"description": "the exact location whose real-time weather is to be determined",
|
38 |
+
},
|
39 |
+
},
|
40 |
+
"required": ["location"]
|
41 |
+
},
|
42 |
+
}
|
43 |
+
]
|
44 |
+
|
45 |
+
response = client.chat.completions.create(
|
46 |
+
model=chat_model,
|
47 |
+
messages=[
|
48 |
+
{"role": "system", "content": "you are an assistant that helps people retrieve real-time weather data/info"},
|
49 |
+
{"role": "user", "content": query}
|
50 |
+
],
|
51 |
+
functions=functions
|
52 |
+
)
|
53 |
+
|
54 |
+
# Accessing the function call correctly
|
55 |
+
function_call = response.choices[0].message.function_call
|
56 |
+
function_argument = json.loads(function_call.arguments)
|
57 |
+
location = function_argument['location'] if 'location' in function_argument else None
|
58 |
+
|
59 |
+
return location
|
60 |
+
|
61 |
+
# Function to get weather details from OpenWeatherMap API
|
62 |
+
def get_weather(location):
|
63 |
+
api_key = "9254fb38dcbdf7bff64fef588a66583b"
|
64 |
+
url = f"https://api.openweathermap.org/data/2.5/weather?q={location}&appid={api_key}"
|
65 |
+
response = requests.get(url, verify=False)
|
66 |
+
weather_data = response.json()
|
67 |
+
|
68 |
+
if 'coord' not in weather_data:
|
69 |
+
st.error("Error: Unable to retrieve weather data for the location.")
|
70 |
+
return None
|
71 |
+
|
72 |
+
latitude = weather_data['coord']['lat']
|
73 |
+
longitude = weather_data['coord']['lon']
|
74 |
+
st.write(f"Latitude: {latitude}")
|
75 |
+
st.write(f"Longitude: {longitude}")
|
76 |
+
|
77 |
+
url_final = f"https://api.openweathermap.org/data/2.5/weather?lat={latitude}&lon={longitude}&appid={api_key}"
|
78 |
+
final_response = requests.get(url_final, verify=False)
|
79 |
+
final_response_json = final_response.json()
|
80 |
+
|
81 |
+
if 'weather' not in final_response_json:
|
82 |
+
st.error("Error: Unable to retrieve weather condition.")
|
83 |
+
return None
|
84 |
+
|
85 |
+
weather = final_response_json['weather'][0]['main']
|
86 |
+
temp = round((final_response_json['main']['temp'] - 273.15), 2)
|
87 |
+
temp_min = round((final_response_json['main']['temp_min'] - 273.15), 2)
|
88 |
+
temp_max = round((final_response_json['main']['temp_max'] - 273.15), 2)
|
89 |
+
pressure = final_response_json['main']['pressure']
|
90 |
+
humidity = final_response_json['main']['humidity']
|
91 |
+
|
92 |
+
st.write(f"Weather Condition: {weather}")
|
93 |
+
st.write(f"Temperature: {temp} °C")
|
94 |
+
st.write(f"Minimum Temperature: {temp_min} °C")
|
95 |
+
st.write(f"Maximum Temperature: {temp_max} °C")
|
96 |
+
st.write(f"Pressure: {pressure} hPa")
|
97 |
+
st.write(f"Humidity: {humidity}%")
|
98 |
+
|
99 |
+
# Streamlit UI
|
100 |
+
def main():
|
101 |
+
st.title("Weather Information")
|
102 |
+
|
103 |
+
# Input for city name
|
104 |
+
query = st.text_input("Enter your query", "How is the weather in Delhi?")
|
105 |
+
|
106 |
+
if st.button("Get Weather"):
|
107 |
+
location = chat_with_openai(query)
|
108 |
+
if location:
|
109 |
+
get_weather(location)
|
110 |
+
|
111 |
+
if __name__ == "__main__":
|
112 |
+
main()
|