Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
|
| 4 |
+
def get_weather(city):
|
| 5 |
+
api_key = "YOUR_REAL_API_KEY" # not recommended for public repos
|
| 6 |
+
url = f"https://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}&units=metric"
|
| 7 |
+
response = requests.get(url)
|
| 8 |
+
data = response.json()
|
| 9 |
+
if data.get("cod") == 200:
|
| 10 |
+
temp = data["main"]["temp"]
|
| 11 |
+
description = data["weather"][0]["description"]
|
| 12 |
+
return f"The current temperature in {city} is {temp}°C with {description}."
|
| 13 |
+
else:
|
| 14 |
+
return "Sorry, I couldn't find weather information for that city."
|
| 15 |
+
|
| 16 |
+
iface = gr.Interface(fn=get_weather, inputs="text", outputs="text")
|
| 17 |
+
|
| 18 |
+
if __name__ == "__main__":
|
| 19 |
+
iface.launch()
|