dummy / app.py
ysharma's picture
ysharma HF staff
aa
a2dca9f
raw history blame
No virus
11.2 kB
import PIL
import requests
import torch
import gradio as gr
import random
from PIL import Image
import os
import time
from diffusers import StableDiffusionInstructPix2PixPipeline, EulerAncestralDiscreteScheduler
#Loading from Diffusers Library
model_id = "timbrooks/instruct-pix2pix"
pipe = StableDiffusionInstructPix2PixPipeline.from_pretrained(model_id, torch_dtype=torch.float16, revision="fp16", safety_checker=None)
pipe.to("cuda")
#pipe.enable_attention_slicing()
pipe.enable_xformers_memory_efficient_attention()
pipe.unet.to(memory_format=torch.channels_last)
help_text = """ """
def previous(image):
return image
def upload_image(file):
return Image.open(file)
def upload_button_config():
return gr.update(visible=False)
def upload_textbox_config(text_in):
return gr.update(visible=True)
def dummy_fn():
return 'dummy'
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)):
progress(0, desc="Starting...")
if prompt != '' and prompt.lower() == 'reverse' : #--to add revert functionality later
history = history or []
temp_img_name = img_name[:-4]+str(int(time.time()))+'.png'
image_oneup.save(temp_img_name)
response = 'Reverted to the last image ' + '<img src="/file=' + temp_img_name + '">'
history.append((prompt, response))
return history, history, image_oneup, temp_img_name, counter_out
if prompt != '' and prompt.lower() == 'restart' : #--to add revert functionality later
history = history or []
temp_img_name = img_name[:-4]+str(int(time.time()))+'.png'
#Resizing the image
basewidth = 512
wpercent = (basewidth/float(image_in.size[0]))
hsize = int((float(image_in.size[1])*float(wpercent)))
image_in = image_in.resize((basewidth,hsize), Image.Resampling.LANCZOS)
image_in.save(temp_img_name)
response = 'Reverted to the last image ' + '<img src="/file=' + temp_img_name + '">'
history.append((prompt, response))
return history, history, image_in, temp_img_name, counter_out
#adding supportive sample text
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?"]
if counter_out == 0:
t1 = time.time()
print(f"Time at start = {t1}")
seed = random.randint(0, 1000000)
img_name = f"./edited_image_{seed}.png"
#convert file object to image
image_in = Image.open(btn_upload)
#Resizing the image
basewidth = 512
wpercent = (basewidth/float(image_in.size[0]))
hsize = int((float(image_in.size[1])*float(wpercent)))
image_in = image_in.resize((basewidth,hsize), Image.Resampling.LANCZOS)
#if os.path.exists(img_name):
# os.remove(img_name)
#with open(img_name, "wb") as fp:
# Save the image to the file-like object
image_in.save(img_name)
#Get the name of the saved image
#saved_image_name0 = fp.name
history = history or []
response = '<img src="/file=' + img_name + '">'
history.append((prompt, response))
counter_out += 1
t2 = time.time()
print(f"Time at end = {t2}")
time_diff = t2-t1
print(f"Time taken = {time_diff}")
return history, history, image_in, img_name, counter_out
elif counter_out == 1:
#instruct-pix2pix inference
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]
if os.path.exists(img_name):
os.remove(img_name)
temp_img_name = img_name[:-4]+str(int(time.time()))[-4:] +'.png'
with open(temp_img_name, "wb") as fp:
# Save the image to the file-like object
edited_image.save(fp)
#Get the name of the saved image
saved_image_name1 = fp.name
history = history or []
response = random.choice(add_text_list) + '<img src="/file=' + saved_image_name1 + '">' #IMG_NAME
history.append((prompt, response))
counter_out += 1
return history, history, edited_image, temp_img_name, counter_out
elif counter_out > 1:
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]
if os.path.exists(img_name):
os.remove(img_name)
temp_img_name = img_name[:-4]+str(int(time.time()))[-4:]+'.png'
# Create a file-like object
with open(temp_img_name, "wb") as fp:
# Save the image to the file-like object
edited_image.save(fp)
#Get the name of the saved image
saved_image_name2 = fp.name
#edited_image.save(temp_img_name) #, overwrite=True)
history = history or []
response = random.choice(add_text_list) + '<img src="/file=' + saved_image_name2 + '">'
history.append((prompt, response))
counter_out += 1
return history, history, edited_image, temp_img_name, counter_out
#Blocks layout
with gr.Blocks(css="style.css") as demo:
with gr.Column(elem_id="col-container") as main_col:
gr.HTML("""<div style="text-align: center; max-width: 700px; margin: 0 auto;">
<div
style="
display: inline-flex;
align-items: center;
gap: 0.8rem;
font-size: 1.75rem;
"
>
<h1 style="font-weight: 900; margin-bottom: 7px; margin-top: 5px;">
ChatPix2Pix: Image Editing by Instructions
</h1>
</div>
<p style="margin-bottom: 10px; font-size: 94%">
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>
<a href="https://huggingface.co/timbrooks/instruct-pix2pix" target="_blank">Diffusers implementation of instruct-pix2pix</a> - InstructPix2Pix: Learning to Follow Image Editing Instructions!
</p>
</div>""")
#gr.Markdown("""<h1><center>dummy</h1></center> """)
with gr.Accordion("Advance settings for Training and Inference", open=False):
image_in = gr.Image(visible=False,type='pil', label="Original Image")
gr.Markdown("Advance settings for - Number of Inference steps, Guidanace scale, and Image guidance scale.")
in_steps = gr.Number(label="Enter the number of Inference steps", value = 20)
in_guidance_scale = gr.Slider(1,10, step=0.5, label="Set Guidance scale", value=7.5)
in_img_guidance_scale = gr.Slider(1,10, step=0.5, label="Set Image Guidance scale", value=1.5)
image_hid = gr.Image(type='pil', visible=False)
image_oneup = gr.Image(type='pil', visible=False)
img_name_temp_out = gr.Textbox(visible=False)
counter_out = gr.Number(visible=False, value=0, precision=0)
dummy_num = gr.Number(visible=False)
#with gr.Row():
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')
btn_upload = gr.UploadButton("Upload image", file_types=["image"], file_count="single", elem_id="upload_button")
chatbot = gr.Chatbot(elem_id = 'chatbot-component')
state_in = gr.State()
#text_out_dummy = gr.Textbox(visbile = False, elem_id = 'dummy_elem')
#btn_upload = gr.UploadButton("Upload image", file_types=["image"], file_count="single", elem_id="upload_button")
#with gr.Row():
# btn_upload = gr.UploadButton("Upload image", file_types=["image"], file_count="single", elem_id="upload_button")
# text_in = gr.Textbox(value='', Placeholder="Enter your instructions here", elem_id = "input_prompt")
# #btn_upload = gr.UploadButton("Upload image", file_types=["image"], file_count="single", elem_id="upload_button")
#text_out_dummy = gr.Textbox(visbile = False, elem_id = 'dummy_elem')
element_dummy = gr.HTML(visbile = False, elem_id = 'dummy_elem')
#Using Event Listeners
btn_upload.upload(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_in, img_name_temp_out, counter_out])
btn_upload.upload(fn = upload_textbox_config, inputs=text_in, outputs = text_in)
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])
text_in.submit(previous, [image_hid], [image_oneup])
chatbot.change(fn = upload_button_config, outputs=btn_upload) #, scroll_to_output = True)
text_in.submit(None, [], [], _js = "() => document.getElementById('#chatbot-component').scrollTop = document.getElementById('#chatbot-component').scrollHeight")
#text_in.submit(None, [], main_col, _js = "(x) => x.scrollIntoView(false)")
#text_in.submit(None, [], main_col, _js = "(x) => x.scrollTo(0, x.scrollHeight)") # or using chatbot
#text_in.submit(None, [], chatbot, _js = "() => {const element = document.getElementById('#chatbot-component'); element.scrollTop = element.scrollHeight; }")
#counter_out.click(fn = upload_button_config, outputs=btn_upload)
#chatbot.change(dummy_fn, inputs=[], outputs=[btn_upload], scroll_to_output = True)
#gr.Markdown(help_text)
#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() }")
#text_in.submit(None, [], None, _js="() => {let chatbot = document.getElementById('chatbot-component'); chatbot.scrollTo(0, chatbot.scrollHeight);}")
#text_in.submit(None, [], None, _js="() => {document.querySelector('#chatbot-component').scrollTop = document.querySelector('#chatbot-component').scrollHeight;}")
#text_in.submit(None, [], None, _js="() => {let chatbot = document.querySelector('#col-container'); chatbot.scrollTop = chatbot.scrollHeight;}")
#demo.load(fn = dummy_fn, outputs=text_out_dummy, scroll_to_output = True)
gr.Markdown(help_text, elem_id = 'help_text')
#gr.HTML("""<a href="#help_text">Expand/Close</a>""")
demo.queue(concurrency_count=3)
demo.launch(debug=True) #, width="80%", height=2000)