update the layout to chat style
Browse files
app.py
CHANGED
@@ -39,23 +39,33 @@ be to the input. This pipeline requires a value of at least `1`. It's possible y
|
|
39 |
def previous(image):
|
40 |
return image
|
41 |
|
42 |
-
def
|
|
|
|
|
|
|
43 |
progress(0, desc="Starting...")
|
44 |
-
if prompt.lower() == 'reverse' : #--to add revert functionality later
|
45 |
history = history or []
|
46 |
temp_img_name = img_name[:-4]+str(int(time.time()))+'.png'
|
47 |
image_oneup.save(temp_img_name)
|
48 |
response = 'Reverted to the last image ' + '<img src="/file=' + temp_img_name + '">'
|
49 |
history.append((prompt, response))
|
50 |
return history, history, image_oneup, temp_img_name, counter_out
|
51 |
-
if prompt.lower() == 'restart' : #--to add revert functionality later
|
52 |
history = history or []
|
53 |
temp_img_name = img_name[:-4]+str(int(time.time()))+'.png'
|
|
|
|
|
|
|
|
|
|
|
54 |
image_in.save(temp_img_name)
|
55 |
response = 'Reverted to the last image ' + '<img src="/file=' + temp_img_name + '">'
|
56 |
history.append((prompt, response))
|
57 |
return history, history, image_in, temp_img_name, counter_out
|
58 |
-
|
|
|
|
|
59 |
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]
|
60 |
if os.path.exists(img_name):
|
61 |
os.remove(img_name)
|
@@ -65,40 +75,54 @@ def chat(image_in, in_steps, in_guidance_scale, in_img_guidance_scale, image_hid
|
|
65 |
# Save the image to the file-like object
|
66 |
edited_image.save(fp)
|
67 |
#Get the name of the saved image
|
68 |
-
|
69 |
#edited_image.save(temp_img_name) #, overwrite=True)
|
|
|
|
|
|
|
70 |
counter_out += 1
|
71 |
-
|
|
|
72 |
seed = random.randint(0, 1000000)
|
73 |
img_name = f"./edited_image_{seed}.png"
|
74 |
-
#
|
75 |
-
|
|
|
|
|
76 |
wpercent = (basewidth/float(image_in.size[0]))
|
77 |
hsize = int((float(image_in.size[1])*float(wpercent)))
|
78 |
image_in = image_in.resize((basewidth,hsize), Image.Resampling.LANCZOS)
|
79 |
-
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]
|
80 |
if os.path.exists(img_name):
|
81 |
os.remove(img_name)
|
82 |
with open(img_name, "wb") as fp:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
# Save the image to the file-like object
|
84 |
edited_image.save(fp)
|
85 |
#Get the name of the saved image
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
history.append((prompt, response))
|
93 |
-
return history, history, edited_image, temp_img_name, counter_out
|
94 |
-
else:
|
95 |
-
response = random.choice(add_text_list) + '<img src="/file=' + saved_image_name2 + '">' #IMG_NAME
|
96 |
-
history.append((prompt, response))
|
97 |
-
counter_out += 1
|
98 |
-
return history, history, edited_image, img_name, counter_out
|
99 |
-
|
100 |
|
101 |
|
|
|
102 |
with gr.Blocks() as demo:
|
103 |
gr.Markdown("""<h1><center> Chat Interface with InstructPix2Pix: Give Image Editing Instructions</h1></center>
|
104 |
<p>For faster inference without waiting in the queue, you may duplicate the space and upgrade to GPU in settings.<br/>
|
@@ -117,15 +141,14 @@ with gr.Blocks() as demo:
|
|
117 |
3. Now you can load larger images (~5 mb) as well
|
118 |
|
119 |
<p/>""")
|
120 |
-
with gr.
|
121 |
-
|
122 |
-
image_in = gr.Image(type='pil', label="Original Image")
|
123 |
-
text_in = gr.Textbox()
|
124 |
state_in = gr.State()
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
with gr.Accordion("Advance settings for Training and Inference", open=False):
|
|
|
129 |
gr.Markdown("Advance settings for - Number of Inference steps, Guidanace scale, and Image guidance scale.")
|
130 |
in_steps = gr.Number(label="Enter the number of Inference steps", value = 20)
|
131 |
in_guidance_scale = gr.Slider(1,10, step=0.5, label="Set Guidance scale", value=7.5)
|
@@ -133,12 +156,13 @@ with gr.Blocks() as demo:
|
|
133 |
image_hid = gr.Image(type='pil', visible=False)
|
134 |
image_oneup = gr.Image(type='pil', visible=False)
|
135 |
img_name_temp_out = gr.Textbox(visible=False)
|
136 |
-
#img_revert = gr.Checkbox(visible=True, value=False,label=to track a revert message)
|
137 |
counter_out = gr.Number(visible=False, value=0, precision=0)
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
|
|
|
|
142 |
gr.Markdown(help_text)
|
143 |
|
144 |
demo.queue(concurrency_count=10)
|
|
|
39 |
def previous(image):
|
40 |
return image
|
41 |
|
42 |
+
def upload_image(file):
|
43 |
+
return Image.open(file)
|
44 |
+
|
45 |
+
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)):
|
46 |
progress(0, desc="Starting...")
|
47 |
+
if prompt != '' and prompt.lower() == 'reverse' : #--to add revert functionality later
|
48 |
history = history or []
|
49 |
temp_img_name = img_name[:-4]+str(int(time.time()))+'.png'
|
50 |
image_oneup.save(temp_img_name)
|
51 |
response = 'Reverted to the last image ' + '<img src="/file=' + temp_img_name + '">'
|
52 |
history.append((prompt, response))
|
53 |
return history, history, image_oneup, temp_img_name, counter_out
|
54 |
+
if prompt != '' and prompt.lower() == 'restart' : #--to add revert functionality later
|
55 |
history = history or []
|
56 |
temp_img_name = img_name[:-4]+str(int(time.time()))+'.png'
|
57 |
+
#Resizing the image
|
58 |
+
basewidth = 512
|
59 |
+
wpercent = (basewidth/float(image_in.size[0]))
|
60 |
+
hsize = int((float(image_in.size[1])*float(wpercent)))
|
61 |
+
image_in = image_in.resize((basewidth,hsize), Image.Resampling.LANCZOS)
|
62 |
image_in.save(temp_img_name)
|
63 |
response = 'Reverted to the last image ' + '<img src="/file=' + temp_img_name + '">'
|
64 |
history.append((prompt, response))
|
65 |
return history, history, image_in, temp_img_name, counter_out
|
66 |
+
#adding supportive sample text
|
67 |
+
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?"]
|
68 |
+
if counter_out > 1:
|
69 |
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]
|
70 |
if os.path.exists(img_name):
|
71 |
os.remove(img_name)
|
|
|
75 |
# Save the image to the file-like object
|
76 |
edited_image.save(fp)
|
77 |
#Get the name of the saved image
|
78 |
+
saved_image_name2 = fp.name
|
79 |
#edited_image.save(temp_img_name) #, overwrite=True)
|
80 |
+
history = history or []
|
81 |
+
response = random.choice(add_text_list) + '<img src="/file=' + saved_image_name2 + '">'
|
82 |
+
history.append((prompt, response))
|
83 |
counter_out += 1
|
84 |
+
return history, history, edited_image, temp_img_name, counter_out
|
85 |
+
elif counter_out == 0:
|
86 |
seed = random.randint(0, 1000000)
|
87 |
img_name = f"./edited_image_{seed}.png"
|
88 |
+
#convert file object to image
|
89 |
+
image_in = Image.open(btn_upload)
|
90 |
+
#Resizing the image
|
91 |
+
basewidth = 512
|
92 |
wpercent = (basewidth/float(image_in.size[0]))
|
93 |
hsize = int((float(image_in.size[1])*float(wpercent)))
|
94 |
image_in = image_in.resize((basewidth,hsize), Image.Resampling.LANCZOS)
|
|
|
95 |
if os.path.exists(img_name):
|
96 |
os.remove(img_name)
|
97 |
with open(img_name, "wb") as fp:
|
98 |
+
# Save the image to the file-like object
|
99 |
+
image_in.save(fp)
|
100 |
+
#Get the name of the saved image
|
101 |
+
saved_image_name0 = fp.name
|
102 |
+
history = history or []
|
103 |
+
response = '<img src="/file=' + img_name + '">' #IMG_NAME
|
104 |
+
history.append((prompt, response))
|
105 |
+
counter_out += 1
|
106 |
+
return history, history, image_in, img_name, counter_out
|
107 |
+
elif counter_out == 1:
|
108 |
+
#instruct-pix2pix inference
|
109 |
+
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]
|
110 |
+
if os.path.exists(img_name):
|
111 |
+
os.remove(img_name)
|
112 |
+
temp_img_name = img_name[:-4]+str(int(time.time()))+'.png'
|
113 |
+
with open(temp_img_name, "wb") as fp:
|
114 |
# Save the image to the file-like object
|
115 |
edited_image.save(fp)
|
116 |
#Get the name of the saved image
|
117 |
+
saved_image_name1 = fp.name
|
118 |
+
history = history or []
|
119 |
+
response = random.choice(add_text_list) + '<img src="/file=' + saved_image_name1 + '">' #IMG_NAME
|
120 |
+
history.append((prompt, response))
|
121 |
+
counter_out += 1
|
122 |
+
return history, history, edited_image, temp_img_name, counter_out
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
123 |
|
124 |
|
125 |
+
#Blocks layout
|
126 |
with gr.Blocks() as demo:
|
127 |
gr.Markdown("""<h1><center> Chat Interface with InstructPix2Pix: Give Image Editing Instructions</h1></center>
|
128 |
<p>For faster inference without waiting in the queue, you may duplicate the space and upgrade to GPU in settings.<br/>
|
|
|
141 |
3. Now you can load larger images (~5 mb) as well
|
142 |
|
143 |
<p/>""")
|
144 |
+
with gr.Column():
|
145 |
+
chatbot = gr.Chatbot()
|
|
|
|
|
146 |
state_in = gr.State()
|
147 |
+
with gr.Row():
|
148 |
+
text_in = gr.Textbox(value='', Plaseholder="Enter your instructions here")
|
149 |
+
btn_upload = gr.UploadButton("Upload image", file_types=["image"], file_count="single")
|
150 |
with gr.Accordion("Advance settings for Training and Inference", open=False):
|
151 |
+
image_in = gr.Image(visible=False,type='pil', label="Original Image")
|
152 |
gr.Markdown("Advance settings for - Number of Inference steps, Guidanace scale, and Image guidance scale.")
|
153 |
in_steps = gr.Number(label="Enter the number of Inference steps", value = 20)
|
154 |
in_guidance_scale = gr.Slider(1,10, step=0.5, label="Set Guidance scale", value=7.5)
|
|
|
156 |
image_hid = gr.Image(type='pil', visible=False)
|
157 |
image_oneup = gr.Image(type='pil', visible=False)
|
158 |
img_name_temp_out = gr.Textbox(visible=False)
|
|
|
159 |
counter_out = gr.Number(visible=False, value=0, precision=0)
|
160 |
+
|
161 |
+
btn_upload.upload(chat,
|
162 |
+
[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],
|
163 |
+
[chatbot, state_in, image_in, img_name_temp_out, counter_out])
|
164 |
+
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])
|
165 |
+
text_in.submit(previous, [image_hid], [image_oneup])
|
166 |
gr.Markdown(help_text)
|
167 |
|
168 |
demo.queue(concurrency_count=10)
|