Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -451,6 +451,7 @@ weather_api_key = os.environ.get("WEATHER_API_KEY")
|
|
| 451 |
|
| 452 |
|
| 453 |
def get_time_tool2(query: str) -> datetime:
|
|
|
|
| 454 |
try:
|
| 455 |
# Step 1: 獲取地點
|
| 456 |
location_prompt = f"""
|
|
@@ -472,7 +473,8 @@ def get_time_tool2(query: str) -> datetime:
|
|
| 472 |
|
| 473 |
# Step 3: 用 GPT 推算具體時間
|
| 474 |
time_query_prompt = f"""
|
| 475 |
-
Given
|
|
|
|
| 476 |
|
| 477 |
Query: "{query}"
|
| 478 |
|
|
@@ -486,19 +488,22 @@ def get_time_tool2(query: str) -> datetime:
|
|
| 486 |
time_str = time_response.choices[0].text.strip()
|
| 487 |
|
| 488 |
# Step 4: 解析並格式化時間
|
| 489 |
-
|
| 490 |
-
|
| 491 |
-
|
| 492 |
-
|
| 493 |
-
|
|
|
|
| 494 |
|
| 495 |
-
|
| 496 |
-
|
| 497 |
|
| 498 |
-
|
|
|
|
|
|
|
| 499 |
|
| 500 |
except Exception as e:
|
| 501 |
-
return f"Error in
|
| 502 |
|
| 503 |
def weather_agent_tool(query: str) -> str:
|
| 504 |
"""Weather Agent: Return current, hourly, or historical weather info using WeatherAPI."""
|
|
|
|
| 451 |
|
| 452 |
|
| 453 |
def get_time_tool2(query: str) -> datetime:
|
| 454 |
+
|
| 455 |
try:
|
| 456 |
# Step 1: 獲取地點
|
| 457 |
location_prompt = f"""
|
|
|
|
| 473 |
|
| 474 |
# Step 3: 用 GPT 推算具體時間
|
| 475 |
time_query_prompt = f"""
|
| 476 |
+
Given that today is {now.strftime('%A')}, {now.strftime('%Y-%m-%d')},
|
| 477 |
+
answer the following:
|
| 478 |
|
| 479 |
Query: "{query}"
|
| 480 |
|
|
|
|
| 488 |
time_str = time_response.choices[0].text.strip()
|
| 489 |
|
| 490 |
# Step 4: 解析並格式化時間
|
| 491 |
+
try:
|
| 492 |
+
# 嘗試解析時間
|
| 493 |
+
if "AM" in time_str or "PM" in time_str:
|
| 494 |
+
target_time = datetime.strptime(time_str, "%I:%M %p")
|
| 495 |
+
else:
|
| 496 |
+
target_time = datetime.strptime(time_str, "%Y-%m-%d %H:%M:%S")
|
| 497 |
|
| 498 |
+
# 添加時區
|
| 499 |
+
target_time = target_time.replace(tzinfo=ZoneInfo(tz_str))
|
| 500 |
|
| 501 |
+
return target_time.strftime("%Y-%m-%d %H:%M:%S")
|
| 502 |
+
except Exception as e:
|
| 503 |
+
return f"Error in processing time: {e}"
|
| 504 |
|
| 505 |
except Exception as e:
|
| 506 |
+
return f"Error in retrieving location or time information: {e}"
|
| 507 |
|
| 508 |
def weather_agent_tool(query: str) -> str:
|
| 509 |
"""Weather Agent: Return current, hourly, or historical weather info using WeatherAPI."""
|