{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [], "gpuType": "T4" }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" }, "accelerator": "GPU", "widgets": { "application/vnd.jupyter.widget-state+json": { "d73ad5cc5b1c45e5bac35da4b105d7d4": { "model_module": "@jupyter-widgets/controls", "model_name": "HBoxModel", "model_module_version": "1.5.0", "state": { "_dom_classes": [], "_model_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "_model_name": "HBoxModel", "_view_count": null, "_view_module": "@jupyter-widgets/controls", "_view_module_version": "1.5.0", "_view_name": "HBoxView", "box_style": "", "children": [ "IPY_MODEL_c4ce5cafe6e84a6cb8a3e9a043c67913", "IPY_MODEL_d0e4bf336ad241958372f92c3fc6cd48", "IPY_MODEL_4f101488783446bfa337e6d4809f9ca5" ], "layout": "IPY_MODEL_a664851d48084d5a8bed4efb15e3eecf" } }, "c4ce5cafe6e84a6cb8a3e9a043c67913": { "model_module": "@jupyter-widgets/controls", "model_name": "HTMLModel", "model_module_version": "1.5.0", "state": { "_dom_classes": [], "_model_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "_model_name": "HTMLModel", "_view_count": null, "_view_module": "@jupyter-widgets/controls", "_view_module_version": "1.5.0", "_view_name": "HTMLView", "description": "", "description_tooltip": null, "layout": "IPY_MODEL_0c8774c074194255961305239d428887", "placeholder": "​", "style": "IPY_MODEL_2d71384e54a94332ae4c21f48fe404d1", "value": "100%" } }, "d0e4bf336ad241958372f92c3fc6cd48": { "model_module": "@jupyter-widgets/controls", "model_name": "FloatProgressModel", "model_module_version": "1.5.0", "state": { "_dom_classes": [], "_model_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "_model_name": "FloatProgressModel", "_view_count": null, "_view_module": "@jupyter-widgets/controls", "_view_module_version": "1.5.0", "_view_name": "ProgressView", "bar_style": "success", "description": "", "description_tooltip": null, "layout": "IPY_MODEL_a30a7bd69266403aa5774d3045087447", "max": 49, "min": 0, "orientation": "horizontal", "style": "IPY_MODEL_7edfc44057194cdb8880a1544cf13d3e", "value": 49 } }, "4f101488783446bfa337e6d4809f9ca5": { "model_module": "@jupyter-widgets/controls", "model_name": "HTMLModel", "model_module_version": "1.5.0", "state": { "_dom_classes": [], "_model_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "_model_name": "HTMLModel", "_view_count": null, "_view_module": "@jupyter-widgets/controls", "_view_module_version": "1.5.0", "_view_name": "HTMLView", "description": "", "description_tooltip": null, "layout": "IPY_MODEL_d332c661fc54456c9e007a6257c08427", "placeholder": "​", "style": "IPY_MODEL_9302974855bc4f25bc4e5239e87fbfc3", "value": " 49/49 [00:37<00:00,  1.23it/s]" } }, "a664851d48084d5a8bed4efb15e3eecf": { "model_module": "@jupyter-widgets/base", "model_name": "LayoutModel", "model_module_version": "1.2.0", "state": { "_model_module": "@jupyter-widgets/base", "_model_module_version": "1.2.0", "_model_name": "LayoutModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "LayoutView", "align_content": null, "align_items": null, "align_self": null, "border": null, "bottom": null, "display": null, "flex": null, "flex_flow": null, "grid_area": null, "grid_auto_columns": null, "grid_auto_flow": null, "grid_auto_rows": null, "grid_column": null, "grid_gap": null, "grid_row": null, "grid_template_areas": null, "grid_template_columns": null, "grid_template_rows": null, "height": null, "justify_content": null, "justify_items": null, "left": null, "margin": null, "max_height": null, "max_width": null, "min_height": null, "min_width": null, "object_fit": null, "object_position": null, "order": null, "overflow": null, "overflow_x": null, "overflow_y": null, "padding": null, "right": null, "top": null, "visibility": null, "width": null } }, "0c8774c074194255961305239d428887": { "model_module": "@jupyter-widgets/base", "model_name": "LayoutModel", "model_module_version": "1.2.0", "state": { "_model_module": "@jupyter-widgets/base", "_model_module_version": "1.2.0", "_model_name": "LayoutModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "LayoutView", "align_content": null, "align_items": null, "align_self": null, "border": null, "bottom": null, "display": null, "flex": null, "flex_flow": null, "grid_area": null, "grid_auto_columns": null, "grid_auto_flow": null, "grid_auto_rows": null, "grid_column": null, "grid_gap": null, "grid_row": null, "grid_template_areas": null, "grid_template_columns": null, "grid_template_rows": null, "height": null, "justify_content": null, "justify_items": null, "left": null, "margin": null, "max_height": null, "max_width": null, "min_height": null, "min_width": null, "object_fit": null, "object_position": null, "order": null, "overflow": null, "overflow_x": null, "overflow_y": null, "padding": null, "right": null, "top": null, "visibility": null, "width": null } }, "2d71384e54a94332ae4c21f48fe404d1": { "model_module": "@jupyter-widgets/controls", "model_name": "DescriptionStyleModel", "model_module_version": "1.5.0", "state": { "_model_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "_model_name": "DescriptionStyleModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "StyleView", "description_width": "" } }, "a30a7bd69266403aa5774d3045087447": { "model_module": "@jupyter-widgets/base", "model_name": "LayoutModel", "model_module_version": "1.2.0", "state": { "_model_module": "@jupyter-widgets/base", "_model_module_version": "1.2.0", "_model_name": "LayoutModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "LayoutView", "align_content": null, "align_items": null, "align_self": null, "border": null, "bottom": null, "display": null, "flex": null, "flex_flow": null, "grid_area": null, "grid_auto_columns": null, "grid_auto_flow": null, "grid_auto_rows": null, "grid_column": null, "grid_gap": null, "grid_row": null, "grid_template_areas": null, "grid_template_columns": null, "grid_template_rows": null, "height": null, "justify_content": null, "justify_items": null, "left": null, "margin": null, "max_height": null, "max_width": null, "min_height": null, "min_width": null, "object_fit": null, "object_position": null, "order": null, "overflow": null, "overflow_x": null, "overflow_y": null, "padding": null, "right": null, "top": null, "visibility": null, "width": null } }, "7edfc44057194cdb8880a1544cf13d3e": { "model_module": "@jupyter-widgets/controls", "model_name": "ProgressStyleModel", "model_module_version": "1.5.0", "state": { "_model_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "_model_name": "ProgressStyleModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "StyleView", "bar_color": null, "description_width": "" } }, "d332c661fc54456c9e007a6257c08427": { "model_module": "@jupyter-widgets/base", "model_name": "LayoutModel", "model_module_version": "1.2.0", "state": { "_model_module": "@jupyter-widgets/base", "_model_module_version": "1.2.0", "_model_name": "LayoutModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "LayoutView", "align_content": null, "align_items": null, "align_self": null, "border": null, "bottom": null, "display": null, "flex": null, "flex_flow": null, "grid_area": null, "grid_auto_columns": null, "grid_auto_flow": null, "grid_auto_rows": null, "grid_column": null, "grid_gap": null, "grid_row": null, "grid_template_areas": null, "grid_template_columns": null, "grid_template_rows": null, "height": null, "justify_content": null, "justify_items": null, "left": null, "margin": null, "max_height": null, "max_width": null, "min_height": null, "min_width": null, "object_fit": null, "object_position": null, "order": null, "overflow": null, "overflow_x": null, "overflow_y": null, "padding": null, "right": null, "top": null, "visibility": null, "width": null } }, "9302974855bc4f25bc4e5239e87fbfc3": { "model_module": "@jupyter-widgets/controls", "model_name": "DescriptionStyleModel", "model_module_version": "1.5.0", "state": { "_model_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "_model_name": "DescriptionStyleModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "StyleView", "description_width": "" } } } } }, "cells": [ { "cell_type": "code", "source": [ "!pip install gradio" ], "metadata": { "id": "sFIrvgM6Zz9i" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "k5XlAek1Zwpz" }, "outputs": [], "source": [ "import torch\n", "from diffusers import AutoPipelineForInpainting\n", "from PIL import Image\n", "\n", "# Check for GPU availability\n", "device = \"cuda\" if torch.cuda.is_available() else \"cpu\"\n", "\n", "# Load the inpainting pipeline\n", "pipe = AutoPipelineForInpainting.from_pretrained(\n", " \"diffusers/stable-diffusion-xl-1.0-inpainting-0.1\",\n", " torch_dtype=torch.float16, # Use float16 for efficiency\n", " variant=\"fp16\"\n", ").to(device)\n" ] }, { "cell_type": "code", "source": [ "# Load input images\n", "background_image = Image.open(\"woman.png\").convert(\"RGB\") # Ensure RGB format\n", "mask_image = Image.open(\"mask.png\").convert(\"L\") # Ensure grayscale format\n", "# Resize the mask to match the background image size\n", "mask_image = mask_image.resize(background_image.size, Image.LANCZOS)" ], "metadata": { "id": "JDCRPTsFM3B7" }, "execution_count": 6, "outputs": [] }, { "cell_type": "code", "source": [ "output = pipe(\n", " prompt=\"A Woman wearing sunglasses, smiling\",\n", " negative_prompt=\"ugly, low quality, blur, distorted, unreal\",\n", " image=background_image,\n", " mask_image=mask_image,\n", " guidance_scale=7.5,\n", " num_inference_steps=50,\n", " strength=0.99,\n", " height=background_image.size[1],\n", " width=background_image.size[0]\n", ")\n", "\n", "\n", "# Save and display output\n", "inpainted_image = output.images[0]\n", "inpainted_image.save(\"output.png\")\n", "inpainted_image.show()\n" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 49, "referenced_widgets": [ "d73ad5cc5b1c45e5bac35da4b105d7d4", "c4ce5cafe6e84a6cb8a3e9a043c67913", "d0e4bf336ad241958372f92c3fc6cd48", "4f101488783446bfa337e6d4809f9ca5", "a664851d48084d5a8bed4efb15e3eecf", "0c8774c074194255961305239d428887", "2d71384e54a94332ae4c21f48fe404d1", "a30a7bd69266403aa5774d3045087447", "7edfc44057194cdb8880a1544cf13d3e", "d332c661fc54456c9e007a6257c08427", "9302974855bc4f25bc4e5239e87fbfc3" ] }, "id": "u4KLSfGPNAZK", "outputId": "f63805bc-5315-4541-8633-e3ddcc9bc4fd" }, "execution_count": 10, "outputs": [ { "output_type": "display_data", "data": { "text/plain": [ " 0%| | 0/49 [00:00