ysharma HF staff commited on
Commit
3fa258b
·
1 Parent(s): 4e6741b

update revert option

Browse files
Files changed (1) hide show
  1. app.py +32 -5
app.py CHANGED
@@ -39,9 +39,23 @@ be to the input. This pipeline requires a value of at least `1`. It's possible y
39
  * Cropping the image so the face takes up a larger portion of the frame.
40
  """
41
 
42
- def chat(image_in, in_steps, in_guidance_scale, in_img_guidance_scale, image_hid, img_name, counter_out, prompt, history, progress=gr.Progress(track_tqdm=True)):
 
 
 
43
  progress(0, desc="Starting...")
44
- #if message == "revert": --to add revert functionality later
 
 
 
 
 
 
 
 
 
 
 
45
  if counter_out > 0:
46
  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]
47
  if os.path.exists(img_name):
@@ -58,11 +72,17 @@ def chat(image_in, in_steps, in_guidance_scale, in_img_guidance_scale, image_hid
58
  else:
59
  seed = random.randint(0, 1000000)
60
  img_name = f"./edited_image_{seed}.png"
61
- #Resizing to handle memory errors
62
  basewidth = 512
63
  wpercent = (basewidth/float(image_in.size[0]))
64
  hsize = int((float(image_in.size[1])*float(wpercent)))
65
  image_in = image_in.resize((basewidth,hsize), Image.Resampling.LANCZOS)
 
 
 
 
 
 
 
66
  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]
67
  if os.path.exists(img_name):
68
  os.remove(img_name)
@@ -84,6 +104,7 @@ def chat(image_in, in_steps, in_guidance_scale, in_img_guidance_scale, image_hid
84
  counter_out += 1
85
  return history, history, edited_image, img_name, counter_out
86
 
 
87
 
88
  with gr.Blocks() as demo:
89
  gr.Markdown("""<h1><center> Chat Interface with InstructPix2Pix: Give Image Editing Instructions</h1></center>
@@ -100,17 +121,23 @@ with gr.Blocks() as demo:
100
  image_in = gr.Image(type='pil', label="Original Image")
101
  text_in = gr.Textbox()
102
  state_in = gr.State()
 
103
  b1 = gr.Button('Edit the image!')
 
104
  with gr.Accordion("Advance settings for Training and Inference", open=False):
105
  gr.Markdown("Advance settings for - Number of Inference steps, Guidanace scale, and Image guidance scale.")
106
  in_steps = gr.Number(label="Enter the number of Inference steps", value = 20)
107
  in_guidance_scale = gr.Slider(1,10, step=0.5, label="Set Guidance scale", value=7.5)
108
  in_img_guidance_scale = gr.Slider(1,10, step=0.5, label="Set Image Guidance scale", value=1.5)
109
- image_hid = gr.Image(type='pil', visible=False)
 
110
  img_name_temp_out = gr.Textbox(visible=False)
 
111
  counter_out = gr.Number(visible=False, value=0, precision=0)
112
  chatbot = gr.Chatbot()
113
- b1.click(chat,[image_in, in_steps, in_guidance_scale, in_img_guidance_scale, image_hid, img_name_temp_out,counter_out, text_in, state_in], [chatbot, state_in, image_hid, img_name_temp_out, counter_out]) #, queue=True)
 
 
114
  gr.Markdown(help_text)
115
 
116
  demo.queue(concurrency_count=10)
 
39
  * Cropping the image so the face takes up a larger portion of the frame.
40
  """
41
 
42
+ def previous(image):
43
+ return image
44
+
45
+ def chat(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 == 'reverse' : #--to add revert functionality later
48
+ history = history or []
49
+ #Resizing (or not) the image for better display and adding supportive sample text
50
+ #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?"]
51
+ #if counter_out > 0:
52
+ temp_img_name = img_name[:-4]+str(int(time.time()))+'.png'
53
+ image_oneup.save(temp_img_name)
54
+ response = 'Reverted to the last image ' + '<img src="/file=' + temp_img_name + '">'
55
+ history.append((prompt, response))
56
+ return history, history, image_oneup, temp_img_name, counter_out
57
+ # Save the resized image
58
+ #img.save("resized_example.jpg", optimize=True, quality=95
59
  if counter_out > 0:
60
  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]
61
  if os.path.exists(img_name):
 
72
  else:
73
  seed = random.randint(0, 1000000)
74
  img_name = f"./edited_image_{seed}.png"
 
75
  basewidth = 512
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
+ # Get the original size of the image
80
+ #original_size = image_in.size
81
+ # Calculate the new size of the image
82
+ #new_size = (int(original_size[0]/2), int(original_size[1]/2))
83
+ # Resize the image
84
+ #img1 = img.resize(new_size)
85
+ #image_in = image_in.resize(new_size,Image.ANTIALIAS) # resample=Image.LANCZOS)
86
  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]
87
  if os.path.exists(img_name):
88
  os.remove(img_name)
 
104
  counter_out += 1
105
  return history, history, edited_image, img_name, counter_out
106
 
107
+
108
 
109
  with gr.Blocks() as demo:
110
  gr.Markdown("""<h1><center> Chat Interface with InstructPix2Pix: Give Image Editing Instructions</h1></center>
 
121
  image_in = gr.Image(type='pil', label="Original Image")
122
  text_in = gr.Textbox()
123
  state_in = gr.State()
124
+ #with gr.Row():
125
  b1 = gr.Button('Edit the image!')
126
+ #b2 = gr.Button('Revert!')
127
  with gr.Accordion("Advance settings for Training and Inference", open=False):
128
  gr.Markdown("Advance settings for - Number of Inference steps, Guidanace scale, and Image guidance scale.")
129
  in_steps = gr.Number(label="Enter the number of Inference steps", value = 20)
130
  in_guidance_scale = gr.Slider(1,10, step=0.5, label="Set Guidance scale", value=7.5)
131
  in_img_guidance_scale = gr.Slider(1,10, step=0.5, label="Set Image Guidance scale", value=1.5)
132
+ image_hid = gr.Image(type='pil', visible=True)
133
+ image_oneup = gr.Image(type='pil', visible=True)
134
  img_name_temp_out = gr.Textbox(visible=False)
135
+ #img_revert = gr.Checkbox(visible=True, value=False,label=to track a revert message)
136
  counter_out = gr.Number(visible=False, value=0, precision=0)
137
  chatbot = gr.Chatbot()
138
+ b1.click(chat,[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]) #, queue=True)
139
+ b1.click(previous, [image_hid], [image_oneup])
140
+ #b2.click(previous, image_oneup, image_hid)
141
  gr.Markdown(help_text)
142
 
143
  demo.queue(concurrency_count=10)