ArceusInception commited on
Commit
f133940
·
verified ·
1 Parent(s): 2852c91

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -79
app.py CHANGED
@@ -1,86 +1,56 @@
1
  import os
2
- import json
3
- import gradio as gr
4
  import requests
5
- from groq import Groq # Ensure this is the correct import
6
-
7
- # Groq client initialization with environment variables
8
- client = Groq(api_key=os.getenv("GROQ_API_KEY"))
9
-
10
- def preprocess_city_name_and_generate_insight(city_name):
11
- tools = [
12
- {
13
- "type": "function",
14
- "function": {
15
- "name": "generate_city_insight",
16
- "description": "Generate a short, interesting fact about a specified city",
17
- "parameters": {
18
- "type": "object",
19
- "properties": {
20
- "city_name": {
21
- "type": "string",
22
- "description": "The name of the city to generate insights for",
23
- },
24
- },
25
- "required": ["city_name"],
26
- },
27
- },
28
  }
29
- ]
30
-
31
- # Simulate a user message asking for insights about a city
32
- response = client.chat.completions.create(
33
- model="mixtral-8x7b-32768",
34
- messages=[
35
- {
36
- "role": "user",
37
- "content": f"What interesting fact can you tell me about {city_name}?",
38
- }
39
- ],
40
- temperature=0,
41
- max_tokens=300,
42
- tools=tools,
43
- tool_choice="auto"
44
- )
45
-
46
- # Extract and handle the response
47
- groq_response = response.choices[0].message
48
- args = json.loads(groq_response.tool_calls[0].function.arguments)
49
-
50
- # Assuming generate_city_insight function is defined somewhere that returns an insight
51
- insight = generate_city_insight(**args)
52
- return city_name, insight
53
-
54
- def get_weather(city_name):
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 "Failed to retrieve data. Please check the city name and try again."
75
 
76
- # Gradio interface definition
77
- iface = gr.Interface(
78
- fn=get_weather,
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
- # Launching the interface
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()