Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,144 +12,148 @@ device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
| 12 |
pipe = AutoPipelineForText2Image.from_pretrained("briaai/BRIA-2.3", torch_dtype=torch.float16, force_zeros_for_empty_prompt=False).to(device)
|
| 13 |
pipe.load_ip_adapter("briaai/DEV-Image-Prompt", subfolder='models', weight_name="ip_adapter_bria.bin")
|
| 14 |
|
| 15 |
-
default_negative_prompt= "" #"Logo,Watermark,Text,Ugly,Morbid,Extra fingers,Poorly drawn hands,Mutation,Blurry,Extra limbs,Gross proportions,Missing arms,Mutated hands,Long neck,Duplicate,Mutilated,Mutilated hands,Poorly drawn face,Deformed,Bad anatomy,Cloned face,Malformed limbs,Missing legs,Too many fingers"
|
| 16 |
|
|
|
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
content = f.read()
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
def predict(image, prompt="high quality, best quality", negative_prompt="", guidance_scale=5, steps=30, ip_adapter_scale = 1.0, width=1024, height=1024, seed=0):
|
| 27 |
-
|
| 28 |
-
pipe.set_ip_adapter_scale(ip_adapter_scale)
|
| 29 |
-
|
| 30 |
-
if negative_prompt == "":
|
| 31 |
-
negative_prompt = None
|
| 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 |
-
.dark .footer {border-color: #303030}
|
| 64 |
-
.dark .footer>p {background: #0b0f19}
|
| 65 |
-
.acknowledgments h4{margin: 1.25em 0 .25em 0;font-weight: bold;font-size: 115%}
|
| 66 |
-
#image_upload .touch-none{display: flex}
|
| 67 |
-
@keyframes spin {
|
| 68 |
-
from {
|
| 69 |
-
transform: rotate(0deg);
|
| 70 |
-
}
|
| 71 |
-
to {
|
| 72 |
-
transform: rotate(360deg);
|
| 73 |
-
}
|
| 74 |
}
|
| 75 |
-
#
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
#
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
#prompt input{width: calc(100% - 160px);border-top-right-radius: 0px;border-bottom-right-radius: 0px;}
|
| 84 |
-
#run_button{position:absolute;margin-top: 11px;right: 0;margin-right: 0.8em;border-bottom-left-radius: 0px;
|
| 85 |
-
border-top-left-radius: 0px;}
|
| 86 |
-
#prompt-container{margin-top:-18px;}
|
| 87 |
-
#prompt-container .form{border-top-left-radius: 0;border-top-right-radius: 0}
|
| 88 |
-
#image_upload{border-bottom-left-radius: 0px;border-bottom-right-radius: 0px}
|
| 89 |
-
'''
|
| 90 |
-
|
| 91 |
-
image_blocks = gr.Blocks(css=css, elem_id="total-container")
|
| 92 |
-
with image_blocks as demo:
|
| 93 |
with gr.Column(elem_id="col-container"):
|
| 94 |
-
gr.Markdown("
|
| 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 |
-
<div class="footer">
|
| 149 |
-
<p>Model by <a href="https://huggingface.co/diffusers" style="text-decoration: underline;" target="_blank">Diffusers</a> - Gradio Demo by 🤗 Hugging Face
|
| 150 |
-
</p>
|
| 151 |
-
</div>
|
| 152 |
-
"""
|
| 153 |
)
|
| 154 |
|
| 155 |
-
|
|
|
|
|
|
|
|
|
| 12 |
pipe = AutoPipelineForText2Image.from_pretrained("briaai/BRIA-2.3", torch_dtype=torch.float16, force_zeros_for_empty_prompt=False).to(device)
|
| 13 |
pipe.load_ip_adapter("briaai/DEV-Image-Prompt", subfolder='models', weight_name="ip_adapter_bria.bin")
|
| 14 |
|
| 15 |
+
# default_negative_prompt= "" #"Logo,Watermark,Text,Ugly,Morbid,Extra fingers,Poorly drawn hands,Mutation,Blurry,Extra limbs,Gross proportions,Missing arms,Mutated hands,Long neck,Duplicate,Mutilated,Mutilated hands,Poorly drawn face,Deformed,Bad anatomy,Cloned face,Malformed limbs,Missing legs,Too many fingers"
|
| 16 |
|
| 17 |
+
MAX_SEED = np.iinfo(np.int32).max
|
| 18 |
|
| 19 |
+
@spaces.GPU
|
| 20 |
+
def predict(prompt, ip_adapter_image, ip_adapter_scale=0.5, negative_prompt="", seed=100, randomize_seed=False, center_crop=False, width=1024, height=1024, guidance_scale=5.0, num_inference_steps=50, progress=gr.Progress(track_tqdm=True)):
|
| 21 |
+
if randomize_seed:
|
| 22 |
+
seed = random.randint(0, MAX_SEED)
|
|
|
|
| 23 |
|
| 24 |
+
if not center_crop:
|
| 25 |
+
ip_adapter_image.resize((224,224))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
generator = torch.Generator(device="cuda").manual_seed(seed)
|
| 28 |
+
pipe.to("cuda")
|
| 29 |
+
image_encoder.to("cuda")
|
| 30 |
+
pipe.image_encoder = image_encoder
|
| 31 |
+
pipe.set_ip_adapter_scale([ip_adapter_scale])
|
| 32 |
|
| 33 |
+
image = pipe(
|
| 34 |
+
prompt=prompt,
|
| 35 |
+
ip_adapter_image=[ip_adapter_image],
|
| 36 |
+
negative_prompt=negative_prompt,
|
| 37 |
+
height=height,
|
| 38 |
+
width=width,
|
| 39 |
+
num_inference_steps=num_inference_steps,
|
| 40 |
+
guidance_scale=guidance_scale,
|
| 41 |
+
num_images_per_prompt=1,
|
| 42 |
+
generator=generator,
|
| 43 |
+
).images[0]
|
| 44 |
+
|
| 45 |
+
return image, seed
|
| 46 |
+
|
| 47 |
+
examples = [
|
| 48 |
+
["A dog", "minta.jpeg", 0.4],
|
| 49 |
+
["A capybara", "king-min.png", 0.5],
|
| 50 |
+
["A cat", "blue_hair.png", 0.5],
|
| 51 |
+
["", "meow.jpeg", 1.0],
|
| 52 |
+
]
|
| 53 |
+
|
| 54 |
+
css="""
|
| 55 |
+
#col-container {
|
| 56 |
+
margin: 0 auto;
|
| 57 |
+
max-width: 720px;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
}
|
| 59 |
+
#result img{
|
| 60 |
+
object-position: top;
|
| 61 |
+
}
|
| 62 |
+
#result .image-container{
|
| 63 |
+
height: 100%
|
| 64 |
+
}
|
| 65 |
+
"""
|
| 66 |
+
with gr.Blocks(css=css) as demo:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
with gr.Column(elem_id="col-container"):
|
| 68 |
+
gr.Markdown(f"""
|
| 69 |
+
# Kolors IP-Adapter - image reference and variations
|
| 70 |
+
""")
|
| 71 |
+
|
| 72 |
+
with gr.Row():
|
| 73 |
+
prompt = gr.Text(
|
| 74 |
+
label="Prompt",
|
| 75 |
+
show_label=False,
|
| 76 |
+
max_lines=1,
|
| 77 |
+
placeholder="Enter your prompt",
|
| 78 |
+
container=False,
|
| 79 |
+
)
|
| 80 |
+
run_button = gr.Button("Run", scale=0)
|
| 81 |
+
|
| 82 |
+
with gr.Row():
|
| 83 |
+
with gr.Column():
|
| 84 |
+
ip_adapter_image = gr.Image(label="IP-Adapter Image", type="pil")
|
| 85 |
+
ip_adapter_scale = gr.Slider(
|
| 86 |
+
label="Image Input Scale",
|
| 87 |
+
info="Use 1 for creating image variations",
|
| 88 |
+
minimum=0.0,
|
| 89 |
+
maximum=1.0,
|
| 90 |
+
step=0.05,
|
| 91 |
+
value=0.5,
|
| 92 |
+
)
|
| 93 |
+
result = gr.Image(label="Result", elem_id="result")
|
| 94 |
+
|
| 95 |
+
with gr.Accordion("Advanced Settings", open=False):
|
| 96 |
+
negative_prompt = gr.Text(
|
| 97 |
+
label="Negative prompt",
|
| 98 |
+
max_lines=1,
|
| 99 |
+
placeholder="Enter a negative prompt",
|
| 100 |
+
)
|
| 101 |
+
seed = gr.Slider(
|
| 102 |
+
label="Seed",
|
| 103 |
+
minimum=0,
|
| 104 |
+
maximum=MAX_SEED,
|
| 105 |
+
step=1,
|
| 106 |
+
value=0,
|
| 107 |
+
)
|
| 108 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
| 109 |
+
center_crop = gr.Checkbox(label="Center Crop image", value=False, info="If not checked, the IP-Adapter image input would be resized to a square.")
|
| 110 |
+
with gr.Row():
|
| 111 |
+
width = gr.Slider(
|
| 112 |
+
label="Width",
|
| 113 |
+
minimum=256,
|
| 114 |
+
maximum=2048,
|
| 115 |
+
step=32,
|
| 116 |
+
value=1024,
|
| 117 |
+
)
|
| 118 |
+
height = gr.Slider(
|
| 119 |
+
label="Height",
|
| 120 |
+
minimum=256,
|
| 121 |
+
maximum=2048,
|
| 122 |
+
step=32,
|
| 123 |
+
value=1024,
|
| 124 |
+
)
|
| 125 |
+
with gr.Row():
|
| 126 |
+
guidance_scale = gr.Slider(
|
| 127 |
+
label="Guidance scale",
|
| 128 |
+
minimum=0.0,
|
| 129 |
+
maximum=10.0,
|
| 130 |
+
step=0.1,
|
| 131 |
+
value=5.0,
|
| 132 |
+
)
|
| 133 |
+
num_inference_steps = gr.Slider(
|
| 134 |
+
label="Number of inference steps",
|
| 135 |
+
minimum=1,
|
| 136 |
+
maximum=100,
|
| 137 |
+
step=1,
|
| 138 |
+
value=25,
|
| 139 |
+
)
|
| 140 |
|
| 141 |
+
|
| 142 |
+
gr.Examples(
|
| 143 |
+
examples=examples,
|
| 144 |
+
fn=predict,
|
| 145 |
+
inputs=[prompt, ip_adapter_image, ip_adapter_scale],
|
| 146 |
+
outputs=[result, seed],
|
| 147 |
+
cache_examples="lazy"
|
| 148 |
+
)
|
| 149 |
+
|
| 150 |
+
gr.on(
|
| 151 |
+
triggers=[run_button.click, prompt.submit],
|
| 152 |
+
fn=predict,
|
| 153 |
+
inputs=[prompt, ip_adapter_image, ip_adapter_scale, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
|
| 154 |
+
outputs=[result, seed]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
)
|
| 156 |
|
| 157 |
+
demo.queue(max_size=25,api_open=False).launch(show_api=False)
|
| 158 |
+
|
| 159 |
+
# image_blocks.queue(max_size=25,api_open=False).launch(show_api=False)
|