微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

fast.ai seach_images_bing抛出PermissionDenied错误

如何解决fast.ai seach_images_bing抛出PermissionDenied错误

我在Gradient GPU服务器上关注fast.ai的jupyter笔记本教程。在第二个笔记本02_production.ipynb中,search_images_bing失败。

代码

!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')

错误

---------------------------------------------------------------------------
ErrorResponseException                    Traceback (most recent call last)
<ipython-input-107-8c0a1d6b3765> in <module>
----> 1 results = search_images_bing(key,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,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'

我已经在Azure / Bing.Search.v7 / F1(免费版)上正确注册并设置了API,并获取了密钥。

如何通过与bing API对话来克服此错误

解决方法

似乎最新版本的Bing Search使用fast.ai的search_images_bing函数中断了连接。

https://forums.fast.ai/t/02-production-permissiondenied-error/65823/25?u=retuso处的解决方法上进行了改进,我能够创建一个搜索功能的本地实例,该实例可以(几乎)完全适合笔记本其余部分的自然使用。

本地功能:

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'])

调用该函数的代码,注意新的bing API不使用原始的content_url标签,而是使用contentUrl

# fits in nicely with the original code
results = search_images_bing(key,'grizzly bear',min_sz=128)
ims = results.attrgot('contentUrl')

注意:我还没有使用min_sz参数。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。