Spaces:
Runtime error
Runtime error
File size: 12,795 Bytes
115169a 5831cdb 115169a 5831cdb 115169a 1e77711 115169a 5831cdb 115169a 1e77711 115169a 5831cdb 115169a 5831cdb 115169a 1e77711 115169a 5c5bd6b 115169a 5c5bd6b 115169a 5c5bd6b 115169a 1e77711 115169a 5c5bd6b 115169a 5c5bd6b 115169a 5c5bd6b 115169a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import requests"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import utils\n",
"\n",
"utils.load_env()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/langchain_core/_api/deprecation.py:141: LangChainDeprecationWarning: The class `GooglePlacesTool` was deprecated in LangChain 0.0.33 and will be removed in 0.3.0. An updated version of the class exists in the langchain-google-community package and should be used instead. To use it run `pip install -U langchain-google-community` and import as `from langchain_google_community import GooglePlacesTool`.\n",
" warn_deprecated(\n",
"/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/langchain_core/_api/deprecation.py:141: LangChainDeprecationWarning: The class `GooglePlacesAPIWrapper` was deprecated in LangChain 0.0.33 and will be removed in 0.3.0. An updated version of the class exists in the langchain-google-community package and should be used instead. To use it run `pip install -U langchain-google-community` and import as `from langchain_google_community import GooglePlacesAPIWrapper`.\n",
" warn_deprecated(\n"
]
},
{
"data": {
"text/plain": [
"'1. Fashion Island\\nAddress: 587-589/7-9, Ram Inthra Rd, Khwaeng Khan Na Yao, Khet Khan Na Yao, Krung Thep Maha Nakhon 10230, Thailand\\nGoogle place ID: ChIJd57HEQRjHTERG_xSTcdQoYs\\nPhone: 02 947 5000\\nWebsite: http://www.fashionisland.co.th/\\n\\n\\n2. Food Island-Fashion Island\\nAddress: 5/5-6 หมู่ 5 Fashion Island, Ram Inthra Rd, Khwaeng Khan Na Yao, บางเขน Krung Thep Maha Nakhon 10220, Thailand\\nGoogle place ID: ChIJxVlvKA9jHTERAaDXThEk0sU\\nPhone: Unknown\\nWebsite: https://www.fashionisland.co.th/\\n\\n\\n3. Grand Station @ Fashion Island\\nAddress: Fashion Island Shopping Mall คันนายาว Khet Khan Na Yao, Krung Thep Maha Nakhon 10230, Thailand\\nGoogle place ID: ChIJgYJ0xNxjHTERqh7EMAXGflk\\nPhone: 02 947 5000\\nWebsite: https://www.fashionisland.co.th/about-us/grandstation/\\n\\n\\n4. Central Department Store Fashion Island\\nAddress: 591 Ram Inthra Rd, Khwaeng Khan Na Yao, Khet Khan Na Yao, Krung Thep Maha Nakhon 10230, Thailand\\nGoogle place ID: ChIJU51oO09jHTERkAkthlfrvMQ\\nPhone: 02 947 5320\\nWebsite: http://www.central.co.th/\\n\\n\\n5. Fashion Island\\nAddress: 401 Newport Center Dr, Newport Beach, CA 92660, USA\\nGoogle place ID: ChIJUetYrofg3IARvxarZNginsY\\nPhone: (949) 721-2000\\nWebsite: https://www.fashionisland.com/?utm_source=soci&utm_medium=organic&utm_campaign=gmb_soci_listing\\n\\n\\n6. Pomelo Fashion Island\\nAddress: Unit GSSH101, 1st Floor, Fashion Island Shopping Mall, 587-589, Ramindra Rd, แขวงคันนายาว เขตคันนายาว กรุงเทพมหานคร 10230, Thailand\\nGoogle place ID: ChIJaYz1YRFjHTERHcxGtqrypsk\\nPhone: 02 000 6811\\nWebsite: https://www.pomelofashion.com/\\n\\n\\n7. Haborland Fashion Island\\nAddress: ศูนย์การค้า แฟชั่นไอส์แลนด์ ชั้น3, Khwaeng Khan Na Yao, Khet Khan Na Yao, Krung Thep Maha Nakhon 10230, Thailand\\nGoogle place ID: ChIJ7wF25A5jHTERyBbgZ24hjGU\\nPhone: 065 848 1000\\nWebsite: https://harborlandgroup.com/\\n\\n\\n8. IF Store art & chara สาขา Fasion Island\\nAddress: KI B006-B007 โซนแกรนด์สเตชั่น ซอย 7 ศูนย์การค้าแฟชั่นไอส์แลนด์ ชั้น 2 587,589, 589 7 9 Ram Inthra Rd, Khwaeng Khan Na Yao, Khet Khan Na Yao, Krung Thep Maha Nakhon 10230, Thailand\\nGoogle place ID: ChIJx5zaF3RjHTER8ROSKii-tVQ\\nPhone: 063 232 6356\\nWebsite: https://shopee.co.th/ifstore_art\\n\\n\\n9. Panya Ramintra\\nAddress: Khan Na Yao, Bangkok 10230, Thailand\\nGoogle place ID: ChIJ9e97xwhjHTERR-MWDuKtUYY\\nPhone: Unknown\\nWebsite: Unknown\\n\\n\\n10. ร้านชุดชั้นใน GIRLY SECRET @Fasion Island\\nAddress: GIRLY SECRET แฟชั่นไอส์แลนด์ ชั้น2 โซนแกรนด์เสตชั่น สตรีท 11 Lat Phrao Road Khwaeng Khan Na Yao, คันนายาว Krung Thep Maha Nakhon 10230, Thailand\\nGoogle place ID: ChIJMRAHkpKd4jARZBQhL3tzF28\\nPhone: 092 697 9698\\nWebsite: Unknown\\n\\n\\n11. OkurimonoJapan! (Fashion Island)\\nAddress: Level 2, Ram Inthra Rd, Khwaeng Khan Na Yao, Khet Khan Na Yao, Krung Thep Maha Nakhon 10230, Thailand\\nGoogle place ID: ChIJUTgHRlBjHTERw3I3e-uH7hI\\nPhone: 080 594 4644\\nWebsite: Unknown\\n\\n\\n12. HomePro Fashion Island\\nAddress: 589, 10 Ram Inthra Rd, Khwaeng Khan Na Yao, Khet Khan Na Yao, Krung Thep Maha Nakhon 10230, Thailand\\nGoogle place ID: ChIJEyIpYLBjHTERibQeHSiSERE\\nPhone: 02 029 7700\\nWebsite: https://lin.ee/nfKDS60\\n\\n\\n13. Bake a Wish (Fashion Island)\\nAddress: ชั้น B, Fashion Island, 587 Ram Inthra Rd, รามอินทรา, Khet Khan Na Yao, Krung Thep Maha Nakhon 10230, Thailand\\nGoogle place ID: ChIJfyU40Q5jHTERTKLwRhRWk-o\\nPhone: 064 121 7146\\nWebsite: Unknown\\n\\n\\n14. Opposite Fashion Island\\nAddress: Khan Na Yao, Bangkok 10230, Thailand\\nGoogle place ID: ChIJV8YMrwhjHTERTd8eEupPwZY\\nPhone: Unknown\\nWebsite: Unknown\\n\\n'"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from langchain_community.tools import GooglePlacesTool\n",
"\n",
"places = GooglePlacesTool()\n",
"places.run(\"fasion islands\")"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"#find places id\n",
"#near by search"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'candidates': [{'formatted_address': '444 ถ. พญาไท แขวงวังใหม่ เขตปทุมวัน กรุงเทพมหานคร 10330 ไทย', 'geometry': {'location': {'lat': 13.744677, 'lng': 100.5295593}, 'viewport': {'northeast': {'lat': 13.74598387989272, 'lng': 100.5309299798927}, 'southwest': {'lat': 13.74328422010728, 'lng': 100.5282303201073}}}, 'name': 'เอ็ม บี เค เซ็นเตอร์', 'opening_hours': {'open_now': True}, 'rating': 4.3}], 'status': 'OK'}\n"
]
}
],
"source": [
"def find_place_from_text(input_text, location=None, radius=2000):\n",
" # Retrieve the API key from environment variables\n",
" api_key = os.getenv('GPLACES_API_KEY')\n",
"\n",
" if not api_key:\n",
" raise ValueError(\"API key not found. Please set the GOOGLE_MAPS_API_KEY environment variable.\")\n",
"\n",
" # Define the endpoint URL\n",
" url = \"https://maps.googleapis.com/maps/api/place/findplacefromtext/json\"\n",
"\n",
" # Define the parameters for the request\n",
" params = {\n",
" 'fields': 'formatted_address,name,rating,opening_hours,geometry',\n",
" 'input': input_text,\n",
" 'inputtype': 'textquery',\n",
" 'key': api_key\n",
" }\n",
" \n",
" params['locationbias'] = f'circle:{radius}@{location}' if location is not None and radius is not None else None\n",
"\n",
" # Make the request to the Google Maps API\n",
" response = requests.get(url, params=params)\n",
"\n",
" # Check if the request was successful\n",
" if response.status_code == 200:\n",
" return response.json() # Return the JSON response\n",
" else:\n",
" response.raise_for_status() # Raise an exception for HTTP errors\n",
"\n",
"# Example usage:\n",
"location = \"15.87,100.9925\" # Latitude and Longitude\n",
"input_text = \"มาบุญครอง\"\n",
"result = find_place_from_text(input_text, location)\n",
"\n",
"print(result)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Coordinates: 13.744677,100.5295593\n"
]
}
],
"source": [
"def find_location(input_text:str, location:str=None, radius=10000):\n",
" # Call the find_place_from_text function to get the location data\n",
" data = find_place_from_text(input_text, location, radius)\n",
"\n",
" # Extract the latitude and longitude from the response\n",
" candidates = data.get('candidates', [])\n",
" if len(candidates)==0:\n",
" raise ValueError(\"No location found.\")\n",
"\n",
" # Assuming we're taking the first candidate\n",
" geometry = candidates[0].get('geometry', {})\n",
" location = geometry.get('location', {})\n",
"\n",
" latitude = location.get('lat')\n",
" longitude = location.get('lng')\n",
"\n",
" if latitude is None or longitude is None:\n",
" raise ValueError(\"Latitude or Longitude not found in the response.\")\n",
"\n",
" # Return the latitude and longitude as a formatted string\n",
" return f\"{latitude},{longitude}\"\n",
"\n",
"\n",
"location = \"15.87,100.9925\" # Latitude and Longitude\n",
"input_text = \"มาบุญครอง\"\n",
"coordinates = find_location(input_text, location)\n",
"print(f\"Coordinates: {coordinates}\")"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"21"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def nearby_search(keyword:str, location:str, radius=2000, place_type=None):\n",
" # Retrieve the API key from environment variables\n",
" api_key = os.getenv('GPLACES_API_KEY')\n",
"\n",
" if not api_key:\n",
" raise ValueError(\"API key not found. Please set the GOOGLE_MAPS_API_KEY environment variable.\")\n",
"\n",
" # Define the endpoint URL\n",
" url = \"https://maps.googleapis.com/maps/api/place/nearbysearch/json\"\n",
"\n",
" # Set up the parameters for the request\n",
" params = {\n",
" 'keyword': keyword,\n",
" 'location': location,\n",
" 'radius': radius,\n",
" 'type': place_type,\n",
" 'key': api_key,\n",
" \"rankPreference\": \"DISTANCE\"\n",
" }\n",
"\n",
" # Send the GET request to the Google Maps API\n",
" response = requests.get(url, params=params)\n",
"\n",
" # Check if the request was successful\n",
" if response.status_code != 200:\n",
" raise Exception(f\"Error with request: {response.status_code}, {response.text}\")\n",
"\n",
" # Parse the JSON response\n",
" data = response.json()\n",
" results = data['results']\n",
"\n",
" # search into next page\n",
" while data.get('next_page_token', False):\n",
" params = {'next_page_token': data['next_page_token']}\n",
" response = requests.get(url, params=params)\n",
" \n",
" if response.status_code != 200:\n",
" raise Exception(f\"Error with request: {response.status_code}, {response.text}\")\n",
" \n",
" data = response.json()\n",
" \n",
" results.append(data['results'])\n",
" \n",
"\n",
" # Return the response data\n",
" return results\n",
"\n",
"\n",
"# Example usage\n",
"location = \"13.744677,100.5295593\" # Latitude and Longitude\n",
"keyword = \"ร้านอาหาร\"\n",
"search_results = nearby_search(keyword, location)\n",
"len(search_results)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.9"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|