update layout, bug fixes
Browse files
app.py
CHANGED
@@ -17,7 +17,6 @@ pipe.enable_xformers_memory_efficient_attention()
|
|
17 |
pipe.unet.to(memory_format=torch.channels_last)
|
18 |
|
19 |
|
20 |
-
|
21 |
help_text = """
|
22 |
**Note: Please be advised that a safety checker has been implemented in this public space.
|
23 |
Any attempts to generate inappropriate or NSFW images will result in the display of a black screen
|
@@ -88,6 +87,12 @@ def previous(image):
|
|
88 |
|
89 |
def upload_image(file):
|
90 |
return Image.open(file)
|
|
|
|
|
|
|
|
|
|
|
|
|
91 |
|
92 |
def chat(btn_upload, image_in, in_steps, in_guidance_scale, in_img_guidance_scale, image_hid, img_name, counter_out, image_oneup, prompt, history, progress=gr.Progress(track_tqdm=True)):
|
93 |
progress(0, desc="Starting...")
|
@@ -112,51 +117,41 @@ def chat(btn_upload, image_in, in_steps, in_guidance_scale, in_img_guidance_scal
|
|
112 |
return history, history, image_in, temp_img_name, counter_out
|
113 |
#adding supportive sample text
|
114 |
add_text_list = ["There you go", "Enjoy your image!", "Nice work! Wonder what you gonna do next!", "Way to go!", "Does this work for you?", "Something like this?"]
|
115 |
-
if counter_out
|
116 |
-
|
117 |
-
|
118 |
-
os.remove(img_name)
|
119 |
-
temp_img_name = img_name[:-4]+str(int(time.time()))[-4:]+'.png'
|
120 |
-
# Create a file-like object
|
121 |
-
with open(temp_img_name, "wb") as fp:
|
122 |
-
# Save the image to the file-like object
|
123 |
-
edited_image.save(fp)
|
124 |
-
#Get the name of the saved image
|
125 |
-
saved_image_name2 = fp.name
|
126 |
-
#edited_image.save(temp_img_name) #, overwrite=True)
|
127 |
-
history = history or []
|
128 |
-
response = random.choice(add_text_list) + '<img src="/file=' + saved_image_name2 + '">'
|
129 |
-
history.append((prompt, response))
|
130 |
-
counter_out += 1
|
131 |
-
return history, history, edited_image, temp_img_name, counter_out
|
132 |
-
elif counter_out == 0:
|
133 |
-
seed = random.randint(0, 1000000)
|
134 |
-
img_name = f"./edited_image_{seed}.png"
|
135 |
#convert file object to image
|
136 |
image_in = Image.open(btn_upload)
|
|
|
137 |
#Resizing the image
|
138 |
basewidth = 512
|
139 |
wpercent = (basewidth/float(image_in.size[0]))
|
140 |
hsize = int((float(image_in.size[1])*float(wpercent)))
|
141 |
image_in = image_in.resize((basewidth,hsize), Image.Resampling.LANCZOS)
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
history = history or []
|
150 |
-
response = '<img src="/file=' + img_name + '">'
|
151 |
history.append((prompt, response))
|
152 |
counter_out += 1
|
|
|
|
|
|
|
|
|
|
|
153 |
return history, history, image_in, img_name, counter_out
|
|
|
154 |
elif counter_out == 1:
|
155 |
#instruct-pix2pix inference
|
156 |
edited_image = pipe(prompt, image=image_in, num_inference_steps=int(in_steps), guidance_scale=float(in_guidance_scale), image_guidance_scale=float(in_img_guidance_scale)).images[0]
|
157 |
if os.path.exists(img_name):
|
158 |
os.remove(img_name)
|
159 |
-
temp_img_name = img_name[:-4]+str(int(time.time()))[-4:]+'.png'
|
160 |
with open(temp_img_name, "wb") as fp:
|
161 |
# Save the image to the file-like object
|
162 |
edited_image.save(fp)
|
@@ -167,6 +162,24 @@ def chat(btn_upload, image_in, in_steps, in_guidance_scale, in_img_guidance_scal
|
|
167 |
history.append((prompt, response))
|
168 |
counter_out += 1
|
169 |
return history, history, edited_image, temp_img_name, counter_out
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
170 |
|
171 |
|
172 |
#Blocks layout
|
@@ -191,12 +204,6 @@ with gr.Blocks(css="style.css") as demo:
|
|
191 |
Based on <a href="https://huggingface.co/timbrooks/instruct-pix2pix" target="_blank">Diffusers implementation</a> of InstructPix2Pix.
|
192 |
</p>
|
193 |
</div>""")
|
194 |
-
#gr.Markdown("""<h1><center>dummy</h1></center> """)
|
195 |
-
chatbot = gr.Chatbot()
|
196 |
-
state_in = gr.State()
|
197 |
-
with gr.Row():
|
198 |
-
text_in = gr.Textbox(value='', placeholder="Type your instructions here and press Enter", label="Type and press Enter")
|
199 |
-
btn_upload = gr.UploadButton("Upload image", file_types=["image"], file_count="single")
|
200 |
with gr.Accordion("Advance settings for Training and Inference", open=False):
|
201 |
image_in = gr.Image(visible=False,type='pil', label="Original Image")
|
202 |
gr.Markdown("Advance settings for - Number of Inference steps, Guidanace scale, and Image guidance scale.")
|
@@ -207,13 +214,28 @@ with gr.Blocks(css="style.css") as demo:
|
|
207 |
image_oneup = gr.Image(type='pil', visible=False)
|
208 |
img_name_temp_out = gr.Textbox(visible=False)
|
209 |
counter_out = gr.Number(visible=False, value=0, precision=0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
210 |
|
|
|
211 |
btn_upload.upload(chat,
|
212 |
[btn_upload, image_in, in_steps, in_guidance_scale, in_img_guidance_scale, image_hid, img_name_temp_out,counter_out, image_oneup, text_in, state_in],
|
213 |
[chatbot, state_in, image_in, img_name_temp_out, counter_out])
|
|
|
|
|
214 |
text_in.submit(chat,[btn_upload, image_in, in_steps, in_guidance_scale, in_img_guidance_scale, image_hid, img_name_temp_out,counter_out, image_oneup, text_in, state_in], [chatbot, state_in, image_hid, img_name_temp_out, counter_out])
|
215 |
text_in.submit(previous, [image_hid], [image_oneup])
|
216 |
-
|
|
|
|
|
|
|
|
|
|
|
217 |
|
218 |
demo.queue(concurrency_count=10)
|
219 |
demo.launch(debug=True, width="80%", height=2000)
|
|
|
17 |
pipe.unet.to(memory_format=torch.channels_last)
|
18 |
|
19 |
|
|
|
20 |
help_text = """
|
21 |
**Note: Please be advised that a safety checker has been implemented in this public space.
|
22 |
Any attempts to generate inappropriate or NSFW images will result in the display of a black screen
|
|
|
87 |
|
88 |
def upload_image(file):
|
89 |
return Image.open(file)
|
90 |
+
|
91 |
+
def upload_button_config():
|
92 |
+
return gr.update(visible=False)
|
93 |
+
|
94 |
+
def upload_textbox_config(text_in):
|
95 |
+
return gr.update(visible=True)
|
96 |
|
97 |
def chat(btn_upload, image_in, in_steps, in_guidance_scale, in_img_guidance_scale, image_hid, img_name, counter_out, image_oneup, prompt, history, progress=gr.Progress(track_tqdm=True)):
|
98 |
progress(0, desc="Starting...")
|
|
|
117 |
return history, history, image_in, temp_img_name, counter_out
|
118 |
#adding supportive sample text
|
119 |
add_text_list = ["There you go", "Enjoy your image!", "Nice work! Wonder what you gonna do next!", "Way to go!", "Does this work for you?", "Something like this?"]
|
120 |
+
if counter_out == 0:
|
121 |
+
t1 = time.time()
|
122 |
+
print(f"Time at start = {t1}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
123 |
#convert file object to image
|
124 |
image_in = Image.open(btn_upload)
|
125 |
+
|
126 |
#Resizing the image
|
127 |
basewidth = 512
|
128 |
wpercent = (basewidth/float(image_in.size[0]))
|
129 |
hsize = int((float(image_in.size[1])*float(wpercent)))
|
130 |
image_in = image_in.resize((basewidth,hsize), Image.Resampling.LANCZOS)
|
131 |
+
|
132 |
+
# Save the image to the file-like object
|
133 |
+
seed = random.randint(0, 1000000)
|
134 |
+
img_name = f"./edited_image_{seed}.png"
|
135 |
+
image_in.save(img_name)
|
136 |
+
|
137 |
+
#add state
|
138 |
history = history or []
|
139 |
+
response = '<img src="/file=' + img_name + '">'
|
140 |
history.append((prompt, response))
|
141 |
counter_out += 1
|
142 |
+
|
143 |
+
t2 = time.time()
|
144 |
+
print(f"Time at end = {t2}")
|
145 |
+
time_diff = t2-t1
|
146 |
+
print(f"Time taken = {time_diff}")
|
147 |
return history, history, image_in, img_name, counter_out
|
148 |
+
|
149 |
elif counter_out == 1:
|
150 |
#instruct-pix2pix inference
|
151 |
edited_image = pipe(prompt, image=image_in, num_inference_steps=int(in_steps), guidance_scale=float(in_guidance_scale), image_guidance_scale=float(in_img_guidance_scale)).images[0]
|
152 |
if os.path.exists(img_name):
|
153 |
os.remove(img_name)
|
154 |
+
temp_img_name = img_name[:-4]+str(int(time.time()))[-4:] +'.png'
|
155 |
with open(temp_img_name, "wb") as fp:
|
156 |
# Save the image to the file-like object
|
157 |
edited_image.save(fp)
|
|
|
162 |
history.append((prompt, response))
|
163 |
counter_out += 1
|
164 |
return history, history, edited_image, temp_img_name, counter_out
|
165 |
+
|
166 |
+
elif counter_out > 1:
|
167 |
+
edited_image = pipe(prompt, image=image_hid, num_inference_steps=int(in_steps), guidance_scale=float(in_guidance_scale), image_guidance_scale=float(in_img_guidance_scale)).images[0]
|
168 |
+
if os.path.exists(img_name):
|
169 |
+
os.remove(img_name)
|
170 |
+
temp_img_name = img_name[:-4]+str(int(time.time()))[-4:]+'.png'
|
171 |
+
# Create a file-like object
|
172 |
+
with open(temp_img_name, "wb") as fp:
|
173 |
+
# Save the image to the file-like object
|
174 |
+
edited_image.save(fp)
|
175 |
+
#Get the name of the saved image
|
176 |
+
saved_image_name2 = fp.name
|
177 |
+
#edited_image.save(temp_img_name) #, overwrite=True)
|
178 |
+
history = history or []
|
179 |
+
response = random.choice(add_text_list) + '<img src="/file=' + saved_image_name2 + '">'
|
180 |
+
history.append((prompt, response))
|
181 |
+
counter_out += 1
|
182 |
+
return history, history, edited_image, temp_img_name, counter_out
|
183 |
|
184 |
|
185 |
#Blocks layout
|
|
|
204 |
Based on <a href="https://huggingface.co/timbrooks/instruct-pix2pix" target="_blank">Diffusers implementation</a> of InstructPix2Pix.
|
205 |
</p>
|
206 |
</div>""")
|
|
|
|
|
|
|
|
|
|
|
|
|
207 |
with gr.Accordion("Advance settings for Training and Inference", open=False):
|
208 |
image_in = gr.Image(visible=False,type='pil', label="Original Image")
|
209 |
gr.Markdown("Advance settings for - Number of Inference steps, Guidanace scale, and Image guidance scale.")
|
|
|
214 |
image_oneup = gr.Image(type='pil', visible=False)
|
215 |
img_name_temp_out = gr.Textbox(visible=False)
|
216 |
counter_out = gr.Number(visible=False, value=0, precision=0)
|
217 |
+
|
218 |
+
#with gr.Row():
|
219 |
+
text_in = gr.Textbox(value='', Placeholder="Type your instructions here and press enter", elem_id = "input_prompt", visible=False, label='Great! Now you can edit your image with Instructions')
|
220 |
+
btn_upload = gr.UploadButton("Upload image", file_types=["image"], file_count="single", elem_id="upload_button")
|
221 |
+
|
222 |
+
chatbot = gr.Chatbot(elem_id = 'chatbot-component')
|
223 |
+
state_in = gr.State()
|
224 |
|
225 |
+
#Using Event Listeners
|
226 |
btn_upload.upload(chat,
|
227 |
[btn_upload, image_in, in_steps, in_guidance_scale, in_img_guidance_scale, image_hid, img_name_temp_out,counter_out, image_oneup, text_in, state_in],
|
228 |
[chatbot, state_in, image_in, img_name_temp_out, counter_out])
|
229 |
+
btn_upload.upload(fn = upload_textbox_config, inputs=text_in, outputs = text_in)
|
230 |
+
|
231 |
text_in.submit(chat,[btn_upload, image_in, in_steps, in_guidance_scale, in_img_guidance_scale, image_hid, img_name_temp_out,counter_out, image_oneup, text_in, state_in], [chatbot, state_in, image_hid, img_name_temp_out, counter_out])
|
232 |
text_in.submit(previous, [image_hid], [image_oneup])
|
233 |
+
|
234 |
+
chatbot.change(fn = upload_button_config, outputs=btn_upload) #, scroll_to_output = True)
|
235 |
+
text_in.submit(None, [], [], _js = "() => document.getElementById('#chatbot-component').scrollTop = document.getElementById('#chatbot-component').scrollHeight")
|
236 |
+
|
237 |
+
with gr.Accordion("Release Notes", open=False):
|
238 |
+
gr.Markdown(help_text)
|
239 |
|
240 |
demo.queue(concurrency_count=10)
|
241 |
demo.launch(debug=True, width="80%", height=2000)
|