ArceusInception commited on
Commit
b72ccd7
·
verified ·
1 Parent(s): 4ce246c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -13
app.py CHANGED
@@ -3,9 +3,9 @@ import requests
3
  import os
4
 
5
  def get_weather(city_name):
6
- API_Key = os.getenv("OPENWEATHER_API_KEY") # Fetch the API key from environment variables
7
  if not API_Key:
8
- return "API Key is not set. Please set the OPENWEATHER_API_KEY environment variable.", "", ""
9
 
10
  url = f'https://api.openweathermap.org/data/2.5/weather?q={city_name}&appid={API_Key}&units=metric'
11
  response = requests.get(url)
@@ -13,23 +13,24 @@ def get_weather(city_name):
13
  if response.status_code == 200:
14
  data = response.json()
15
  weather = data['weather'][0]['description']
16
- temp = f"Current Temperature: {data['main']['temp']}°C"
17
- humidity = f"Humidity: {data['main']['humidity']}%"
18
- return weather, temp, humidity
 
 
 
 
 
19
  else:
20
- return "Failed to retrieve data", "", ""
21
 
22
  # Define Gradio interface
23
  iface = gr.Interface(
24
  fn=get_weather,
25
- inputs=gr.Textbox(label="Enter City Name"),
26
- outputs=[
27
- gr.Textbox(label="Weather"),
28
- gr.Textbox(label="Temperature"),
29
- gr.Textbox(label="Humidity")
30
- ],
31
  title="Weather App",
32
- description="Enter a city name to get the current weather description, temperature, and humidity."
33
  )
34
 
35
  # Launch the interface
 
3
  import os
4
 
5
  def get_weather(city_name):
6
+ API_Key = os.getenv("OPENWEATHER_API_KEY")
7
  if not API_Key:
8
+ return "API Key is not set. Please set the OPENWEATHER_API_KEY environment variable."
9
 
10
  url = f'https://api.openweathermap.org/data/2.5/weather?q={city_name}&appid={API_Key}&units=metric'
11
  response = requests.get(url)
 
13
  if response.status_code == 200:
14
  data = response.json()
15
  weather = data['weather'][0]['description']
16
+ temp = data['main']['temp']
17
+ humidity = data['main']['humidity']
18
+
19
+ # Adding emojis and creative text
20
+ weather_emoji = "☀️" if "clear" in weather else "☁️" if "cloud" in weather else "🌧️" if "rain" in weather else "❄️" if "snow" in weather else "🌫️"
21
+ return (f"In {city_name}, the weather is currently {weather} {weather_emoji}. "
22
+ f"The temperature is a comfy {temp}°C 🌡️. "
23
+ f"Also, humidity levels are at {humidity}%, so keep hydrated! 💧")
24
  else:
25
+ return "Failed to retrieve data. Please check the city name and try again."
26
 
27
  # Define Gradio interface
28
  iface = gr.Interface(
29
  fn=get_weather,
30
+ inputs=gr.Textbox(label="Enter City Name", placeholder="Type here..."),
31
+ outputs=gr.Textbox(label="Weather Update"),
 
 
 
 
32
  title="Weather App",
33
+ description="Enter a city name to get a lively description of the current weather, temperature, and humidity."
34
  )
35
 
36
  # Launch the interface