Spaces:
Sleeping
Sleeping
File size: 9,151 Bytes
119105c 684fb8f 119105c 98feb90 684fb8f 1b0d680 684fb8f 119105c 3f68383 872c46f 119105c 8913269 119105c 3f68383 6015068 3f68383 1601fa4 98feb90 cb90e16 119105c 54b8f2e 119105c 962e77d 98feb90 08a0e57 962e77d 98feb90 119105c 2b90a75 119105c 98feb90 119105c 3441cff 119105c f1d1956 119105c ac3a4de 119105c f1d1956 08a0e57 f1d1956 08a0e57 119105c 8913269 43f386d 8913269 119105c 962e77d 8913269 119105c 8913269 74df120 119105c 98feb90 f1d1956 119105c 6015068 3f68383 119105c 3441cff ac3a4de 8913269 6015068 3f68383 872c46f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
#!/usr/bin/env python
"""Demo app for https://github.com/ziqihuangg/ReVersion.
The code in this repo is partly adapted from the following repository:
https://github.com/ziqihuangg/ReVersion
S-Lab License 1.0
Copyright 2023 S-Lab
Redistribution and use for non-commercial purpose in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
4. In the event that redistribution and/or use for commercial purpose in source or binary forms, with or without modification is required, please contact the contributor(s) of the work.
"""
from __future__ import annotations
import sys
import os
import pathlib
import argparse
import gradio as gr
import torch
from inference import inference_fn
# def parse_args() -> argparse.Namespace:
# parser = argparse.ArgumentParser()
# parser.add_argument('--device', type=str, default='cpu')
# parser.add_argument('--theme', type=str)
# parser.add_argument('--share', action='store_true')
# parser.add_argument('--port', type=int)
# parser.add_argument('--disable-queue',
# dest='enable_queue',
# action='store_false')
# return parser.parse_args()
TITLE = '# ReVersion'
DESCRIPTION = '''
This is a demo for **ReVersion: Diffusion-Based Relation Inversion from Images**
[[Paper](https://arxiv.org/abs/2303.13495)] | [[Project Page](https://ziqihuangg.github.io/projects/reversion.html)] | [[GitHub Code](https://github.com/ziqihuangg/ReVersion)] | [[Video](https://www.youtube.com/watch?v=pkal3yjyyKQ)]
It is recommended to upgrade to GPU in Settings after duplicating this space to use it. <a href="https://huggingface.co/spaces/Ziqi/ReVersion?duplicate=true"><img src="https://bit.ly/3gLdBN6" alt="Duplicate Space"></a>
'''
DETAILDESCRIPTION='''
ReVersion
'''
DETAILDESCRIPTION='''
We propose a new task, **Relation Inversion**: Given a few exemplar images, where a relation co-exists in every image, we aim to find a relation prompt **\<R>** to capture this interaction, and apply the relation to new entities to synthesize new scenes.
Here we give several pre-trained relation prompts for you to play with. You can choose a set of exemplar images from the examples, and use **\<R>** in your prompt for relation-specific text-to-image generation.
'''
ORIGINAL_SPACE_ID = 'Ziqi/ReVersion'
SPACE_ID = os.getenv('SPACE_ID', ORIGINAL_SPACE_ID)
if os.getenv('SYSTEM') == 'spaces' and SPACE_ID != ORIGINAL_SPACE_ID:
SETTINGS = f'<a href="https://huggingface.co/spaces/{SPACE_ID}/settings">Settings</a>'
else:
SETTINGS = 'Settings'
CUDA_NOT_AVAILABLE_WARNING = f'''# Attention - Running on CPU.
<center>
You can assign a GPU in the {SETTINGS} tab if you are running this on HF Spaces.
"T4 small" is sufficient to run this demo.
</center>
'''
# os.system("git clone https://github.com/ziqihuangg/ReVersion")
# sys.path.append("ReVersion")
def show_warning(warning_text: str) -> gr.Blocks:
with gr.Blocks() as demo:
with gr.Box():
gr.Markdown(warning_text)
return demo
def set_example_image(example: list):
return gr.update(value=example[0])
def create_inference_demo(func: inference_fn) -> gr.Blocks:
with gr.Blocks() as demo:
with gr.Row():
with gr.Column():
exemplar_img = gr.Image(
label='Exemplar Images',
type='pil',
interaction=False
)
# paths = sorted(pathlib.Path('exemplars').glob('*.jpg'))
# exemplar_dataset = gr.Dataset(components=[exemplar_img],
# samples=[[path.as_posix()]
# for path in paths])
exemplar_dataset = gr.Dataset(
components=[exemplar_img],
samples = [
['exemplars/painted_on.jpg'],
['exemplars/carved_by.jpg'],
['exemplars/inside.jpg']
]
)
# model_id = gr.Dropdown(
# choices=['painted_on', 'carved_by', 'inside'],
# value='painted_on',
# label='Relation',
# visible=True)
prompt = gr.Textbox(
label='Prompt',
max_lines=1,
placeholder='Example: "cat <R> stone"')
# placeholder_string = gr.Textbox(
# label='Placeholder String',
# max_lines=1,
# placeholder='Example: "<R>"')
with gr.Accordion('Other Parameters', open=False):
num_samples = gr.Slider(label='Number of Images to Generate',
minimum=4,
maximum=8,
step=2,
value=6)
guidance_scale = gr.Slider(label='Classifier-Free Guidance Scale',
minimum=0,
maximum=50,
step=0.1,
value=7.5)
ddim_steps = gr.Slider(label='Number of DDIM Sampling Steps',
minimum=10,
maximum=100,
step=1,
value=50)
run_button = gr.Button('Generate')
with gr.Column():
result = gr.Image(label='Result')
exemplar_dataset.click(fn=set_example_image,
inputs=exemplar_dataset,
outputs=exemplar_dataset.components,
queue=False)
prompt.submit(
fn=func,
# inputs=[
# model_id,
# prompt,
# num_samples,
# guidance_scale,
# ddim_steps
# ],
inputs=[
exemplar_dataset,
prompt,
num_samples,
guidance_scale,
ddim_steps
],
outputs=result,
queue=False
)
run_button.click(
fn=func,
# inputs=[
# model_id,
# prompt,
# num_samples,
# guidance_scale,
# ddim_steps
# ],
inputs=[
exemplar_dataset,
prompt,
num_samples,
guidance_scale,
ddim_steps
],
outputs=result,
queue=False
)
return demo
# args = parse_args()
# args.device = 'cuda' if torch.cuda.is_available() else 'cpu'
# print('*** Now using %s.'%(args.device))
if torch.cuda.is_available():
print('*** Now using %s.'%('cuda'))
else:
print('*** Now using %s.'%('cpu'))
with gr.Blocks(css='style.css') as demo:
if not torch.cuda.is_available():
show_warning(CUDA_NOT_AVAILABLE_WARNING)
gr.Markdown(TITLE)
gr.Markdown(DESCRIPTION)
gr.Markdown(DETAILDESCRIPTION)
with gr.Tabs():
with gr.TabItem('Relation-Specific Text-to-Image Generation'):
create_inference_demo(inference_fn)
demo.queue(default_enabled=False).launch(share=False)
# demo.launch(
# enable_queue=args.enable_queue,
# server_port=args.port,
# share=args.share
# )
# demo.queue(default_enabled=False).launch(server_port=args.port, share=args.share)
|