ddg搜索时出现错误,使用fastapi的pip版本

huangapple go评论89阅读模式
英文:

ddg search error when using fastapi's pip version

问题

我一直在尝试使用duckduckgo获取图像,使用以下代码:

from duckduckgo_search import DDGS
from fastcore.all import *

ddgs = DDGS()
def search_images(term, max_images=30):
    print(f"Searching for '{term}'")
    keywords = 'stop sign'
    return L(ddgs.images((keywords, term, max_results=max_images)).itemgot('image'))

但是它报错了:Cell In[30],第8行
return L(ddgs.images((keywords, term, max_results=max_images)).itemgot('image'))
^
SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?

我查了一下,似乎是fastbook-0.0.16.tar.gz的pip版本中存在问题,其中文件init.py覆盖并破坏了search_images_ddg的工作版本。有没有办法修复这个问题?
如果不能修复,是否有其他好用于获取图像的搜索引擎API?
我正在使用jupyter notebook创建一个基本的图像识别工具,使用fastapi。

英文:

I've been trying to get images from duckduckgo using

from duckduckgo_search import DDGS
from fastcore.all import *

ddgs = DDGS()
def search_images(term, max_images=30):
    print(f"Searching for '{term}'")
    keywords = 'stop sign'
    return L(ddgs.images((keywords, term, max_results=max_images)).itemgot('image'))

but it gives an error:  Cell In[30], line 8
    return L(ddgs.images((keywords, term, max_results=max_images)).itemgot('image'))
                                          ^
SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?

did some looking up and seems there's some issue with the pip version of fastbook-0.0.16.tar.gz where the file: init.py overwrites and breaks the working version of search_images_ddg
Is there any way to fix this?
If not, is there another search engine API that's good for getting images?
I'm making a basic image recognition thing with fastapi
in jupyter notebook

答案1

得分: 1

DDGS的images()函数不再支持max_results参数,因此我们必须在使用images()函数创建生成器后的一个单独步骤中执行此操作。
生成器没有itemgot()方法,因此我们还需要在另一个步骤中获取URL。

这是基于当前duckduckgo_search生成器实现的search_images函数的可插拔替代方法。

from duckduckgo_search import DDGS
from fastcore.all import *

def search_images(term, max_images=30):
    print(f"搜索'{term}'的图片")
    with DDGS() as ddgs:
        # 产生一个字典的生成器:
        # {'title','image','thumbnail','url','height','width','source'}
        search_results = ddgs.images(keywords=term)
        # 获取最大数量的urls
        image_urls = [next(search_results).get("image") for _ in range(max_images)]
        # 转换为L(来自fastai的功能扩展列表类)
        return L(image_urls)

# 示例用法:
urls = search_images("狗 图片", max_images=10)
print(urls[0])

如果您对fastbook的pip版本有问题,也许直接安装fastai和duckduckgo_search包会更好。

在Jupyter Notebook中安装:

%pip install -U fastai duckduckgo_search

# 如果您在本地使用Anaconda,最好使用conda或mamba(mambaforge)而不是pip:
!mamba install fastai 
%pip install -U duckduckgo_search 

# 或者
%conda install fastai
%pip install -U duckduckgo_search
英文:

The images() function of DDGS no longer supports the max_results argument, so we have to do it in a separate step after creating the generator with the images() function.
A generator has no method itemgot(), so we also need to get the urls in another step.

Here is a drop-in replacement for the function search_images based on the current generator implementation of duckduckgo_search.

from duckduckgo_search import DDGS
from fastcore.all import *

def search_images(term, max_images=30):
    print(f"Searching for '{term}'")
    with DDGS() as ddgs:
        # generator which yields dicts with:
        # {'title','image','thumbnail','url','height','width','source'}
        search_results = ddgs.images(keywords=term)       
        # grap number of max_images urls
        image_urls = [next(search_results).get("image") for _ in range(max_images)]
        # convert to L (functionally extended list class from fastai)
        return L(image_urls)

# example usage:
urls = search_images("dog images", max_images=10)
print(urls[0])

If you have problems with the pip version of fastbook, maybe just install the fastai and duckduckgo_search packages directly.

For installing from a Jupyter Notebook:

%pip install -U fastai duckduckgo_search

# if you use Anaconda locally it is better to use 
# conda or mamba (mambaforge) instead of pip:
!mamba install fastai 
%pip install -U duckduckgo_search 

# or
%conda install fastai
%pip install -U duckduckgo_search

答案2

得分: 0

我遇到了相同的问题。原来教程中使用的函数已经过时了,而更新后的duckduckgo image()函数返回一个生成器而不是一个列表,所以只是改变函数调用并不能解决问题。你可以尝试以下方法来解决:

from duckduckgo_search import DDGS
from fastcore.all import *

def search_images(term, max_images=30):
    print(f"Searching for '{term}'")
    with DDGS() as ddgs:
        ddgs_images_gen = ddgs.images(term)
        count = 0
        ddgs_images_list = []
        while count < max_images:
            image = next(ddgs_images_gen)
            ddgs_images_list.append(image.get('image'))
            count = count + 1
        return ddgs_images_list

这段代码的作用是将所需数量的图像添加到一个列表中,然后返回该列表。

然后你可以使用以下代码来获取正确的结果:

urls = search_images('stop sign', max_images=1)

urls[0]

现在应该可以得到正确的结果了。希望这对你有帮助!

英文:

I had the same issue. Turns out the function used in the tutorial is outdated, and the updated duckduckgo image() function returns a generator instead of a list, so just changing the function call doesn't solve the problem. What you can do instead is to work around the new function. Try the following:

from duckduckgo_search import DDGS
from fastcore.all import *

def search_images(term, max_images=30):
    print(f&quot;Searching for &#39;{term}&#39;&quot;)
    with DDGS() as ddgs:
        ddgs_images_gen = ddgs.images(term)
        count = 0
        ddgs_images_list = []
        while count &lt; max_images:
            image = next(ddgs_images_gen)
            ddgs_images_list.append(image.get(&#39;image&#39;))
            count = count+1
        return ddgs_images_list

All the code does is add the desired number of images to a list, then return that list.

urls = search_images(&#39;stop sign&#39;, max_images=1)

urls[0]

This should now give the proper result. Hope this helps!

huangapple
  • 本文由 发表于 2023年5月28日 01:02:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76348048.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定