Spaces:
Sleeping
Sleeping
akhilhsingh
commited on
Commit
•
5356b03
1
Parent(s):
514bb80
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,126 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import openai
|
3 |
+
import json
|
4 |
+
import requests
|
5 |
+
import streamlit as st
|
6 |
+
#from dotenv import load_dotenv
|
7 |
+
|
8 |
+
# Set API keys
|
9 |
+
openai.api_key = os.environ['OPENAI_API_KEY']
|
10 |
+
OPENWEATHER_API_KEY = os.environ['OPENWEATHER_API_KEY']
|
11 |
+
|
12 |
+
# Function to get the current weather
|
13 |
+
def get_current_weather(location):
|
14 |
+
"""Get the current weather in a given location"""
|
15 |
+
base_url = "http://api.openweathermap.org/data/2.5/weather?"
|
16 |
+
complete_url = f"{base_url}appid={OPENWEATHER_API_KEY}&q={location}"
|
17 |
+
|
18 |
+
response = requests.get(complete_url)
|
19 |
+
|
20 |
+
if response.status_code == 200:
|
21 |
+
data = response.json()
|
22 |
+
weather = data['weather'][0]['main']
|
23 |
+
temperature = data['main']['temp'] - 273.15 # Convert Kelvin to Celsius
|
24 |
+
return {
|
25 |
+
"city": location,
|
26 |
+
"weather": weather,
|
27 |
+
"temperature": round(temperature, 2)
|
28 |
+
}
|
29 |
+
else:
|
30 |
+
return {"city": location, "weather": "Data Fetch Error", "temperature": "N/A"}
|
31 |
+
|
32 |
+
# Streamlit app
|
33 |
+
st.title("Weather Info Assistant")
|
34 |
+
#st.write("Get the current weather information for a specific location. Just replace Gurgaon with the location")
|
35 |
+
|
36 |
+
user_input = st.text_input("Get the current weather information for a specific location. Just replace Gurgaon with the location", "What is the weather like in Gurgaon?")
|
37 |
+
|
38 |
+
if st.button("Get Weather"):
|
39 |
+
# Use GPT-3.5-turbo to parse the user query
|
40 |
+
response = openai.chat.completions.create(
|
41 |
+
model="gpt-3.5-turbo",
|
42 |
+
messages=[
|
43 |
+
{
|
44 |
+
"role": "user",
|
45 |
+
"content": user_input
|
46 |
+
}
|
47 |
+
],
|
48 |
+
temperature=0,
|
49 |
+
max_tokens=300,
|
50 |
+
functions=[
|
51 |
+
{
|
52 |
+
"name": "get_current_weather",
|
53 |
+
"description": "Get the current weather in a given location",
|
54 |
+
"parameters": {
|
55 |
+
"type": "object",
|
56 |
+
"properties": {
|
57 |
+
"location": {
|
58 |
+
"type": "string",
|
59 |
+
"description": "The city and state, e.g. San Francisco, CA",
|
60 |
+
},
|
61 |
+
},
|
62 |
+
"required": ["location"],
|
63 |
+
},
|
64 |
+
}
|
65 |
+
],
|
66 |
+
function_call="auto"
|
67 |
+
)
|
68 |
+
|
69 |
+
# Extract the location from the response
|
70 |
+
try:
|
71 |
+
# Extract the function call information
|
72 |
+
function_call_info = response.choices[0].message.function_call
|
73 |
+
function_name = function_call_info.name # This should be 'get_current_weather'
|
74 |
+
function_arguments = function_call_info.arguments # This is a JSON string
|
75 |
+
|
76 |
+
# Parse the JSON string to get the arguments dictionary
|
77 |
+
args = json.loads(function_arguments)
|
78 |
+
location = args['location']
|
79 |
+
|
80 |
+
# Now you can fetch the weather information using the extracted location
|
81 |
+
weather_info = get_current_weather(location)
|
82 |
+
|
83 |
+
# Define a dictionary to map weather conditions to emoticons
|
84 |
+
weather_icons = {
|
85 |
+
"Clear": "☀️", # Sun icon
|
86 |
+
"Clouds": "☁️", # Cloud icon
|
87 |
+
"Rain": "🌧️", # Rain icon
|
88 |
+
"Snow": "❄️", # Snow icon
|
89 |
+
"Thunderstorm": "⛈️", # Thunderstorm icon
|
90 |
+
"Mist": "🌫️", # Fog icon
|
91 |
+
"Smoke": "💨", # Smoke icon
|
92 |
+
"Haze": "🌫️", # Haze icon
|
93 |
+
"Dust": "🌪️", # Dust icon
|
94 |
+
"Fog": "🌫️", # Fog icon
|
95 |
+
"Sand": "🏜️", # Sand icon
|
96 |
+
"Ash": "🌋", # Volcanic ash icon
|
97 |
+
"Squall": "🌬️", # Wind face icon
|
98 |
+
"Tornado": "🌪️" # Tornado icon
|
99 |
+
}
|
100 |
+
|
101 |
+
if weather_info["weather"] != "Data Fetch Error":
|
102 |
+
icon = weather_icons.get(weather_info["weather"], "❓") # Default to question mark if no match
|
103 |
+
weather_description = f"The weather in **{weather_info['city']}** is currently `{weather_info['weather']}` with a temperature of `{weather_info['temperature']}°C`."
|
104 |
+
chat_response = openai.chat.completions.create(
|
105 |
+
model="gpt-3.5-turbo",
|
106 |
+
messages=[
|
107 |
+
{
|
108 |
+
"role": "system",
|
109 |
+
"content": "Generate a conversational response for weather information. Please include the weather and temperature details in the response and provide suggestion on things that can be done best in this weather"
|
110 |
+
},
|
111 |
+
{
|
112 |
+
"role": "user",
|
113 |
+
"content": weather_description
|
114 |
+
}
|
115 |
+
],
|
116 |
+
temperature=0.5,
|
117 |
+
max_tokens=300
|
118 |
+
)
|
119 |
+
|
120 |
+
# Use HTML to style the font size, color, and include the icon in the displayed message
|
121 |
+
st.markdown(f"<div style='font-size: 24px; color: blue; font-weight: bold;'> Weather: {weather_info['weather']} {icon}          Temperature: {weather_info['temperature']}°C</div>", unsafe_allow_html=True)
|
122 |
+
st.markdown(f"<div style='font-size: 20px;'> {chat_response.choices[0].message.content}", unsafe_allow_html=True)
|
123 |
+
else:
|
124 |
+
st.markdown(f"<div style='font-size: 20px;'>**Could not fetch weather data for {weather_info['city']}. Please try again.**</div>", unsafe_allow_html=True)
|
125 |
+
except Exception as e:
|
126 |
+
print(f"Error processing the response or fetching the weather: {e}")
|