Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import requests | |
| import os | |
| from huggingface_hub import InferenceClient | |
| def preprocess_city_name(city_name): | |
| token = os.getenv("HF_TOKEN") | |
| if not token: | |
| return "Hugging Face token is not set in the environment variables." | |
| client = InferenceClient(model="gpt2", token=token) | |
| result = client(inputs=city_name) | |
| # Assuming the result contains a more standardized city name | |
| corrected_city_name = result['generated_text'].strip() | |
| return corrected_city_name | |
| def get_weather(city_name): | |
| corrected_city_name = preprocess_city_name(city_name) | |
| API_Key = os.getenv("OPENWEATHER_API_KEY") | |
| if not API_Key: | |
| return "API Key is not set. Please set the OPENWEATHER_API_KEY environment variable." | |
| url = f'https://api.openweathermap.org/data/2.5/weather?q={corrected_city_name}&appid={API_Key}&units=metric' | |
| response = requests.get(url) | |
| if response.status_code == 200: | |
| data = response.json() | |
| weather = data['weather'][0]['description'] | |
| temp = data['main']['temp'] | |
| humidity = data['main']['humidity'] | |
| weather_emoji = "☀️" if "clear" in weather else "☁️" if "cloud" in weather else "🌧️" if "rain" in weather else "❄️" if "snow" in weather else "🌫️" | |
| return (f"In {corrected_city_name}, the weather is currently {weather} {weather_emoji}. " | |
| f"The temperature is a comfy {temp}°C 🌡️. " | |
| f"And the humidity levels are at {humidity}%, so keep hydrated! 💧") | |
| else: | |
| return "Failed to retrieve data. Please check the city name and try again." | |
| # Defining Gradio interface | |
| iface = gr.Interface( | |
| fn=get_weather, | |
| inputs=gr.Textbox(label="Enter City Name", placeholder="Type here..."), | |
| outputs=gr.Textbox(label="Weather Update"), | |
| title="Weather App with AI City Name Correction", | |
| description="Enter a city name to get a lively description of the current weather, temperature, and humidity. The city name input is preprocessed by GPT-2 to correct any errors or standardize naming." | |
| ) | |
| # Launching the interface | |
| iface.launch() | |