diff --git a/aihub/deep-learning/dalle-flow/demo.ipynb b/aihub/deep-learning/dalle-flow/demo.ipynb new file mode 100644 index 00000000..c910009b --- /dev/null +++ b/aihub/deep-learning/dalle-flow/demo.ipynb @@ -0,0 +1,237 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 7, + "id": "74b1a73c-e487-4f3a-9c92-5c0ebf9c1426", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Looking in indexes: https://mirrors.tencent.com/pypi/simple/, https://mirrors.tencent.com/repository/pypi/tencent_pypi/simple\n", + "Requirement already satisfied: docarray[common]>=0.13.5 in /usr/local/lib/python3.8/dist-packages (0.16.5)\n", + "Requirement already satisfied: jina in /usr/local/lib/python3.8/dist-packages (3.9.2)\n", + "Requirement already satisfied: rich>=12.0.0 in /usr/local/lib/python3.8/dist-packages (from docarray[common]>=0.13.5) (12.5.1)\n", + "Requirement already satisfied: numpy in /usr/local/lib/python3.8/dist-packages (from docarray[common]>=0.13.5) (1.22.1)\n", + "Requirement already satisfied: requests in /usr/local/lib/python3.8/dist-packages (from docarray[common]>=0.13.5) (2.27.1)\n", + "Requirement already satisfied: jina-hubble-sdk>=0.11.0 in /usr/local/lib/python3.8/dist-packages (from docarray[common]>=0.13.5) (0.17.1)\n", + "Requirement already satisfied: protobuf>=3.13.0 in /usr/local/lib/python3.8/dist-packages (from docarray[common]>=0.13.5) (4.21.6)\n", + "Requirement already satisfied: lz4 in /usr/local/lib/python3.8/dist-packages (from docarray[common]>=0.13.5) (4.0.2)\n", + "Requirement already satisfied: matplotlib in /usr/local/lib/python3.8/dist-packages (from docarray[common]>=0.13.5) (3.6.0)\n", + "Requirement already satisfied: Pillow in /usr/local/lib/python3.8/dist-packages (from docarray[common]>=0.13.5) (9.1.0)\n", + "Requirement already satisfied: fastapi in /usr/local/lib/python3.8/dist-packages (from docarray[common]>=0.13.5) (0.85.0)\n", + "Requirement already satisfied: uvicorn in /usr/local/lib/python3.8/dist-packages (from docarray[common]>=0.13.5) (0.18.3)\n", + "Requirement already satisfied: aiofiles in /usr/local/lib/python3.8/dist-packages (from jina) (22.1.0)\n", + "Requirement already satisfied: pyyaml>=5.3.1 in /usr/local/lib/python3.8/dist-packages (from jina) (5.4.1)\n", + "Requirement already satisfied: uvloop in /usr/local/lib/python3.8/dist-packages (from jina) (0.16.0)\n", + "Requirement already satisfied: python-multipart in /usr/local/lib/python3.8/dist-packages (from jina) (0.0.5)\n", + "Requirement already satisfied: aiostream in /usr/local/lib/python3.8/dist-packages (from jina) (0.4.5)\n", + "Requirement already satisfied: grpcio<1.48.1,>=1.46.0 in /usr/local/lib/python3.8/dist-packages (from jina) (1.47.0)\n", + "Requirement already satisfied: cryptography in /usr/local/lib/python3.8/dist-packages (from jina) (3.4.8)\n", + "Requirement already satisfied: pathspec in /usr/local/lib/python3.8/dist-packages (from jina) (0.9.0)\n", + "Requirement already satisfied: filelock in /usr/local/lib/python3.8/dist-packages (from jina) (3.8.0)\n", + "Requirement already satisfied: packaging>=20.0 in /usr/local/lib/python3.8/dist-packages (from jina) (21.3)\n", + "Requirement already satisfied: aiohttp in /usr/local/lib/python3.8/dist-packages (from jina) (3.8.1)\n", + "Requirement already satisfied: prometheus-client in /usr/local/lib/python3.8/dist-packages (from jina) (0.8.0)\n", + "Requirement already satisfied: pydantic in /usr/local/lib/python3.8/dist-packages (from jina) (1.9.2)\n", + "Requirement already satisfied: jcloud>=0.0.35 in /usr/local/lib/python3.8/dist-packages (from jina) (0.0.35)\n", + "Requirement already satisfied: grpcio-reflection<1.48.1,>=1.46.0 in /usr/local/lib/python3.8/dist-packages (from jina) (1.47.0)\n", + "Requirement already satisfied: docker in /usr/local/lib/python3.8/dist-packages (from jina) (6.0.0)\n", + "Requirement already satisfied: websockets in /usr/local/lib/python3.8/dist-packages (from jina) (10.1)\n", + "Requirement already satisfied: grpcio-health-checking<1.48.1,>=1.46.0 in /usr/local/lib/python3.8/dist-packages (from jina) (1.47.0)\n", + "Requirement already satisfied: starlette==0.20.4 in /usr/local/lib/python3.8/dist-packages (from fastapi->docarray[common]>=0.13.5) (0.20.4)\n", + "Requirement already satisfied: typing-extensions>=3.10.0 in /usr/local/lib/python3.8/dist-packages (from starlette==0.20.4->fastapi->docarray[common]>=0.13.5) (4.0.1)\n", + "Requirement already satisfied: anyio<5,>=3.4.0 in /usr/local/lib/python3.8/dist-packages (from starlette==0.20.4->fastapi->docarray[common]>=0.13.5) (3.5.0)\n", + "Requirement already satisfied: six>=1.5.2 in /usr/local/lib/python3.8/dist-packages (from grpcio<1.48.1,>=1.46.0->jina) (1.16.0)\n", + "Requirement already satisfied: python-dotenv in /usr/local/lib/python3.8/dist-packages (from jcloud>=0.0.35->jina) (0.21.0)\n", + "Requirement already satisfied: multidict<7.0,>=4.5 in /usr/local/lib/python3.8/dist-packages (from aiohttp->jina) (5.2.0)\n", + "Requirement already satisfied: aiosignal>=1.1.2 in /usr/local/lib/python3.8/dist-packages (from aiohttp->jina) (1.2.0)\n", + "Requirement already satisfied: yarl<2.0,>=1.0 in /usr/local/lib/python3.8/dist-packages (from aiohttp->jina) (1.7.2)\n", + "Requirement already satisfied: attrs>=17.3.0 in /usr/local/lib/python3.8/dist-packages (from aiohttp->jina) (21.4.0)\n", + "Requirement already satisfied: charset-normalizer<3.0,>=2.0 in /usr/local/lib/python3.8/dist-packages (from aiohttp->jina) (2.0.10)\n", + "Requirement already satisfied: frozenlist>=1.1.1 in /usr/local/lib/python3.8/dist-packages (from aiohttp->jina) (1.3.0)\n", + "Requirement already satisfied: async-timeout<5.0,>=4.0.0a3 in /usr/local/lib/python3.8/dist-packages (from aiohttp->jina) (4.0.2)\n", + "Requirement already satisfied: importlib-metadata in /usr/local/lib/python3.8/dist-packages (from jina-hubble-sdk>=0.11.0->docarray[common]>=0.13.5) (4.12.0)\n", + "Requirement already satisfied: pyparsing!=3.0.5,>=2.0.2 in /usr/local/lib/python3.8/dist-packages (from packaging>=20.0->jina) (3.0.6)\n", + "Requirement already satisfied: pygments<3.0.0,>=2.6.0 in /usr/local/lib/python3.8/dist-packages (from rich>=12.0.0->docarray[common]>=0.13.5) (2.11.2)\n", + "Requirement already satisfied: commonmark<0.10.0,>=0.9.0 in /usr/local/lib/python3.8/dist-packages (from rich>=12.0.0->docarray[common]>=0.13.5) (0.9.1)\n", + "Requirement already satisfied: cffi>=1.12 in /usr/local/lib/python3.8/dist-packages (from cryptography->jina) (1.15.0)\n", + "Requirement already satisfied: urllib3>=1.26.0 in /usr/local/lib/python3.8/dist-packages (from docker->jina) (1.26.12)\n", + "Requirement already satisfied: websocket-client>=0.32.0 in /usr/local/lib/python3.8/dist-packages (from docker->jina) (1.2.3)\n", + "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.8/dist-packages (from requests->docarray[common]>=0.13.5) (2021.10.8)\n", + "Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.8/dist-packages (from requests->docarray[common]>=0.13.5) (3.3)\n", + "Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.8/dist-packages (from matplotlib->docarray[common]>=0.13.5) (0.11.0)\n", + "Requirement already satisfied: python-dateutil>=2.7 in /usr/local/lib/python3.8/dist-packages (from matplotlib->docarray[common]>=0.13.5) (2.8.2)\n", + "Requirement already satisfied: contourpy>=1.0.1 in /usr/local/lib/python3.8/dist-packages (from matplotlib->docarray[common]>=0.13.5) (1.0.5)\n", + "Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.8/dist-packages (from matplotlib->docarray[common]>=0.13.5) (1.4.4)\n", + "Requirement already satisfied: fonttools>=4.22.0 in /usr/local/lib/python3.8/dist-packages (from matplotlib->docarray[common]>=0.13.5) (4.37.2)\n", + "Requirement already satisfied: h11>=0.8 in /usr/local/lib/python3.8/dist-packages (from uvicorn->docarray[common]>=0.13.5) (0.13.0)\n", + "Requirement already satisfied: click>=7.0 in /usr/local/lib/python3.8/dist-packages (from uvicorn->docarray[common]>=0.13.5) (8.0.3)\n", + "Requirement already satisfied: watchfiles>=0.13 in /usr/local/lib/python3.8/dist-packages (from uvicorn->docarray[common]>=0.13.5) (0.17.0)\n", + "Requirement already satisfied: httptools>=0.4.0 in /usr/local/lib/python3.8/dist-packages (from uvicorn->docarray[common]>=0.13.5) (0.5.0)\n", + "Requirement already satisfied: pycparser in /usr/local/lib/python3.8/dist-packages (from cffi>=1.12->cryptography->jina) (2.21)\n", + "Requirement already satisfied: zipp>=0.5 in /usr/local/lib/python3.8/dist-packages (from importlib-metadata->jina-hubble-sdk>=0.11.0->docarray[common]>=0.13.5) (3.8.1)\n", + "Requirement already satisfied: sniffio>=1.1 in /usr/local/lib/python3.8/dist-packages (from anyio<5,>=3.4.0->starlette==0.20.4->fastapi->docarray[common]>=0.13.5) (1.2.0)\n", + "\u001b[33mWARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv\u001b[0m\n", + "\u001b[33mWARNING: You are using pip version 21.3.1; however, version 22.2.2 is available.\n", + "You should consider upgrading via the '/usr/bin/python -m pip install --upgrade pip' command.\u001b[0m\n" + ] + } + ], + "source": [ + "!pip install \"docarray[common]>=0.13.5\" jina" + ] + }, + { + "cell_type": "markdown", + "id": "e57713e5-c6ed-4dc9-959c-f812ae5d686b", + "metadata": {}, + "source": [ + "server_url是官方提供的服务,我们也可以按照文档,将模型部署到自己的服务器(需要有GPU)。" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "bd1ab908-d358-417f-a7d5-6a3b5bc7577b", + "metadata": {}, + "outputs": [], + "source": [ + "# !docker run -p 51005:51005 -it -v $HOME/.cache:/home/dalle/.cache --gpus all jinaai/dalle-flow" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "9a0b8fec-bef1-46f8-8bd7-6ea229b1fe76", + "metadata": {}, + "outputs": [], + "source": [ + "server_url = 'grpc://dalle-flow.jina.ai:51005'" + ] + }, + { + "cell_type": "markdown", + "id": "3de80bcb-33b2-45be-add3-b29c6d8add8e", + "metadata": {}, + "source": [ + "将文本提交到服务器,获得候选图片。" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "332f199f-498d-4dfa-871a-2b5e4732de27", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n" + ], + "text/plain": [ + "\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "
ERROR GRPCClient@1981 gRPC error: StatusCode.UNAVAILABLE failed to connect to all addresses [09/21/22 21:52:07]\n", + " The ongoing request is terminated as the server is not available or closed already. \n", + "\n" + ], + "text/plain": [ + "\u001b[1;31mERROR \u001b[0m GRPCClient@\u001b[1;36m1981\u001b[0m gRPC error: StatusCode.UNAVAILABLE failed to connect to all addresses \u001b[2m[09/21/22 21:52:07]\u001b[0m\n", + " The ongoing request is terminated as the server is not available or closed already. \u001b[2m \u001b[0m\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Exception in thread Thread-17:\n", + "Traceback (most recent call last):\n", + " File \"/usr/lib/python3.8/threading.py\", line 932, in _bootstrap_inner\n", + " self.run()\n", + " File \"/usr/local/lib/python3.8/dist-packages/jina/helper.py\", line 1299, in run\n", + " self.result = asyncio.run(func(*args, **kwargs))\n", + " File \"/usr/lib/python3.8/asyncio/runners.py\", line 44, in run\n", + " return loop.run_until_complete(main)\n", + " File \"/usr/lib/python3.8/asyncio/base_events.py\", line 616, in run_until_complete\n", + " return future.result()\n", + " File \"/usr/local/lib/python3.8/dist-packages/jina/clients/mixin.py\", line 256, in _get_results\n", + " async for resp in c._get_results(*args, **kwargs):\n", + " File \"/usr/local/lib/python3.8/dist-packages/jina/clients/base/grpc.py\", line 119, in _get_results\n", + " raise ConnectionError(my_details) from None\n", + "ConnectionError: failed to connect to all addresses\n" + ] + }, + { + "ename": "BadClient", + "evalue": "something wrong when running the eventloop, result can not be retrieved", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", + "File \u001b[0;32m/usr/local/lib/python3.8/dist-packages/jina/helper.py:1314\u001b[0m, in \u001b[0;36mrun_async\u001b[0;34m(func, *args, **kwargs)\u001b[0m\n\u001b[1;32m 1313\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m-> 1314\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mthread\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mresult\u001b[49m\n\u001b[1;32m 1315\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mAttributeError\u001b[39;00m:\n", + "\u001b[0;31mAttributeError\u001b[0m: '_RunThread' object has no attribute 'result'", + "\nDuring handling of the above exception, another exception occurred:\n", + "\u001b[0;31mBadClient\u001b[0m Traceback (most recent call last)", + "Input \u001b[0;32mIn [13]\u001b[0m, in \u001b[0;36m