linoyts HF Staff commited on
Commit
8fc365a
·
verified ·
1 Parent(s): 6d76fa4

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +138 -0
app.py ADDED
@@ -0,0 +1,138 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ import spaces
4
+ import torch
5
+ import random
6
+ import os
7
+
8
+ from diffusers import QwenImageEditInpaintPipeline
9
+ from PIL import Image
10
+
11
+ # Set environment variable for parallel loading
12
+ os.environ["HF_ENABLE_PARALLEL_LOADING"] = "YES"
13
+
14
+ MAX_SEED = np.iinfo(np.int32).max
15
+ MAX_IMAGE_SIZE = 2048
16
+
17
+ # Initialize Qwen Image Edit pipeline
18
+ pipe = QwenImageEditInpaintPipeline.from_pretrained("Qwen/Qwen-Image-Edit", torch_dtype=torch.bfloat16).to("cuda")
19
+
20
+ @spaces.GPU
21
+ def infer(edit_images, prompt, negative_prompt="", seed=42, randomize_seed=False, strength=1.0, num_inference_steps=35, true_cfg_scale=4.0, progress=gr.Progress(track_tqdm=True)):
22
+ image = edit_images["background"]
23
+ mask = edit_images["layers"][0]
24
+
25
+ if randomize_seed:
26
+ seed = random.randint(0, MAX_SEED)
27
+
28
+ # Generate image using Qwen pipeline
29
+ result_image = pipe(
30
+ prompt=prompt,
31
+ negative_prompt=negative_prompt,
32
+ image=image,
33
+ mask_image=mask,
34
+ strength=strength,
35
+ num_inference_steps=num_inference_steps,
36
+ true_cfg_scale=true_cfg_scale,
37
+ generator=torch.Generator(device="cuda").manual_seed(seed)
38
+ ).images[0]
39
+
40
+ return result_image, seed
41
+
42
+ examples = [
43
+ "change the hat to red",
44
+ "make the background a beautiful sunset",
45
+ "replace the object with a flower vase",
46
+ ]
47
+
48
+ css="""
49
+ #col-container {
50
+ margin: 0 auto;
51
+ max-width: 1000px;
52
+ }
53
+ """
54
+
55
+ with gr.Blocks(css=css) as demo:
56
+
57
+ with gr.Column(elem_id="col-container"):
58
+ gr.Markdown(f"""# Qwen Image Edit Inpainting
59
+ Advanced image inpainting using Qwen's Image Edit model
60
+ [[model](https://huggingface.co/Qwen/Qwen-Image-Edit)] [[paper](https://arxiv.org/abs/2412.20710)]
61
+ """)
62
+ with gr.Row():
63
+ with gr.Column():
64
+ edit_image = gr.ImageEditor(
65
+ label='Upload and draw mask for inpainting',
66
+ type='pil',
67
+ sources=["upload", "webcam"],
68
+ image_mode='RGB',
69
+ layers=False,
70
+ brush=gr.Brush(colors=["#FFFFFF"], color_mode="fixed"),
71
+ height=600
72
+ )
73
+ prompt = gr.Text(
74
+ label="Prompt",
75
+ show_label=False,
76
+ max_lines=1,
77
+ placeholder="Enter your prompt (e.g., 'change the hat to red')",
78
+ container=False,
79
+ )
80
+ negative_prompt = gr.Text(
81
+ label="Negative Prompt",
82
+ show_label=True,
83
+ max_lines=1,
84
+ placeholder="Enter what you don't want (optional)",
85
+ container=False,
86
+ value=""
87
+ )
88
+ run_button = gr.Button("Run")
89
+
90
+ result = gr.Image(label="Result", show_label=False)
91
+
92
+ with gr.Accordion("Advanced Settings", open=False):
93
+
94
+ seed = gr.Slider(
95
+ label="Seed",
96
+ minimum=0,
97
+ maximum=MAX_SEED,
98
+ step=1,
99
+ value=42,
100
+ )
101
+
102
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
103
+
104
+ with gr.Row():
105
+ strength = gr.Slider(
106
+ label="Strength",
107
+ minimum=0.0,
108
+ maximum=2.0,
109
+ step=0.1,
110
+ value=1.0,
111
+ info="Controls how much the inpainted region should change"
112
+ )
113
+
114
+ true_cfg_scale = gr.Slider(
115
+ label="True CFG Scale",
116
+ minimum=1.0,
117
+ maximum=20.0,
118
+ step=0.5,
119
+ value=4.0,
120
+ info="Classifier-free guidance scale"
121
+ )
122
+
123
+ num_inference_steps = gr.Slider(
124
+ label="Number of inference steps",
125
+ minimum=10,
126
+ maximum=100,
127
+ step=1,
128
+ value=35,
129
+ )
130
+
131
+ gr.on(
132
+ triggers=[run_button.click, prompt.submit],
133
+ fn = infer,
134
+ inputs = [edit_image, prompt, negative_prompt, seed, randomize_seed, strength, num_inference_steps, true_cfg_scale],
135
+ outputs = [result, seed]
136
+ )
137
+
138
+ demo.launch()