Spaces:
Build error
Build error
smdbs commited on
Commit ·
a98de27
1
Parent(s): 757c20b
feat: add advanced config options
Browse files
app.py
CHANGED
|
@@ -9,6 +9,10 @@ import torch.nn.functional as F
|
|
| 9 |
from huggingface_hub import InferenceClient
|
| 10 |
from diffusers import StableDiffusionPipeline, EulerDiscreteScheduler
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
# Make sure to set the environment variable HF_TOKEN to your Hugging Face token for using InferenceClient
|
| 13 |
HF_TOKEN = os.environ.get("HF_TOKEN", "YOUR_HF_TOKEN")
|
| 14 |
model_name = "stabilityai/stable-diffusion-2-1-base"
|
|
@@ -16,12 +20,6 @@ client = InferenceClient(model_name, token=HF_TOKEN)
|
|
| 16 |
|
| 17 |
scheduler = None
|
| 18 |
pipe = None
|
| 19 |
-
config = {
|
| 20 |
-
"negative_prompt": "",
|
| 21 |
-
"width": 256,
|
| 22 |
-
"height": 256,
|
| 23 |
-
"num_inference_steps": 20,
|
| 24 |
-
}
|
| 25 |
|
| 26 |
|
| 27 |
def load_model():
|
|
@@ -38,20 +36,48 @@ def load_model():
|
|
| 38 |
|
| 39 |
|
| 40 |
def sd_2_1_base(
|
| 41 |
-
prompt: str,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
) -> Union[np.ndarray, Image.Image, str, Path, None]:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
if is_local:
|
| 44 |
if scheduler is None or pipe is None:
|
| 45 |
load_model()
|
| 46 |
|
| 47 |
image = pipe(
|
| 48 |
prompt,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
).images[0]
|
| 50 |
-
|
|
|
|
| 51 |
|
| 52 |
else:
|
| 53 |
-
output = client.text_to_image(
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
|
| 57 |
with gr.Blocks() as ui:
|
|
@@ -66,16 +92,83 @@ with gr.Blocks() as ui:
|
|
| 66 |
label="Prompt",
|
| 67 |
)
|
| 68 |
submit = gr.Button("Generate Image", variant="primary")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
with gr.Column():
|
| 70 |
is_local = gr.Checkbox(
|
| 71 |
label="Check this box to use a local model 🖥️",
|
| 72 |
)
|
| 73 |
-
|
| 74 |
gr.HTML(
|
| 75 |
f"<p style='text-align: center;'>This app uses the <a href='https://huggingface.co/{model_name}'>{model_name}</a> model.</p>"
|
| 76 |
)
|
| 77 |
|
| 78 |
-
submit.click(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
if __name__ == "__main__":
|
| 81 |
ui.launch(share=True)
|
|
|
|
| 9 |
from huggingface_hub import InferenceClient
|
| 10 |
from diffusers import StableDiffusionPipeline, EulerDiscreteScheduler
|
| 11 |
|
| 12 |
+
MAX_SEED = np.iinfo(np.int32).max
|
| 13 |
+
MAX_IMAGE_SIZE = 1280
|
| 14 |
+
|
| 15 |
+
|
| 16 |
# Make sure to set the environment variable HF_TOKEN to your Hugging Face token for using InferenceClient
|
| 17 |
HF_TOKEN = os.environ.get("HF_TOKEN", "YOUR_HF_TOKEN")
|
| 18 |
model_name = "stabilityai/stable-diffusion-2-1-base"
|
|
|
|
| 20 |
|
| 21 |
scheduler = None
|
| 22 |
pipe = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
|
| 25 |
def load_model():
|
|
|
|
| 36 |
|
| 37 |
|
| 38 |
def sd_2_1_base(
|
| 39 |
+
prompt: str,
|
| 40 |
+
is_local: bool,
|
| 41 |
+
negative_prompt: str,
|
| 42 |
+
seed: int,
|
| 43 |
+
randomize_seed: bool,
|
| 44 |
+
guidance_scale: float,
|
| 45 |
+
num_inference_steps: int,
|
| 46 |
+
width: int,
|
| 47 |
+
height: int,
|
| 48 |
) -> Union[np.ndarray, Image.Image, str, Path, None]:
|
| 49 |
+
if randomize_seed:
|
| 50 |
+
seed = np.random.randint(0, MAX_SEED)
|
| 51 |
+
generator = torch.Generator().manual_seed(seed)
|
| 52 |
+
|
| 53 |
if is_local:
|
| 54 |
if scheduler is None or pipe is None:
|
| 55 |
load_model()
|
| 56 |
|
| 57 |
image = pipe(
|
| 58 |
prompt,
|
| 59 |
+
negative_prompt=negative_prompt,
|
| 60 |
+
guidance_scale=guidance_scale,
|
| 61 |
+
num_inference_steps=num_inference_steps,
|
| 62 |
+
width=width,
|
| 63 |
+
height=height,
|
| 64 |
+
generator=generator,
|
| 65 |
).images[0]
|
| 66 |
+
|
| 67 |
+
return image, seed
|
| 68 |
|
| 69 |
else:
|
| 70 |
+
output = client.text_to_image(
|
| 71 |
+
prompt,
|
| 72 |
+
negative_prompt=negative_prompt,
|
| 73 |
+
seed=seed,
|
| 74 |
+
randomize_seed=randomize_seed,
|
| 75 |
+
guidance_scale=guidance_scale,
|
| 76 |
+
num_inference_steps=num_inference_steps,
|
| 77 |
+
width=width,
|
| 78 |
+
height=height,
|
| 79 |
+
)
|
| 80 |
+
return output, seed
|
| 81 |
|
| 82 |
|
| 83 |
with gr.Blocks() as ui:
|
|
|
|
| 92 |
label="Prompt",
|
| 93 |
)
|
| 94 |
submit = gr.Button("Generate Image", variant="primary")
|
| 95 |
+
# This part is adopted from Stabilityai's stable-diffusion-3-medium space
|
| 96 |
+
# https://huggingface.co/spaces/stabilityai/stable-diffusion-3-medium
|
| 97 |
+
with gr.Accordion("Advanced Settings", open=False):
|
| 98 |
+
negative_prompt = gr.Text(
|
| 99 |
+
label="Negative prompt",
|
| 100 |
+
max_lines=1,
|
| 101 |
+
placeholder="Enter a negative prompt",
|
| 102 |
+
)
|
| 103 |
+
|
| 104 |
+
seed = gr.Slider(
|
| 105 |
+
label="Seed",
|
| 106 |
+
minimum=0,
|
| 107 |
+
maximum=MAX_SEED,
|
| 108 |
+
step=1,
|
| 109 |
+
value=0,
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
| 113 |
+
|
| 114 |
+
with gr.Row():
|
| 115 |
+
|
| 116 |
+
width = gr.Slider(
|
| 117 |
+
label="Width",
|
| 118 |
+
minimum=256,
|
| 119 |
+
maximum=MAX_IMAGE_SIZE,
|
| 120 |
+
step=50,
|
| 121 |
+
value=512,
|
| 122 |
+
)
|
| 123 |
+
|
| 124 |
+
height = gr.Slider(
|
| 125 |
+
label="Height",
|
| 126 |
+
minimum=256,
|
| 127 |
+
maximum=MAX_IMAGE_SIZE,
|
| 128 |
+
step=50,
|
| 129 |
+
value=512,
|
| 130 |
+
)
|
| 131 |
+
|
| 132 |
+
with gr.Row():
|
| 133 |
+
guidance_scale = gr.Slider(
|
| 134 |
+
label="Guidance scale",
|
| 135 |
+
minimum=0.0,
|
| 136 |
+
maximum=10.0,
|
| 137 |
+
step=0.1,
|
| 138 |
+
value=5.0,
|
| 139 |
+
)
|
| 140 |
+
|
| 141 |
+
num_inference_steps = gr.Slider(
|
| 142 |
+
label="Number of inference steps",
|
| 143 |
+
minimum=1,
|
| 144 |
+
maximum=50,
|
| 145 |
+
step=1,
|
| 146 |
+
value=28,
|
| 147 |
+
)
|
| 148 |
with gr.Column():
|
| 149 |
is_local = gr.Checkbox(
|
| 150 |
label="Check this box to use a local model 🖥️",
|
| 151 |
)
|
| 152 |
+
img_out = gr.Image(label="Generated Image")
|
| 153 |
gr.HTML(
|
| 154 |
f"<p style='text-align: center;'>This app uses the <a href='https://huggingface.co/{model_name}'>{model_name}</a> model.</p>"
|
| 155 |
)
|
| 156 |
|
| 157 |
+
submit.click(
|
| 158 |
+
sd_2_1_base,
|
| 159 |
+
inputs=[
|
| 160 |
+
prompt,
|
| 161 |
+
is_local,
|
| 162 |
+
negative_prompt,
|
| 163 |
+
seed,
|
| 164 |
+
randomize_seed,
|
| 165 |
+
guidance_scale,
|
| 166 |
+
num_inference_steps,
|
| 167 |
+
width,
|
| 168 |
+
height,
|
| 169 |
+
],
|
| 170 |
+
outputs=[img_out, seed],
|
| 171 |
+
)
|
| 172 |
|
| 173 |
if __name__ == "__main__":
|
| 174 |
ui.launch(share=True)
|