Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
import openai
|
3 |
+
import gradio as gr
|
4 |
+
import re
|
5 |
+
|
6 |
+
OPENWEATHER_API_KEY = "b997eb592e775cf75a8a7504c93f1e5d"
|
7 |
+
OPENAI_API_KEY = "sk-proj-y4J48FDxQppGfxbTlf7iT3BlbkFJYwhSmv6QDXRKyeV386eh"
|
8 |
+
|
9 |
+
def get_weather(city):
|
10 |
+
url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={OPENWEATHER_API_KEY}&units=metric"
|
11 |
+
response = requests.get(url).json()
|
12 |
+
|
13 |
+
if response["cod"] == 200:
|
14 |
+
temp = response["main"]["temp"]
|
15 |
+
condition = response["weather"][0]["description"]
|
16 |
+
return f"The temperature in {city} is {temp}°C with {condition}."
|
17 |
+
else:
|
18 |
+
return "City not found."
|
19 |
+
|
20 |
+
def ask_ai_about_weather(city):
|
21 |
+
weather_info = get_weather(city)
|
22 |
+
|
23 |
+
response = openai.chat.completions.create(
|
24 |
+
model="gpt-4",
|
25 |
+
messages=[
|
26 |
+
{"role": "system", "content": "You are a weather assistant, answer using the provided content below from openweather"},
|
27 |
+
{"role": "user", "content": f"What is the weather in {city}?"},
|
28 |
+
{"role": "assistant", "content": weather_info}
|
29 |
+
]
|
30 |
+
)
|
31 |
+
|
32 |
+
return response.choices[0].message.content
|
33 |
+
|
34 |
+
def extract_city_from_query(query):
|
35 |
+
# Pattern to match "weather in CITY" or just "CITY"
|
36 |
+
patterns = [
|
37 |
+
r"(?:weather in|weather for|weather at|weather of|what is the weather in|how is the weather in)\s+([a-zA-Z\s]+)(?:\?)?",
|
38 |
+
r"^([a-zA-Z\s]+)$" # Just the city name
|
39 |
+
]
|
40 |
+
|
41 |
+
for pattern in patterns:
|
42 |
+
match = re.search(pattern, query.lower().strip())
|
43 |
+
if match:
|
44 |
+
return match.group(1).strip()
|
45 |
+
|
46 |
+
return None
|
47 |
+
|
48 |
+
# Wrapper function for Gradio
|
49 |
+
def process_query(query):
|
50 |
+
city = extract_city_from_query(query)
|
51 |
+
|
52 |
+
if city:
|
53 |
+
try:
|
54 |
+
return ask_ai_about_weather(city)
|
55 |
+
except Exception as e:
|
56 |
+
return f"An error occurred: {str(e)}"
|
57 |
+
else:
|
58 |
+
return "I couldn't determine which city you're asking about. Please provide a city name or ask about the weather in a specific location."
|
59 |
+
|
60 |
+
# Create Gradio Interface
|
61 |
+
with gr.Blocks(title="Weather Assistant") as demo:
|
62 |
+
gr.Markdown("# Weather Information Center")
|
63 |
+
gr.Markdown("Ask about the weather in any city around the world.")
|
64 |
+
|
65 |
+
with gr.Row():
|
66 |
+
with gr.Column():
|
67 |
+
query_input = gr.Textbox(label="Your Question", placeholder="What is the weather in London?", lines=2)
|
68 |
+
submit_btn = gr.Button("Get Weather")
|
69 |
+
|
70 |
+
output = gr.Textbox(label="Weather Information", lines=10)
|
71 |
+
|
72 |
+
submit_btn.click(fn=process_query, inputs=query_input, outputs=output)
|
73 |
+
|
74 |
+
gr.Examples(
|
75 |
+
examples=[
|
76 |
+
["What is the weather in London?"],
|
77 |
+
["Weather in Tokyo"],
|
78 |
+
["New York"],
|
79 |
+
["How is the weather in Lagos?"]
|
80 |
+
],
|
81 |
+
inputs=query_input
|
82 |
+
)
|
83 |
+
|
84 |
+
# Launch the app with public sharing enabled
|
85 |
+
if __name__ == "__main__":
|
86 |
+
demo.launch(share=True) # share=True creates a public URL
|