Spaces:
Sleeping
Sleeping
File size: 12,341 Bytes
de6500f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
import requests
from collections import defaultdict
from datetime import datetime, timedelta
import json
import pandas as pd
import threading
import time
import os
import signal
api_key = "c6dfc4d92a8f972d237ef696ec87b37a"
def shutdown():
# Wait a bit before shutdown to allow the response to be returned
def stop():
time.sleep(1)
os.kill(os.getpid(), signal.SIGTERM) # Send SIGTERM to the current process to stop Gradio
os._exit(0)
threading.Thread(target=stop).start()
return "Shutting down and closing the Gradio window..."
def get_weather_info(city):
"""Fetches current weather information for a city using OpenWeatherMap API."""
url_current = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}&units=metric"
response_current = requests.get(url_current)
if response_current.status_code != 200:
return "Error: Could not fetch weather data."
data_current = response_current.json()
weather_description = data_current['weather'][0]['description']
temperature_current = data_current['main']['temp']
temperature_feels_like = data_current['main']['feels_like']
temperature_min = data_current['main']['temp_min']
temperature_max = data_current['main']['temp_max']
pressure_sea_level = data_current['main'].get('sea_level', data_current['main']['pressure'])
pressure_ground_level = data_current['main'].get('grnd_level', data_current['main']['pressure'])
humidity = data_current['main']['humidity']
visibility = data_current['visibility']
wind_speed = data_current['wind']['speed']
wind_deg = data_current['wind']['deg']
clouds = data_current['clouds']['all']
rain = data_current.get('rain', {}).get('1h', 0)
dt = datetime.utcfromtimestamp(data_current['dt']).strftime('%Y-%m-%d %H:%M:%S')
timezone = data_current['timezone']
city_name = data_current['name']
response_code = data_current['cod']
formatted_info = (
f"Weather: {weather_description}, "
f"Temperature: current {temperature_current}°C, feels like {temperature_feels_like}°C, min {temperature_min}°C, max {temperature_max}°C, "
f"Pressure: sea level {pressure_sea_level} hPa, ground level {pressure_ground_level} hPa, "
f"Humidity: {humidity}%, "
f"Visibility: {visibility} meters, "
f"Wind: speed {wind_speed} m/s, deg {wind_deg}, "
f"Clouds: {clouds}%, "
f"Rain: {rain} mm, "
f"Date/Time: {dt}, "
f"Timezone: {timezone} seconds, "
f"City Name: {city_name}, "
f"Response Code: {response_code}"
)
return formatted_info
def get_forecast(city):
"""Fetches 2-day weather forecast for a city using OpenWeatherMap API and restructures the data into a table format."""
url_forecast = f"http://api.openweathermap.org/data/2.5/forecast?q={city}&appid={api_key}&units=metric"
response_forecast = requests.get(url_forecast)
if response_forecast.status_code != 200:
return "Error: Could not fetch forecast data."
forecast_json = response_forecast.json()
current_date = datetime.now().date()
forecast_dates = [current_date + timedelta(days=i) for i in range(1, 3)]
important_hours = ['09:00:00', '15:00:00', '21:00:00']
data = []
for entry in forecast_json['list']:
date, time = entry['dt_txt'].split()
date_obj = datetime.strptime(date, '%Y-%m-%d').date()
if date_obj in forecast_dates and time in important_hours:
data.append({
'Date': date,
'Time': time,
'Temperature': entry['main']['temp'],
'Feels Like': entry['main']['feels_like'],
'Temp Min': entry['main']['temp_min'],
'Temp Max': entry['main']['temp_max'],
'Pressure': entry['main']['pressure'],
'Humidity': entry['main']['humidity'],
'Weather': entry['weather'][0]['description'],
'Icon': entry['weather'][0]['icon'],
'Wind Speed': entry['wind']['speed'],
'Wind Deg': entry['wind']['deg'],
'Visibility': entry['visibility'],
'Pop': entry['pop'],
'Rain': entry['rain']['3h'] if 'rain' in entry else 0,
'Clouds': entry['clouds']['all']
})
df = pd.DataFrame(data)
df.set_index(['Date', 'Time'], inplace=True)
return df
def restructure_forecast_00(forecast_json):
"""Restructures the forecast JSON data into a single-line sentence format."""
current_date = datetime.now().date()
forecast_dates = [current_date + timedelta(days=i) for i in range(1, 3)]
important_hours = ['09:00:00', '12:00:00', '15:00:00', '18:00:00', '21:00:00']
structured_data = defaultdict(dict)
for entry in forecast_json['list']:
date, time = entry['dt_txt'].split()
date_obj = datetime.strptime(date, '%Y-%m-%d').date()
if date_obj in forecast_dates and time in important_hours:
structured_data[date][time] = {
'temperature': entry['main']['temp'],
'feels like': entry['main']['feels_like'],
'temp min': entry['main']['temp_min'],
'temp max': entry['main']['temp_max'],
'pressure': entry['main']['pressure'],
'humidity': entry['main']['humidity'],
'weather': entry['weather'][0]['description'],
'icon': entry['weather'][0]['icon'],
'wind speed': entry['wind']['speed'],
'wind deg': entry['wind']['deg'],
'visibility': entry['visibility'],
'pop': entry['pop'],
'rain': entry['rain']['3h'] if 'rain' in entry else 0,
'clouds': entry['clouds']['all']
}
return format_forecast(structured_data, forecast_dates)
def format_forecast_00(structured_data, forecast_dates):
"""Formats the structured forecast data into a single-line sentence format."""
formatted_forecast = []
for date in forecast_dates:
date_str = str(date)
for time, data in structured_data[date_str].items():
formatted_forecast.append(
f"{date_str} : {time} ( " +
", ".join(f"{key} - {value}" for key, value in data.items()) +
" )"
)
return "\n".join(formatted_forecast)
def restructure_forecast2(forecast_json):
"""Restructures the forecast JSON data into a nested dictionary by date and time, including the next three days."""
current_date = datetime.now().date()
forecast_dates = [current_date + timedelta(days=i) for i in range(1, 3)]
structured_data = defaultdict(dict)
for entry in forecast_json['list']:
date, time = entry['dt_txt'].split()
date_obj = datetime.strptime(date, '%Y-%m-%d').date()
if date_obj in forecast_dates:
structured_data[date][time] = {
'temperature': entry['main']['temp'],
'feels_like': entry['main']['feels_like'],
'temp_min': entry['main']['temp_min'],
'temp_max': entry['main']['temp_max'],
'pressure': entry['main']['pressure'],
'humidity': entry['main']['humidity'],
'weather': entry['weather'][0]['description'],
'icon': entry['weather'][0]['icon'],
'wind_speed': entry['wind']['speed'],
'wind_deg': entry['wind']['deg'],
'visibility': entry['visibility'],
'pop': entry['pop'],
'rain': entry['rain']['3h'] if 'rain' in entry else 0,
'clouds': entry['clouds']['all']
}
return {str(date): structured_data[str(date)] for date in forecast_dates}
def restructure_forecast_0(forecast_json):
"""Restructures the forecast JSON data into a nested dictionary by date and specific times."""
current_date = datetime.now().date()
forecast_dates = [current_date + timedelta(days=i) for i in range(1, 3)]
important_hours = ['09:00:00', '12:00:00', '15:00:00', '18:00:00', '21:00:00']
structured_data = defaultdict(dict)
for entry in forecast_json['list']:
date, time = entry['dt_txt'].split()
date_obj = datetime.strptime(date, '%Y-%m-%d').date()
if date_obj in forecast_dates and time in important_hours:
structured_data[date][time] = {
'temperature': entry['main']['temp'],
'feels_like': entry['main']['feels_like'],
'temp_min': entry['main']['temp_min'],
'temp_max': entry['main']['temp_max'],
'pressure': entry['main']['pressure'],
'humidity': entry['main']['humidity'],
'weather': entry['weather'][0]['description'],
'icon': entry['weather'][0]['icon'],
'wind_speed': entry['wind']['speed'],
'wind_deg': entry['wind']['deg'],
'visibility': entry['visibility'],
'pop': entry['pop'],
'rain': entry['rain']['3h'] if 'rain' in entry else 0,
'clouds': entry['clouds']['all']
}
return {str(date): structured_data[str(date)] for date in forecast_dates}
def restructure_forecast3(forecast_json):
"""Restructures the forecast JSON data into a nested dictionary by date and time, including the next three days."""
current_date = datetime.now().date()
forecast_dates = [current_date + timedelta(days=i) for i in range(1, 4)]
structured_data = defaultdict(dict)
for entry in forecast_json['list']:
date, time = entry['dt_txt'].split()
date_obj = datetime.strptime(date, '%Y-%m-%d').date()
if date_obj in forecast_dates:
structured_data[date][time] = {
'temperature': entry['main']['temp'],
'feels_like': entry['main']['feels_like'],
'temp_min': entry['main']['temp_min'],
'temp_max': entry['main']['temp_max'],
'pressure': entry['main']['pressure'],
'humidity': entry['main']['humidity'],
'weather': entry['weather'][0]['description'],
'icon': entry['weather'][0]['icon'],
'wind_speed': entry['wind']['speed'],
'wind_deg': entry['wind']['deg'],
'visibility': entry['visibility'],
'pop': entry['pop'],
'rain': entry['rain']['3h'] if 'rain' in entry else 0,
'clouds': entry['clouds']['all']
}
return {str(date): structured_data[str(date)] for date in forecast_dates}
def get_weather_info_0(city):
"""Fetches current weather information for a city using OpenWeatherMap API."""
url_current = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}&units=metric"
response_current = requests.get(url_current)
if response_current.status_code != 200:
return "Error: Could not fetch weather data."
data_current = response_current.json()
response = {
'coordinates': data_current['coord'],
'weather': data_current['weather'][0],
'temperature': {
'current': data_current['main']['temp'],
'feels_like': data_current['main']['feels_like'],
'min': data_current['main']['temp_min'],
'max': data_current['main']['temp_max']
},
'pressure': {
'sea_level': data_current['main'].get('sea_level', data_current['main']['pressure']),
'ground_level': data_current['main'].get('grnd_level', data_current['main']['pressure'])
},
'humidity': data_current['main']['humidity'],
'visibility': data_current['visibility'],
'wind': data_current['wind'],
'clouds': data_current['clouds'],
'rain': data_current.get('rain', {}),
'dt': data_current['dt'],
'sys': data_current['sys'],
'timezone': data_current['timezone'],
'id': data_current['id'],
'name': data_current['name'],
'cod': data_current['cod']
}
return json.dumps(response, indent=2)
|