ysharma/dummy
0
1import PIL2import requests3import torch4import gradio as gr 5import random6from PIL import Image 7import os 8import time9from diffusers import StableDiffusionInstructPix2PixPipeline, EulerAncestralDiscreteScheduler10 11#Loading from Diffusers Library12model_id = "timbrooks/instruct-pix2pix"13pipe = StableDiffusionInstructPix2PixPipeline.from_pretrained(model_id, torch_dtype=torch.float16, revision="fp16", safety_checker=None)14pipe.to("cuda")15#pipe.enable_attention_slicing()16pipe.enable_xformers_memory_efficient_attention()17pipe.unet.to(memory_format=torch.channels_last)18 19help_text = """ 20**Note: Please be advised that a safety checker has been implemented in this public space. 21 Any attempts to generate inappropriate or NSFW images will result in the display of a black screen 22 as a precautionary measure to protect all users. We appreciate your cooperation in 23 maintaining a safe and appropriate environment for all members of our community.**24 25 New features and bug-fixes: 26 27 1. Chat style interface28 2. Now use **'reverse'** as prompt to get back the previous image after an unwanted edit29 3. Use **'restart'** as prompt to get back to original image and start over!30 4. Now you can load larger image files (~5 mb) as well31 32Some notes from the official [instruct-pix2pix](https://huggingface.co/spaces/timbrooks/instruct-pix2pix) Space by the authors and from the official [Diffusers docs](https://huggingface.co/docs/diffusers/main/en/api/pipelines/stable_diffusion/pix2pix) -33 34If you're not getting what you want, there may be a few reasons:351. Is the image not changing enough? Your guidance_scale may be too low. It should be >1. Higher guidance scale encourages to generate images 36that are closely linked to the text `prompt`, usually at the expense of lower image quality. This value dictates how similar the output should 37be to the input. This pipeline requires a value of at least `1`. It's possible your edit requires larger changes from the original image. 38 392. Alternatively, you can toggle image_guidance_scale. Image guidance scale is to push the generated image towards the inital image. Image guidance 40 scale is enabled by setting `image_guidance_scale > 1`. Higher image guidance scale encourages to generate images that are closely 41 linked to the source image `image`, usually at the expense of lower image quality. 423. I have observed that rephrasing the instruction sometimes improves results (e.g., "turn him into a dog" vs. "make him a dog" vs. "as a dog").434. Increasing the number of steps sometimes improves results.445. Do faces look weird? The Stable Diffusion autoencoder has a hard time with faces that are small in the image. Try:45 * Cropping the image so the face takes up a larger portion of the frame.46"""47 48def previous(image):49 return image 50 51def upload_image(file):52 return Image.open(file)53 54def upload_button_config():55 return gr.update(visible=False)56 57def upload_textbox_config(text_in):58 return gr.update(visible=True)59 60def dummy_fn():61 return 'dummy'62 63def 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)):64 progress(0, desc="Starting...")65 if prompt != '' and prompt.lower() == 'reverse' : #--to add revert functionality later66 history = history or []67 temp_img_name = img_name[:-4]+str(int(time.time()))+'.png' 68 image_oneup.save(temp_img_name)69 response = 'Reverted to the last image ' + '<img src="/file=' + temp_img_name + '">' 70 history.append((prompt, response))71 return history, history, image_oneup, temp_img_name, counter_out72 if prompt != '' and prompt.lower() == 'restart' : #--to add revert functionality later73 history = history or []74 temp_img_name = img_name[:-4]+str(int(time.time()))+'.png' 75 #Resizing the image76 basewidth = 51277 wpercent = (basewidth/float(image_in.size[0]))78 hsize = int((float(image_in.size[1])*float(wpercent)))79 image_in = image_in.resize((basewidth,hsize), Image.Resampling.LANCZOS)80 image_in.save(temp_img_name)81 response = 'Reverted to the last image ' + '<img src="/file=' + temp_img_name + '">' 82 history.append((prompt, response))83 return history, history, image_in, temp_img_name, counter_out84 #adding supportive sample text85 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?"] 86 if counter_out == 0:87 t1 = time.time()88 print(f"Time at start = {t1}")89 seed = random.randint(0, 1000000)90 img_name = f"./edited_image_{seed}.png"91 #convert file object to image92 image_in = Image.open(btn_upload)93 #Resizing the image94 basewidth = 51295 wpercent = (basewidth/float(image_in.size[0]))96 hsize = int((float(image_in.size[1])*float(wpercent)))97 image_in = image_in.resize((basewidth,hsize), Image.Resampling.LANCZOS)98 99 #if os.path.exists(img_name):100 # os.remove(img_name)101 #with open(img_name, "wb") as fp:102 103 # Save the image to the file-like object104 image_in.save(img_name)105 106 #Get the name of the saved image107 #saved_image_name0 = fp.name108 109 history = history or []110 response = '<img src="/file=' + img_name + '">'111 history.append((prompt, response))112 counter_out += 1113 114 t2 = time.time()115 print(f"Time at end = {t2}")116 time_diff = t2-t1117 print(f"Time taken = {time_diff}")118 return history, history, image_in, img_name, counter_out119 120 elif counter_out == 1: 121 #instruct-pix2pix inference122 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]123 if os.path.exists(img_name):124 os.remove(img_name)125 temp_img_name = img_name[:-4]+str(int(time.time()))[-4:] +'.png' 126 with open(temp_img_name, "wb") as fp:127 # Save the image to the file-like object128 edited_image.save(fp)129 #Get the name of the saved image130 saved_image_name1 = fp.name131 history = history or []132 response = random.choice(add_text_list) + '<img src="/file=' + saved_image_name1 + '">' #IMG_NAME133 history.append((prompt, response))134 counter_out += 1135 return history, history, edited_image, temp_img_name, counter_out136 elif counter_out > 1:137 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]138 if os.path.exists(img_name):139 os.remove(img_name)140 temp_img_name = img_name[:-4]+str(int(time.time()))[-4:]+'.png' 141 # Create a file-like object142 with open(temp_img_name, "wb") as fp:143 # Save the image to the file-like object144 edited_image.save(fp)145 #Get the name of the saved image146 saved_image_name2 = fp.name147 #edited_image.save(temp_img_name) #, overwrite=True)148 history = history or []149 response = random.choice(add_text_list) + '<img src="/file=' + saved_image_name2 + '">' 150 history.append((prompt, response))151 counter_out += 1152 return history, history, edited_image, temp_img_name, counter_out153 154 155#Blocks layout156with gr.Blocks(css="style.css") as demo:157 with gr.Column(elem_id="col-container") as main_col:158 gr.HTML("""<div style="text-align: center; max-width: 700px; margin: 0 auto;">159 <div160 style="161 display: inline-flex;162 align-items: center;163 gap: 0.8rem;164 font-size: 1.75rem;165 "166 >167 <h1 style="font-weight: 900; margin-bottom: 7px; margin-top: 5px;">168 ChatPix2Pix: Image Editing by Instructions169 </h1>170 </div>171 <p style="margin-bottom: 10px; font-size: 94%">172 For faster inference without waiting in the queue, you may duplicate the space and upgrade to GPU in settings <a href="https://huggingface.co/spaces/ysharma/InstructPix2Pix_Chatbot?duplicate=true"><img style="margin-top: 0em; margin-bottom: 0em" src="https://bit.ly/3gLdBN6" alt="Duplicate Space"></a>173 <a href="https://huggingface.co/timbrooks/instruct-pix2pix" target="_blank">Diffusers implementation of instruct-pix2pix</a> - InstructPix2Pix: Learning to Follow Image Editing Instructions!174 </p>175 </div>""")176 #gr.Markdown("""<h1><center>dummy</h1></center> """)177 178 with gr.Accordion("Advance settings for Training and Inference", open=False):179 image_in = gr.Image(visible=False,type='pil', label="Original Image")180 gr.Markdown("Advance settings for - Number of Inference steps, Guidanace scale, and Image guidance scale.")181 in_steps = gr.Number(label="Enter the number of Inference steps", value = 20)182 in_guidance_scale = gr.Slider(1,10, step=0.5, label="Set Guidance scale", value=7.5)183 in_img_guidance_scale = gr.Slider(1,10, step=0.5, label="Set Image Guidance scale", value=1.5)184 image_hid = gr.Image(type='pil', visible=False)185 image_oneup = gr.Image(type='pil', visible=False)186 img_name_temp_out = gr.Textbox(visible=False)187 counter_out = gr.Number(visible=False, value=0, precision=0)188 dummy_num = gr.Number(visible=False)189 190 #with gr.Row():191 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')192 btn_upload = gr.UploadButton("Upload image", file_types=["image"], file_count="single", elem_id="upload_button")193 194 chatbot = gr.Chatbot(elem_id = 'chatbot-component')195 state_in = gr.State()196 197 #text_out_dummy = gr.Textbox(visbile = False, elem_id = 'dummy_elem')198 199 #btn_upload = gr.UploadButton("Upload image", file_types=["image"], file_count="single", elem_id="upload_button")200 #with gr.Row():201 # btn_upload = gr.UploadButton("Upload image", file_types=["image"], file_count="single", elem_id="upload_button")202 # text_in = gr.Textbox(value='', Placeholder="Enter your instructions here", elem_id = "input_prompt")203 # #btn_upload = gr.UploadButton("Upload image", file_types=["image"], file_count="single", elem_id="upload_button")204 #text_out_dummy = gr.Textbox(visbile = False, elem_id = 'dummy_elem')205 element_dummy = gr.HTML(visbile = False, elem_id = 'dummy_elem')206 207 #Using Event Listeners208 btn_upload.upload(chat,209 [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], 210 [chatbot, state_in, image_in, img_name_temp_out, counter_out])211 btn_upload.upload(fn = upload_textbox_config, inputs=text_in, outputs = text_in)212 213 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])214 text_in.submit(previous, [image_hid], [image_oneup])215 216 chatbot.change(fn = upload_button_config, outputs=btn_upload) #, scroll_to_output = True)217 text_in.submit(None, [], [], _js = "() => document.getElementById('#chatbot-component').scrollTop = document.getElementById('#chatbot-component').scrollHeight")218 #text_in.submit(None, [], main_col, _js = "(x) => x.scrollIntoView(false)")219 #text_in.submit(None, [], main_col, _js = "(x) => x.scrollTo(0, x.scrollHeight)") # or using chatbot220 #text_in.submit(None, [], chatbot, _js = "() => {const element = document.getElementById('#chatbot-component'); element.scrollTop = element.scrollHeight; }")221 222 #counter_out.click(fn = upload_button_config, outputs=btn_upload)223 #chatbot.change(dummy_fn, inputs=[], outputs=[btn_upload], scroll_to_output = True)224 #gr.Markdown(help_text)225 #text_in.submit(None, [text_in], text_out, _js="(x) => {let newElement = document.createElement('div') newElement.innerHTML = x document.getElementById('chatbot-component').appendChild(newElement) newElement.scrollIntoView() }")226 #text_in.submit(None, [], None, _js="() => {let chatbot = document.getElementById('chatbot-component'); chatbot.scrollTo(0, chatbot.scrollHeight);}")227 #text_in.submit(None, [], None, _js="() => {document.querySelector('#chatbot-component').scrollTop = document.querySelector('#chatbot-component').scrollHeight;}")228 #text_in.submit(None, [], None, _js="() => {let chatbot = document.querySelector('#col-container'); chatbot.scrollTop = chatbot.scrollHeight;}")229 #demo.load(fn = dummy_fn, outputs=text_out_dummy, scroll_to_output = True)230 gr.Markdown(help_text, elem_id = 'help_text')231 #gr.HTML("""<a href="#help_text">Expand/Close</a>""")232 233demo.queue(concurrency_count=3)234demo.launch(debug=True) #, width="80%", height=2000)