Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,56 +1,40 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import requests
|
| 3 |
import gradio as gr
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
from langchain_core.output_parsers import StrOutputParser
|
| 7 |
-
|
| 8 |
-
groq_api_key = os.getenv("groq")
|
| 9 |
-
|
| 10 |
-
def weather(city):
|
| 11 |
-
url = 'http://api.openweathermap.org/data/2.5/weather?q=' + city + '&appid=' + os.getenv("openweather") + '&units=metric'
|
| 12 |
-
res = requests.get(url)
|
| 13 |
-
data = res.json()
|
| 14 |
-
if res.status_code == 200:
|
| 15 |
-
weather_details = {
|
| 16 |
-
"humidity": data.get('main', {}).get('humidity'),
|
| 17 |
-
"pressure": data.get('main', {}).get('pressure'),
|
| 18 |
-
"wind": data.get('wind', {}).get('speed'),
|
| 19 |
-
"description": data.get('weather', [{}])[0].get('description'),
|
| 20 |
-
"temp": data.get('main', {}).get('temp')
|
| 21 |
-
}
|
| 22 |
-
return weather_details
|
| 23 |
-
else:
|
| 24 |
-
return None
|
| 25 |
-
|
| 26 |
-
def generate_weather_report(city):
|
| 27 |
-
weather_res = weather(city)
|
| 28 |
-
if weather_res:
|
| 29 |
-
humidity = weather_res['humidity']
|
| 30 |
-
pressure = weather_res['pressure']
|
| 31 |
-
wind = weather_res['wind']
|
| 32 |
-
description = weather_res['description']
|
| 33 |
-
temp = weather_res['temp']
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
])
|
| 44 |
-
chat = ChatGroq(api_key=groq_api_key, model_name="llama3-70b-8192")
|
| 45 |
-
chain = prompt | chat | StrOutputParser()
|
| 46 |
-
output = chain.invoke({"text": city})
|
| 47 |
-
return output
|
| 48 |
else:
|
| 49 |
-
return "
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
-
|
| 56 |
-
iface.launch()
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
def get_weather(city_name):
|
| 6 |
+
API_Key = os.getenv("OPENWEATHER_API_KEY")
|
| 7 |
+
if not API_Key:
|
| 8 |
+
return "API Key is not set. Please set the OPENWEATHER_API_KEY environment variable."
|
| 9 |
+
|
| 10 |
+
url = f'https://api.openweathermap.org/data/2.5/weather?q={city_name}&appid={API_Key}&units=metric'
|
| 11 |
+
response = requests.get(url)
|
| 12 |
+
if response.status_code == 200:
|
| 13 |
+
data = response.json()
|
| 14 |
+
weather = data['weather'][0]['description']
|
| 15 |
+
temp = data['main']['temp']
|
| 16 |
+
humidity = data['main']['humidity']
|
| 17 |
|
| 18 |
+
# Adding emojis and creative text
|
| 19 |
+
weather_emoji = "☀️" if "clear" in weather else "☁️" if "cloud" in weather else "🌧️" if "rain" in weather else "❄️" if "snow" in weather else "🌫️"
|
| 20 |
+
return (f"In {city_name}, the weather is currently {weather} {weather_emoji}. "
|
| 21 |
+
f"The temperature is a comfy {temp}°C 🌡️. "
|
| 22 |
+
f"Also, humidity levels are at {humidity}%, so keep hydrated! 💧")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
else:
|
| 24 |
+
return "Failed to retrieve data. Please check the city name and try again."
|
| 25 |
|
| 26 |
+
# Define Gradio interface
|
| 27 |
+
iface = gr.Interface(
|
| 28 |
+
fn=get_weather,
|
| 29 |
+
inputs=gr.Textbox(label="Enter City Name"),
|
| 30 |
+
outputs=[
|
| 31 |
+
gr.Textbox(label="Weather"),
|
| 32 |
+
gr.Textbox(label="Temperature"),
|
| 33 |
+
gr.Textbox(label="Humidity")
|
| 34 |
+
],
|
| 35 |
+
title="Weather App",
|
| 36 |
+
description="Enter a city name to get the current weather description, temperature, and humidity."
|
| 37 |
+
)
|
| 38 |
|
| 39 |
+
# Launch the interface
|
| 40 |
+
iface.launch()
|