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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
return "Hello " + name + "!!"
|
| 5 |
|
| 6 |
-
iface = gr.Interface(fn=greet, inputs="text", outputs="text")
|
| 7 |
-
iface.launch()
|
|
|
|
| 1 |
+
#!/usr/bin/env python
|
| 2 |
+
"""Demo app for https://github.com/adobe-research/custom-diffusion.
|
| 3 |
+
The code in this repo is partly adapted from the following repository:
|
| 4 |
+
https://huggingface.co/spaces/hysts/LoRA-SD-training
|
| 5 |
+
MIT License
|
| 6 |
+
Copyright (c) 2022 hysts
|
| 7 |
+
==========================================================================================
|
| 8 |
+
Adobe’s modifications are Copyright 2022 Adobe Research. All rights reserved.
|
| 9 |
+
Adobe’s modifications are licensed under the Adobe Research License. To view a copy of the license, visit
|
| 10 |
+
LICENSE.
|
| 11 |
+
==========================================================================================
|
| 12 |
+
"""
|
| 13 |
+
|
| 14 |
+
from __future__ import annotations
|
| 15 |
+
import sys
|
| 16 |
+
import os
|
| 17 |
+
import pathlib
|
| 18 |
+
|
| 19 |
import gradio as gr
|
| 20 |
+
import torch
|
| 21 |
+
|
| 22 |
+
from inference import InferencePipeline
|
| 23 |
+
from trainer import Trainer
|
| 24 |
+
from uploader import upload
|
| 25 |
+
|
| 26 |
+
TITLE = '# Custom Diffusion + StableDiffusion Training UI'
|
| 27 |
+
DESCRIPTION = '''This is a demo for [https://github.com/adobe-research/custom-diffusion](https://github.com/adobe-research/custom-diffusion).
|
| 28 |
+
It is recommended to upgrade to GPU in Settings after duplicating this space to use it.
|
| 29 |
+
<a href="https://huggingface.co/spaces/nupurkmr9/custom-diffusion?duplicate=true"><img src="https://bit.ly/3gLdBN6" alt="Duplicate Space"></a>
|
| 30 |
+
'''
|
| 31 |
+
DETAILDESCRIPTION='''
|
| 32 |
+
Custom Diffusion allows you to fine-tune text-to-image diffusion models, such as Stable Diffusion, given a few images of a new concept (~4-20).
|
| 33 |
+
We fine-tune only a subset of model parameters, namely key and value projection matrices, in the cross-attention layers and the modifier token used to represent the object.
|
| 34 |
+
This also reduces the extra storage for each additional concept to 75MB. Our method also allows you to use a combination of concepts. There's still limitations on which compositions work. For more analysis please refer to our [website](https://www.cs.cmu.edu/~custom-diffusion/).
|
| 35 |
+
<center>
|
| 36 |
+
<img src="https://huggingface.co/spaces/nupurkmr9/custom-diffusion/resolve/main/method.jpg" width="600" align="center" >
|
| 37 |
+
</center>
|
| 38 |
+
'''
|
| 39 |
+
|
| 40 |
+
ORIGINAL_SPACE_ID = 'nupurkmr9/custom-diffusion'
|
| 41 |
+
SPACE_ID = os.getenv('SPACE_ID', ORIGINAL_SPACE_ID)
|
| 42 |
+
SHARED_UI_WARNING = f'''# Attention - This Space doesn't work in this shared UI. You can duplicate and use it with a paid private T4 GPU.
|
| 43 |
+
<center><a class="duplicate-button" style="display:inline-block" target="_blank" href="https://huggingface.co/spaces/{SPACE_ID}?duplicate=true"><img src="https://img.shields.io/badge/-Duplicate%20Space-blue?labelColor=white&style=flat&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAP5JREFUOE+lk7FqAkEURY+ltunEgFXS2sZGIbXfEPdLlnxJyDdYB62sbbUKpLbVNhyYFzbrrA74YJlh9r079973psed0cvUD4A+4HoCjsA85X0Dfn/RBLBgBDxnQPfAEJgBY+A9gALA4tcbamSzS4xq4FOQAJgCDwV2CPKV8tZAJcAjMMkUe1vX+U+SMhfAJEHasQIWmXNN3abzDwHUrgcRGmYcgKe0bxrblHEB4E/pndMazNpSZGcsZdBlYJcEL9Afo75molJyM2FxmPgmgPqlWNLGfwZGG6UiyEvLzHYDmoPkDDiNm9JR9uboiONcBXrpY1qmgs21x1QwyZcpvxt9NS09PlsPAAAAAElFTkSuQmCC&logoWidth=14" alt="Duplicate Space"></a></center>
|
| 44 |
+
'''
|
| 45 |
+
if os.getenv('SYSTEM') == 'spaces' and SPACE_ID != ORIGINAL_SPACE_ID:
|
| 46 |
+
SETTINGS = f'<a href="https://huggingface.co/spaces/{SPACE_ID}/settings">Settings</a>'
|
| 47 |
+
|
| 48 |
+
else:
|
| 49 |
+
SETTINGS = 'Settings'
|
| 50 |
+
CUDA_NOT_AVAILABLE_WARNING = f'''# Attention - Running on CPU.
|
| 51 |
+
<center>
|
| 52 |
+
You can assign a GPU in the {SETTINGS} tab if you are running this on HF Spaces.
|
| 53 |
+
"T4 small" is sufficient to run this demo.
|
| 54 |
+
</center>
|
| 55 |
+
'''
|
| 56 |
+
|
| 57 |
+
os.system("git clone https://github.com/ziqihuangg/ReVersion")
|
| 58 |
+
sys.path.append("ReVersion")
|
| 59 |
+
|
| 60 |
+
from ReVersion.inference import *
|
| 61 |
+
|
| 62 |
+
def show_warning(warning_text: str) -> gr.Blocks:
|
| 63 |
+
with gr.Blocks() as demo:
|
| 64 |
+
with gr.Box():
|
| 65 |
+
gr.Markdown(warning_text)
|
| 66 |
+
return demo
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
def update_output_files() -> dict:
|
| 70 |
+
paths = sorted(pathlib.Path('results').glob('*.bin'))
|
| 71 |
+
paths = [path.as_posix() for path in paths] # type: ignore
|
| 72 |
+
return gr.update(value=paths or None)
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
def find_weight_files() -> list[str]:
|
| 76 |
+
curr_dir = pathlib.Path(__file__).parent
|
| 77 |
+
paths = sorted(curr_dir.rglob('*.bin'))
|
| 78 |
+
paths = [path for path in paths if '.lfs' not in str(path)]
|
| 79 |
+
return [path.relative_to(curr_dir).as_posix() for path in paths]
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
def reload_custom_diffusion_weight_list() -> dict:
|
| 83 |
+
return gr.update(choices=find_weight_files())
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
def create_inference_demo(pipe: InferencePipeline) -> gr.Blocks:
|
| 87 |
+
with gr.Blocks() as demo:
|
| 88 |
+
with gr.Row():
|
| 89 |
+
with gr.Column():
|
| 90 |
+
base_model = gr.Dropdown(
|
| 91 |
+
choices=['ReVersion/experiments/painted_on'],
|
| 92 |
+
value='ReVersion/experiments/painted_on',
|
| 93 |
+
label='Base Model',
|
| 94 |
+
visible=True)
|
| 95 |
+
resolution = gr.Dropdown(choices=[512, 768],
|
| 96 |
+
value=512,
|
| 97 |
+
label='Resolution',
|
| 98 |
+
visible=True)
|
| 99 |
+
reload_button = gr.Button('Reload Weight List')
|
| 100 |
+
weight_name = gr.Dropdown(choices=find_weight_files(),
|
| 101 |
+
value='ReVersion/experiments/painted_on',
|
| 102 |
+
label='ReVersion/experiments/painted_on')
|
| 103 |
+
prompt = gr.Textbox(
|
| 104 |
+
label='Prompt',
|
| 105 |
+
max_lines=1,
|
| 106 |
+
placeholder='Example: "cat <R> stone"')
|
| 107 |
+
seed = gr.Slider(label='Seed',
|
| 108 |
+
minimum=0,
|
| 109 |
+
maximum=100000,
|
| 110 |
+
step=1,
|
| 111 |
+
value=42)
|
| 112 |
+
with gr.Accordion('Other Parameters', open=False):
|
| 113 |
+
num_steps = gr.Slider(label='Number of Steps',
|
| 114 |
+
minimum=0,
|
| 115 |
+
maximum=500,
|
| 116 |
+
step=1,
|
| 117 |
+
value=100)
|
| 118 |
+
guidance_scale = gr.Slider(label='CFG Scale',
|
| 119 |
+
minimum=0,
|
| 120 |
+
maximum=50,
|
| 121 |
+
step=0.1,
|
| 122 |
+
value=6)
|
| 123 |
+
eta = gr.Slider(label='DDIM eta',
|
| 124 |
+
minimum=0,
|
| 125 |
+
maximum=1.,
|
| 126 |
+
step=0.1,
|
| 127 |
+
value=1.)
|
| 128 |
+
batch_size = gr.Slider(label='Batch Size',
|
| 129 |
+
minimum=0,
|
| 130 |
+
maximum=10.,
|
| 131 |
+
step=1,
|
| 132 |
+
value=1)
|
| 133 |
+
|
| 134 |
+
run_button = gr.Button('Generate')
|
| 135 |
+
|
| 136 |
+
gr.Markdown('''
|
| 137 |
+
- Models with names starting with "custom-diffusion-models/" are the pretrained models provided in the [original repo](https://github.com/adobe-research/custom-diffusion), and the ones with names starting with "results/delta.bin" are your trained models.
|
| 138 |
+
- After training, you can press "Reload Weight List" button to load your trained model names.
|
| 139 |
+
- Increase number of steps in Other parameters for better samples qualitatively.
|
| 140 |
+
''')
|
| 141 |
+
with gr.Column():
|
| 142 |
+
result = gr.Image(label='Result')
|
| 143 |
+
|
| 144 |
+
reload_button.click(fn=reload_custom_diffusion_weight_list,
|
| 145 |
+
inputs=None,
|
| 146 |
+
outputs=weight_name)
|
| 147 |
+
prompt.submit(fn=pipe.run,
|
| 148 |
+
inputs=[
|
| 149 |
+
base_model,
|
| 150 |
+
weight_name,
|
| 151 |
+
prompt,
|
| 152 |
+
seed,
|
| 153 |
+
num_steps,
|
| 154 |
+
guidance_scale,
|
| 155 |
+
eta,
|
| 156 |
+
batch_size,
|
| 157 |
+
resolution
|
| 158 |
+
],
|
| 159 |
+
outputs=result,
|
| 160 |
+
queue=False)
|
| 161 |
+
run_button.click(fn=pipe.run,
|
| 162 |
+
inputs=[
|
| 163 |
+
base_model,
|
| 164 |
+
weight_name,
|
| 165 |
+
prompt,
|
| 166 |
+
seed,
|
| 167 |
+
num_steps,
|
| 168 |
+
guidance_scale,
|
| 169 |
+
eta,
|
| 170 |
+
batch_size,
|
| 171 |
+
resolution
|
| 172 |
+
],
|
| 173 |
+
outputs=result,
|
| 174 |
+
queue=False)
|
| 175 |
+
return demo
|
| 176 |
+
|
| 177 |
+
|
| 178 |
+
pipe = InferencePipeline()
|
| 179 |
+
trainer = Trainer()
|
| 180 |
+
|
| 181 |
+
with gr.Blocks(css='style.css') as demo:
|
| 182 |
+
if os.getenv('IS_SHARED_UI'):
|
| 183 |
+
show_warning(SHARED_UI_WARNING)
|
| 184 |
+
if not torch.cuda.is_available():
|
| 185 |
+
show_warning(CUDA_NOT_AVAILABLE_WARNING)
|
| 186 |
+
|
| 187 |
+
gr.Markdown(TITLE)
|
| 188 |
+
gr.Markdown(DESCRIPTION)
|
| 189 |
+
gr.Markdown(DETAILDESCRIPTION)
|
| 190 |
+
|
| 191 |
+
with gr.Tabs():
|
| 192 |
+
# with gr.TabItem('Train'):
|
| 193 |
+
# create_training_demo(trainer, pipe)
|
| 194 |
+
with gr.TabItem('Inference'):
|
| 195 |
+
create_inference_demo(pipe)
|
| 196 |
+
# with gr.TabItem('Upload'):
|
| 197 |
+
# create_upload_demo()
|
| 198 |
|
| 199 |
+
demo.queue(default_enabled=False).launch(share=False)
|
|
|
|
| 200 |
|
|
|
|
|
|
app_000.py
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
|
| 3 |
+
def greet(name):
|
| 4 |
+
return "Hello " + name + "!!"
|
| 5 |
+
|
| 6 |
+
iface = gr.Interface(fn=greet, inputs="text", outputs="text")
|
| 7 |
+
iface.launch()
|