Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,86 +1,56 @@
|
|
| 1 |
import os
|
| 2 |
-
import json
|
| 3 |
-
import gradio as gr
|
| 4 |
import requests
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
},
|
| 24 |
-
},
|
| 25 |
-
"required": ["city_name"],
|
| 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 |
-
corrected_city_name, city_insight = preprocess_city_name_and_generate_insight(city_name)
|
| 56 |
-
API_Key = os.getenv("OPENWEATHER_API_KEY")
|
| 57 |
-
if not API_Key:
|
| 58 |
-
return "API Key is not set. Please set the OPENWEATHER_API_KEY environment variable."
|
| 59 |
-
|
| 60 |
-
url = f'https://api.openweathermap.org/data/2.5/weather?q={corrected_city_name}&appid={API_Key}&units=metric'
|
| 61 |
-
response = requests.get(url)
|
| 62 |
-
|
| 63 |
-
if response.status_code == 200:
|
| 64 |
-
data = response.json()
|
| 65 |
-
weather = data['weather'][0]['description']
|
| 66 |
-
temp = data['main']['temp']
|
| 67 |
-
humidity = data['main']['humidity']
|
| 68 |
-
weather_emoji = "☀️" if "clear" in weather else "☁️" if "cloud" in weather else "🌧️" if "rain" in weather else "❄️" if "snow" in weather else "🌫️"
|
| 69 |
-
return (f"In {corrected_city_name}, the weather is currently {weather} {weather_emoji}. "
|
| 70 |
-
f"The temperature is a comfy {temp}°C 🌡️. "
|
| 71 |
-
f"And the humidity levels are at {humidity}% 💧. "
|
| 72 |
-
f"Did you know? {city_insight}")
|
| 73 |
else:
|
| 74 |
-
return "
|
| 75 |
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
inputs=gr.Textbox(label="Enter City Name", placeholder="Type here..."),
|
| 80 |
-
outputs=gr.Textbox(label="Weather Update"),
|
| 81 |
-
title="WeatherAssistantApp",
|
| 82 |
-
description="Enter a city name to get a lively description of the current weather, temperature, and humidity."
|
| 83 |
-
)
|
| 84 |
|
| 85 |
-
|
| 86 |
iface.launch()
|
|
|
|
| 1 |
import os
|
|
|
|
|
|
|
| 2 |
import requests
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from langchain_groq import ChatGroq
|
| 5 |
+
from langchain_core.prompts import ChatPromptTemplate
|
| 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 |
+
# Adding weather emoji based on the description
|
| 36 |
+
weather_emoji = "☀️" if "clear" in description else "☁️" if "cloud" in description else "🌧️" if "rain" in description else "❄️" if "snow" in description else "🌫️"
|
| 37 |
+
|
| 38 |
+
system = f"You are a resourceful weather expert of {city}. Compile a short weather summary based on information like {temp}°C is the current temperature of the city, {humidity}% is the humidity percentage for the city, and the weather of city can be best described with an interesting insight about the city {description} {weather_emoji}."
|
| 39 |
+
human = "{text}"
|
| 40 |
+
prompt = ChatPromptTemplate.from_messages([
|
| 41 |
+
("system", system),
|
| 42 |
+
("human", human)
|
| 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 "Weather data not available. Please check the city name or try again later."
|
| 50 |
|
| 51 |
+
def main(city):
|
| 52 |
+
weather_report = generate_weather_report(city)
|
| 53 |
+
return weather_report
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
+
iface = gr.Interface(fn=main, inputs=[gr.Textbox(label="Enter City Name")], outputs=[gr.Textbox(label="Weather Report")])
|
| 56 |
iface.launch()
|