Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,42 +1,49 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import requests
|
| 3 |
import os
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
def get_weather_and_insight(
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
weather_emoji = "☀️" if "clear" in weather else "☁️" if "cloud" in weather else "🌧️" if "rain" in weather else "❄️" if "snow" in weather else "🌫️"
|
| 27 |
-
return (f"🔍 {insight} | In {city_name}, it's {weather} {weather_emoji}. "
|
| 28 |
-
f"Temperature: {temp}°C 🌡️, Humidity: {humidity}% 💧")
|
| 29 |
-
else:
|
| 30 |
-
return "Failed to retrieve data. Please check the city name and try again."
|
| 31 |
-
|
| 32 |
-
# Define Gradio interface
|
| 33 |
iface = gr.Interface(
|
| 34 |
fn=get_weather_and_insight,
|
| 35 |
-
inputs=gr.Textbox(label="Enter City Name", placeholder="Type here..."),
|
| 36 |
-
outputs=gr.Textbox(label="Weather
|
| 37 |
title="WeatherAssistantApp",
|
| 38 |
-
description="Enter a city name to get
|
| 39 |
)
|
| 40 |
|
| 41 |
-
# Launch the interface
|
| 42 |
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 |
+
def get_weather_and_insight(city):
|
| 9 |
+
# Fetch weather data
|
| 10 |
+
url = f'http://api.openweathermap.org/data/2.5/weather?q={city}&appid={os.getenv("OPENWEATHER_API_KEY")}&units=metric'
|
| 11 |
+
res = requests.get(url)
|
| 12 |
+
data = res.json()
|
| 13 |
+
|
| 14 |
+
if res.status_code != 200:
|
| 15 |
+
return "Failed to retrieve weather data. Please check the city name and try again."
|
| 16 |
+
|
| 17 |
+
humidity = data.get('main', {}).get('humidity')
|
| 18 |
+
temp = data.get('main', {}).get('temp')
|
| 19 |
+
description = data.get('weather', [{}])[0].get('description')
|
| 20 |
+
|
| 21 |
+
# Generate a creative weather summary using LangChain LLM
|
| 22 |
+
groq_api_key = os.getenv("GROQ_API_KEY")
|
| 23 |
+
system = f"You are a weather expert reporter of {city}. You must compile a short weather summary based on information like {temp} degree celsius is the current temperature of the city, {humidity} percent is the humidity percentage for the city. The weather of the city can be best described as {description}."
|
| 24 |
+
human = "{text}"
|
| 25 |
+
prompt = ChatPromptTemplate.from_messages(
|
| 26 |
+
[("system", system), ("human", human)]
|
| 27 |
+
)
|
| 28 |
+
chat = ChatGroq(api_key=groq_api_key, model_name="llama3-70b-8192")
|
| 29 |
+
chain = prompt | chat | StrOutputParser()
|
| 30 |
+
output = chain.invoke({"text": city})
|
| 31 |
+
|
| 32 |
+
# Add emojis for visual flair
|
| 33 |
+
weather_emoji = "☀️" if "clear" in description else "☁️" if "cloud" in description else "🌧️" if "rain" in description else "❄️" if "snow" in description else "🌫️"
|
| 34 |
|
| 35 |
+
# Create the final response string
|
| 36 |
+
final_response = f"🌍 {city.upper()} Weather Insight: {output} {weather_emoji}\n🌡️ Temp: {temp}°C, 💧 Humidity: {humidity}%."
|
| 37 |
+
|
| 38 |
+
return final_response
|
| 39 |
+
|
| 40 |
+
# Gradio Interface
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
iface = gr.Interface(
|
| 42 |
fn=get_weather_and_insight,
|
| 43 |
+
inputs=gr.Textbox(label="Enter City Name", placeholder="Type city here..."),
|
| 44 |
+
outputs=gr.Textbox(label="Weather Report and Insight"),
|
| 45 |
title="WeatherAssistantApp",
|
| 46 |
+
description="Enter a city name to get a detailed weather report with an AI-generated insight."
|
| 47 |
)
|
| 48 |
|
|
|
|
| 49 |
iface.launch()
|