Created
February 20, 2023 15:25
-
-
Save wesslen/f7f79848f1d4fc31b2bf97fa78159b96 to your computer and use it in GitHub Desktop.
Dog API Prodigy recipe
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import prodigy | |
| import requests | |
| import random | |
| from typing import List | |
| from prodigy.components.preprocess import fetch_media | |
| from prodigy.util import split_string | |
| from prodigy import set_hashes | |
| def get_stream(labels): | |
| while True: | |
| images = [] | |
| for label in labels: | |
| res = requests.get("https://dog.ceo/api/breed/" + label + "/images/random/10").json() | |
| for i in res["message"]: | |
| images.append({"image": i, "meta": {"breed": label}}) | |
| random.shuffle(images) | |
| for i in images: | |
| yield {"image": i["image"], "meta": {"breed": i["meta"]["breed"]}} | |
| @prodigy.recipe( | |
| "dog-image-recipe", | |
| dataset=("Dataset to save answers to", "positional", None, str), | |
| label=("One or more comma-separated labels", "positional", "l", split_string), | |
| ) | |
| def dog_image_recipe( | |
| dataset: str, | |
| label: List[str], | |
| ): | |
| stream = get_stream(label) | |
| stream = (set_hashes(eg) for eg in stream) | |
| return { | |
| "dataset": dataset, | |
| "view_id": "image_manual", | |
| "stream": fetch_media(stream, ["image"], skip=True), | |
| "config": { | |
| "labels": label, | |
| }, | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment