vigneshgautam commited on
Commit
db806bf
·
verified ·
1 Parent(s): 82a0540

Code As It is on CoLab

Browse files
Files changed (1) hide show
  1. app.py +17 -40
app.py CHANGED
@@ -1,11 +1,6 @@
1
- # Install required packages (uncomment if running in Colab)
2
- # !pip install gradio
3
- # !pip install folium
4
-
5
  import requests
6
  import gradio as gr
7
 
8
-
9
  # OpenWeatherMap API key
10
  API_KEY = "ff3fe364119d79a928965cae2f3cd040" # Replace with your OpenWeatherMap API key
11
 
@@ -18,7 +13,7 @@ def get_weather(city_name):
18
  "units": "metric" # Fetch data in Celsius
19
  }
20
  response = requests.get(base_url, params=params)
21
-
22
  if response.status_code == 200:
23
  data = response.json()
24
  city = data.get('name', 'Unknown')
@@ -26,9 +21,7 @@ def get_weather(city_name):
26
  feels_like = data['main'].get('feels_like', 'N/A')
27
  humidity = data['main'].get('humidity', 'N/A')
28
  weather_desc = data['weather'][0].get('description', 'N/A')
29
- lat = data['coord']['lat']
30
- lon = data['coord']['lon']
31
-
32
  result = (
33
  f"Weather in {city}:\n"
34
  f"Temperature: {temp}°C\n"
@@ -36,53 +29,37 @@ def get_weather(city_name):
36
  f"Humidity: {humidity}%\n"
37
  f"Condition: {weather_desc.capitalize()}"
38
  )
39
- return result, lat, lon
40
  else:
41
  result = "Could not retrieve weather data. Please check the city name and try again."
42
- return result, None, None
43
 
44
- # Function to generate the map
45
- def generate_map(lat, lon, weather_info):
46
- weather_map = folium.Map(location=[lat, lon], zoom_start=10)
47
- folium.Marker(
48
- location=[lat, lon],
49
- popup=folium.Popup(weather_info, max_width=300),
50
- tooltip="Click for weather details"
51
- ).add_to(weather_map)
52
-
53
- map_file = "weather_map.html"
54
- weather_map.save(map_file)
55
- with open(map_file, "r") as f:
56
- map_html = f.read()
57
- return map_html
58
 
59
- # Gradio interface function
60
  def gradio_interface(city_name):
61
- weather_info, lat, lon = get_weather(city_name)
62
- if lat is not None and lon is not None:
63
- map_html = generate_map(lat, lon, weather_info)
64
- return weather_info, map_html
65
- else:
66
- return weather_info, "<p>Map could not be generated.</p>"
67
 
68
  # Set up the Gradio app
69
  autocomplete_box = gr.Textbox(
70
  label="Enter City Name",
71
  placeholder="Start typing a city name...",
 
72
  lines=1
73
  )
74
 
75
  interface = gr.Interface(
76
  fn=gradio_interface,
77
  inputs=autocomplete_box,
78
- outputs=[
79
- "text", # Original weather details output
80
- gr.HTML(label="Weather Map") # Map display in the same Gradio context
81
- ],
82
- title="Weather Checker with Map",
83
- description="Enter a city name to get the current weather and view it on a map."
84
  )
85
 
86
- # Launch the Gradio app with shareable link
87
  if __name__ == "__main__":
88
- interface.launch(share=True)
 
 
 
 
 
1
  import requests
2
  import gradio as gr
3
 
 
4
  # OpenWeatherMap API key
5
  API_KEY = "ff3fe364119d79a928965cae2f3cd040" # Replace with your OpenWeatherMap API key
6
 
 
13
  "units": "metric" # Fetch data in Celsius
14
  }
15
  response = requests.get(base_url, params=params)
16
+
17
  if response.status_code == 200:
18
  data = response.json()
19
  city = data.get('name', 'Unknown')
 
21
  feels_like = data['main'].get('feels_like', 'N/A')
22
  humidity = data['main'].get('humidity', 'N/A')
23
  weather_desc = data['weather'][0].get('description', 'N/A')
24
+
 
 
25
  result = (
26
  f"Weather in {city}:\n"
27
  f"Temperature: {temp}°C\n"
 
29
  f"Humidity: {humidity}%\n"
30
  f"Condition: {weather_desc.capitalize()}"
31
  )
 
32
  else:
33
  result = "Could not retrieve weather data. Please check the city name and try again."
 
34
 
35
+ return result
36
+
37
+ # List of cities for autocomplete (Add more cities as needed)
38
+ city_suggestions = [
39
+ "Dubai", "London", "New York", "Tokyo", "Sydney", "Paris",
40
+ "Berlin", "Moscow", "Mumbai", "Beijing", "Rome", "Los Angeles"
41
+ ]
 
 
 
 
 
 
 
42
 
43
+ # Gradio interface
44
  def gradio_interface(city_name):
45
+ return get_weather(city_name)
 
 
 
 
 
46
 
47
  # Set up the Gradio app
48
  autocomplete_box = gr.Textbox(
49
  label="Enter City Name",
50
  placeholder="Start typing a city name...",
51
+ #autocomplete="on", # Enables autocomplete
52
  lines=1
53
  )
54
 
55
  interface = gr.Interface(
56
  fn=gradio_interface,
57
  inputs=autocomplete_box,
58
+ outputs="text",
59
+ title="Weather Checker",
60
+ description="Enter a city name to get the current weather. Autocomplete suggestions are available."
 
 
 
61
  )
62
 
63
+ # Launch the Gradio app
64
  if __name__ == "__main__":
65
+ interface.launch()