From a84c357cde3ef8208e61a4ac49bba6ca4b54da48 Mon Sep 17 00:00:00 2001
From: Giannis <ipanagiv@users.noreply.github.com>
Date: Wed, 15 Mar 2023 09:33:51 +0200
Subject: [PATCH] Created using Colaboratory

---
 Generating_AI_Photos_Of_You.ipynb | 212 ++++++++++++++++++++++++++++++
 1 file changed, 212 insertions(+)
 create mode 100644 Generating_AI_Photos_Of_You.ipynb

diff --git a/Generating_AI_Photos_Of_You.ipynb b/Generating_AI_Photos_Of_You.ipynb
new file mode 100644
index 000000000000..864c8e91f269
--- /dev/null
+++ b/Generating_AI_Photos_Of_You.ipynb
@@ -0,0 +1,212 @@
+{
+  "nbformat": 4,
+  "nbformat_minor": 0,
+  "metadata": {
+    "colab": {
+      "provenance": [],
+      "include_colab_link": true
+    },
+    "kernelspec": {
+      "name": "python3",
+      "display_name": "Python 3"
+    },
+    "language_info": {
+      "name": "python"
+    }
+  },
+  "cells": [
+    {
+      "cell_type": "markdown",
+      "metadata": {
+        "id": "view-in-github",
+        "colab_type": "text"
+      },
+      "source": [
+        "<a href=\"https://colab.research.google.com/github/ipanagiv/Python/blob/master/Generating_AI_Photos_Of_You.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
+      ]
+    },
+    {
+      "cell_type": "code",
+      "execution_count": null,
+      "metadata": {
+        "id": "oh3n9k2M910E"
+      },
+      "outputs": [],
+      "source": [
+        "import requests\n",
+        "import json\n",
+        "import time\n",
+        "\n",
+        "API_KEY = \"YOUR API KEY GOES HERE\"\n",
+        "\n",
+        "HEADERS = {\n",
+        "    \"accept\": \"application/json\",\n",
+        "    \"content-type\": \"application/json\",\n",
+        "    \"authorization\": f\"Bearer {API_KEY}\"\n",
+        "}\n",
+        "\n",
+        "IMAGES = [\n",
+        "    \"https://i.natgeofe.com/k/271050d8-1821-49b8-bf0b-3a4a72b6384a/obama-portrait__3x2.jpg\",\n",
+        "    \"https://d3hjzzsa8cr26l.cloudfront.net/516e6836-d278-11ea-a709-979a0378f022.jpg\",\n",
+        "    \"https://hips.hearstapps.com/hmg-prod/images/gettyimages-1239961811.jpg\"\n",
+        "]"
+      ]
+    },
+    {
+      "cell_type": "code",
+      "source": [
+        "def create_model(title):\n",
+        "    url = \"https://api.tryleap.ai/api/v1/images/models\"\n",
+        "\n",
+        "    payload = {\n",
+        "        \"title\": title,\n",
+        "        \"subjectKeyword\": \"@me\"\n",
+        "    }\n",
+        "\n",
+        "    response = requests.post(url, json=payload, headers=HEADERS)\n",
+        "\n",
+        "    model_id = json.loads(response.text)[\"id\"]\n",
+        "    return model_id\n",
+        "\n",
+        "\n",
+        "def upload_image_samples(model_id):\n",
+        "    url = f\"https://api.tryleap.ai/api/v1/images/models/{model_id}/samples/url\"\n",
+        "\n",
+        "    payload = {\"images\": IMAGES}\n",
+        "    response = requests.post(url, json=payload, headers=HEADERS)\n",
+        "\n",
+        "\n",
+        "def queue_training_job(model_id):\n",
+        "    url = f\"https://api.tryleap.ai/api/v1/images/models/{model_id}/queue\"\n",
+        "    response = requests.post(url, headers=HEADERS)\n",
+        "    data = json.loads(response.text)\n",
+        "\n",
+        "    print(response.text)\n",
+        "\n",
+        "    version_id = data[\"id\"]\n",
+        "    status = data[\"status\"]\n",
+        "\n",
+        "    print(f\"Version ID: {version_id}. Status: {status}\")\n",
+        "\n",
+        "    return version_id, status\n",
+        "\n",
+        "\n",
+        "def get_model_version(model_id, version_id):\n",
+        "    url = f\"https://api.tryleap.ai/api/v1/images/models/{model_id}/versions/{version_id}\"\n",
+        "    response = requests.get(url, headers=HEADERS)\n",
+        "    data = json.loads(response.text)\n",
+        "\n",
+        "    version_id = data[\"id\"]\n",
+        "    status = data[\"status\"]\n",
+        "\n",
+        "    print(f\"Version ID: {version_id}. Status: {status}\")\n",
+        "\n",
+        "    return version_id, status\n",
+        "\n",
+        "\n",
+        "def generate_image(model_id, prompt):\n",
+        "    url = f\"https://api.tryleap.ai/api/v1/images/models/{model_id}/inferences\"\n",
+        "\n",
+        "    payload = {\n",
+        "        \"prompt\": prompt,\n",
+        "        \"steps\": 50,\n",
+        "        \"width\": 512,\n",
+        "        \"height\": 512,\n",
+        "        \"numberOfImages\": 1,\n",
+        "        \"seed\": 4523184\n",
+        "    }\n",
+        "\n",
+        "    response = requests.post(url, json=payload, headers=HEADERS)\n",
+        "    data = json.loads(response.text)\n",
+        "\n",
+        "    inference_id = data[\"id\"]\n",
+        "    status = data[\"status\"]\n",
+        "\n",
+        "    print(f\"Inference ID: {inference_id}. Status: {status}\")\n",
+        "\n",
+        "    return inference_id, status\n",
+        "\n",
+        "\n",
+        "def get_inference_job(model_id, inference_id):\n",
+        "    url = f\"https://api.tryleap.ai/api/v1/images/models/{model_id}/inferences/{inference_id}\"\n",
+        "\n",
+        "    response = requests.get(url, headers=HEADERS)\n",
+        "    data = json.loads(response.text)\n",
+        "\n",
+        "    inference_id = data[\"id\"]\n",
+        "    state = data[\"state\"]\n",
+        "    image = None\n",
+        "\n",
+        "    if len(data[\"images\"]):\n",
+        "        image = data[\"images\"][0][\"uri\"]\n",
+        "\n",
+        "    print(f\"Inference ID: {inference_id}. State: {state}\")\n",
+        "\n",
+        "    return inference_id, state, image\n",
+        "\n",
+        "\n"
+      ],
+      "metadata": {
+        "id": "seoBu0hK_ijD"
+      },
+      "execution_count": null,
+      "outputs": []
+    },
+    {
+      "cell_type": "code",
+      "source": [
+        "# Let's create a custom model so we can fine tune it.\n",
+        "model_id = create_model(\"Sample\")\n",
+        "\n",
+        "# We now upload the images to fine tune this model.\n",
+        "upload_image_samples(model_id)\n",
+        "\n",
+        "# Now it's time to fine tune the model. Notice how I'm continuously \n",
+        "# getting the status of the training job and waiting until it's\n",
+        "# finished before moving on.\n",
+        "version_id, status = queue_training_job(model_id)\n",
+        "while status != \"finished\":\n",
+        "    time.sleep(10)\n",
+        "    version_id, status = get_model_version(model_id, version_id)\n",
+        "\n"
+      ],
+      "metadata": {
+        "id": "qhmrCli2BwoK"
+      },
+      "execution_count": null,
+      "outputs": []
+    },
+    {
+      "cell_type": "code",
+      "source": [
+        "# Now that we have a fine-tuned version of a model, we can\n",
+        "# generate images using it. Notice how I'm using '@me' to \n",
+        "# indicate I want pictures similar to the ones we upload to \n",
+        "# fine tune this model.\n",
+        "inference_id, status = generate_image(\n",
+        "    model_id, \n",
+        "    prompt=\"A photo of @me with a tall black hat and sunglasses\"\n",
+        ")\n",
+        "while status != \"finished\":\n",
+        "    time.sleep(10)\n",
+        "    inference_id, status, image = get_inference_job(model_id, inference_id)\n",
+        "\n",
+        "print(image)"
+      ],
+      "metadata": {
+        "id": "_pXpe_iiCkqq"
+      },
+      "execution_count": null,
+      "outputs": []
+    },
+    {
+      "cell_type": "code",
+      "source": [],
+      "metadata": {
+        "id": "nJPkQfJhRih6"
+      },
+      "execution_count": null,
+      "outputs": []
+    }
+  ]
+}
\ No newline at end of file
