shahin-hashim commited on
Commit
ecefc28
1 Parent(s): 761d40e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +107 -0
app.py ADDED
@@ -0,0 +1,107 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ import streamlit as st
3
+ import json
4
+ from dotenv import load_dotenv
5
+ import os
6
+ from groq import Groq
7
+
8
+ load_dotenv()
9
+
10
+ # Get GROQ API key from environment variable
11
+ client = Groq(
12
+ api_key=os.getenv("GROQ_API_KEY"),
13
+ )
14
+ # Get OpenWeatherMap API key from environment variable
15
+ weather_api_key = os.getenv("OPENWEATHERMAP_API_KEY")
16
+ # OpenWeatherMap API endpoint
17
+ API_URL = "http://api.openweathermap.org/data/2.5/weather"
18
+
19
+
20
+ def get_current_weather(location):
21
+ """Get the current weather in a given location"""
22
+ params = {
23
+ "q": location,
24
+ "appid": weather_api_key,
25
+ "units": "metric"
26
+ }
27
+
28
+ response_data = requests.get(API_URL, params=params)
29
+
30
+ if response_data.status_code == 200:
31
+ data = response_data.json()
32
+ print("data-", data)
33
+ location_resp = data['name']
34
+ weather = data['weather'][0]['main']
35
+ temperature = round(data['main']['temp'], 2)
36
+ description = data['weather'][0]['description']
37
+ humidity = data['main']['humidity']
38
+ pressure = data['main']['pressure']
39
+ wind = data['wind']['speed']
40
+
41
+ report = (f"We have an update on the weather for {location_resp} location. Currently, it is {weather} "
42
+ f"({description}) with a temperature of {temperature} °C. There's {humidity}% humidity, "
43
+ f"atmospheric pressure is {pressure} hPa, and winds are blowing at {wind} m/s.")
44
+ return report
45
+ else:
46
+ return f"Uh oh! We're unable to retrieve weather data for {location}. Please try again later."
47
+
48
+
49
+ def define_tools():
50
+ """Define tools for the OpenAI API call"""
51
+ tools = [
52
+ {
53
+ "type": "function",
54
+ "function": {
55
+ "name": "get_current_weather",
56
+ "description": "Get the current weather in a given location",
57
+ "parameters": {
58
+ "type": "object",
59
+ "properties": {
60
+ "location": {
61
+ "type": "string",
62
+ "description": "The city and state, e.g. San Francisco, CA",
63
+ },
64
+ },
65
+ "required": ["location"],
66
+ },
67
+ },
68
+ }
69
+ ]
70
+ return tools
71
+
72
+
73
+ st.title("Weather App")
74
+
75
+ location = st.text_input("Enter a location (city and state):")
76
+
77
+ if st.button("Get Weather"):
78
+ tools = define_tools()
79
+
80
+ response = client.chat.completions.create(
81
+ model="mixtral-8x7b-32768",
82
+ messages=[
83
+ {
84
+ "role": "user",
85
+ "content": f"What is the weather like in {location}?",
86
+ }
87
+ ],
88
+ temperature=0,
89
+ max_tokens=300,
90
+ tools=tools,
91
+ tool_choice="auto"
92
+ )
93
+
94
+ # Handle potential NoneType errors
95
+ if response and response.choices and response.choices[0].message:
96
+ groq_response = response.choices[0].message
97
+
98
+ if groq_response and groq_response.tool_calls and groq_response.tool_calls[0].function.arguments:
99
+ args = json.loads(groq_response.tool_calls[0].function.arguments)
100
+ weather_report = get_current_weather(**args)
101
+ st.write("**Weather Report:**")
102
+ st.write(weather_report)
103
+ else:
104
+ st.write("Oops! There seems to be an issue retrieving the weather data. Please check the location provided "
105
+ "correctly")
106
+ else:
107
+ st.write("Oops! There seems to be an issue retrieving the weather data. Please try again later.")