animrods commited on
Commit
342f262
·
verified ·
1 Parent(s): e82a196

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +135 -131
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
- def read_content(file_path: str) -> str:
19
- """read the content of target file
20
- """
21
- with open(file_path, 'r', encoding='utf-8') as f:
22
- content = f.read()
23
 
24
- return content
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
- init_image = image['composite'].convert("RGB")
34
- # init_image = image.convert("RGB")
35
- # if center_crop is False:
36
- # init_image = init_image.resize((224, 224))
 
37
 
38
- generator = torch.Generator(device="cpu").manual_seed(int(seed))
39
-
40
- output = pipe(
41
- prompt=prompt,
42
- negative_prompt=negative_prompt,
43
- ip_adapter_image=init_image,
44
- num_inference_steps=int(steps),
45
- generator=generator,
46
- height=int(height), width=int(width),
47
- guidance_scale=guidance_scale
48
- )
49
-
50
- torch.cuda.empty_cache
51
- return output.images[0] #, gr.update(visible=True)
52
-
53
-
54
- css = '''
55
- .gradio-container{max-width: 1100px !important}
56
- #image_upload{min-height:400px}
57
- #image_upload [data-testid="image"], #image_upload [data-testid="image"] > div{min-height: 400px}
58
- #mask_radio .gr-form{background:transparent; border: none}
59
- #word_mask{margin-top: .75em !important}
60
- #word_mask textarea:disabled{opacity: 0.3}
61
- .footer {margin-bottom: 45px;margin-top: 35px;text-align: center;border-bottom: 1px solid #e5e5e5}
62
- .footer>p {font-size: .8rem; display: inline-block; padding: 0 10px;transform: translateY(10px);background: white}
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
- #share-btn-container {padding-left: 0.5rem !important; padding-right: 0.5rem !important; background-color: #000000; justify-content: center; align-items: center; border-radius: 9999px !important; max-width: 13rem; margin-left: auto;}
76
- div#share-btn-container > div {flex-direction: row;background: black;align-items: center}
77
- #share-btn-container:hover {background-color: #060606}
78
- #share-btn {all: initial; color: #ffffff;font-weight: 600; cursor:pointer; font-family: 'IBM Plex Sans', sans-serif; margin-left: 0.5rem !important; padding-top: 0.5rem !important; padding-bottom: 0.5rem !important;right:0;}
79
- #share-btn * {all: unset}
80
- #share-btn-container div:nth-child(-n+2){width: auto !important;min-height: 0px !important;}
81
- #share-btn-container .wrap {display: none !important}
82
- #share-btn-container.hidden {display: none!important}
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("## BRIA 2.3")
95
- gr.HTML('''
96
- <p style="margin-bottom: 10px; font-size: 94%">
97
- This is a demo for
98
- <a href="https://huggingface.co/briaai/BRIA-2.3" target="_blank">BRIA 2.3 text-to-image </a>.
99
- BRIA 2.3 improve the generation of humans and illustrations compared to BRIA 2.2 while still trained on licensed data, and so provide full legal liability coverage for copyright and privacy infringement.
100
- </p>
101
- ''')
102
- with gr.Row():
103
- with gr.Column():
104
- # image = gr.ImageEditor(elem_id="image_upload", type="pil", label="Upload", height=400)
105
- image = gr.ImageEditor(type='pil', image_mode="RGB", width=512, height=512, interactive=True, )
106
-
107
- # image = gr.Image(sources=None, type="pil") # None for upload, ctrl+v and webcam
108
-
109
-
110
- with gr.Row(elem_id="prompt-container", equal_height=True):
111
- with gr.Row():
112
- prompt = gr.Textbox(placeholder="Your prompt (you can leave it empty if you only want the image prompt as input)", show_label=False, elem_id="prompt")
113
- btn = gr.Button("Generate!", elem_id="run_button")
114
-
115
- with gr.Accordion(label="Settings", open=True):
116
- with gr.Row(equal_height=True):
117
- ip_adapter_scale = gr.Number(value=1.0, minimum=0.01, maximum=1.0, step=0.01, label="ip_adapter_scale")
118
- width = gr.Number(value=1024, minimum=300, maximum=2000, step=1, label="width")
119
- height = gr.Number(value=1024, minimum=300, maximum=2000, step=1, label="height")
120
-
121
- with gr.Accordion(label="Advanced Settings", open=False):
122
- with gr.Row(equal_height=True):
123
- guidance_scale = gr.Number(value=7, minimum=1.0, maximum=10.0, step=0.5, label="guidance_scale")
124
- steps = gr.Number(value=30, minimum=10, maximum=100, step=1, label="steps")
125
- seed = gr.Number(value=0, minimum=0, maximum=100000, step=1, label="seed")
126
- negative_prompt = gr.Textbox(label="negative_prompt", value=default_negative_prompt, placeholder=default_negative_prompt, info="what you don't want to see in the image")
127
- center_crop = gr.Checkbox(label="center_crop", info="If not checked, the image would be resized to square before it's fed to the model."),
128
-
129
-
130
- with gr.Column():
131
- image_out = gr.Image(label="Output", elem_id="output-img", height=400)
132
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
133
 
134
-
135
- btn.click(fn=predict, inputs=[image, prompt, negative_prompt, guidance_scale, steps, ip_adapter_scale, width, height, seed], outputs=[image_out], api_name='run')
136
- prompt.submit(fn=predict, inputs=[image, prompt, negative_prompt, guidance_scale, steps, ip_adapter_scale, width, height, seed], outputs=[image_out])
137
-
138
- # gr.Examples(
139
- # examples=[
140
- # ["./imgs/example.png"],
141
- # ],
142
- # fn=predict,
143
- # inputs=[image],
144
- # cache_examples=False,
145
- # )
146
- gr.HTML(
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
- image_blocks.queue(max_size=25,api_open=False).launch(show_api=False)
 
 
 
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)