|
4 | 4 | "metadata": {
|
5 | 5 | "colab": {
|
6 | 6 | "provenance": [],
|
7 |
| - "authorship_tag": "ABX9TyNiCJitZw07PsgK/m4d0hNo", |
| 7 | + "authorship_tag": "ABX9TyML/Sgx5/cEAkxsyW+WQDVc", |
8 | 8 | "include_colab_link": true
|
9 | 9 | },
|
10 | 10 | "kernelspec": {
|
|
2573 | 2573 | "id": "j40VtILbJmbd"
|
2574 | 2574 | }
|
2575 | 2575 | },
|
| 2576 | + { |
| 2577 | + "cell_type": "markdown", |
| 2578 | + "source": [ |
| 2579 | + "## Pipeline API\n", |
| 2580 | + "\n", |
| 2581 | + "If you don't bother about details of pre- and postprocessing, we have the [pipelines](https://huggingface.co/docs/transformers.js/pipelines) which make inference very easy!" |
| 2582 | + ], |
| 2583 | + "metadata": { |
| 2584 | + "id": "lDw5QpSjNidk" |
| 2585 | + } |
| 2586 | + }, |
| 2587 | + { |
| 2588 | + "cell_type": "code", |
| 2589 | + "source": [ |
| 2590 | + "from transformers import pipeline\n", |
| 2591 | + "from PIL import Image\n", |
| 2592 | + "import requests\n", |
| 2593 | + "\n", |
| 2594 | + "# load pipe\n", |
| 2595 | + "image_classifier = pipeline(task=\"zero-shot-image-classification\", model=\"google/siglip-so400m-patch14-384\")\n", |
| 2596 | + "\n", |
| 2597 | + "# load image\n", |
| 2598 | + "url = 'http://images.cocodataset.org/val2017/000000039769.jpg'\n", |
| 2599 | + "image = Image.open(requests.get(url, stream=True).raw)\n", |
| 2600 | + "\n", |
| 2601 | + "# inference\n", |
| 2602 | + "outputs = image_classifier(image, candidate_labels=[\"2 cats\", \"a plane\", \"a remote\"])\n", |
| 2603 | + "outputs = [{\"score\": round(output[\"score\"], 4), \"label\": output[\"label\"] } for output in outputs]\n", |
| 2604 | + "print(outputs)" |
| 2605 | + ], |
| 2606 | + "metadata": { |
| 2607 | + "colab": { |
| 2608 | + "base_uri": "https://localhost:8080/" |
| 2609 | + }, |
| 2610 | + "id": "jqAmisBiJX_n", |
| 2611 | + "outputId": "e75ad0df-66bb-4bcc-bb11-88b0cc7a422d" |
| 2612 | + }, |
| 2613 | + "execution_count": 9, |
| 2614 | + "outputs": [ |
| 2615 | + { |
| 2616 | + "output_type": "stream", |
| 2617 | + "name": "stdout", |
| 2618 | + "text": [ |
| 2619 | + "[{'score': 0.5089, 'label': '2 cats'}, {'score': 0.0, 'label': 'a remote'}, {'score': 0.0, 'label': 'a plane'}]\n" |
| 2620 | + ] |
| 2621 | + } |
| 2622 | + ] |
| 2623 | + }, |
2576 | 2624 | {
|
2577 | 2625 | "cell_type": "code",
|
2578 | 2626 | "source": [],
|
2579 | 2627 | "metadata": {
|
2580 |
| - "id": "jqAmisBiJX_n" |
| 2628 | + "id": "Yw4locEcNplK" |
2581 | 2629 | },
|
2582 | 2630 | "execution_count": null,
|
2583 | 2631 | "outputs": []
|
|
0 commit comments