Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -123,6 +123,7 @@ async def google_route(loc1: str, loc2: str, prefer_public: bool, scenic_route:
|
|
| 123 |
response = requests.post('https://routes.googleapis.com/directions/v2:computeRoutes',
|
| 124 |
headers=headers, json=json_data)
|
| 125 |
content = json.loads(response.content)
|
|
|
|
| 126 |
if routes := content.get('routes'):
|
| 127 |
route_chosen = 0
|
| 128 |
if get_alternates and len(routes) > 1:
|
|
@@ -134,8 +135,6 @@ async def google_route(loc1: str, loc2: str, prefer_public: bool, scenic_route:
|
|
| 134 |
route_chosen = route_num
|
| 135 |
if legs := routes[route_chosen].get('legs'):
|
| 136 |
leg = legs[0]
|
| 137 |
-
pline = leg.get('polyline').get('encodedPolyline')
|
| 138 |
-
polyLocs = polyline.decode(pline, 5)
|
| 139 |
if loc_vals := leg.get('localizedValues'):
|
| 140 |
distance = loc_vals.get('distance','unknown')
|
| 141 |
duration = loc_vals.get('duration', 'unknown')
|
|
@@ -155,23 +154,15 @@ async def google_route(loc1: str, loc2: str, prefer_public: bool, scenic_route:
|
|
| 155 |
overview += f'\n via: {tname}'
|
| 156 |
elif steps := leg['steps']:
|
| 157 |
all_locs =[]
|
| 158 |
-
totalLocs = 0
|
| 159 |
overview = '\n\nDriving Instructions:'
|
| 160 |
for step in steps:
|
| 161 |
locs = []
|
| 162 |
-
stepPline = step.get('polyline').get('encodedPolyline')
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
ratio = float(len(stepLocs)) / float(stepDist)
|
| 169 |
-
totalLocs += len(stepLocs)
|
| 170 |
-
if len(locs) == 0:
|
| 171 |
-
ll = step.get("startLocation").get("latLng")
|
| 172 |
-
locs.append(f"{ll.get('latitude')},{ll.get('longitude')}")
|
| 173 |
-
ll = step.get("endLocation").get("latLng")
|
| 174 |
-
locs.append(f"{ll.get('latitude')},{ll.get('longitude')}")
|
| 175 |
sdist = '?? mi'
|
| 176 |
if step_loc_vals := step.get('localizedValues'):
|
| 177 |
if dterm := step_loc_vals.get('distance'):
|
|
@@ -251,17 +242,18 @@ async def get_distance(addr1: str, addr2: str)->float:
|
|
| 251 |
return distance
|
| 252 |
|
| 253 |
def make_google_static_map_url(all_locs):
|
|
|
|
|
|
|
| 254 |
interval = 1
|
| 255 |
while True:
|
| 256 |
pruned_locs = []
|
| 257 |
for (dist, locs) in all_locs:
|
| 258 |
-
|
| 259 |
-
if dist > 500:
|
| 260 |
pruned_locs.extend(locs[::interval])
|
| 261 |
else:
|
| 262 |
pruned_locs.extend(locs)
|
| 263 |
url = "https://maps.googleapis.com/maps/api/staticmap?"
|
| 264 |
-
url += "size=
|
| 265 |
url += "&maptype=roadmap"
|
| 266 |
url += "&path=color:0x0000ff|weight:4|"
|
| 267 |
# url += f"enc:{locations}"
|
|
@@ -553,7 +545,8 @@ async def chat(prompt_window, user_window, password, history, output_window,
|
|
| 553 |
raw_id = item.raw_item['call_id']
|
| 554 |
if raw_id == map_image_id:
|
| 555 |
map_url = item.output[3]
|
| 556 |
-
|
|
|
|
| 557 |
|
| 558 |
if item.type == "reasoning_item":
|
| 559 |
for o in item.raw_item.summary:
|
|
|
|
| 123 |
response = requests.post('https://routes.googleapis.com/directions/v2:computeRoutes',
|
| 124 |
headers=headers, json=json_data)
|
| 125 |
content = json.loads(response.content)
|
| 126 |
+
url = None
|
| 127 |
if routes := content.get('routes'):
|
| 128 |
route_chosen = 0
|
| 129 |
if get_alternates and len(routes) > 1:
|
|
|
|
| 135 |
route_chosen = route_num
|
| 136 |
if legs := routes[route_chosen].get('legs'):
|
| 137 |
leg = legs[0]
|
|
|
|
|
|
|
| 138 |
if loc_vals := leg.get('localizedValues'):
|
| 139 |
distance = loc_vals.get('distance','unknown')
|
| 140 |
duration = loc_vals.get('duration', 'unknown')
|
|
|
|
| 154 |
overview += f'\n via: {tname}'
|
| 155 |
elif steps := leg['steps']:
|
| 156 |
all_locs =[]
|
|
|
|
| 157 |
overview = '\n\nDriving Instructions:'
|
| 158 |
for step in steps:
|
| 159 |
locs = []
|
| 160 |
+
if stepPline := step.get('polyline').get('encodedPolyline'):
|
| 161 |
+
stepLocs = polyline.decode(stepPline, 5)
|
| 162 |
+
for lat, long in stepLocs:
|
| 163 |
+
locs.append(f"{lat},{long}")
|
| 164 |
+
if stepDist := step.get('distanceMeters'):
|
| 165 |
+
all_locs.append((int(stepDist), locs))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
sdist = '?? mi'
|
| 167 |
if step_loc_vals := step.get('localizedValues'):
|
| 168 |
if dterm := step_loc_vals.get('distance'):
|
|
|
|
| 242 |
return distance
|
| 243 |
|
| 244 |
def make_google_static_map_url(all_locs):
|
| 245 |
+
if len(all_locs) == 0:
|
| 246 |
+
return None
|
| 247 |
interval = 1
|
| 248 |
while True:
|
| 249 |
pruned_locs = []
|
| 250 |
for (dist, locs) in all_locs:
|
| 251 |
+
if dist > 500 and len(locs) > 3:
|
|
|
|
| 252 |
pruned_locs.extend(locs[::interval])
|
| 253 |
else:
|
| 254 |
pruned_locs.extend(locs)
|
| 255 |
url = "https://maps.googleapis.com/maps/api/staticmap?"
|
| 256 |
+
url += "size=1024x1024"
|
| 257 |
url += "&maptype=roadmap"
|
| 258 |
url += "&path=color:0x0000ff|weight:4|"
|
| 259 |
# url += f"enc:{locations}"
|
|
|
|
| 545 |
raw_id = item.raw_item['call_id']
|
| 546 |
if raw_id == map_image_id:
|
| 547 |
map_url = item.output[3]
|
| 548 |
+
if map_url:
|
| 549 |
+
image_window = gr.Image(value=map_url, visible=True)
|
| 550 |
|
| 551 |
if item.type == "reasoning_item":
|
| 552 |
for o in item.raw_item.summary:
|