Spaces:
Sleeping
Sleeping
File size: 2,938 Bytes
54e1a44 |
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 |
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()
|