File size: 3,706 Bytes
d23b320
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
169902b
 
d23b320
169902b
 
8fc365a
d23b320
 
 
 
 
 
 
 
8fc365a
d23b320
 
 
 
 
 
 
 
 
 
 
 
 
8fc365a
d23b320
8fc365a
 
d23b320
 
 
 
8fc365a
d23b320
c428701
 
 
 
 
 
 
 
8fc365a
 
c428701
 
 
 
 
d23b320
 
 
c428701
 
 
 
 
8fc365a
 
 
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
110
with gr.Blocks(css=css, theme=gr.themes.Citrus()) as demo:
    gr.HTML("<h1 style='text-align: center'>Qwen-Image with InstantX Inpainting ControlNet</style>")
    gr.Markdown(
        "Generate images with the [InstantX/Qwen-Image-ControlNet-Inpainting](https://huggingface.co/InstantX/Qwen-Image-ControlNet-Inpainting) that takes depth, pose and canny conditionings"
    )
    with gr.Row():
        with gr.Column():
            edit_image = gr.ImageEditor(
                label='Upload and draw mask for inpainting',
                type='pil',
                sources=["upload", "webcam"],
                image_mode='RGB',
                layers=False,
                brush=gr.Brush(colors=["#FFFFFF"], color_mode="fixed"),
                height=600
            )
            prompt = gr.Text(
                label="Prompt",
                show_label=False,
                max_lines=1,
                placeholder="Enter your prompt (e.g., 'change the hat to red')",
                container=False,
            )
            negative_prompt = gr.Text(
                label="Negative Prompt",
                show_label=True,
                max_lines=1,
                placeholder="Enter what you don't want (optional)",
                container=False,
                value="",
                visible=False
            )
            run_button = gr.Button("Run")
            
        with gr.Column():
            result = gr.ImageSlider(label="Result", show_label=False, interactive=False)
            use_as_input_button = gr.Button("🔄 Use as Input Image", visible=False, variant="secondary")
    
    with gr.Accordion("Advanced Settings", open=False):
        
        seed = gr.Slider(
            label="Seed",
            minimum=0,
            maximum=MAX_SEED,
            step=1,
            value=42,
        )
        
        randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
        
        
        with gr.Row():
            strength = gr.Slider(
                label="Conditioning Scale",
                minimum=0.0,
                maximum=1.0,
                step=0.1,
                value=1.0,
                info="Controls how much the inpainted region should change"
            )
            
            true_cfg_scale = gr.Slider(
                label="True CFG Scale",
                minimum=1.0,
                maximum=10.0,
                step=0.5,
                value=4.0,
                info="Classifier-free guidance scale"
            )

            num_inference_steps = gr.Slider(
                label="Number of inference steps",
                minimum=1,
                maximum=50,
                step=1,
                value=30,
            )
            
        rewrite_prompt = gr.Checkbox(
            label="Enhance prompt (using HF Inference)", 
            value=True
        )

    # Event handlers for reuse functionality (MUST be inside gr.Blocks context with 4 spaces)
    use_as_input_button.click(
        fn=use_output_as_input,
        inputs=[result],
        outputs=[edit_image],
        show_api=False
    )

    # Main generation pipeline with result clearing and button visibility
    gr.on(
        triggers=[run_button.click, prompt.submit],
        fn=clear_result,
        inputs=None,
        outputs=result,
        show_api=False
    ).then(
        fn=infer,
        inputs=[edit_image, prompt, negative_prompt, seed, randomize_seed, strength, num_inference_steps, true_cfg_scale, rewrite_prompt],
        outputs=[result, seed]
    ).then(
        fn=lambda: gr.update(visible=True),
        inputs=None,
        outputs=use_as_input_button,
        show_api=False
    )

demo.launch()