Spaces:
Running
Running
import gradio as gr | |
import requests | |
# Function to get real-time weather data from OpenWeatherMap | |
def get_weather_data(city): | |
# Replace with your own API key from OpenWeatherMap | |
api_key = "b66e24b9c417559e4302f9e0253b70d8" | |
base_url = "http://api.openweathermap.org/data/2.5/weather?" | |
# Create the complete URL | |
complete_url = f"{base_url}q={city}&appid={api_key}&units=metric" | |
# Send request to the OpenWeatherMap API | |
try: | |
response = requests.get(complete_url) | |
data = response.json() | |
# Check if the request was successful | |
if data.get("cod") != "404": # Successful request | |
main_data = data["main"] | |
weather_data = data["weather"][0] | |
# Extract necessary weather data | |
temperature = main_data["temp"] | |
weather_type = weather_data["main"] | |
humidity = main_data["humidity"] | |
light_intensity = 0.8 if weather_type == "Clear" else 0.5 # Simplified for demo | |
return temperature, weather_type, light_intensity | |
else: | |
return None, None, None # If city is not found or error occurs | |
except Exception as e: | |
return None, None, None # In case of any network or API issues | |
# Example function that simulates clothing changes based on external factors | |
def adaptive_clothing(city): | |
temperature, weather_type, light_intensity = get_weather_data(city) | |
if temperature is None: | |
return "Error: Could not retrieve weather data. Please check the city name or try again later." | |
# Dummy logic for adaptive clothing adjustments based on weather data | |
if weather_type == "Rain": | |
color = "Dark Blue" | |
insulation = "High" | |
elif weather_type == "Clear": | |
color = "Bright Yellow" | |
insulation = "Low" | |
elif temperature < 10: | |
color = "Light Blue" | |
insulation = "High" | |
elif temperature > 25: | |
color = "Light Pink" | |
insulation = "Low" | |
else: | |
color = "Neutral Grey" | |
insulation = "Medium" | |
# Simulate pattern change based on light intensity | |
pattern = "Striped" if light_intensity > 0.7 else "Solid" | |
return f"Weather: {weather_type}, Temperature: {temperature}°C, Recommended Color: {color}, Insulation: {insulation}, Pattern: {pattern}" | |
# Gradio interface with real-time weather-based adaptive clothing | |
def create_gradio_interface(): | |
# Create a Gradio interface where users input a city name to get adaptive clothing suggestions | |
interface = gr.Interface( | |
fn=adaptive_clothing, | |
inputs=gr.Textbox(label="Enter City Name"), | |
outputs=gr.Textbox(label="Adaptive Clothing Suggestions"), | |
live=True, # Enables live updates | |
description="Enter a city name to get real-time weather-based adaptive clothing suggestions." | |
) | |
interface.launch() | |
create_gradio_interface() | |