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

update restart feature

Browse files
Files changed (1) hide show
  1. app.py +17 -13
app.py CHANGED
@@ -44,18 +44,20 @@ def previous(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,17 +74,11 @@ def chat(image_in, in_steps, in_guidance_scale, in_img_guidance_scale, image_hid
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)
@@ -111,10 +107,18 @@ with gr.Blocks() as demo:
111
  <p>For faster inference without waiting in the queue, you may duplicate the space and upgrade to GPU in settings.<br/>
112
  <a href="https://huggingface.co/spaces/ysharma/InstructPix2Pix_Chatbot?duplicate=true">
113
  <img style="margin-top: 0em; margin-bottom: 0em" src="https://bit.ly/3gLdBN6" alt="Duplicate Space"></a>
 
114
  **Note: Please be advised that a safety checker has been implemented in this public space.
115
  Any attempts to generate inappropriate or NSFW images will result in the display of a black screen
116
  as a precautionary measure for the protection of all users. We appreciate your cooperation in
117
  maintaining a safe and appropriate environment for all members of our community.**
 
 
 
 
 
 
 
118
  <p/>""")
119
  with gr.Row():
120
  with gr.Column():
 
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.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.lower() == 'restart' : #--to add revert functionality later
55
+ history = history or []
56
+ temp_img_name = img_name[:-4]+str(int(time.time()))+'.png'
57
+ image_in.save(temp_img_name)
58
+ response = 'Reverted to the last image ' + '<img src="/file=' + temp_img_name + '">'
59
+ history.append((prompt, response))
60
+ return history, history, image_in, temp_img_name, counter_out
61
  if counter_out > 0:
62
  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]
63
  if os.path.exists(img_name):
 
74
  else:
75
  seed = random.randint(0, 1000000)
76
  img_name = f"./edited_image_{seed}.png"
77
+ #Resizing the image
78
  basewidth = 512
79
  wpercent = (basewidth/float(image_in.size[0]))
80
  hsize = int((float(image_in.size[1])*float(wpercent)))
81
  image_in = image_in.resize((basewidth,hsize), Image.Resampling.LANCZOS)
 
 
 
 
 
 
 
82
  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]
83
  if os.path.exists(img_name):
84
  os.remove(img_name)
 
107
  <p>For faster inference without waiting in the queue, you may duplicate the space and upgrade to GPU in settings.<br/>
108
  <a href="https://huggingface.co/spaces/ysharma/InstructPix2Pix_Chatbot?duplicate=true">
109
  <img style="margin-top: 0em; margin-bottom: 0em" src="https://bit.ly/3gLdBN6" alt="Duplicate Space"></a>
110
+
111
  **Note: Please be advised that a safety checker has been implemented in this public space.
112
  Any attempts to generate inappropriate or NSFW images will result in the display of a black screen
113
  as a precautionary measure for the protection of all users. We appreciate your cooperation in
114
  maintaining a safe and appropriate environment for all members of our community.**
115
+
116
+ New features and bug-fixes:
117
+
118
+ 1. Now use 'reverse' as prompt to get back the previous image after an unwanted edit
119
+ 2. Use 'restart' as prompt to get back to original image and start over!
120
+ 3. Now you can load larger images (~5 mb) as well
121
+
122
  <p/>""")
123
  with gr.Row():
124
  with gr.Column():