Skip to content

Commit

Permalink
feat: add imagineapi.dev templates
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorentLvr committed Nov 1, 2023
1 parent e1e4edb commit 3d81810
Show file tree
Hide file tree
Showing 3 changed files with 679 additions and 31 deletions.
146 changes: 115 additions & 31 deletions ImagineAPI.dev/ImagineAPI.dev_Create_Image.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"tags": []
},
"source": [
"# ImagineAPI.dev - Create Image"
"# ImagineAPI.dev - Generate and Display Image"
]
},
{
Expand All @@ -30,7 +30,7 @@
"tags": []
},
"source": [
"**Tags:** #imagineapi #image #create #dev #api #python"
"**Tags:** #imagineapi #image #generate #display #api #python #midjourney"
]
},
{
Expand Down Expand Up @@ -63,7 +63,7 @@
"tags": []
},
"source": [
"**Description:** This notebook will show how to create an image using ImagineAPI.dev. It is usefull for organizations that need to quickly create images for their website or marketing campaigns."
"**Description:** This notebook will show how to create an image from a prompt using ImagineAPI.dev."
]
},
{
Expand All @@ -74,7 +74,8 @@
"tags": []
},
"source": [
"**References:**\n- [ImagineAPI.dev Documentation](https://docs.imagineapi.dev/aypeeeye/image)\n- [ImagineAPI.dev API Reference](https://docs.imagineapi.dev/reference)"
"**References:**\n",
"- [ImagineAPI.dev Documentation](https://docs.imagineapi.dev/aypeeeye/image)"
]
},
{
Expand Down Expand Up @@ -107,8 +108,14 @@
"papermill": {},
"tags": []
},
"source": "import requests",
"outputs": []
"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",
Expand All @@ -118,7 +125,10 @@
"tags": []
},
"source": [
"### Setup variables\n- `api_key`: API key provided by ImagineAPI.dev. [Get your API key](https://docs.imagineapi.dev/getting-started/authentication)"
"### 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"
]
},
{
Expand All @@ -129,52 +139,121 @@
"papermill": {},
"tags": []
},
"source": "api_key = \"YOUR_API_KEY\"",
"outputs": []
},
{
"cell_type": "markdown",
"id": "09580a7d-e9ed-4e87-b71b-1919b8a615de",
"metadata": {
"papermill": {},
"tags": []
},
"outputs": [],
"source": [
"## Model"
"api_key = naas.secret.get(\"IMAGINEAPI_API_KEY\")\n",
"imagineapi_url = \"https://cl-151.imagineapi.dev/admin/login\"\n",
"prompt = \"Barack Obama funny profile picture\""
]
},
{
"cell_type": "markdown",
"id": "e25008dd-fcba-4fb9-9b8c-ee7bea73e616",
"id": "09580a7d-e9ed-4e87-b71b-1919b8a615de",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"### Create image"
"## Model"
]
},
{
"cell_type": "markdown",
"id": "d825b2df-adbf-4dd1-89ea-ae1fc272a79e",
"id": "cc743139-0145-4861-8b13-97c60c2a4ff6",
"metadata": {
"papermill": {},
"tags": []
"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": [
"This function will create an image using ImagineAPI.dev."
"### Create Image"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c46e47dd-dac1-4e54-9256-60701f4eec69",
"id": "f1bcb812-329a-4b8f-a7bf-8fedb614a888",
"metadata": {
"papermill": {},
"tags": []
},
"source": "url = \"https://api.imagineapi.dev/v1/image\"\nheaders = {\"Authorization\": \"Bearer \" + api_key}\ndata = {\"width\": 500, \"height\": 500, \"background\": \"#FF0000\"}\nresponse = requests.post(url, headers=headers, data=data)",
"outputs": []
"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",
Expand Down Expand Up @@ -206,8 +285,13 @@
"papermill": {},
"tags": []
},
"source": "print(response.json())",
"outputs": []
"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",
Expand Down Expand Up @@ -249,4 +333,4 @@
},
"nbformat": 4,
"nbformat_minor": 5
}
}
Loading

0 comments on commit 3d81810

Please sign in to comment.