animrods commited on
Commit
e525714
1 Parent(s): d174eab

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +120 -46
app.py CHANGED
@@ -4,15 +4,17 @@ import numpy as np
4
  import diffusers
5
  import os
6
  import random
 
7
  from PIL import Image
8
-
9
  hf_token = os.environ.get("HF_TOKEN")
10
  from diffusers import AutoPipelineForText2Image
11
 
12
- device = "cuda" # if torch.cuda.is_available() else "cpu"
 
13
  pipe = AutoPipelineForText2Image.from_pretrained("briaai/BRIA-2.3", torch_dtype=torch.float16, force_zeros_for_empty_prompt=False).to(device)
14
  pipe.load_ip_adapter("briaai/Image-Prompt", subfolder='models', weight_name="ip_adapter_bria.bin")
15
  pipe.to(device)
 
16
 
17
  MAX_SEED = np.iinfo(np.int32).max
18
 
@@ -20,19 +22,17 @@ MAX_SEED = np.iinfo(np.int32).max
20
  def predict(prompt, ip_adapter_images, 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
  # Optionally resize images if center crop is not selected
25
  if not center_crop:
26
  ip_adapter_images = [image.resize((224, 224)) for image in ip_adapter_images]
27
 
28
- # Create a generator for reproducible random seed
29
  generator = torch.Generator(device="cuda").manual_seed(seed)
30
  pipe.set_ip_adapter_scale([ip_adapter_scale])
31
 
32
- # Pass all images at once to the pipe
33
- result_images = pipe(
34
  prompt=prompt,
35
- ip_adapter_image=ip_adapter_images, # Pass the list of images
36
  negative_prompt=negative_prompt,
37
  height=height,
38
  width=width,
@@ -40,61 +40,135 @@ def predict(prompt, ip_adapter_images, ip_adapter_scale=0.5, negative_prompt="",
40
  guidance_scale=guidance_scale,
41
  num_images_per_prompt=1,
42
  generator=generator,
43
- ).images
44
 
45
- return result_images, seed
46
 
47
  examples = [
48
- ["high quality", ["example1.png", "example2.png"], 1.0, "", 1000, False, False, 1152, 896],
 
49
  ]
50
 
51
- css = """
52
  #col-container {
53
- display: flex;
54
- flex-direction: column;
55
- align-items: center;
56
- padding: 10px;
 
 
 
 
57
  }
58
  """
59
-
60
  with gr.Blocks(css=css) as demo:
61
- with gr.Column():
 
 
 
 
62
  with gr.Row():
63
- prompt = gr.Textbox(label="Prompt", lines=1)
64
- ip_adapter_images = gr.Gallery(label="Input Images", elem_id="image-gallery").style(grid=[2], preview=True)
65
-
66
- ip_adapter_scale = gr.Slider(label="IP Adapter Scale", minimum=0.0, maximum=1.0, step=0.1, value=0.5)
67
- negative_prompt = gr.Textbox(label="Negative Prompt", placeholder="Optional", lines=1)
68
-
69
- with gr.Row():
70
- seed = gr.Number(label="Seed", value=100)
71
- randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
72
- center_crop = gr.Checkbox(label="Center Crop Image", value=False, info="If not checked, the images will be resized.")
73
-
 
 
 
 
 
 
 
 
 
 
74
  with gr.Row():
75
- guidance_scale = gr.Slider(
76
- label="Guidance Scale", minimum=0.0, maximum=10.0, step=0.1, value=7.0
 
 
 
 
77
  )
78
- num_inference_steps = gr.Slider(
79
- label="Number of Inference Steps", minimum=1, maximum=100, step=1, value=25
 
 
 
 
80
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81
 
82
- result = gr.Gallery(label="Generated Images").style(grid=[2], preview=True)
83
-
84
- run_button = gr.Button("Run")
85
-
86
- run_button.click(
87
- predict,
 
 
 
 
 
88
  inputs=[prompt, ip_adapter_images, ip_adapter_scale, negative_prompt, seed, randomize_seed, center_crop, width, height, guidance_scale, num_inference_steps],
89
  outputs=[result, seed]
90
  )
91
 
92
- gr.Examples(
93
- examples=examples,
94
- fn=predict,
95
- inputs=[prompt, ip_adapter_images, ip_adapter_scale, negative_prompt, seed, randomize_seed, center_crop, width, height],
96
- outputs=[result, seed],
97
- cache_examples="lazy"
98
- )
99
 
100
- demo.queue(max_size=25, api_open=False).launch(show_api=False)
 
4
  import diffusers
5
  import os
6
  import random
7
+ import spaces
8
  from PIL import Image
 
9
  hf_token = os.environ.get("HF_TOKEN")
10
  from diffusers import AutoPipelineForText2Image
11
 
12
+
13
+ device = "cuda" #if torch.cuda.is_available() else "cpu"
14
  pipe = AutoPipelineForText2Image.from_pretrained("briaai/BRIA-2.3", torch_dtype=torch.float16, force_zeros_for_empty_prompt=False).to(device)
15
  pipe.load_ip_adapter("briaai/Image-Prompt", subfolder='models', weight_name="ip_adapter_bria.bin")
16
  pipe.to(device)
17
+ # 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"
18
 
19
  MAX_SEED = np.iinfo(np.int32).max
20
 
 
22
  def predict(prompt, ip_adapter_images, 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)):
23
  if randomize_seed:
24
  seed = random.randint(0, MAX_SEED)
25
+
26
  # Optionally resize images if center crop is not selected
27
  if not center_crop:
28
  ip_adapter_images = [image.resize((224, 224)) for image in ip_adapter_images]
29
 
 
30
  generator = torch.Generator(device="cuda").manual_seed(seed)
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,
 
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
+ ["high quality", "example1.png", 1.0, "", 1000, False, False, 1152, 896],
49
+ ["capybara", "example2.png", 0.7, "", 1000, False, False, 1152, 896],
50
  ]
51
 
52
+ css="""
53
  #col-container {
54
+ margin: 0 auto;
55
+ max-width: 1024px;
56
+ }
57
+ #result img{
58
+ object-position: top;
59
+ }
60
+ #result .image-container{
61
+ height: 100%
62
  }
63
  """
 
64
  with gr.Blocks(css=css) as demo:
65
+ with gr.Column(elem_id="col-container"):
66
+ gr.Markdown(f"""
67
+ # Bria's Image-Prompt-Adapter
68
+ """)
69
+
70
  with gr.Row():
71
+ with gr.Column():
72
+ ip_adapter_images = gr.Gallery(label="Input Images", elem_id="image-gallery").style(grid=[2], preview=True)
73
+ ip_adapter_scale = gr.Slider(
74
+ label="Image Input Scale",
75
+ info="Use 1 for creating image variations",
76
+ minimum=0.0,
77
+ maximum=1.0,
78
+ step=0.05,
79
+ value=1.0,
80
+ )
81
+ with gr.Column():
82
+ result = gr.Image(label="Result", elem_id="result", format="png")
83
+ prompt = gr.Text(
84
+ label="Prompt",
85
+ show_label=True,
86
+ lines=1,
87
+ placeholder="Enter your prompt",
88
+ container=True,
89
+ info='For image variation, leave empty or try a prompt like: "high quality".'
90
+ )
91
+
92
  with gr.Row():
93
+ width = gr.Slider(
94
+ label="Width",
95
+ minimum=256,
96
+ maximum=2048,
97
+ step=32,
98
+ value=1024,
99
  )
100
+ height = gr.Slider(
101
+ label="Height",
102
+ minimum=256,
103
+ maximum=2048,
104
+ step=32,
105
+ value=1024,
106
  )
107
+ run_button = gr.Button("Run", scale=0)
108
+
109
+
110
+ with gr.Accordion("Advanced Settings", open=False):
111
+ negative_prompt = gr.Text(
112
+ label="Negative prompt",
113
+ max_lines=1,
114
+ placeholder="Enter a negative prompt",
115
+ )
116
+ seed = gr.Slider(
117
+ label="Seed",
118
+ minimum=0,
119
+ maximum=MAX_SEED,
120
+ step=1,
121
+ value=1000,
122
+ )
123
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
124
+ 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.")
125
+ # with gr.Row():
126
+ # width = gr.Slider(
127
+ # label="Width",
128
+ # minimum=256,
129
+ # maximum=2048,
130
+ # step=32,
131
+ # value=1024,
132
+ # )
133
+ # height = gr.Slider(
134
+ # label="Height",
135
+ # minimum=256,
136
+ # maximum=2048,
137
+ # step=32,
138
+ # value=1024,
139
+ # )
140
+ with gr.Row():
141
+ guidance_scale = gr.Slider(
142
+ label="Guidance scale",
143
+ minimum=0.0,
144
+ maximum=10.0,
145
+ step=0.1,
146
+ value=7.0,
147
+ )
148
+ num_inference_steps = gr.Slider(
149
+ label="Number of inference steps",
150
+ minimum=1,
151
+ maximum=100,
152
+ step=1,
153
+ value=25,
154
+ )
155
+
156
 
157
+ # gr.Examples(
158
+ # examples=examples,
159
+ # fn=predict,
160
+ # inputs=[prompt, ip_adapter_images, ip_adapter_scale, negative_prompt, seed, randomize_seed, center_crop, width, height],
161
+ # outputs=[result, seed],
162
+ # cache_examples="lazy"
163
+ # )
164
+
165
+ gr.on(
166
+ triggers=[run_button.click, prompt.submit],
167
+ fn=predict,
168
  inputs=[prompt, ip_adapter_images, ip_adapter_scale, negative_prompt, seed, randomize_seed, center_crop, width, height, guidance_scale, num_inference_steps],
169
  outputs=[result, seed]
170
  )
171
 
172
+ demo.queue(max_size=25,api_open=False).launch(show_api=False)
 
 
 
 
 
 
173
 
174
+ # image_blocks.queue(max_size=25,api_open=False).launch(show_api=False)