ArceusInception commited on
Commit
772206c
·
verified ·
1 Parent(s): b299241

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -50
app.py CHANGED
@@ -1,56 +1,40 @@
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()
 
 
 
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()