Skip to content

Commit

Permalink
Created using Colaboratory
Browse files Browse the repository at this point in the history
  • Loading branch information
yvrjsharma committed Jan 17, 2024
1 parent 820b0b3 commit 0761b07
Showing 1 changed file with 180 additions and 0 deletions.
180 changes: 180 additions & 0 deletions PhotoMaker_Demo.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/github/yvrjsharma/HugginFace_Gradio/blob/main/PhotoMaker_Demo.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"source": [
"!git clone https://github.com/TencentARC/PhotoMaker.git\n",
"%cd PhotoMaker\n",
"# Install requirements\n",
"!pip install -r requirements.txt\n",
"# Install photomaker\n",
"!pip install git+https://github.com/TencentARC/PhotoMaker.git\n",
"\n",
"from photomaker import PhotoMakerStableDiffusionXLPipeline\n",
"from huggingface_hub import hf_hub_download\n",
"import torch\n",
"import os\n",
"from diffusers.utils import load_image\n",
"from diffusers import EulerDiscreteScheduler\n",
"from photomaker import PhotoMakerStableDiffusionXLPipeline\n",
"from PIL import Image\n",
"import gradio as gr\n",
"\n",
"photomaker_path = hf_hub_download(repo_id=\"TencentARC/PhotoMaker\", filename=\"photomaker-v1.bin\", repo_type=\"model\")\n",
"base_model_path = 'SG161222/RealVisXL_V3.0'\n",
"device = \"cuda\" if torch.cuda.is_available() else \"cpu\"\n",
"\n",
"### Load base model\n",
"pipe = PhotoMakerStableDiffusionXLPipeline.from_pretrained(\n",
" base_model_path, # can change to any base model based on SDXL\n",
" torch_dtype=torch.bfloat16,\n",
" use_safetensors=True,\n",
" variant=\"fp16\"\n",
").to(device)\n",
"\n",
"### Load PhotoMaker checkpoint\n",
"pipe.load_photomaker_adapter(\n",
" os.path.dirname(photomaker_path),\n",
" subfolder=\"\",\n",
" weight_name=os.path.basename(photomaker_path),\n",
" trigger_word=\"img\" # define the trigger word\n",
")\n",
"pipe.scheduler = EulerDiscreteScheduler.from_config(pipe.scheduler.config)"
],
"metadata": {
"id": "Bprrd6l026v8"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"def generate_image(files, prompt, negative_prompt):\n",
" # load the input ID images\n",
" files_list = []\n",
" for f in files:\n",
" img = Image.open(f.name)\n",
" files_list.append(img)\n",
" # Note that the trigger word `img` must follow the class word for personalization\n",
" generator = torch.Generator(device=device).manual_seed(42)\n",
" images = pipe(\n",
" prompt=prompt,\n",
" input_id_images=files_list,\n",
" negative_prompt=negative_prompt,\n",
" num_images_per_prompt=1,\n",
" num_inference_steps=50,\n",
" start_merge_step=10,\n",
" generator=generator,).images[0]\n",
" return [images]"
],
"metadata": {
"id": "vaFMd4YDUfgu"
},
"execution_count": 19,
"outputs": []
},
{
"cell_type": "code",
"source": [
"logo = r\"\"\"<center><img src='https://photo-maker.github.io/assets/logo.png' alt='PhotoMaker logo' style=\"width:80px; margin-bottom:10px\"></center>\"\"\"\n",
"title = r\"\"\"<h1 align=\"center\">PhotoMaker: Customizing Realistic Human Photos via Stacked ID Embedding</h1>\"\"\"\n",
"with gr.Blocks() as demo:\n",
" with gr.Row():\n",
" gr.Markdown(title)\n",
" gr.Markdown(logo)\n",
" with gr.Row():\n",
" files = gr.Files(\n",
" label=\"Drag (Select) 1 or more photos of your face\",\n",
" file_types=[\"image\"])\n",
" gallery = gr.Gallery(label=\"Generated Images\")\n",
" with gr.Row():\n",
" prompt = gr.Textbox(label=\"Prompt\",\n",
" info=\"Try something like 'a photo of a man/woman img', 'img' is the trigger word.\",\n",
" placeholder=\"A photo of a [man/woman img]...\")\n",
" negative_prompt = gr.Textbox(\n",
" label=\"Negative Prompt\",\n",
" placeholder=\"low quality\",\n",
" value=\"nsfw, lowres, bad anatomy, bad hands, text, error, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality, normal quality, jpeg artifacts, signature, watermark, username, blurry\",)\n",
" submit = gr.Button(\"Submit\")\n",
"\n",
"\n",
" submit.click(generate_image, [files, prompt, negative_prompt], [gallery])\n",
"\n",
"demo.launch()"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 646
},
"id": "4qu3zIr_QNYL",
"outputId": "9405dfa2-ef80-4371-a9e4-2f8b828a785c"
},
"execution_count": 21,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Setting queue=True in a Colab notebook requires sharing enabled. Setting `share=True` (you can turn this off by setting `share=False` in `launch()` explicitly).\n",
"\n",
"Colab notebook detected. To show errors in colab notebook, set debug=True in launch()\n",
"Running on public URL: https://1443074086d29b3276.gradio.live\n",
"\n",
"This share link expires in 72 hours. For free permanent hosting and GPU upgrades, run `gradio deploy` from Terminal to deploy to Spaces (https://huggingface.co/spaces)\n"
]
},
{
"output_type": "display_data",
"data": {
"text/plain": [
"<IPython.core.display.HTML object>"
],
"text/html": [
"<div><iframe src=\"https://1443074086d29b3276.gradio.live\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
]
},
"metadata": {}
},
{
"output_type": "execute_result",
"data": {
"text/plain": []
},
"metadata": {},
"execution_count": 21
}
]
}
],
"metadata": {
"colab": {
"provenance": [],
"machine_shape": "hm",
"gpuType": "V100",
"authorship_tag": "ABX9TyM9Cs+Imsu6rOs34njO9WoP",
"include_colab_link": true
},
"kernelspec": {
"display_name": "Python 3",
"name": "python3"
},
"language_info": {
"name": "python"
},
"accelerator": "GPU"
},
"nbformat": 4,
"nbformat_minor": 0
}

0 comments on commit 0761b07

Please sign in to comment.