{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "initial_id", "metadata": { "ExecuteTime": { "end_time": "2024-12-09T09:44:30.641366Z", "start_time": "2024-12-09T09:44:11.789050Z" } }, "outputs": [], "source": [ "import os\n", "\n", "import gradio as gr\n", "from diffusers import DiffusionPipeline\n", "import matplotlib.pyplot as plt\n", "import torch\n", "from PIL import Image\n" ] }, { "cell_type": "code", "execution_count": 2, "id": "ddf33e0d3abacc2c", "metadata": {}, "outputs": [], "source": [ "import sys\n", "#append current path\n", "sys.path.extend(\"/afs/csail.mit.edu/u/h/huiren/code/diffusion/stable_diffusion/release/hf_demo\")" ] }, { "cell_type": "code", "execution_count": 3, "id": "643e49fd601daf8f", "metadata": { "ExecuteTime": { "end_time": "2024-12-09T09:44:35.790962Z", "start_time": "2024-12-09T09:44:35.779496Z" } }, "outputs": [], "source": [ "os.environ[\"CUDA_VISIBLE_DEVICES\"] = \"1\"\n", "device = \"cuda\" if torch.cuda.is_available() else \"cpu\"\n", "dtype=torch.bfloat16 if torch.cuda.is_available() else torch.float16" ] }, { "cell_type": "code", "execution_count": 4, "id": "e03aae2a4e5676dd", "metadata": { "ExecuteTime": { "end_time": "2024-12-09T09:44:44.157412Z", "start_time": "2024-12-09T09:44:37.138452Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/data/vision/torralba/selfmanaged/torralba/scratch/jomat/sam_dataset/miniforge3/envs/diffusion/lib/python3.9/site-packages/huggingface_hub/file_download.py:1132: FutureWarning: `resume_download` is deprecated and will be removed in version 1.0.0. Downloads always resume when possible. If you want to force a new download, use `force_download=True`.\n", " warnings.warn(\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "acc42f294243439798e4d77d1a59296d", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Loading pipeline components...: 0%| | 0/7 [00:00" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "block = gr.Blocks()\n", "# Direct infer\n", "# Direct infer\n", "with block:\n", " with gr.Group():\n", " gr.Markdown(\" # Art-Free Diffusion Demo\")\n", " gr.Markdown(\"(More features in development...)\")\n", " with gr.Row():\n", " text = gr.Textbox(\n", " label=\"Enter your prompt(long and detailed would be better):\",\n", " max_lines=2,\n", " placeholder=\"Enter your prompt(long and detailed would be better)\",\n", " container=True,\n", " value=\"Park with cherry blossom trees, picnicker’s and a clear blue pond.\",\n", " )\n", "\n", " with gr.Tab('Generation'):\n", " with gr.Row():\n", " with gr.Column():\n", " # gr.Markdown(\"## Art-Free Generation\")\n", " # gr.Markdown(\"Generate images from text prompts.\")\n", "\n", " gallery_gen_ori = gr.Image(\n", " label=\"W/O Adapter\",\n", " show_label=True,\n", " elem_id=\"gallery\",\n", " height=\"auto\"\n", " )\n", "\n", "\n", " with gr.Column():\n", " # gr.Markdown(\"## Art-Free Generation\")\n", " # gr.Markdown(\"Generate images from text prompts.\")\n", " gallery_gen_art = gr.Image(\n", " label=\"W/ Adapter\",\n", " show_label=True,\n", " elem_id=\"gallery\",\n", " height=\"auto\"\n", " )\n", "\n", "\n", " with gr.Row():\n", " btn_gen_ori = gr.Button(\"Art-Free Generate\", scale=1)\n", " btn_gen_art = gr.Button(\"Artistic Generate\", scale=1)\n", "\n", "\n", " with gr.Tab('Stylization'):\n", " with gr.Row():\n", "\n", " with gr.Column():\n", " # gr.Markdown(\"## Art-Free Generation\")\n", " # gr.Markdown(\"Generate images from text prompts.\")\n", "\n", " gallery_stylization_ref = gr.Image(\n", " label=\"Ref Image\",\n", " show_label=True,\n", " elem_id=\"gallery\",\n", " height=\"auto\",\n", " scale=1,\n", " )\n", " with gr.Column(scale=2):\n", " with gr.Row():\n", " with gr.Column():\n", " # gr.Markdown(\"## Art-Free Generation\")\n", " # gr.Markdown(\"Generate images from text prompts.\")\n", " \n", " gallery_stylization_ori = gr.Image(\n", " label=\"W/O Adapter\",\n", " show_label=True,\n", " elem_id=\"gallery\",\n", " height=\"auto\",\n", " scale=1,\n", " )\n", " \n", " \n", " with gr.Column():\n", " # gr.Markdown(\"## Art-Free Generation\")\n", " # gr.Markdown(\"Generate images from text prompts.\")\n", " gallery_stylization_art = gr.Image(\n", " label=\"W/ Adapter\",\n", " show_label=True,\n", " elem_id=\"gallery\",\n", " height=\"auto\",\n", " scale=1,\n", " )\n", " start_timestep = gr.Slider(label=\"Adapter Timestep\", minimum=0, maximum=1000, value=800, step=1)\n", " with gr.Row():\n", " btn_style_ori = gr.Button(\"Art-Free Stylization\", scale=1)\n", " btn_style_art = gr.Button(\"Artistic Stylization\", scale=1)\n", "\n", "\n", " with gr.Row():\n", " # with gr.Column():\n", " # samples = gr.Slider(label=\"Images\", minimum=1, maximum=4, value=1, step=1, scale=1)\n", " scale = gr.Slider(\n", " label=\"Guidance Scale\", minimum=0, maximum=20, value=7.5, step=0.1\n", " )\n", " # with gr.Column():\n", " adapter_choice = gr.Dropdown(\n", " label=\"Select Art Adapter\",\n", " choices=[ \"Andre Derain (fauvism)\",\"Vincent van Gogh (post impressionism)\",\"Andy Warhol (pop art)\",\n", " \"Camille Corot (realism)\", \"Claude Monet (impressionism)\", \"Pablo Picasso (cubism)\", \"Gerhard Richter (abstract expressionism)\",\n", " \"Hokusai (ukiyo-e)\", \"Gustav Klimt (art nouveau)\", \"Henri Matisse (abstract expressionism)\",\n", " \"Walter Battiss\", \"Jackson Pollock\", \"M.C. Escher\", \"Albert Gleizes\", \"Wassily Kandinsky\",\n", " \"Roy Lichtenstein\", \"Joan Miro\"\n", " ],\n", " value=\"Andre Derain (fauvism)\",\n", " scale=1\n", " )\n", "\n", " with gr.Row():\n", " steps = gr.Slider(label=\"Steps\", minimum=1, maximum=50, value=20, step=1)\n", " adapter_scale = gr.Slider(label=\"Stylization Scale\", minimum=0, maximum=1.5, value=1., step=0.1, scale=1)\n", "\n", " with gr.Row():\n", " seed = gr.Slider(label=\"Seed\",minimum=0,maximum=2147483647,step=1,randomize=True,scale=1)\n", "\n", "\n", " gr.on([btn_gen_ori.click], demo_inference_gen_ori, inputs=[text, seed, steps, scale], outputs=gallery_gen_ori)\n", " gr.on([btn_gen_art.click], demo_inference_gen_artistic, inputs=[adapter_choice, text, seed, steps, scale, adapter_scale], outputs=gallery_gen_art)\n", "\n", " gr.on([btn_style_ori.click], demo_inference_stylization_ori, inputs=[gallery_stylization_ref, text, seed, steps, scale, start_timestep], outputs=gallery_stylization_ori)\n", " gr.on([btn_style_art.click], demo_inference_stylization_artistic, inputs=[gallery_stylization_ref, adapter_choice, text, seed, steps, scale, adapter_scale, start_timestep], outputs=gallery_stylization_art)\n", "\n", "block.launch(share=True)" ] }, { "cell_type": "code", "execution_count": null, "id": "3239c12167a5f2cd", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.18" } }, "nbformat": 4, "nbformat_minor": 5 }