gradio-app / gradio

Build and share delightful machine learning apps, all in Python. 🌟 Star to support our work!
http://www.gradio.app
Apache License 2.0
31.21k stars 2.33k forks source link

ImageEditor is very useful, but upload a image takes 5 second and other issue. #6740

Open Dexter-Wang opened 7 months ago

Dexter-Wang commented 7 months ago

Describe the bug

gr.ImageEditor is very useful, but upload a image takes 5 second sometimes.

There is another issue as bellow. Transfer Image from the gr.Image at one Tab to gr.ImageEditor at another Tab, it got black image quite often.

Have you searched existing issues? 🔎

Reproduction

import torch from diffusers import StableDiffusionImg2ImgPipeline
from diffusers import DiffusionPipeline

NegPrompt ="""low res, bad hands, text, error, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality, normal quality, jpeg artifacts, signature, watermark, username, blurry, ugly"""

modelID1 = "pagebrain/dreamshaper-v8"
Img2Img1 = StableDiffusionImg2ImgPipeline.from_pretrained(modelID1, torch_dtype=torch.float16, safety_checker=None, requires_safety_checker=False).to("cuda")

'''--- ImageToImage --------------------------------------------- '''
def ImageToImage0(imgIn=None, prompt="", strength=0.75, seed=0): if (imgIn==None or prompt==""): return None
gene = torch.Generator(device="cuda").manual_seed(0) imgOut = Img2Img1(prompt=prompt, negative_prompt=NegPrompt, image=imgIn, generator=gene, strength=strength, guidance_scale=7.5).images[0]
return imgOut

'''--- TextToImage --------------------------------------------- ''' Text2Img1 = DiffusionPipeline.from_pretrained(modelID1, torch_dtype=torch.float16, safety_checker=None, requires_safety_checker=False).to("cuda")

def TextToImage0(prompt=""): if (prompt==""): return None image = Text2Img1(prompt, negative_prompt=NegPrompt).images[0]
return image

'''--- Create UI --------------------------------------------- ''' import numpy as np from PIL import ImageFilter import gradio as gr

def ImgToInput(imgOut, imgIn):
if (imgOut==None): return None imgIn["background"]=imgOut.copy()
return imgIn

def ImageToImage(imgIn, prompt, strength):
if (imgIn==None): return None imgIn = imgIn["composite"].copy()
imgOut = ImageToImage0(imgIn, prompt, strength)
return imgOut

def TextToImage(prompt): if (prompt==None): return None imgOut = TextToImage0(prompt)
return imgOut

def ImageBlur(imgIn, radius=30):
imgIn = imgIn["composite"].copy()
imgOut = imgIn.filter(ImageFilter.GaussianBlur(radius=radius))
return imgOut

Text1 = "A beautiful country side landscape, romantic" with gr.Blocks() as MainForm:
Prompt = gr.Textbox(value=Text1, label='Prompt')
with gr.Tab("ImageToImage"): with gr.Row():
with gr.Column():
IgClear = gr.Button("Clear")
IgImgIn = gr.ImageEditor(type='pil', image_mode="RGB", width=512, height=512, interactive=True, )
Strength = gr.Slider(0.1, 1.0, 0.75, step=0.01, label='Strength')
with gr.Column():
Ig2ImgGN = gr.Button("Generate") IgImgOut = gr.Image(type='pil', width=512, height=512, )
IgToInput = gr.Button("To Input")

with gr.Tab("TextToImage"):      
    with gr.Row():                               
        with gr.Column():                         
            TxClear = gr.Button("Clear")                                  
        with gr.Column():                         
            Tx2ImgGN = gr.Button("Generate") 
            TxImgOut = gr.Image(type='pil', width=512, height=512, )
            TxToIgIn = gr.Button("To ImageToImage Input") 

with gr.Tab("Preprocess"):      
    with gr.Row():                              
        with gr.Column():                        
            PrClear = gr.Button("Clear") 
            PrImgIn = gr.ImageEditor(type='pil', image_mode="RGB",  width=512, height=512, interactive=True, )                        
            with gr.Row():                      
                BlurNm  = gr.Number(minimum=0, maximum=50, value=30, step=5, label="Blur", interactive=True, show_label=False)                     
                BlurBtn = gr.Button("Blur") 
        with gr.Column():                                                        
            PrImgOut  = gr.Image(type='pil', width=512, height=512, )
            with gr.Row():
                PrToInput = gr.Button("To Input") 
                PrToIgIn  = gr.Button("To IamgeToImage Input") 

#---  ImgToImg ------ 
IgClear.click(fn=lambda:[None,None], inputs=[], outputs=[IgImgIn,IgImgOut])           
Ig2ImgGN.click(fn=ImageToImage, inputs=[IgImgIn,Prompt,Strength], outputs=IgImgOut) 
IgToInput.click(fn=ImgToInput, inputs=IgImgOut, outputs=IgImgIn) 

