# Amazon ML Challenge

Machyon was born in this challenge. If you're in it now, the clock is running: here's the cell to paste, then everything you might need when something goes sideways.

> [!NOTE]
>
> Machyon is an independent open-source project, not affiliated with or endorsed by Amazon. Always follow your challenge's rules.

## 1\. Paste this

One notebook cell

```python
!pip install -q machyon
import machyon

# Same file names as the starter kit: images/train/71XfHPR36-L.jpg
opts = dict(url_col="image_link", names="{stem}{ext}")
train = await machyon.adownload("dataset/train.csv", "images/train", **opts)
test = await machyon.adownload("dataset/test.csv", "images/test", **opts)
print(train)
print(test)
```

download\_images.py

```python
import machyon

# Same file names as the starter kit: images/train/71XfHPR36-L.jpg
opts = dict(url_col="image_link", names="{stem}{ext}")
train = machyon.download("dataset/train.csv", "images/train", **opts)
test = machyon.download("dataset/test.csv", "images/test", **opts)
print(train)
print(test)
```

```bash
pip install machyon
machyon dataset/train.csv -o images/train --url-col image_link --names "{stem}{ext}"
machyon dataset/test.csv  -o images/test  --url-col image_link --names "{stem}{ext}"
```

That's the whole download. Files are named exactly as the starter kit's `download_images()` names them (the last part of the URL, such as `71XfHPR36-L.jpg`), so the rest of your notebook works unchanged.

> [!TIP]
>
> **In a notebook, it's `await`**
>
> Notebooks already run an event loop, so use `await machyon.adownload(...)` there, as in the cell above. `machyon.download(...)` is for scripts and the terminal; in a notebook it stops with `RuntimeError: machyon.download() can't run inside a running event loop`.

## 2\. Already using the starter kit?

Keep your code and swap one function. Paste this over the `download_images` you import from `src/utils.py`:

Drop-in replacement

```python
import asyncio
from concurrent.futures import ThreadPoolExecutor

import pandas as pd

import machyon


def download_images(image_links, download_folder, allow_multiprocessing=True):
    """Drop-in for the starter kit's version: same arguments, same file names.
    Works in scripts and in notebooks (src/test.ipynb calls it from a notebook)."""
    links = [str(u) for u in image_links]

    def job():
        return machyon.download(links, download_folder, names="{stem}{ext}")

    try:
        asyncio.get_running_loop()       # inside a notebook: an event loop is running
    except RuntimeError:
        return job()
    with ThreadPoolExecutor(1) as pool:  # so download on a worker thread instead
        return pool.submit(job).result()


train = pd.read_csv("dataset/train.csv")
print(download_images(train["image_link"], "images/train"))
```

Same arguments, same file names. What changes is underneath: the speed tunes itself instead of using a fixed pool, transient failures are retried with backoff, everything that still fails is written down with a reason, and an HTML error page can never end up saved as a `.jpg`.

## 3\. Join images back to rows

After the download

```python
from pathlib import Path

import pandas as pd

train = pd.read_csv("dataset/train.csv")
names = [Path(u).name for u in train["image_link"]]
train["image_path"] = [f"images/train/{n}" for n in names]
train["has_image"] = [Path(p).is_file() for p in train["image_path"]]
print(train["has_image"].value_counts())  # False? use text features
```

The file name comes from the URL, so the path is a one-liner and nothing needs a lookup table. Rows without an image happen in any large scraped dataset, because some links stop serving an image. Keep those rows, and give your model a text-only path for `has_image == False`.

Want the exact reasons? They're in `images/train/failed_downloads.jsonl`, one line per missing image. [Reading the ledger](<https://divyanshusingh96.github.io/machyon/reliability/failure-ledger/index.md>)

## When something goes sideways

****The runtime disconnected or the kernel restarted.****

Run the same cell again. Finished images are skipped in seconds and the rest continue. [Resuming](<https://divyanshusingh96.github.io/machyon/reliability/resuming/index.md>)

****Some images failed.****

Look at the reasons at the end of the output. `http_404` means the image is gone: retrying won't help, so move on. For timeouts or `http_5xx`, run `machyon retry -o images/train` (or `!machyon retry -o images/train` in a notebook) once things calm down.

****It's slow, or you see `http_429`.****

Machyon is already slowing down for the server; raising concurrency makes it worse. Hosted notebooks often share network addresses with other users, so the image server may be throttling the whole address, not just you. Let it run, and retry stragglers later.

****The same image appears on many rows.****

That's expected, and free: each URL is downloaded once. With `{stem}{ext}` every row that shares a URL shares the file.

****Disk space.****

Check before you start: `!df -h .`. On Colab, download to the local disk (`/content/…`), not into a mounted Google Drive folder: hundreds of thousands of small files are very slow to write there. Zip the folder and copy the one archive to Drive if you need to keep it.

****Your teammates want to help.****

Split the rows with `--start` and `--end` (for example `--start 0 --end 130000` and `--start 130000`), let each person download their half, then unzip everything into one folder. Names come from the URLs, so halves never collide.

## Other hackathons

The same recipe works for any CSV of image URLs. Two settings adapt it:

- `url_col` is the column with the links (`image_link` here). Machyon also guesses common names on its own.
- `names` decides the file names. `{stem}{ext}` keeps the URL's own name; `{sample_id}{ext}` or `{index}{ext}` names files after an id column or the row number; `{label}/{stem}{ext}` adds a folder per class.

Preview the names before a long run: `machyon names dataset/train.csv --names "{sample_id}{ext}"`. [Filenames](<https://divyanshusingh96.github.io/machyon/guides/naming/index.md>)

## Why this exists

During the challenge, our team lost hours to downloading: scripts that died halfway, retried nothing, and saved HTML error pages as `.jpg` files that we only discovered during training. Machyon is the downloader we wished we'd had. If it saves you an evening, it has done its job. Good luck!
