-
Notifications
You must be signed in to change notification settings - Fork 467
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2338 from jupyter-naas/2337-imagineapi.dev-create…
…-image feat: ImagineAPI.dev - Create Image
- Loading branch information
Showing
3 changed files
with
900 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,336 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"id": "b6b1a64d-4df1-4b28-bd7a-0ada6403afe7", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"<img width=\"10%\" alt=\"Naas\" src=\"https://landen.imgix.net/jtci2pxwjczr/assets/5ice39g4.png?w=160\"/>" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "df00530f-7eee-41a9-aac0-f28882245df6", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"# ImagineAPI.dev - Generate and Display Image" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "ba19db57-07b2-4bfd-91db-e5d667a0eb42", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"**Tags:** #imagineapi #image #generate #display #api #python #midjourney" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "bcc06ae9-c9a1-452b-87ea-ca7f60d7d663", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"**Author:** [Florent Ravenel](https://www.linkedin.com/in/florent-ravenel/)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "916fe723-d780-4dbc-93ad-d66c79db17af", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"**Last update:** 2023-10-31 (Created: 2023-10-31)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "55baec05-74fa-46eb-86ef-500355591afc", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"**Description:** This notebook will show how to create an image from a prompt using ImagineAPI.dev." | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "6b16e0cd-51f9-42c3-9de1-ec2d1f3dfe26", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"**References:**\n", | ||
"- [ImagineAPI.dev Documentation](https://docs.imagineapi.dev/aypeeeye/image)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "b9519186-d516-4355-bc5b-0362fa63d3d8", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"## Input" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "02be5c3e-28c0-4172-b1e1-1d22f648370e", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"### Import libraries" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "00ff9910-da51-4a76-bf69-834f59f327e8", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"import naas\n", | ||
"import requests\n", | ||
"import pydash as py\n", | ||
"import time\n", | ||
"from IPython.display import Image, display" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "0256d3c2-bcbd-4524-a1fb-858ceb06b7a3", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"### Setup variables\n", | ||
"- `api_key`: API key provided by ImagineAPI.dev. [Get your API key](https://docs.imagineapi.dev/aypeeeye/authentication)\n", | ||
"- `imagineapi_url`: Your ImagineAPI URL\n", | ||
"- `prompt`: Prompt to generate image" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "d33361d2-c441-404c-86ff-48a161f21239", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"api_key = naas.secret.get(\"IMAGINEAPI_API_KEY\")\n", | ||
"imagineapi_url = \"https://xxxxx.imagineapi.dev/admin/login\"\n", | ||
"prompt = \"Barack Obama funny profile picture\"" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "09580a7d-e9ed-4e87-b71b-1919b8a615de", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"## Model" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "cc743139-0145-4861-8b13-97c60c2a4ff6", | ||
"metadata": { | ||
"execution": { | ||
"iopub.execute_input": "2023-11-01T15:33:40.491438Z", | ||
"iopub.status.busy": "2023-11-01T15:33:40.491134Z", | ||
"iopub.status.idle": "2023-11-01T15:33:40.494624Z", | ||
"shell.execute_reply": "2023-11-01T15:33:40.493994Z", | ||
"shell.execute_reply.started": "2023-11-01T15:33:40.491405Z" | ||
} | ||
}, | ||
"source": [ | ||
"### Create Image" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "f1bcb812-329a-4b8f-a7bf-8fedb614a888", | ||
"metadata": { | ||
"tags": [] | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"def create_image(\n", | ||
" api_key,\n", | ||
" base_url,\n", | ||
" prompt\n", | ||
"):\n", | ||
" data = {\n", | ||
" \"prompt\": prompt,\n", | ||
" }\n", | ||
" headers = {\n", | ||
" 'Authorization': f'Bearer {api_key}',\n", | ||
" 'Content-Type': 'application/json'\n", | ||
" }\n", | ||
" domain = base_url.split(\"https://\")[1].split(\"/\")[0]\n", | ||
" url = f\"https://{domain}/items/images/\"\n", | ||
" res = requests.post(url, json=data, headers=headers)\n", | ||
" res.raise_for_status\n", | ||
" return py.get(res.json(), \"data.id\")\n", | ||
"\n", | ||
"def get_image(\n", | ||
" api_key,\n", | ||
" base_url,\n", | ||
" image_id\n", | ||
"):\n", | ||
" headers = {\n", | ||
" 'Authorization': f'Bearer {api_key}',\n", | ||
" 'Content-Type': 'application/json'\n", | ||
" }\n", | ||
" domain = base_url.split(\"https://\")[1].split(\"/\")[0]\n", | ||
" url = f\"https://{domain}/items/images/{image_id}\"\n", | ||
" res = requests.get(url, headers=headers)\n", | ||
" res.raise_for_status\n", | ||
" return res.json()\n", | ||
"\n", | ||
"def generate_image(\n", | ||
" api_key,\n", | ||
" base_url,\n", | ||
" prompt\n", | ||
"):\n", | ||
" # Init\n", | ||
" data = {}\n", | ||
" \n", | ||
" # Create Image\n", | ||
" image_id = create_image(\n", | ||
" api_key,\n", | ||
" base_url,\n", | ||
" prompt\n", | ||
" )\n", | ||
" \n", | ||
" while True:\n", | ||
" # Check status\n", | ||
" data = get_image(\n", | ||
" api_key,\n", | ||
" base_url,\n", | ||
" image_id\n", | ||
" )\n", | ||
" status = py.get(data, \"data.status\")\n", | ||
" progress = py.get(data, \"data.progress\")\n", | ||
" if status == \"pending\":\n", | ||
" progress = 0\n", | ||
" if status == \"completed\":\n", | ||
" progress = 100\n", | ||
" print(f\"- Status: {status}\")\n", | ||
" print(f\"- Progress: {progress}%\")\n", | ||
" print()\n", | ||
" if status in [\"completed\", \"failed\"]:\n", | ||
" break\n", | ||
" time.sleep(5)\n", | ||
" return data\n", | ||
" \n", | ||
"image_data = generate_image(api_key, imagineapi_url, prompt)\n", | ||
"image_data" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "bbcd25df-07f5-4331-be7b-056548847a12", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"## Output" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "4b349e9b-7293-406d-9501-6143f92f161b", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"### Display result" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "db5adf3d-6742-4739-9d3c-e1b17d830e8b", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"urls = py.get(image_data, \"data.upscaled_urls\")\n", | ||
"for url in urls:\n", | ||
" print(\"URL:\", url)\n", | ||
" display(Image(url=url, width=200, height=200))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "437177de-32ac-43f3-ae18-b878b2d70ae8", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
" " | ||
] | ||
} | ||
], | ||
"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.9.6" | ||
}, | ||
"widgets": { | ||
"application/vnd.jupyter.widget-state+json": { | ||
"state": {}, | ||
"version_major": 2, | ||
"version_minor": 0 | ||
} | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |
Oops, something went wrong.