#---  TextToImg ------    
TxClear.click(fn=lambda:[None,None], inputs=[], outputs=[TxImgOut])        
Tx2ImgGN.click(fn=TextToImage, inputs=Prompt, outputs=TxImgOut)
TxToIgIn.click(fn=ImgToInput, inputs=[TxImgOut,IgImgIn], outputs=IgImgIn) 

#---  Preprocess ------    
BlurBtn.click  (fn=ImageBlur, inputs=[PrImgIn,BlurNm], outputs=PrImgOut)
PrToInput.click(fn=ImgToInput, inputs=[PrImgOut,PrImgIn], outputs=PrImgIn)      
PrToIgIn.click (fn=ImgToInput, inputs=[PrImgOut,IgImgIn], outputs=IgImgIn)   

MainForm.launch()

Screenshot

ImageEditor_issue

Logs

no error

System Info

gradio                       4.7.1
gradio_client                0.7.0
python-dateutil              2.8.2
python-multipart             0.0.6

Severity

I can work around it

pngwn commented 7 months ago

Do you have a minimal reproduction demonstrating this issue? There is a lot of unrelated code in the snippet you posted.

pngwn commented 7 months ago

cc @aliabid94 re: perf improvements.

Dexter-Wang commented 7 months ago

import torch from diffusers import StableDiffusionImg2ImgPipeline

modelID1 = "pagebrain/dreamshaper-v8"
Img2Img1 = StableDiffusionImg2ImgPipeline.from_pretrained(modelID1, torch_dtype=torch.float16, safety_checker=None, requires_safety_checker=False).to("cuda")

'''--- ImageToImage --------------------------------------------- '''
def ImageToImage0(imgIn=None, prompt="", strength=0.75, seed=0): if (imgIn==None or prompt==""): return None
gene = torch.Generator(device="cuda").manual_seed(0) imgOut = Img2Img1(prompt=prompt, image=imgIn, generator=gene, strength=strength, guidance_scale=7.5).images[0]
return imgOut

'''--- Create UI --------------------------------------------- ''' from PIL import ImageFilter import gradio as gr

def ImgToInput(imgOut, imgIn):
if (imgOut==None): return None imgIn["background"]=imgOut.copy()
return imgIn

def ImageToImage(imgIn, prompt):
if (imgIn==None): return None imgIn = imgIn["composite"].copy()
imgOut = ImageToImage0(imgIn, prompt)
return imgOut

def ImageBlur(imgIn, radius=30):
imgIn = imgIn["composite"].copy()
imgOut = imgIn.filter(ImageFilter.GaussianBlur(radius=radius))
return imgOut

with gr.Blocks() as MainForm:
Prompt = gr.Textbox(value="A beautiful country side landscape, romantic", label='Prompt')
with gr.Tab("ImageToImage"): with gr.Row():
with gr.Column():
IgClear = gr.Button("Clear")
IgImgIn = gr.ImageEditor(type='pil', image_mode="RGB", width=512, height=512, interactive=True, ) with gr.Column():
Ig2ImgGN = gr.Button("Generate") IgImgOut = gr.Image(type='pil', width=512, height=512, )

with gr.Tab("Preprocess"):      
    with gr.Row():                              
        with gr.Column():                        
            PrClear = gr.Button("Clear") 
            PrImgIn = gr.ImageEditor(type='pil', image_mode="RGB",  width=512, height=512, interactive=True, ) 
            BlurBtn = gr.Button("Blur") 
        with gr.Column():                                                        
            PrImgOut  = gr.Image(type='pil', width=512, height=512, )
            with gr.Row():                    
                PrToIgIn  = gr.Button("To IamgeToImage Input") 

#---  ImgToImg ------ 
IgClear.click(fn=lambda:[None,None], inputs=[], outputs=[IgImgIn,IgImgOut])           
Ig2ImgGN.click(fn=ImageToImage, inputs=[IgImgIn,Prompt], outputs=IgImgOut) 

#---  Preprocess ------   
PrClear.click(fn=lambda:[None,None], inputs=[], outputs=[PrImgIn,PrImgOut])
BlurBtn.click  (fn=ImageBlur, inputs=PrImgIn, outputs=PrImgOut)    
PrToIgIn.click (fn=ImgToInput, inputs=[PrImgOut,IgImgIn], outputs=IgImgIn)   

MainForm.launch()

Dexter-Wang commented 7 months ago

Thank you for your quick reply. Above is a simplify code. Black image happen less. Following is the working flow to make Black image happen.

  1. upload a image to the input of "Preprocess", press "Blur", then a blur image generated at "Output". It work fine.
  2. Press "To ImageToImage Input". It work fine.
  3. At "Image To Image" page, input prompt and press "Generate", It work fine.
  4. At "Image To Image" page, upload a image to the input frame, press "Generate". Problem happen at this moment. The input frame either shows the wrong image or the black image.

however, ImageEditor is a very good design.

Dexter-Wang commented 7 months ago

I also put the above code at Colab, Following is the address, if you need it.

https://colab.research.google.com/drive/1QhhGt955SkCrlGc8TJyLYDZkiJ2-EGDr