Cannot start very first exercise "Is it a bird" because of ducktogo error

I am trying to run this part but it crashes:

from duckduckgo_search import ddg_images
from fastcore.all import *

def search_images(term, max_images=30):
print(f"Searching for ‘{term}’")
return L(ddg_images(term, max_results=max_images)).itemgot(‘image’)

Blockquote---------------------------------------------------------------------------
HTTPError Traceback (most recent call last)
/tmp/ipykernel_51/2432147335.py in
1 #NB: search_images depends on duckduckgo.com, which doesn’t always return correct responses.
2 # If you get a JSON error, just try running it again (it may take a couple of tries).
----> 3 urls = search_images(‘bird photos’, max_images=1)
4 urls[0]

/tmp/ipykernel_51/1717929076.py in search_images(term, max_images)
4 def search_images(term, max_images=30):
5 print(f"Searching for ‘{term}’")
----> 6 return L(ddg_images(term, max_results=max_images)).itemgot(‘image’)

/opt/conda/lib/python3.7/site-packages/duckduckgo_search/compat.py in ddg_images(keywords, region, safesearch, time, size, color, type_image, layout, license_image, max_results, page, output, download)
80 type_image=type_image,
81 layout=layout,
—> 82 license_image=license_image,
83 ):
84 results.append(r)

/opt/conda/lib/python3.7/site-packages/duckduckgo_search/duckduckgo_search.py in images(self, keywords, region, safesearch, timelimit, size, color, type_image, layout, license_image)
403 assert keywords, “keywords is mandatory”
404
→ 405 vqd = self._get_vqd(keywords)
406 assert vqd, “error in getting vqd”
407

/opt/conda/lib/python3.7/site-packages/duckduckgo_search/duckduckgo_search.py in _get_vqd(self, keywords)
93 def _get_vqd(self, keywords: str) → Optional[str]:
94 “”“Get vqd value for a search query.”“”
—> 95 resp = self._get_url(“POST”, “https://duckduckgo.com”, data={“q”: keywords})
96 if resp:
97 for c1, c2 in (

/opt/conda/lib/python3.7/site-packages/duckduckgo_search/duckduckgo_search.py in _get_url(self, method, url, **kwargs)
87 logger.warning(f"_get_url() {url} {type(ex).name} {ex}")
88 if i >= 2 or “418” in str(ex):
—> 89 raise ex
90 sleep(3)
91 return None

/opt/conda/lib/python3.7/site-packages/duckduckgo_search/duckduckgo_search.py in _get_url(self, method, url, **kwargs)
80 )
81 if self._is_500_in_url(str(resp.url)) or resp.status_code == 202:
—> 82 raise httpx._exceptions.HTTPError(“”)
83 resp.raise_for_status()
84 if resp.status_code == 200:

HTTPError:

Hi Alfred,
I have been having the same problem - I found this modified notebook that works for me: [FastAI][Updated 11.2023] Is it a bird? | Kaggle

2 Likes

Hi

Firstly, you need to got to the notebook options and for the “ENVIRONMENT” option, select “Always use latest environment”.

Then you need to use following code instead of calling ddg_images:

with DDGS() as ddgs:
        return L(ddgs.images(term, max_results=max_images)).itemgot('image')

Also remember to import DDGS instead of ddg_images at the top.

This has solved my image downloading issue. I’m yet to test the rest of the notebook with this.

2 Likes

Hello Sameer, wondering if you could answer an unrelated question for me.

In the very beginning of lesson 1 we are doing a search and import of images. The code is below. I understand how the code works, but am wondering more about the syntax. This seems to be a loop function looping on the two searches= ‘forest’, ‘bird’, and the {o} joins the search text.

I am not a coder, but am familiar with code concepts. Is this syntax Python or fast.ai? Where would I go to learn about the syntax for this block of code?

searches = ‘forest’,‘bird’
path = Path(‘bird_or_not’)
from time import sleep

for o in searches:
dest = (path/o)
dest.mkdir(exist_ok=True, parents=True)
download_images(dest, urls=search_images(f’{o} photo’))
sleep(10) # Pause between searches to avoid over-loading server
download_images(dest, urls=search_images(f’{o} sun photo’))
sleep(10)
download_images(dest, urls=search_images(f’{o} shade photo’))
sleep(10)
resize_images(path/o, max_size=400, dest=path/o) [quote=“Sameer, post:3, topic:110209, full:true”]

The syntax is Python. If you’re using Colab, you can just hover over different functions and it will give you additional information about what arguments they require, what parameters can be set, and details about what the function returns. FWIW I’ve found ChatGPT to be very helpful in explaining what functions are doing. Here’s a link for a step-by-step explainer: ChatGPT

1 Like

Hi miketran,

rob_lh has indeed given a good answer already.
To add to that, yes… technically speaking, the “syntax” belongs to the language rather than any library.
The library will have ready-made functions/classes etc. defined for the user to use.
This particular block contains some of the primitive control flows, including the for loop, function calls etc. It also contains a bit of string formatting as you correctly pointed out about the object o joining the rest of the strings.
As these are the primitive constructs/building blocks of any imperative programming language, any material for Python beginners should cover these in the very early chapters.
The keywords you should be looking for to learn more about the block is “Python control flow mechanisms”.
Hope that helps…
Good luck! :slight_smile:

1 Like

Thank you! I am using Kaggle…Sounds like I might want to switch to Colab and Chatgpt.

Thank you Sameer! Very helpful! :pray:

1 Like

Using ChatGPT as a tutor is a great idea…I have been using it for many other things, but did not think about asking it to tutor me on the Python syntax…I have been using it the last few hours it is very helpful…

Thanks for the suggestion!

1 Like