Spaces:
Running
Running
Thiwanka01
commited on
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import requests
|
3 |
+
|
4 |
+
# Function to get real-time weather data from OpenWeatherMap
|
5 |
+
def get_weather_data(city):
|
6 |
+
# Replace with your own API key from OpenWeatherMap
|
7 |
+
api_key = "b66e24b9c417559e4302f9e0253b70d8"
|
8 |
+
base_url = "http://api.openweathermap.org/data/2.5/weather?"
|
9 |
+
|
10 |
+
# Create the complete URL
|
11 |
+
complete_url = f"{base_url}q={city}&appid={api_key}&units=metric"
|
12 |
+
|
13 |
+
# Send request to the OpenWeatherMap API
|
14 |
+
try:
|
15 |
+
response = requests.get(complete_url)
|
16 |
+
data = response.json()
|
17 |
+
|
18 |
+
# Check if the request was successful
|
19 |
+
if data.get("cod") != "404": # Successful request
|
20 |
+
main_data = data["main"]
|
21 |
+
weather_data = data["weather"][0]
|
22 |
+
|
23 |
+
# Extract necessary weather data
|
24 |
+
temperature = main_data["temp"]
|
25 |
+
weather_type = weather_data["main"]
|
26 |
+
humidity = main_data["humidity"]
|
27 |
+
light_intensity = 0.8 if weather_type == "Clear" else 0.5 # Simplified for demo
|
28 |
+
|
29 |
+
return temperature, weather_type, light_intensity
|
30 |
+
else:
|
31 |
+
return None, None, None # If city is not found or error occurs
|
32 |
+
except Exception as e:
|
33 |
+
return None, None, None # In case of any network or API issues
|
34 |
+
|
35 |
+
# Example function that simulates clothing changes based on external factors
|
36 |
+
def adaptive_clothing(city):
|
37 |
+
temperature, weather_type, light_intensity = get_weather_data(city)
|
38 |
+
|
39 |
+
if temperature is None:
|
40 |
+
return "Error: Could not retrieve weather data. Please check the city name or try again later."
|
41 |
+
|
42 |
+
# Dummy logic for adaptive clothing adjustments based on weather data
|
43 |
+
if weather_type == "Rain":
|
44 |
+
color = "Dark Blue"
|
45 |
+
insulation = "High"
|
46 |
+
elif weather_type == "Clear":
|
47 |
+
color = "Bright Yellow"
|
48 |
+
insulation = "Low"
|
49 |
+
elif temperature < 10:
|
50 |
+
color = "Light Blue"
|
51 |
+
insulation = "High"
|
52 |
+
elif temperature > 25:
|
53 |
+
color = "Light Pink"
|
54 |
+
insulation = "Low"
|
55 |
+
else:
|
56 |
+
color = "Neutral Grey"
|
57 |
+
insulation = "Medium"
|
58 |
+
|
59 |
+
# Simulate pattern change based on light intensity
|
60 |
+
pattern = "Striped" if light_intensity > 0.7 else "Solid"
|
61 |
+
|
62 |
+
return f"Weather: {weather_type}, Temperature: {temperature}°C, Recommended Color: {color}, Insulation: {insulation}, Pattern: {pattern}"
|
63 |
+
|
64 |
+
# Gradio interface with real-time weather-based adaptive clothing
|
65 |
+
def create_gradio_interface():
|
66 |
+
# Create a Gradio interface where users input a city name to get adaptive clothing suggestions
|
67 |
+
interface = gr.Interface(
|
68 |
+
fn=adaptive_clothing,
|
69 |
+
inputs=gr.Textbox(label="Enter City Name"),
|
70 |
+
outputs=gr.Textbox(label="Adaptive Clothing Suggestions"),
|
71 |
+
live=True, # Enables live updates
|
72 |
+
description="Enter a city name to get real-time weather-based adaptive clothing suggestions."
|
73 |
+
)
|
74 |
+
interface.launch()
|
75 |
+
|
76 |
+
create_gradio_interface()
|