File size: 4,068 Bytes
7a3d678
 
 
 
 
469d515
7a3d678
 
 
 
 
 
 
 
397db89
7a3d678
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
397db89
7a3d678
 
 
 
 
 
 
 
 
 
469d515
 
 
 
 
 
 
 
7a3d678
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
397db89
7a3d678
 
38c623c
7a3d678
 
 
 
 
 
397db89
 
 
 
 
 
7a3d678
469d515
 
 
7a3d678
 
 
397db89
7a3d678
469d515
7a3d678
469d515
 
 
 
7a3d678
 
 
469d515
7a3d678
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import spaces
import gradio as gr
import re
from PIL import Image
import flux1_inpaint
import os

def sanitize_prompt(prompt):
  # Allow only alphanumeric characters, spaces, and basic punctuation
  allowed_chars = re.compile(r"[^a-zA-Z0-9\s.,!?-]")
  sanitized_prompt = allowed_chars.sub("", prompt)
  return sanitized_prompt

@spaces.GPU(duration=180)
def process_images(image, image2=None,prompt="a girl",inpaint_model="black-forest-labs/FLUX.1-schnell",strength=0.75,seed=0):
    # I'm not sure when this happen
    if not isinstance(image, dict):
        if image2 == None:
             print("empty mask")
             return image
        else:
            image = dict({'background': image, 'layers': [image2]})

    if image2!=None:
         #print("use image2")
         mask = image2
    else:
         if len(image['layers']) == 0:
              print("empty mask")
              return image
         print("use layer")
         mask = image['layers'][0]


    output = flux1_inpaint.process_image(image["background"],mask,prompt,inpaint_model,strength,seed)
        
    return output
    

def read_file(path: str) -> str:
    with open(path, 'r', encoding='utf-8') as f:
        content = f.read()

    return content

def example_out(image,image_mask,prompt,strength,example_id):
    # input
    #parent,file=os.path.split(image_mask) # image is complex dict
    #base,ext = os.path.splitext(file)
    #key = base.split("_")[0]

    return f"images/{example_id}.jpg"

css="""
#col-left {
    margin: 0 auto;
    max-width: 640px;
}
#col-right {
    margin: 0 auto;
    max-width: 640px;
}
"""
demo_blocks = gr.Blocks(css=css, elem_id="demo-container")
with demo_blocks as demo:
    with gr.Column():
        gr.HTML(read_file("demo_header.html"))
    with gr.Row():
                with gr.Column():
                    image = gr.ImageEditor(height=800,sources=['upload','clipboard'],transforms=[],image_mode='RGB', layers=False,  elem_id="image_upload", type="pil", label="Upload",brush=gr.Brush(colors=["#fff"], color_mode="fixed"))
                    with gr.Row(elem_id="prompt-container",  equal_height=False):
                        with gr.Row():
                            prompt = gr.Textbox(label="Prompt",value="a eyes closed girl,shut eyes",placeholder="Your prompt (what you want in place of what is erased)", elem_id="prompt")
                            
                    btn = gr.Button("Inpaint!", elem_id="run_button")
                    
                    image_mask = gr.Image(sources=['upload','clipboard'],  elem_id="mask_upload", type="pil", label="Mask_Upload",height=400, value=None)
                    with gr.Accordion(label="Advanced Settings", open=False):
                        with gr.Row( equal_height=True):
                            strength = gr.Number(value=0.75, minimum=0, maximum=1.0, step=0.01, label="Inpaint strength")
                            seed = gr.Number(value=0, minimum=0, step=1, label="Inpaint seed")
                        models = ["black-forest-labs/FLUX.1-schnell"]
                        inpaint_model = gr.Dropdown(label="modes", choices=models, value="black-forest-labs/FLUX.1-schnell") 
                    
                            
                with gr.Column():
                    id_input=gr.Text(label="Name", visible=False)
                    image_out = gr.Image(height=800,sources=[],label="Output", elem_id="output-img",format="jpeg")

                    
            

    btn.click(fn=process_images, inputs=[image, image_mask,prompt,inpaint_model,strength,seed], outputs =image_out, api_name='infer')
    gr.Examples(
               examples=[["images/00207245_18.jpg", "images/00207245_18_mask.jpg","a beautiful girl,mouth opened",0.3,"00207245"]]
,
                fn=example_out,
                inputs=[image,image_mask,prompt,strength,id_input],
                outputs=[image_out],
                cache_examples=True,
    )
    gr.HTML(
        """
            
        """
    )

demo_blocks.queue(max_size=25).launch(share=False,debug=True)