I'm following fast.ai's jupyter notebook tutorials on the Gradient GPU server. In the second notebook 02_production.ipynb
, the search_images_bing
fails.
Code:
!pip install -Uqq fastbook
import fastbook
fastbook.setup_book()
from fastbook import *
from fastai.vision.widgets import *
key = os.environ.get('AZURE_SEARCH_KEY', 'XXX') # not showing my key :)
results = search_images_bing(key, 'grizzly bear', min_sz=128)
ims = results.attrgot('content_url')
Error:
---------------------------------------------------------------------------
ErrorResponseException Traceback (most recent call last)
<ipython-input-107-8c0a1d6b3765> in <module>
----> 1 results = search_images_bing(key, 'grizzly bear', min_sz=128)
2 ims = results.attrgot('content_url')
3
4 # ims = search_bing_by_term('grizzly bear', 100)
5 len(ims)
/opt/conda/envs/fastai/lib/python3.8/site-packages/fastbook/__init__.py in search_images_bing(key, term, min_sz)
50 def search_images_bing(key, term, min_sz=128):
51 client = api('https://api.cognitive.microsoft.com', auth(key))
---> 52 return L(client.images.search(query=term, count=150, min_height=min_sz, min_width=min_sz).value)
53
54 def plot_function(f, tx=None, ty=None, title=None, min=-2, max=2, figsize=(6,4)):
/opt/conda/envs/fastai/lib/python3.8/site-packages/azure/cognitiveservices/search/imagesearch/operations/_images_operations.py in search(self, query, accept_language, user_agent, client_id, client_ip, location, aspect, color, country_code, count, freshness, height, id, image_content, image_type, license, market, max_file_size, max_height, max_width, min_file_size, min_height, min_width, offset, safe_search, size, set_lang, width, custom_headers, raw, **operation_config)
489
490 if response.status_code not in [200]:
--> 491 raise models.ErrorResponseException(self._deserialize, response)
492
493 deserialized = None
ErrorResponseException: Operation returned an invalid status code 'PermissionDenied'
I've registered and set up the API correctly on Azure/Bing.Search.v7/F1 (free tier) and gotten the key.
How do I overcome this error with talking to the bing API?
Seems like the latest version of Bing Search breaks the connection with fast.ai's search_images_bing
function.
Improving on a workaround here https://forums.fast.ai/t/02-production-permissiondenied-error/65823/25?u=retuso, I was able to create a local instance of the search function that fits in (almost) perfectly with the natural use in the remainder of the notebook.
Local function:
def search_images_bing(key, term, max_images: int = 100, **kwargs):
params = {'q':term, 'count':max_images}
headers = {"Ocp-Apim-Subscription-Key":key}
search_url = "https://api.bing.microsoft.com/v7.0/images/search"
response = requests.get(search_url, headers=headers, params=params)
response.raise_for_status()
search_results = response.json()
# returns an L object to be identical to the original function.
return L(search_results['value'])
Code to call the function, noting that the new bing API doesn't use the original content_url
tag but instead uses contentUrl
:
# fits in nicely with the original code
results = search_images_bing(key, 'grizzly bear', min_sz=128)
ims = results.attrgot('contentUrl')
NB: I don't use the min_sz parameter (yet).