{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# text to image" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2024-04-04T10:19:57.369099Z", "start_time": "2024-04-04T10:19:57.328298Z" } }, "outputs": [], "source": [ "import requests\n", "import json\n", "\n", "# Vincent diagram example\n", "host = \"http://127.0.0.1:8888\"\n", "\n", "def text2img(params: dict) -> dict:\n", " \"\"\"\n", " Vincent picture\n", " \"\"\"\n", " response = requests.post(\n", " url=f\"{host}/v1/generation/text-to-image\",\n", " data=json.dumps(params),\n", " headers={\"Content-Type\": \"application/json\"})\n", " return response.json()\n", "\n", "result =text2img({\n", " \"performance_selection\": \"Lightning\",\n", " \"async_process\": True\n", "})\n", "print(result)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# upscale or vary" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import requests\n", "import json\n", "\n", "\n", "# upscale or vary v1 Interface example\n", "host = \"http://127.0.0.1:8888\"\n", "image = open(\"./imgs/bear.jpg\", \"rb\").read()\n", "\n", "def upscale_vary(image, params: dict) -> dict:\n", " \"\"\"\n", " Upscale or Vary\n", " \"\"\"\n", " response = requests.post(\n", " url=f\"{host}/v1/generation/image-upscale-vary\",\n", " data=params,\n", " files={\"input_image\": image})\n", " return response.json()\n", "\n", "result =upscale_vary(\n", " image=image,\n", " params={\n", " \"uov_method\": \"Vary\",\n", " \"async_process\": True\n", " })\n", "print(json.dumps(result, indent=4, ensure_ascii=False))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import requests\n", "import json\n", "import base64\n", "\n", "\n", "# upscale or vary v2 Interface example\n", "host = \"http://127.0.0.1:8888\"\n", "image = open(\"./imgs/bear.jpg\", \"rb\").read()\n", "\n", "def upscale_vary(params: dict) -> dict:\n", " \"\"\"\n", " Upscale or Vary\n", " \"\"\"\n", " response = requests.post(\n", " url=f\"{host}/v2/generation/image-upscale-vary\",\n", " data=json.dumps(params),\n", " headers={\"Content-Type\": \"application/json\"},\n", " timeout=300)\n", " return response.json()\n", "\n", "result =upscale_vary(\n", " params={\n", " \"input_image\": base64.b64encode(image).decode('utf-8'),\n", " \"uov_method\": \"Upscale (2x)\",\n", " \"async_process\": True\n", " })\n", "print(json.dumps(result, indent=4, ensure_ascii=False))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# inpaint or outpaint" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import requests\n", "import json\n", "\n", "# Partial redraw v1 interface example\n", "host = \"http://127.0.0.1:8888\"\n", "image = open(\"./imgs/bear.jpg\", \"rb\").read()\n", "\n", "def inpaint_outpaint(params: dict, input_image: bytes, input_mask: bytes = None) -> dict:\n", " \"\"\"\n", " Partial redraw v1 interface example\n", " \"\"\"\n", " response = requests.post(\n", " url=f\"{host}/v1/generation/image-inpaint-outpaint\",\n", " data=params,\n", " files={\"input_image\": input_image,\n", " \"input_mask\": input_mask})\n", " return response.json()\n", "\n", "\n", "# Image extension example\n", "result = inpaint_outpaint(\n", " params={\n", " \"outpaint_selections\": \"Left,Right\",\n", " \"async_process\": True},\n", " input_image=image,\n", " input_mask=None)\n", "print(json.dumps(result, indent=4, ensure_ascii=False))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#Partial redraw example\n", "source = open(\"./imgs/inpaint_source.jpg\", \"rb\").read()\n", "mask = open(\"./imgs/inpaint_mask.png\", \"rb\").read()\n", "result = inpaint_outpaint(\n", " params={\n", " \"prompt\": \"a cat\",\n", " \"async_process\": True\n", " },\n", " input_image=source,\n", " input_mask=mask)\n", "print(json.dumps(result, indent=4, ensure_ascii=False))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import requests\n", "import json\n", "import base64\n", "\n", "\n", "# Partial redraw v2 interface example\n", "host = \"http://127.0.0.1:8888\"\n", "image = open(\"./imgs/bear.jpg\", \"rb\").read()\n", "\n", "def inpaint_outpaint(params: dict) -> dict:\n", " \"\"\"\n", " Partial redraw v2 interface example\n", " \"\"\"\n", " response = requests.post(\n", " url=f\"{host}/v2/generation/image-inpaint-outpaint\",\n", " data=json.dumps(params),\n", " headers={\"Content-Type\": \"application/json\"})\n", " return response.json()\n", "\n", "# Image extension example\n", "result = inpaint_outpaint(\n", " params={\n", " \"input_image\": base64.b64encode(image).decode('utf-8'),\n", " \"input_mask\": None,\n", " \"outpaint_selections\": [\"Left\", \"Right\"],\n", " \"async_process\": True\n", " })\n", "print(json.dumps(result, indent=4, ensure_ascii=False))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Partial redraw example\n", "source = open(\"./imgs/inpaint_source.jpg\", \"rb\").read()\n", "mask = open(\"./imgs/inpaint_mask.png\", \"rb\").read()\n", "result = inpaint_outpaint(\n", " params={\n", " \"prompt\": \"a cat\",\n", " \"input_image\": base64.b64encode(source).decode('utf-8'),\n", " \"input_mask\": base64.b64encode(mask).decode('utf-8'),\n", " \"async_process\": True\n", " })\n", "print(json.dumps(result, indent=4, ensure_ascii=False))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# image prompts" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import requests\n", "import json\n", "\n", "\n", "# image_prompt v1 Interface example\n", "host = \"http://127.0.0.1:8888\"\n", "image = open(\"./imgs/bear.jpg\", \"rb\").read()\n", "source = open(\"./imgs/inpaint_source.jpg\", \"rb\").read()\n", "mask = open(\"./imgs/inpaint_mask.png\", \"rb\").read()\n", "\n", "def image_prompt(\n", " params: dict,\n", " input_image: bytes=None,\n", " input_mask: bytes=None,\n", " cn_img1: bytes=None,\n", " cn_img2: bytes=None,\n", " cn_img3: bytes=None,\n", " cn_img4: bytes=None,) -> dict:\n", " \"\"\"\n", " image prompt\n", " \"\"\"\n", " response = requests.post(\n", " url=f\"{host}/v1/generation/image-prompt\",\n", " data=params,\n", " files={\n", " \"input_image\": input_image,\n", " \"input_mask\": input_mask,\n", " \"cn_img1\": cn_img1,\n", " \"cn_img2\": cn_img2,\n", " \"cn_img3\": cn_img3,\n", " \"cn_img4\": cn_img4,\n", " })\n", " return response.json()\n", "\n", "# image extension\n", "params = {\n", " \"outpaint_selections\": [\"Left\", \"Right\"],\n", " \"image_prompts\": [] # Required parameters, can be an empty list\n", "}\n", "result = image_prompt(params=params, input_image=image)\n", "print(json.dumps(result, indent=4, ensure_ascii=False))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# partial redraw\n", "\n", "params = {\n", " \"prompt\": \"1girl sitting on the chair\",\n", " \"image_prompts\": [], # Required parameters, can be an empty list\n", " \"async_process\": True\n", "}\n", "result = image_prompt(params=params, input_image=source, input_mask=mask)\n", "print(json.dumps(result, indent=4, ensure_ascii=False))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# image prompt\n", "\n", "params = {\n", " \"prompt\": \"1girl sitting on the chair\",\n", " \"image_prompts\": [\n", " {\n", " \"cn_stop\": 0.6,\n", " \"cn_weight\": 0.6,\n", " \"cn_type\": \"ImagePrompt\"\n", " },{\n", " \"cn_stop\": 0.6,\n", " \"cn_weight\": 0.6,\n", " \"cn_type\": \"ImagePrompt\"\n", " }]\n", " }\n", "result = image_prompt(params=params, cn_img1=image, cn_img2=source)\n", "print(json.dumps(result, indent=4, ensure_ascii=False))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import requests\n", "import json\n", "import base64\n", "\n", "# image_prompt v2 Interface example\n", "host = \"http://127.0.0.1:8888\"\n", "image = open(\"./imgs/bear.jpg\", \"rb\").read()\n", "source = open(\"./imgs/inpaint_source.jpg\", \"rb\").read()\n", "mask = open(\"./imgs/inpaint_mask.png\", \"rb\").read()\n", "\n", "def image_prompt(params: dict) -> dict:\n", " \"\"\"\n", " image prompt\n", " \"\"\"\n", " response = requests.post(\n", " url=f\"{host}/v2/generation/image-prompt\",\n", " data=json.dumps(params),\n", " headers={\"Content-Type\": \"application/json\"})\n", " return response.json()\n", "\n", "# image extension\n", "params = {\n", " \"input_image\": base64.b64encode(image).decode('utf-8'),\n", " \"outpaint_selections\": [\"Left\", \"Right\"],\n", " \"image_prompts\": [] # Required parameters, can be an empty list\n", "}\n", "result = image_prompt(params)\n", "print(json.dumps(result, indent=4, ensure_ascii=False))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# partial redraw\n", "\n", "params = {\n", " \"prompt\": \"1girl sitting on the chair\",\n", " \"input_image\": base64.b64encode(source).decode('utf-8'),\n", " \"input_mask\": base64.b64encode(mask).decode('utf-8'),\n", " \"image_prompts\": [], # Required parameters, can be an empty list\n", " \"async_process\": True\n", "}\n", "result = image_prompt(params)\n", "print(json.dumps(result, indent=4, ensure_ascii=False))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# image prompt\n", "\n", "params = {\n", " \"prompt\": \"1girl sitting on the chair\",\n", " \"image_prompts\": [\n", " {\n", " \"cn_img\": base64.b64encode(source).decode('utf-8'),\n", " \"cn_stop\": 0.6,\n", " \"cn_weight\": 0.6,\n", " \"cn_type\": \"ImagePrompt\"\n", " },{\n", " \"cn_img\": base64.b64encode(image).decode('utf-8'),\n", " \"cn_stop\": 0.6,\n", " \"cn_weight\": 0.6,\n", " \"cn_type\": \"ImagePrompt\"\n", " }]\n", " }\n", "result = image_prompt(params)\n", "print(json.dumps(result, indent=4, ensure_ascii=False))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " # text to image with image prompt" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import requests\n", "import json\n", "import base64\n", "\n", "# text to image with image prompt Example\n", "host = \"http://127.0.0.1:8888\"\n", "image = open(\"./imgs/image_prompt-1.png\", \"rb\").read()\n", "source = open(\"./imgs/image_prompt-0.jpg\", \"rb\").read()\n", "def image_prompt(params: dict) -> dict:\n", " \"\"\"\n", " image prompt\n", " \"\"\"\n", " response = requests.post(\n", " url=f\"{host}/v2/generation/text-to-image-with-ip\",\n", " data=json.dumps(params),\n", " headers={\"Content-Type\": \"application/json\"})\n", " return response.json()\n", "\n", "params = {\n", " \"prompt\": \"A bear\",\n", " \"image_prompts\": [\n", " {\n", " \"cn_img\": base64.b64encode(source).decode('utf-8'),\n", " \"cn_stop\": 0.6,\n", " \"cn_weight\": 0.6,\n", " \"cn_type\": \"ImagePrompt\"\n", " },{\n", " \"cn_img\": base64.b64encode(image).decode('utf-8'),\n", " \"cn_stop\": 0.6,\n", " \"cn_weight\": 0.6,\n", " \"cn_type\": \"ImagePrompt\"\n", " }\n", " ]\n", "}\n", "result = image_prompt(params)\n", "print(json.dumps(result, indent=4, ensure_ascii=False))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# describe" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import requests\n", "\n", "image = open(\"./imgs/target_face.png\", \"rb\").read()\n", "def describe_image(image: bytes,\n", " params: dict = {\"type\": \"Photo\"}) -> dict:\n", " \"\"\"\n", " describe-image\n", " \"\"\"\n", " response = requests.post(\n", " url=\"http://127.0.0.1:8888/v1/tools/describe-image\",\n", " params=params,\n", " files={\n", " \"image\": image\n", " },\n", " timeout=30)\n", " return response.json()\n", "\n", "describe_image(image=image)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.13" } }, "nbformat": 4, "nbformat_minor": 2 }