weather-test / app.py
decodingdatascience's picture
Create app.py
0ade6d5 verified
raw
history blame contribute delete
698 Bytes
import gradio as gr
import requests
def get_weather(city):
api_key = "YOUR_REAL_API_KEY" # not recommended for public repos
url = f"https://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}&units=metric"
response = requests.get(url)
data = response.json()
if data.get("cod") == 200:
temp = data["main"]["temp"]
description = data["weather"][0]["description"]
return f"The current temperature in {city} is {temp}°C with {description}."
else:
return "Sorry, I couldn't find weather information for that city."
iface = gr.Interface(fn=get_weather, inputs="text", outputs="text")
if __name__ == "__main__":
iface.launch()