Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -11,32 +11,24 @@ load_dotenv()
|
|
11 |
openai.api_key = os.getenv("OPENAI_API_KEY1")
|
12 |
|
13 |
# Define function to get current weather
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
},
|
33 |
-
"additionalProperties": false,
|
34 |
-
"required": [
|
35 |
-
"location",
|
36 |
-
"unit"
|
37 |
-
]
|
38 |
-
}
|
39 |
-
}
|
40 |
|
41 |
# Function definition and initial message handling
|
42 |
def weather_chat(user_message):
|
|
|
11 |
openai.api_key = os.getenv("OPENAI_API_KEY1")
|
12 |
|
13 |
# Define function to get current weather
|
14 |
+
def get_current_weather(location, unit='celsius'):
|
15 |
+
weather_api_key = os.getenv("WEATHER_API_KEY")
|
16 |
+
base_url = f"http://api.openweathermap.org/data/2.5/weather?q={location}&appid={weather_api_key}&units=metric"
|
17 |
+
try:
|
18 |
+
response = requests.get(base_url, timeout=10) # Increased timeout for the request
|
19 |
+
response.raise_for_status() # Check if the request was successful
|
20 |
+
data = response.json()
|
21 |
+
weather_description = data['weather'][0]['description']
|
22 |
+
return {
|
23 |
+
"location": location,
|
24 |
+
"temperature": data['main']['temp'],
|
25 |
+
"weather": weather_description
|
26 |
+
}
|
27 |
+
except requests.exceptions.Timeout:
|
28 |
+
return {"error": "Request timed out. Please try again later."}
|
29 |
+
except requests.exceptions.RequestException as e:
|
30 |
+
print(f"Request failed: {e}")
|
31 |
+
return {"error": str(e)}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
# Function definition and initial message handling
|
34 |
def weather_chat(user_message):
|