ArceusInception commited on
Commit
ea2ebfd
·
verified ·
1 Parent(s): 29771be

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -33
app.py CHANGED
@@ -1,42 +1,49 @@
1
- import gradio as gr
2
- import requests
3
  import os
4
- from groq import Groq # Assuming Groq or a similar LLM can provide insights
 
 
 
 
5
 
6
- def get_weather_and_insight(city_name):
7
- # Initialize the Groq client with your API key
8
- groq_client = Groq(api_key=os.getenv("GROQ_API_KEY"))
9
-
10
- # Use Groq to get a short insight about the city
11
- insight = groq_client.get_insight(city_name, max_words=10) # Assumed method and parameter
12
-
13
- API_Key = os.getenv("OPENWEATHER_API_KEY")
14
- if not API_Key:
15
- return "API Key is not set. Please set the OPENWEATHER_API_KEY environment variable."
16
-
17
- url = f'https://api.openweathermap.org/data/2.5/weather?q={city_name}&appid={API_Key}&units=metric'
18
- response = requests.get(url)
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
- if response.status_code == 200:
21
- data = response.json()
22
- weather = data['weather'][0]['description']
23
- temp = data['main']['temp']
24
- humidity = data['main']['humidity']
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 Update and Insight"),
37
  title="WeatherAssistantApp",
38
- description="Enter a city name to get an interesting insight and the current weather details."
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()