dgoot commited on
Commit
3005122
0 Parent(s):

Inpainting

Browse files
Files changed (4) hide show
  1. .gitattributes +35 -0
  2. README.md +12 -0
  3. app.py +152 -0
  4. requirements.txt +6 -0
.gitattributes ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tar filter=lfs diff=lfs merge=lfs -text
29
+ *.tflite filter=lfs diff=lfs merge=lfs -text
30
+ *.tgz filter=lfs diff=lfs merge=lfs -text
31
+ *.wasm filter=lfs diff=lfs merge=lfs -text
32
+ *.xz filter=lfs diff=lfs merge=lfs -text
33
+ *.zip filter=lfs diff=lfs merge=lfs -text
34
+ *.zst filter=lfs diff=lfs merge=lfs -text
35
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
README.md ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Image To Image
3
+ emoji: 📊
4
+ colorFrom: gray
5
+ colorTo: pink
6
+ sdk: gradio
7
+ sdk_version: 4.44.0
8
+ app_file: app.py
9
+ pinned: false
10
+ ---
11
+
12
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,152 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+
3
+ import gradio as gr
4
+ import spaces
5
+ import torch
6
+ from diffusers import AutoPipelineForInpainting
7
+ from loguru import logger
8
+ from PIL import Image
9
+
10
+ SUPPORTED_MODELS = [
11
+ "stabilityai/sdxl-turbo",
12
+ "stabilityai/stable-diffusion-3-medium-diffusers",
13
+ "stabilityai/stable-diffusion-xl-base-1.0",
14
+ "timbrooks/instruct-pix2pix",
15
+ ]
16
+ DEFAULT_MODEL = "stabilityai/stable-diffusion-xl-base-1.0"
17
+
18
+
19
+ model = os.environ.get("MODEL_ID", DEFAULT_MODEL)
20
+ gpu_duration = int(os.environ.get("GPU_DURATION", 60))
21
+
22
+
23
+ def load_pipeline(model):
24
+ return AutoPipelineForInpainting.from_pretrained(
25
+ model, torch_dtype=torch.float16, use_safetensors=True, variant="fp16"
26
+ )
27
+
28
+
29
+ logger.debug(f"Loading pipeline: {dict(model=model)}")
30
+ pipe = load_pipeline(model).to("cuda")
31
+
32
+
33
+ @logger.catch(reraise=True)
34
+ @spaces.GPU(duration=gpu_duration)
35
+ def infer(
36
+ prompt: str,
37
+ image_editor: dict,
38
+ negative_prompt: str,
39
+ strength: float,
40
+ num_inference_steps: int,
41
+ guidance_scale: float,
42
+ progress=gr.Progress(track_tqdm=True),
43
+ ):
44
+ logger.info(
45
+ f"Starting image generation: {dict(model=model, prompt=prompt, image_editor=image_editor)}"
46
+ )
47
+
48
+ init_image: Image.Image = image_editor["background"].convert("RGB")
49
+ mask_image: Image.Image = image_editor["composite"].convert("RGB")
50
+
51
+ # Downscale the image
52
+ init_image.thumbnail((1024, 1024))
53
+ mask_image.thumbnail((1024, 1024))
54
+
55
+ additional_args = {
56
+ k: v
57
+ for k, v in dict(
58
+ strength=strength,
59
+ num_inference_steps=num_inference_steps,
60
+ guidance_scale=guidance_scale,
61
+ ).items()
62
+ if v
63
+ }
64
+
65
+ logger.debug(f"Generating image: {dict(prompt=prompt, **additional_args)}")
66
+
67
+ images = pipe(
68
+ prompt=prompt,
69
+ image=init_image,
70
+ mask_image=mask_image,
71
+ negative_prompt=negative_prompt,
72
+ **additional_args,
73
+ ).images
74
+ return images[0]
75
+
76
+
77
+ css = """
78
+ @media (max-width: 1280px) {
79
+ #images-container {
80
+ flex-direction: column;
81
+ }
82
+ }
83
+ """
84
+
85
+ with gr.Blocks(css=css) as demo:
86
+ with gr.Column():
87
+ gr.Markdown("# Inpainting")
88
+ gr.Markdown(f"## Model: `{model}`")
89
+
90
+ with gr.Row():
91
+ prompt = gr.Text(
92
+ label="Prompt",
93
+ show_label=False,
94
+ max_lines=1,
95
+ placeholder="Enter your prompt",
96
+ container=False,
97
+ )
98
+
99
+ run_button = gr.Button("Run", scale=0, variant="primary")
100
+
101
+ with gr.Row(elem_id="images-container"):
102
+ image_editor = gr.ImageMask(label="Initial image", type="pil")
103
+
104
+ result = gr.Image(label="Result")
105
+
106
+ with gr.Accordion("Advanced Settings", open=False):
107
+ negative_prompt = gr.Text(
108
+ label="Negative prompt",
109
+ max_lines=1,
110
+ placeholder="Enter a negative prompt",
111
+ )
112
+
113
+ with gr.Row():
114
+ strength = gr.Slider(
115
+ label="Strength",
116
+ minimum=0.0,
117
+ maximum=1.0,
118
+ step=0.01,
119
+ value=0.0,
120
+ )
121
+
122
+ num_inference_steps = gr.Slider(
123
+ label="Number of inference steps",
124
+ minimum=0,
125
+ maximum=100,
126
+ step=1,
127
+ value=0,
128
+ )
129
+
130
+ guidance_scale = gr.Slider(
131
+ label="Guidance scale",
132
+ minimum=0.0,
133
+ maximum=100.0,
134
+ step=0.1,
135
+ value=0.0,
136
+ )
137
+ gr.on(
138
+ triggers=[run_button.click, prompt.submit],
139
+ fn=infer,
140
+ inputs=[
141
+ prompt,
142
+ image_editor,
143
+ negative_prompt,
144
+ strength,
145
+ num_inference_steps,
146
+ guidance_scale,
147
+ ],
148
+ outputs=[result],
149
+ )
150
+
151
+ if __name__ == "__main__":
152
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ accelerate
2
+ diffusers
3
+ loguru
4
+ sentencepiece
5
+ torch
6
+ transformers