Spaces:
Runtime error
Runtime error
import requests | |
import os | |
import json | |
from ...tool import Tool | |
def build_tool(config) -> Tool: | |
tool = Tool( | |
"Map Info form bing map api", | |
"Look up map information", | |
name_for_model="BingMap", | |
description_for_model="Plugin for look up map information", | |
logo_url="https://your-app-url.com/.well-known/logo.png", | |
contact_email="[email protected]", | |
legal_info_url="[email protected]", | |
) | |
KEY = config["subscription_key"] | |
BASE_URL = "http://dev.virtualearth.net/REST/V1/" | |
def get_distance(start: str, end: str): | |
"""Get the distance between two locations in miles""" | |
# Request URL | |
url = ( | |
BASE_URL | |
+ "Routes/Driving?o=json&wp.0=" | |
+ start | |
+ "&wp.1=" | |
+ end | |
+ "&key=" | |
+ KEY | |
) | |
# GET request | |
r = requests.get(url) | |
data = json.loads(r.text) | |
# Extract route information | |
route = data["resourceSets"][0]["resources"][0] | |
# Extract distance in miles | |
distance = route["travelDistance"] | |
return distance | |
def get_route(start: str, end: str): | |
"""Get the route between two locations in miles""" | |
# Request URL | |
url = ( | |
BASE_URL | |
+ "Routes/Driving?o=json&wp.0=" | |
+ start | |
+ "&wp.1=" | |
+ end | |
+ "&key=" | |
+ KEY | |
) | |
# GET request | |
r = requests.get(url) | |
data = json.loads(r.text) | |
# Extract route information | |
route = data["resourceSets"][0]["resources"][0] | |
itinerary = route["routeLegs"][0]["itineraryItems"] | |
# Extract route text information | |
route_text = [] | |
for item in itinerary: | |
if "instruction" in item: | |
route_text.append(item["instruction"]["text"]) | |
return route_text | |
def get_coordinates(location: str): | |
"""Get the coordinates of a location""" | |
url = BASE_URL + "Locations" | |
params = {"query": location, "key": KEY} | |
response = requests.get(url, params=params) | |
json_data = response.json() | |
coordinates = json_data["resourceSets"][0]["resources"][0]["point"][ | |
"coordinates" | |
] | |
return coordinates | |
def search_nearyby( | |
search_term: str = "restaurant", | |
latitude: float = 0.0, | |
longitude: float = 0.0, | |
places: str = "unknown", | |
radius: int = 5000, | |
): # radius in meters | |
"""Search for places nearby a location, within a given radius, and return the results into a list""" | |
url = BASE_URL + "LocalSearch" | |
if places != "unknown": | |
latitude = get_coordinates(places)[0] | |
longitude = get_coordinates(places)[1] | |
# Build the request query string | |
params = { | |
"query": search_term, | |
"userLocation": f"{latitude},{longitude}", | |
"radius": radius, | |
"key": KEY, | |
} | |
# Make the request | |
response = requests.get(url, params=params) | |
# Parse the response | |
response_data = json.loads(response.content) | |
# Get the results | |
results = response_data["resourceSets"][0]["resources"] | |
addresses = [] | |
for result in results: | |
name = result["name"] | |
address = result["Address"]["formattedAddress"] | |
addresses.append(f"{name}: {address}") | |
return addresses | |
return tool | |