Spaces:
Sleeping
Sleeping
Update app.py
Browse filesAdded scenic route option to Google route feature
app.py
CHANGED
|
@@ -53,7 +53,7 @@ GOOGLE_KEY=os.getenv('GOOGLE_KEY')
|
|
| 53 |
# 'docx': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'}
|
| 54 |
|
| 55 |
@function_tool
|
| 56 |
-
async def google_route(loc1: str, loc2: str, prefer_public: bool):
|
| 57 |
'''Uses Google Maps API to obtain a route plan overview, and distance in miles and duration
|
| 58 |
for a travel leg between two addresses.
|
| 59 |
|
|
@@ -61,6 +61,7 @@ async def google_route(loc1: str, loc2: str, prefer_public: bool):
|
|
| 61 |
loc1: text of the starting point address
|
| 62 |
loc2: text of the destination point address
|
| 63 |
prefer_public: boolean should be false unless user desired public transportation
|
|
|
|
| 64 |
|
| 65 |
Returns:
|
| 66 |
a tuple of (distance, duration, overview of the route plan)
|
|
@@ -82,6 +83,9 @@ async def google_route(loc1: str, loc2: str, prefer_public: bool):
|
|
| 82 |
if prefer_public:
|
| 83 |
modes.reverse()
|
| 84 |
for travel_mode in modes:
|
|
|
|
|
|
|
|
|
|
| 85 |
json_data = {
|
| 86 |
'origin': {
|
| 87 |
'address': f'{loc1}',
|
|
@@ -91,7 +95,7 @@ async def google_route(loc1: str, loc2: str, prefer_public: bool):
|
|
| 91 |
},
|
| 92 |
'travelMode': travel_mode,
|
| 93 |
# 'routingPreference': 'TRAFFIC_AWARE',
|
| 94 |
-
'computeAlternativeRoutes':
|
| 95 |
'routeModifiers': {
|
| 96 |
'avoidTolls': False,
|
| 97 |
'avoidHighways': False,
|
|
@@ -105,7 +109,15 @@ async def google_route(loc1: str, loc2: str, prefer_public: bool):
|
|
| 105 |
headers=headers, json=json_data)
|
| 106 |
content = json.loads(response.content)
|
| 107 |
if routes := content.get('routes'):
|
| 108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
leg = legs[0]
|
| 110 |
if loc_vals := leg.get('localizedValues'):
|
| 111 |
distance = loc_vals.get('distance','unknown')
|
|
@@ -560,7 +572,8 @@ def show_help():
|
|
| 560 |
Better results are obtained by including detailed descriptions and instructions
|
| 561 |
of what you want in the prompt.
|
| 562 |
For Google route planning, specify each leg's start/end addresses, and list them
|
| 563 |
-
separately if multiple legs are involved.
|
|
|
|
| 564 |
Start a new session whenever memory of previous inputs and responses is no longer
|
| 565 |
needed as context. The agent can only remember so much.
|
| 566 |
'''
|
|
|
|
| 53 |
# 'docx': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'}
|
| 54 |
|
| 55 |
@function_tool
|
| 56 |
+
async def google_route(loc1: str, loc2: str, prefer_public: bool, scenic_route: bool):
|
| 57 |
'''Uses Google Maps API to obtain a route plan overview, and distance in miles and duration
|
| 58 |
for a travel leg between two addresses.
|
| 59 |
|
|
|
|
| 61 |
loc1: text of the starting point address
|
| 62 |
loc2: text of the destination point address
|
| 63 |
prefer_public: boolean should be false unless user desired public transportation
|
| 64 |
+
scenic_route: boolean should be false unless user desired scenic or slow route
|
| 65 |
|
| 66 |
Returns:
|
| 67 |
a tuple of (distance, duration, overview of the route plan)
|
|
|
|
| 83 |
if prefer_public:
|
| 84 |
modes.reverse()
|
| 85 |
for travel_mode in modes:
|
| 86 |
+
get_alternates = False
|
| 87 |
+
if travel_mode == 'DRIVE' and scenic_route == True:
|
| 88 |
+
get_alternates = True
|
| 89 |
json_data = {
|
| 90 |
'origin': {
|
| 91 |
'address': f'{loc1}',
|
|
|
|
| 95 |
},
|
| 96 |
'travelMode': travel_mode,
|
| 97 |
# 'routingPreference': 'TRAFFIC_AWARE',
|
| 98 |
+
'computeAlternativeRoutes': get_alternates,
|
| 99 |
'routeModifiers': {
|
| 100 |
'avoidTolls': False,
|
| 101 |
'avoidHighways': False,
|
|
|
|
| 109 |
headers=headers, json=json_data)
|
| 110 |
content = json.loads(response.content)
|
| 111 |
if routes := content.get('routes'):
|
| 112 |
+
route_chosen = 0
|
| 113 |
+
if get_alternates and len(routes) > 1:
|
| 114 |
+
max_distance = 0
|
| 115 |
+
for route_num in range(len(routes)):
|
| 116 |
+
if dist_meters := routes[route_num].get('distanceMeters'):
|
| 117 |
+
if dist_meters > max_distance:
|
| 118 |
+
max_distance = dist_meters
|
| 119 |
+
route_chosen = route_num
|
| 120 |
+
if legs := routes[route_chosen].get('legs'):
|
| 121 |
leg = legs[0]
|
| 122 |
if loc_vals := leg.get('localizedValues'):
|
| 123 |
distance = loc_vals.get('distance','unknown')
|
|
|
|
| 572 |
Better results are obtained by including detailed descriptions and instructions
|
| 573 |
of what you want in the prompt.
|
| 574 |
For Google route planning, specify each leg's start/end addresses, and list them
|
| 575 |
+
separately if multiple legs are involved. Also, if you want a scenic route or
|
| 576 |
+
favor public transportation, say so.
|
| 577 |
Start a new session whenever memory of previous inputs and responses is no longer
|
| 578 |
needed as context. The agent can only remember so much.
|
| 579 |
'''
|