2023-04-18 03:49:10 +00:00
|
|
|
{
|
|
|
|
"cells": [
|
|
|
|
{
|
|
|
|
"cell_type": "markdown",
|
|
|
|
"id": "ddb208a0-617e-433e-b9de-69099bc456f8",
|
|
|
|
"metadata": {},
|
|
|
|
"source": [
|
|
|
|
"# Image captions\n",
|
2023-05-02 22:24:53 +00:00
|
|
|
"\n",
|
|
|
|
"By default, the loader utilizes the pre-trained [Salesforce BLIP image captioning model](https://huggingface.co/Salesforce/blip-image-captioning-base).\n",
|
|
|
|
"\n",
|
|
|
|
"\n",
|
2023-05-06 00:44:54 +00:00
|
|
|
"This notebook shows how to use the `ImageCaptionLoader` to generate a query-able index of image captions"
|
2023-04-18 03:49:10 +00:00
|
|
|
]
|
|
|
|
},
|
2023-05-02 22:24:53 +00:00
|
|
|
{
|
|
|
|
"cell_type": "code",
|
|
|
|
"execution_count": null,
|
|
|
|
"id": "9f78585a-a2fa-4ece-834f-66692b959efb",
|
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"#!pip install transformers"
|
|
|
|
]
|
|
|
|
},
|
2023-04-18 03:49:10 +00:00
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2023-09-01 22:15:12 +00:00
|
|
|
"execution_count": null,
|
2023-04-18 03:49:10 +00:00
|
|
|
"id": "ac0a2a76-c36a-4952-b511-7906ca840e08",
|
|
|
|
"metadata": {
|
|
|
|
"pycharm": {
|
|
|
|
"is_executing": true
|
|
|
|
},
|
|
|
|
"tags": []
|
|
|
|
},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"from langchain.document_loaders import ImageCaptionLoader"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "markdown",
|
|
|
|
"id": "faefe80f-08f2-4683-a325-4efd61fae0bf",
|
|
|
|
"metadata": {},
|
|
|
|
"source": [
|
|
|
|
"### Prepare a list of image urls from Wikimedia"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2023-09-01 22:15:12 +00:00
|
|
|
"execution_count": null,
|
2023-04-18 03:49:10 +00:00
|
|
|
"id": "9a400568-5fea-47e6-8703-d9c1a1cc00ea",
|
|
|
|
"metadata": {
|
|
|
|
"tags": []
|
|
|
|
},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"list_image_urls = [\n",
|
2023-06-16 18:52:56 +00:00
|
|
|
" \"https://upload.wikimedia.org/wikipedia/commons/thumb/5/5a/Hyla_japonica_sep01.jpg/260px-Hyla_japonica_sep01.jpg\",\n",
|
|
|
|
" \"https://upload.wikimedia.org/wikipedia/commons/thumb/7/71/Tibur%C3%B3n_azul_%28Prionace_glauca%29%2C_canal_Fayal-Pico%2C_islas_Azores%2C_Portugal%2C_2020-07-27%2C_DD_14.jpg/270px-Tibur%C3%B3n_azul_%28Prionace_glauca%29%2C_canal_Fayal-Pico%2C_islas_Azores%2C_Portugal%2C_2020-07-27%2C_DD_14.jpg\",\n",
|
|
|
|
" \"https://upload.wikimedia.org/wikipedia/commons/thumb/2/21/Thure_de_Thulstrup_-_Battle_of_Shiloh.jpg/251px-Thure_de_Thulstrup_-_Battle_of_Shiloh.jpg\",\n",
|
|
|
|
" \"https://upload.wikimedia.org/wikipedia/commons/thumb/2/21/Passion_fruits_-_whole_and_halved.jpg/270px-Passion_fruits_-_whole_and_halved.jpg\",\n",
|
|
|
|
" \"https://upload.wikimedia.org/wikipedia/commons/thumb/5/5e/Messier83_-_Heic1403a.jpg/277px-Messier83_-_Heic1403a.jpg\",\n",
|
|
|
|
" \"https://upload.wikimedia.org/wikipedia/commons/thumb/b/b6/2022-01-22_Men%27s_World_Cup_at_2021-22_St._Moritz%E2%80%93Celerina_Luge_World_Cup_and_European_Championships_by_Sandro_Halank%E2%80%93257.jpg/288px-2022-01-22_Men%27s_World_Cup_at_2021-22_St._Moritz%E2%80%93Celerina_Luge_World_Cup_and_European_Championships_by_Sandro_Halank%E2%80%93257.jpg\",\n",
|
|
|
|
" \"https://upload.wikimedia.org/wikipedia/commons/thumb/9/99/Wiesen_Pippau_%28Crepis_biennis%29-20220624-RM-123950.jpg/224px-Wiesen_Pippau_%28Crepis_biennis%29-20220624-RM-123950.jpg\",\n",
|
2023-04-18 03:49:10 +00:00
|
|
|
"]"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "markdown",
|
|
|
|
"id": "be585acd-6e28-4400-9e8f-17fdde11e02c",
|
|
|
|
"metadata": {},
|
|
|
|
"source": [
|
|
|
|
"### Create the loader"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2023-09-01 22:15:12 +00:00
|
|
|
"execution_count": null,
|
2023-04-18 03:49:10 +00:00
|
|
|
"id": "fb392517-72d8-416e-852c-da90b77267ed",
|
|
|
|
"metadata": {
|
|
|
|
"tags": []
|
|
|
|
},
|
2023-09-01 22:15:12 +00:00
|
|
|
"outputs": [],
|
2023-04-18 03:49:10 +00:00
|
|
|
"source": [
|
|
|
|
"loader = ImageCaptionLoader(path_images=list_image_urls)\n",
|
|
|
|
"list_docs = loader.load()\n",
|
|
|
|
"list_docs"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2023-09-01 22:15:12 +00:00
|
|
|
"execution_count": null,
|
2023-04-18 03:49:10 +00:00
|
|
|
"id": "0f56db67-99bb-4543-ba40-1871a58b2da5",
|
|
|
|
"metadata": {
|
|
|
|
"tags": []
|
|
|
|
},
|
2023-09-01 22:15:12 +00:00
|
|
|
"outputs": [],
|
2023-04-18 03:49:10 +00:00
|
|
|
"source": [
|
|
|
|
"from PIL import Image\n",
|
|
|
|
"import requests\n",
|
|
|
|
"\n",
|
2023-06-16 18:52:56 +00:00
|
|
|
"Image.open(requests.get(list_image_urls[0], stream=True).raw).convert(\"RGB\")"
|
2023-04-18 03:49:10 +00:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "markdown",
|
|
|
|
"id": "52193308-e2c5-4757-8f86-a73c07510f73",
|
|
|
|
"metadata": {},
|
|
|
|
"source": [
|
|
|
|
"### Create the index"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2023-09-01 22:15:12 +00:00
|
|
|
"execution_count": null,
|
2023-04-18 03:49:10 +00:00
|
|
|
"id": "7b7a15ac-d2c7-4359-9c5c-a543c8eebf80",
|
|
|
|
"metadata": {
|
|
|
|
"tags": []
|
|
|
|
},
|
2023-09-01 22:15:12 +00:00
|
|
|
"outputs": [],
|
2023-04-18 03:49:10 +00:00
|
|
|
"source": [
|
|
|
|
"from langchain.indexes import VectorstoreIndexCreator\n",
|
2023-06-16 18:52:56 +00:00
|
|
|
"\n",
|
2023-04-18 03:49:10 +00:00
|
|
|
"index = VectorstoreIndexCreator().from_loaders([loader])"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "markdown",
|
|
|
|
"id": "677398d8-6ab7-4224-8e4a-4b94a7fb2a94",
|
|
|
|
"metadata": {},
|
|
|
|
"source": [
|
|
|
|
"### Query"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2023-09-01 22:15:12 +00:00
|
|
|
"execution_count": null,
|
2023-04-18 03:49:10 +00:00
|
|
|
"id": "e03e31c6-3018-434d-bcad-5c25144509e1",
|
|
|
|
"metadata": {
|
|
|
|
"tags": []
|
|
|
|
},
|
2023-09-01 22:15:12 +00:00
|
|
|
"outputs": [],
|
2023-04-18 03:49:10 +00:00
|
|
|
"source": [
|
|
|
|
"query = \"What's the painting about?\"\n",
|
|
|
|
"index.query(query)"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2023-09-01 22:15:12 +00:00
|
|
|
"execution_count": null,
|
2023-04-18 03:49:10 +00:00
|
|
|
"id": "c3ec2b5a-9c03-4e32-b571-be5af9a22223",
|
|
|
|
"metadata": {
|
|
|
|
"tags": []
|
|
|
|
},
|
2023-09-01 22:15:12 +00:00
|
|
|
"outputs": [],
|
2023-04-18 03:49:10 +00:00
|
|
|
"source": [
|
|
|
|
"query = \"What kind of images are there?\"\n",
|
|
|
|
"index.query(query)"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"metadata": {
|
|
|
|
"kernelspec": {
|
|
|
|
"display_name": "Python 3 (ipykernel)",
|
|
|
|
"language": "python",
|
|
|
|
"name": "python3"
|
|
|
|
},
|
|
|
|
"language_info": {
|
|
|
|
"codemirror_mode": {
|
|
|
|
"name": "ipython",
|
|
|
|
"version": 3
|
|
|
|
},
|
|
|
|
"file_extension": ".py",
|
|
|
|
"mimetype": "text/x-python",
|
|
|
|
"name": "python",
|
|
|
|
"nbconvert_exporter": "python",
|
|
|
|
"pygments_lexer": "ipython3",
|
2023-09-01 22:15:12 +00:00
|
|
|
"version": "3.11.3"
|
2023-04-18 03:49:10 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
"nbformat": 4,
|
|
|
|
"nbformat_minor": 5
|
|
|
|
}
|