Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- .gitattributes +1 -0
- app.py +259 -0
- requirements.txt +3 -0
- weather_bot.jpg +3 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
weather_bot.jpg filter=lfs diff=lfs merge=lfs -text
|
app.py
ADDED
|
@@ -0,0 +1,259 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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|
+
import requests
|
| 5 |
+
import openai
|
| 6 |
+
from dotenv import load_dotenv
|
| 7 |
+
|
| 8 |
+
# Load environment variables
|
| 9 |
+
load_dotenv()
|
| 10 |
+
|
| 11 |
+
# Initialize OpenAI client your openai key
|
| 12 |
+
openai.api_key = os.getenv('OPENAI_API_KEY')
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def get_current_weather(location, unit='celsius'):
|
| 16 |
+
weather_api_key = userdata.get('WEATHER_API_KEY')
|
| 17 |
+
base_url = f"http://api.openweathermap.org/data/2.5/weather?q={location}&appid={weather_api_key}&units=metric"
|
| 18 |
+
response = requests.get(base_url)
|
| 19 |
+
data = response.json()
|
| 20 |
+
|
| 21 |
+
weather_description = data['weather'][0]['description']
|
| 22 |
+
return {
|
| 23 |
+
"location": location,
|
| 24 |
+
"temperature": data['main']['temp'],
|
| 25 |
+
"weather": weather_description
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
#print(get_current_weather("fremont"))
|
| 29 |
+
|
| 30 |
+
# format is referred from opeai Function Chat->Tools->Add->Function->get_weather()
|
| 31 |
+
functions = [
|
| 32 |
+
{
|
| 33 |
+
"name": "get_current_weather",
|
| 34 |
+
"description": "Get the current weather in a given location",
|
| 35 |
+
"parameters": {
|
| 36 |
+
"type": "object",
|
| 37 |
+
"properties": {
|
| 38 |
+
"location": {
|
| 39 |
+
"type": "string",
|
| 40 |
+
"description": "The city, e.g. San Francisco"
|
| 41 |
+
},
|
| 42 |
+
"unit": {
|
| 43 |
+
"type": "string",
|
| 44 |
+
"enum": ["celsius", "fahrenheit"]
|
| 45 |
+
}
|
| 46 |
+
},
|
| 47 |
+
"required": ["location"]
|
| 48 |
+
}
|
| 49 |
+
}
|
| 50 |
+
]
|
| 51 |
+
|
| 52 |
+
functions
|
| 53 |
+
|
| 54 |
+
#Function Definition and Initial Message Handling
|
| 55 |
+
def weather_chat(user_message):
|
| 56 |
+
messages=[]
|
| 57 |
+
messages.append({"role": "user", "content": user_message})
|
| 58 |
+
messages.append({"role": "assistant", "content": "You are a weather bot . Answer only in Celsius and answer only related to weather related questions. If user asks other than weather then politely decline it"})
|
| 59 |
+
|
| 60 |
+
# Sending Initial Message to OpenAI
|
| 61 |
+
response = openai.ChatCompletion.create(
|
| 62 |
+
model="gpt-3.5-turbo",
|
| 63 |
+
temperature = 0,
|
| 64 |
+
max_tokens=256,
|
| 65 |
+
top_p=0.2,
|
| 66 |
+
frequency_penalty=0,
|
| 67 |
+
presence_penalty=0,
|
| 68 |
+
messages=messages,
|
| 69 |
+
functions=functions
|
| 70 |
+
)
|
| 71 |
+
#print(f'response1 is : {response}')
|
| 72 |
+
|
| 73 |
+
#Handling Function Calls and Fetching Weather Data
|
| 74 |
+
try:
|
| 75 |
+
function_call = response['choices'][0]['message']['function_call']
|
| 76 |
+
#print(f'function_call : {function_call}')
|
| 77 |
+
# Extract function name and arguments
|
| 78 |
+
arguments = eval(function_call['arguments']) # we are passing locatioon in arguments. this line of code is taking the string representation of the arguments returned by the OpenAI API and converting it into a usable Python dictionary, which you then use to call your get_current_weather function.
|
| 79 |
+
#print(f'arguments : {arguments}')
|
| 80 |
+
# Fetch weather data using the extracted arguments
|
| 81 |
+
weather_data = get_current_weather(arguments['location'])
|
| 82 |
+
|
| 83 |
+
# Append the function call and weather data to the messages
|
| 84 |
+
messages.append({"role": "assistant", "content": None, "function_call": {"name": "get_current_weather", "arguments": str(arguments)}})
|
| 85 |
+
messages.append({"role": "function", "name": "get_current_weather", "content": str(weather_data)})
|
| 86 |
+
|
| 87 |
+
#print(f'message : {messages}')
|
| 88 |
+
#magic of llm
|
| 89 |
+
response = openai.ChatCompletion.create(
|
| 90 |
+
model="gpt-3.5-turbo",
|
| 91 |
+
messages=messages
|
| 92 |
+
)
|
| 93 |
+
#print(f'response2 : {response}')
|
| 94 |
+
return response['choices'][0]['message']['content']
|
| 95 |
+
except Exception as e:
|
| 96 |
+
return "I'm here to provide weather updates. Please ask me questions related to weather."
|
| 97 |
+
|
| 98 |
+
#import gradio as gr
|
| 99 |
+
# Define Gradio interface
|
| 100 |
+
# iface = gr.Interface(
|
| 101 |
+
# fn=weather_chat,
|
| 102 |
+
# inputs=gr.Textbox(label="Weather Queries"),
|
| 103 |
+
# outputs=gr.Textbox(label="Weather Updates", lines=5),
|
| 104 |
+
# title = "Weather Bot",
|
| 105 |
+
# description = "Ask me anything about weather!"
|
| 106 |
+
# )
|
| 107 |
+
|
| 108 |
+
# Launch the Gradio interface
|
| 109 |
+
#iface.launch(share="True")
|
| 110 |
+
|
| 111 |
+
# To create a customized Gradio Interference, following is the prompt.
|
| 112 |
+
# import gradio as gr
|
| 113 |
+
# # Define Gradio interface
|
| 114 |
+
# iface = gr.Interface(
|
| 115 |
+
# fn=weather_chat,
|
| 116 |
+
# inputs=gr.Textbox(label="Weather Queries"),
|
| 117 |
+
# outputs=gr.Textbox(label="Weather Updates", lines=5),
|
| 118 |
+
# title = "Weather Bot",
|
| 119 |
+
# description = "Ask me anything about weather!"
|
| 120 |
+
# )
|
| 121 |
+
|
| 122 |
+
# # Launch the Gradio interface
|
| 123 |
+
# iface.launch(share="True")
|
| 124 |
+
|
| 125 |
+
# make the above gradio app more professional, use blocks and add 2 columns format, give option for famous cities in a drop down or give a option of manually submitting the city, also add the logo
|
| 126 |
+
# https://github.com/Decoding-Data-Science/airesidency/blob/main/dds_logo.jpg
|
| 127 |
+
|
| 128 |
+
# List of famous cities for the dropdown
|
| 129 |
+
FAMOUS_CITIES = [
|
| 130 |
+
"New York", "London", "Tokyo", "Paris", "Dubai",
|
| 131 |
+
"Singapore", "Hong Kong", "Sydney", "Mumbai", "Toronto",
|
| 132 |
+
"Los Angeles", "Chicago", "San Francisco", "Berlin", "Madrid",
|
| 133 |
+
"Rome", "Barcelona", "Amsterdam", "Bangkok", "Seoul",
|
| 134 |
+
"Shanghai", "Beijing", "Istanbul", "Moscow", "Mexico City"
|
| 135 |
+
]
|
| 136 |
+
|
| 137 |
+
def process_weather_query(city_dropdown, custom_city, query):
|
| 138 |
+
"""
|
| 139 |
+
Process weather query based on city selection and custom input
|
| 140 |
+
"""
|
| 141 |
+
# Determine which city to use
|
| 142 |
+
city = custom_city.strip() if custom_city.strip() else city_dropdown
|
| 143 |
+
|
| 144 |
+
# Combine city with query if query exists
|
| 145 |
+
if query.strip():
|
| 146 |
+
full_query = f"{query} in {city}"
|
| 147 |
+
else:
|
| 148 |
+
full_query = f"What's the weather in {city}?"
|
| 149 |
+
|
| 150 |
+
# Call your weather_chat function
|
| 151 |
+
response = weather_chat(full_query)
|
| 152 |
+
return response
|
| 153 |
+
|
| 154 |
+
# Create the Gradio Blocks interface
|
| 155 |
+
with gr.Blocks(theme=gr.themes.Soft(), title="Weather Bot") as iface:
|
| 156 |
+
|
| 157 |
+
# Header with logo
|
| 158 |
+
with gr.Row():
|
| 159 |
+
gr.Image(
|
| 160 |
+
"https://github.com/Decoding-Data-Science/airesidency/blob/main/dds_logo.jpg?raw=true",
|
| 161 |
+
label=None,
|
| 162 |
+
show_label=False,
|
| 163 |
+
height=100,
|
| 164 |
+
width=200,
|
| 165 |
+
show_download_button=False,
|
| 166 |
+
container=False
|
| 167 |
+
)
|
| 168 |
+
|
| 169 |
+
# Title and description
|
| 170 |
+
gr.Markdown(
|
| 171 |
+
"""
|
| 172 |
+
# π€οΈ Weather Bot
|
| 173 |
+
### Get real-time weather information for any city around the world
|
| 174 |
+
"""
|
| 175 |
+
)
|
| 176 |
+
|
| 177 |
+
# Main content with two columns
|
| 178 |
+
with gr.Row():
|
| 179 |
+
# Left Column - Input Section
|
| 180 |
+
with gr.Column(scale=1):
|
| 181 |
+
gr.Markdown("### π Select or Enter City")
|
| 182 |
+
|
| 183 |
+
city_dropdown = gr.Dropdown(
|
| 184 |
+
choices=FAMOUS_CITIES,
|
| 185 |
+
value="New York",
|
| 186 |
+
label="Choose from Popular Cities",
|
| 187 |
+
info="Select a city from the dropdown"
|
| 188 |
+
)
|
| 189 |
+
|
| 190 |
+
gr.Markdown("**OR**")
|
| 191 |
+
|
| 192 |
+
custom_city = gr.Textbox(
|
| 193 |
+
label="Enter Custom City",
|
| 194 |
+
placeholder="Type any city name...",
|
| 195 |
+
info="Leave empty to use the dropdown selection"
|
| 196 |
+
)
|
| 197 |
+
|
| 198 |
+
query_input = gr.Textbox(
|
| 199 |
+
label="Additional Query (Optional)",
|
| 200 |
+
placeholder="e.g., temperature, forecast, humidity...",
|
| 201 |
+
lines=2,
|
| 202 |
+
info="Ask specific weather questions"
|
| 203 |
+
)
|
| 204 |
+
|
| 205 |
+
with gr.Row():
|
| 206 |
+
submit_btn = gr.Button("Get Weather π", variant="primary", size="lg")
|
| 207 |
+
clear_btn = gr.Button("Clear π", variant="secondary")
|
| 208 |
+
|
| 209 |
+
# Right Column - Output Section
|
| 210 |
+
with gr.Column(scale=1):
|
| 211 |
+
gr.Markdown("### π‘οΈ Weather Information")
|
| 212 |
+
|
| 213 |
+
output = gr.Textbox(
|
| 214 |
+
label="Weather Updates",
|
| 215 |
+
lines=12,
|
| 216 |
+
placeholder="Weather information will appear here...",
|
| 217 |
+
show_copy_button=True
|
| 218 |
+
)
|
| 219 |
+
|
| 220 |
+
# Example queries
|
| 221 |
+
gr.Markdown("### π‘ Example Queries")
|
| 222 |
+
gr.Examples(
|
| 223 |
+
examples=[
|
| 224 |
+
["London", "", "What's the current temperature?"],
|
| 225 |
+
["", "Seattle", "Will it rain today?"],
|
| 226 |
+
["Tokyo", "", "7-day forecast"],
|
| 227 |
+
["", "Mumbai", "humidity levels"],
|
| 228 |
+
["Paris", "", ""]
|
| 229 |
+
],
|
| 230 |
+
inputs=[city_dropdown, custom_city, query_input],
|
| 231 |
+
label="Try these examples"
|
| 232 |
+
)
|
| 233 |
+
|
| 234 |
+
# Event handlers
|
| 235 |
+
submit_btn.click(
|
| 236 |
+
fn=process_weather_query,
|
| 237 |
+
inputs=[city_dropdown, custom_city, query_input],
|
| 238 |
+
outputs=output
|
| 239 |
+
)
|
| 240 |
+
|
| 241 |
+
clear_btn.click(
|
| 242 |
+
fn=lambda: ("New York", "", "", ""),
|
| 243 |
+
inputs=None,
|
| 244 |
+
outputs=[city_dropdown, custom_city, query_input, output]
|
| 245 |
+
)
|
| 246 |
+
|
| 247 |
+
# Footer
|
| 248 |
+
gr.Markdown(
|
| 249 |
+
"""
|
| 250 |
+
---
|
| 251 |
+
<p style='text-align: center; color: #666;'>
|
| 252 |
+
Powered by AI | Built with Gradio
|
| 253 |
+
</p>
|
| 254 |
+
"""
|
| 255 |
+
)
|
| 256 |
+
|
| 257 |
+
# Launch the interface
|
| 258 |
+
if __name__ == "__main__":
|
| 259 |
+
iface.launch(share=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio==3.38.0
|
| 2 |
+
openai==0.28
|
| 3 |
+
python-dotenv
|
weather_bot.jpg
ADDED
|
Git LFS Details
|