Weather_bot / app.py
Kiruthikaramalingam's picture
Update app.py
8033f4d verified
import openai
import gradio as gr
import os
import requests
from datetime import datetime, timedelta
# βœ… API keys
openai.api_key = os.getenv("OPENAI_API_KEY")
OWM_API_KEY = os.getenv("OPENWEATHER_API_KEY")
# βœ… GPT-based Weather Response
def weather_response(top_city, custom_city, user_query):
city = custom_city.strip() if custom_city and custom_city.strip() != "" else top_city
if not city or not user_query:
return "❗ Please enter both a city and a weather question."
prompt = f"""
You are a friendly Gulf-region weather assistant. Answer this user query: '{user_query}' in {city}.
Include temperature, advice, and 2+ relevant emojis (🌑️, β˜€οΈ, 🌧️, etc.).
"""
try:
response = openai.ChatCompletion.create(
model="gpt-4o",
messages=[
{"role": "system", "content": "You are a cheerful Gulf weather bot with emojis."},
{"role": "user", "content": prompt}
]
)
return response.choices[0].message["content"].strip()
except Exception as e:
return f"⚠️ OpenAI Error: {e}"
# βœ… Tomorrow's Forecast (Real)
def forecast_tomorrow(top_city, custom_city):
city = custom_city.strip() if custom_city and custom_city.strip() != "" else top_city
if not city:
return "❗ Please select or enter a city."
try:
url = f"http://api.openweathermap.org/data/2.5/forecast?q={city}&appid={OWM_API_KEY}&units=metric"
response = requests.get(url)
data = response.json()
tomorrow = data['list'][8] # ~24 hours later
date = datetime.fromtimestamp(tomorrow['dt']).strftime("%A, %B %d")
desc = tomorrow['weather'][0]['description'].title()
temp = tomorrow['main']['temp']
humidity = tomorrow['main']['humidity']
wind = tomorrow['wind']['speed']
return f"""πŸ“ **Forecast for {city.title()} – {date}**
- 🌑️ Temp: {temp}°C
- 🌬️ Wind: {wind} m/s
- πŸ’§ Humidity: {humidity}%
- 🌀️ Condition: {desc}
Stay safe and enjoy your day! β˜€οΈ
"""
except Exception as e:
return f"⚠️ Could not fetch weather data: {str(e)}"
# βœ… 5-Day Forecast (Real)
def forecast_week(top_city, custom_city):
city = custom_city.strip() if custom_city and custom_city.strip() != "" else top_city
if not city:
return "❗ Please select or enter a city."
try:
url = f"http://api.openweathermap.org/data/2.5/forecast?q={city}&appid={OWM_API_KEY}&units=metric"
response = requests.get(url)
data = response.json()
if "list" not in data:
return f"⚠️ Error from OpenWeather: {data.get('message', 'No data returned')}"
forecasts = data["list"]
forecast_msg = f"πŸ“… **5-Day Forecast for {city.title()}**\n\n"
used_days = set()
for entry in forecasts:
dt = datetime.fromtimestamp(entry["dt"])
day = dt.strftime("%A, %B %d")
if day in used_days:
continue
temp = entry["main"]["temp"]
desc = entry["weather"][0]["description"].title()
humidity = entry["main"]["humidity"]
wind = entry["wind"]["speed"]
forecast_msg += f"πŸ“Œ **{day}**\n🌀️ {desc}\n🌑️ {temp}Β°C | πŸ’§ {humidity}% | 🌬️ {wind} m/s\n\n"
used_days.add(day)
if len(used_days) == 5:
break
if not used_days:
return "⚠️ Could not extract 5-day forecast. Try again later."
return forecast_msg.strip()
except Exception as e:
return f"⚠️ Forecast error: {str(e)}"
# βœ… City List
gulf_cities = [
"Dubai", "Abu Dhabi", "Doha", "Riyadh", "Jeddah", "Muscat",
"Kuwait City", "Manama", "Amman", "Beirut", "Baghdad"
]
# βœ… UI
with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue")) as app:
gr.Markdown("""
<h2 style='text-align: center;'>🌞 <b>Real-Time Weather Bot - Powered by OpenWeather</b> 🌞</h2>
<p style='text-align: center;'>Ask questions or get actual forecasts ⬇️</p>
""")
with gr.Row():
with gr.Column():
top_city = gr.Dropdown(choices=gulf_cities, label="Top Gulf Cities", value=None,allow_custom_value=True )
custom_city = gr.Textbox(label="Or enter a city not listed")
with gr.Column():
query = gr.Textbox(label="Ask a Weather Question (GPT)", placeholder="e.g., Is it hot in Muscat?")
result = gr.Textbox(label="🌀️ Weather Update", lines=10, interactive=False)
with gr.Row():
submit_btn = gr.Button("🌦️ GPT Answer")
tomorrow_btn = gr.Button("β˜€οΈ Real Tomorrow Forecast")
week_btn = gr.Button("πŸ“… Real 5-Day Forecast")
clear_btn = gr.Button("🧹 Clear All")
submit_btn.click(fn=weather_response, inputs=[top_city, custom_city, query], outputs=result)
tomorrow_btn.click(fn=forecast_tomorrow, inputs=[top_city, custom_city], outputs=result)
week_btn.click(fn=forecast_week, inputs=[top_city, custom_city], outputs=result)
clear_btn.click(fn=lambda: ("", "", "", ""), inputs=[], outputs=[top_city, custom_city, query, result])
app.launch()