{ "cells": [ { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "from langchain.document_loaders import ImageCaptionLoader" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "list_image_urls = [\n", " 'https://t7.baidu.com/it/u=2168645659,3174029352&fm=193&f=GIF',\n", " 'https://t7.baidu.com/it/u=2851687453,2321283050&fm=193&f=GIF',\n", " 'https://t7.baidu.com/it/u=3601447414,1764260638&fm=193&f=GIF',\n", " 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fblog%2F202111%2F17%2F20211117092914_579a7.thumb.1000_0.jpeg&refer=http%3A%2F%2Fc-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1685104926&t=62afde36bb3200aa727ced1c8fb18eab',\n", " 'https://img1.baidu.com/it/u=219752959,2207818343&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=725',\n", " 'https://img2.baidu.com/it/u=3813658383,1119780612&fm=253&fmt=auto&app=120&f=JPEG?w=1280&h=800',\n", "]" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/Users/tutu/anaconda3/lib/python3.10/site-packages/transformers/generation/utils.py:1313: UserWarning: Using `max_length`'s default (20) to control the generation length. This behaviour is deprecated and will be removed from the config in v5 of Transformers -- we recommend using `max_new_tokens` to control the maximum length of the generation.\n", " warnings.warn(\n" ] }, { "data": { "text/plain": [ "[Document(page_content='an image of two women in kimono [SEP]', metadata={'image_path': 'https://t7.baidu.com/it/u=2168645659,3174029352&fm=193&f=GIF'}),\n", " Document(page_content='an image of a cat eating fish [SEP]', metadata={'image_path': 'https://t7.baidu.com/it/u=2851687453,2321283050&fm=193&f=GIF'}),\n", " Document(page_content='an image of a cat and dog [SEP]', metadata={'image_path': 'https://t7.baidu.com/it/u=3601447414,1764260638&fm=193&f=GIF'}),\n", " Document(page_content='an image of a girl in a school uniform [SEP]', metadata={'image_path': 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fblog%2F202111%2F17%2F20211117092914_579a7.thumb.1000_0.jpeg&refer=http%3A%2F%2Fc-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1685104926&t=62afde36bb3200aa727ced1c8fb18eab'}),\n", " Document(page_content='an image of a woman in a blue sweater [SEP]', metadata={'image_path': 'https://img1.baidu.com/it/u=219752959,2207818343&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=725'}),\n", " Document(page_content='an image of a woman in a yellow dress [SEP]', metadata={'image_path': 'https://img2.baidu.com/it/u=3813658383,1119780612&fm=253&fmt=auto&app=120&f=JPEG?w=1280&h=800'})]" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "loader = ImageCaptionLoader(path_images=list_image_urls)\n", "list_docs = loader.load()\n", "list_docs" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Using embedded DuckDB without persistence: data will be transient\n" ] } ], "source": [ "from langchain.indexes import VectorstoreIndexCreator\n", "index = VectorstoreIndexCreator().from_loaders([loader])" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "' 我不知道。'" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "query = \"用中文回答,狗是什么品种\"\n", "index.query(query)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "' There are images of a cat and dog, a cat eating fish, two women in kimono, and a woman in a blue sweater.'" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "query = \"What kind of images are there?\"\n", "index.query(query)" ] } ], "metadata": { "kernelspec": { "display_name": "base", "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", "version": "3.10.10" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }