Spaces:
Runtime error
Runtime error
Upload 3 files
Browse files- README.md +18 -10
- app.py +54 -86
- requirements.txt +4 -3
README.md
CHANGED
|
@@ -1,12 +1,20 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
-
sdk: gradio
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
---
|
| 11 |
-
|
| 12 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 1 |
---
|
| 2 |
+
title: "My Cool Space"
|
| 3 |
+
emoji: "π"
|
| 4 |
+
colorFrom: "blue"
|
| 5 |
+
colorTo: "purple"
|
| 6 |
+
sdk: "gradio"
|
| 7 |
+
python_version: "3.10"
|
| 8 |
+
sdk_version: "3.28.0"
|
| 9 |
+
suggested_hardware: "t4-small"
|
| 10 |
+
app_file: "app.py"
|
| 11 |
+
models:
|
| 12 |
+
- runwayml/stable-diffusion-v1-5
|
| 13 |
+
datasets:
|
| 14 |
+
- mozilla-foundation/common_voice_13_0
|
| 15 |
+
tags:
|
| 16 |
+
- image-generation
|
| 17 |
+
- AI
|
| 18 |
+
short_description: "A space for generating AI-based images."
|
| 19 |
+
thumbnail: "https://example.com/thumbnail.png"
|
| 20 |
---
|
|
|
|
|
|
app.py
CHANGED
|
@@ -1,74 +1,56 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
| 3 |
import random
|
| 4 |
-
|
| 5 |
-
# import spaces #[uncomment to use ZeroGPU]
|
| 6 |
-
from diffusers import DiffusionPipeline
|
| 7 |
import torch
|
|
|
|
| 8 |
|
|
|
|
| 9 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 10 |
-
model_repo_id = "stabilityai/sdxl-turbo" # Replace to the model you would like to use
|
| 11 |
|
| 12 |
-
|
| 13 |
-
torch_dtype = torch.float16
|
| 14 |
-
else:
|
| 15 |
-
torch_dtype = torch.float32
|
| 16 |
-
|
| 17 |
-
pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype)
|
| 18 |
-
pipe = pipe.to(device)
|
| 19 |
|
| 20 |
MAX_SEED = np.iinfo(np.int32).max
|
| 21 |
-
MAX_IMAGE_SIZE =
|
| 22 |
-
|
| 23 |
|
| 24 |
-
|
| 25 |
-
def infer(
|
| 26 |
-
prompt,
|
| 27 |
-
negative_prompt,
|
| 28 |
-
seed,
|
| 29 |
-
randomize_seed,
|
| 30 |
-
width,
|
| 31 |
-
height,
|
| 32 |
-
guidance_scale,
|
| 33 |
-
num_inference_steps,
|
| 34 |
-
progress=gr.Progress(track_tqdm=True),
|
| 35 |
-
):
|
| 36 |
if randomize_seed:
|
| 37 |
seed = random.randint(0, MAX_SEED)
|
| 38 |
-
|
| 39 |
generator = torch.Generator().manual_seed(seed)
|
| 40 |
-
|
| 41 |
image = pipe(
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
).images[0]
|
| 50 |
-
|
| 51 |
return image, seed
|
| 52 |
-
|
| 53 |
-
|
| 54 |
examples = [
|
| 55 |
-
"
|
| 56 |
-
"
|
| 57 |
-
"
|
| 58 |
]
|
| 59 |
|
| 60 |
-
css
|
| 61 |
#col-container {
|
| 62 |
margin: 0 auto;
|
| 63 |
-
max-width:
|
| 64 |
}
|
| 65 |
"""
|
| 66 |
|
| 67 |
with gr.Blocks(css=css) as demo:
|
|
|
|
| 68 |
with gr.Column(elem_id="col-container"):
|
| 69 |
-
gr.Markdown("
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
| 71 |
with gr.Row():
|
|
|
|
| 72 |
prompt = gr.Text(
|
| 73 |
label="Prompt",
|
| 74 |
show_label=False,
|
|
@@ -76,19 +58,13 @@ with gr.Blocks(css=css) as demo:
|
|
| 76 |
placeholder="Enter your prompt",
|
| 77 |
container=False,
|
| 78 |
)
|
| 79 |
-
|
| 80 |
-
run_button = gr.Button("Run", scale=0
|
| 81 |
-
|
| 82 |
result = gr.Image(label="Result", show_label=False)
|
| 83 |
-
|
| 84 |
with gr.Accordion("Advanced Settings", open=False):
|
| 85 |
-
|
| 86 |
-
label="Negative prompt",
|
| 87 |
-
max_lines=1,
|
| 88 |
-
placeholder="Enter a negative prompt",
|
| 89 |
-
visible=False,
|
| 90 |
-
)
|
| 91 |
-
|
| 92 |
seed = gr.Slider(
|
| 93 |
label="Seed",
|
| 94 |
minimum=0,
|
|
@@ -96,59 +72,51 @@ with gr.Blocks(css=css) as demo:
|
|
| 96 |
step=1,
|
| 97 |
value=0,
|
| 98 |
)
|
| 99 |
-
|
| 100 |
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
| 101 |
-
|
| 102 |
with gr.Row():
|
|
|
|
| 103 |
width = gr.Slider(
|
| 104 |
label="Width",
|
| 105 |
minimum=256,
|
| 106 |
maximum=MAX_IMAGE_SIZE,
|
| 107 |
step=32,
|
| 108 |
-
value=1024,
|
| 109 |
)
|
| 110 |
-
|
| 111 |
height = gr.Slider(
|
| 112 |
label="Height",
|
| 113 |
minimum=256,
|
| 114 |
maximum=MAX_IMAGE_SIZE,
|
| 115 |
step=32,
|
| 116 |
-
value=1024,
|
| 117 |
)
|
| 118 |
-
|
| 119 |
with gr.Row():
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
minimum=0.0,
|
| 123 |
-
maximum=10.0,
|
| 124 |
-
step=0.1,
|
| 125 |
-
value=0.0, # Replace with defaults that work for your model
|
| 126 |
-
)
|
| 127 |
-
|
| 128 |
num_inference_steps = gr.Slider(
|
| 129 |
label="Number of inference steps",
|
| 130 |
minimum=1,
|
| 131 |
maximum=50,
|
| 132 |
step=1,
|
| 133 |
-
value=
|
| 134 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
|
| 136 |
-
gr.Examples(examples=examples, inputs=[prompt])
|
| 137 |
gr.on(
|
| 138 |
triggers=[run_button.click, prompt.submit],
|
| 139 |
-
fn=infer,
|
| 140 |
-
inputs=[
|
| 141 |
-
|
| 142 |
-
negative_prompt,
|
| 143 |
-
seed,
|
| 144 |
-
randomize_seed,
|
| 145 |
-
width,
|
| 146 |
-
height,
|
| 147 |
-
guidance_scale,
|
| 148 |
-
num_inference_steps,
|
| 149 |
-
],
|
| 150 |
-
outputs=[result, seed],
|
| 151 |
)
|
| 152 |
|
| 153 |
-
|
| 154 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
| 3 |
import random
|
| 4 |
+
import spaces
|
|
|
|
|
|
|
| 5 |
import torch
|
| 6 |
+
from diffusers import DiffusionPipeline
|
| 7 |
|
| 8 |
+
dtype = torch.bfloat16
|
| 9 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
|
|
| 10 |
|
| 11 |
+
pipe = DiffusionPipeline.from_pretrained("black-forest-labs/FLUX.1-schnell", torch_dtype=dtype).to(device)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
MAX_SEED = np.iinfo(np.int32).max
|
| 14 |
+
MAX_IMAGE_SIZE = 2048
|
|
|
|
| 15 |
|
| 16 |
+
@spaces.GPU()
|
| 17 |
+
def infer(prompt, seed=42, randomize_seed=False, width=1024, height=1024, num_inference_steps=4, progress=gr.Progress(track_tqdm=True)):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
if randomize_seed:
|
| 19 |
seed = random.randint(0, MAX_SEED)
|
|
|
|
| 20 |
generator = torch.Generator().manual_seed(seed)
|
|
|
|
| 21 |
image = pipe(
|
| 22 |
+
prompt = prompt,
|
| 23 |
+
width = width,
|
| 24 |
+
height = height,
|
| 25 |
+
num_inference_steps = num_inference_steps,
|
| 26 |
+
generator = generator,
|
| 27 |
+
guidance_scale=0.0
|
| 28 |
+
).images[0]
|
|
|
|
|
|
|
| 29 |
return image, seed
|
| 30 |
+
|
|
|
|
| 31 |
examples = [
|
| 32 |
+
"a tiny astronaut hatching from an egg on the moon",
|
| 33 |
+
"a cat holding a sign that says hello world",
|
| 34 |
+
"an anime illustration of a wiener schnitzel",
|
| 35 |
]
|
| 36 |
|
| 37 |
+
css="""
|
| 38 |
#col-container {
|
| 39 |
margin: 0 auto;
|
| 40 |
+
max-width: 520px;
|
| 41 |
}
|
| 42 |
"""
|
| 43 |
|
| 44 |
with gr.Blocks(css=css) as demo:
|
| 45 |
+
|
| 46 |
with gr.Column(elem_id="col-container"):
|
| 47 |
+
gr.Markdown(f"""# FLUX.1 [schnell]
|
| 48 |
+
12B param rectified flow transformer distilled from [FLUX.1 [pro]](https://blackforestlabs.ai/) for 4 step generation
|
| 49 |
+
[[blog](https://blackforestlabs.ai/announcing-black-forest-labs/)] [[model](https://huggingface.co/black-forest-labs/FLUX.1-schnell)]
|
| 50 |
+
""")
|
| 51 |
+
|
| 52 |
with gr.Row():
|
| 53 |
+
|
| 54 |
prompt = gr.Text(
|
| 55 |
label="Prompt",
|
| 56 |
show_label=False,
|
|
|
|
| 58 |
placeholder="Enter your prompt",
|
| 59 |
container=False,
|
| 60 |
)
|
| 61 |
+
|
| 62 |
+
run_button = gr.Button("Run", scale=0)
|
| 63 |
+
|
| 64 |
result = gr.Image(label="Result", show_label=False)
|
| 65 |
+
|
| 66 |
with gr.Accordion("Advanced Settings", open=False):
|
| 67 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
seed = gr.Slider(
|
| 69 |
label="Seed",
|
| 70 |
minimum=0,
|
|
|
|
| 72 |
step=1,
|
| 73 |
value=0,
|
| 74 |
)
|
| 75 |
+
|
| 76 |
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
| 77 |
+
|
| 78 |
with gr.Row():
|
| 79 |
+
|
| 80 |
width = gr.Slider(
|
| 81 |
label="Width",
|
| 82 |
minimum=256,
|
| 83 |
maximum=MAX_IMAGE_SIZE,
|
| 84 |
step=32,
|
| 85 |
+
value=1024,
|
| 86 |
)
|
| 87 |
+
|
| 88 |
height = gr.Slider(
|
| 89 |
label="Height",
|
| 90 |
minimum=256,
|
| 91 |
maximum=MAX_IMAGE_SIZE,
|
| 92 |
step=32,
|
| 93 |
+
value=1024,
|
| 94 |
)
|
| 95 |
+
|
| 96 |
with gr.Row():
|
| 97 |
+
|
| 98 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
num_inference_steps = gr.Slider(
|
| 100 |
label="Number of inference steps",
|
| 101 |
minimum=1,
|
| 102 |
maximum=50,
|
| 103 |
step=1,
|
| 104 |
+
value=4,
|
| 105 |
)
|
| 106 |
+
|
| 107 |
+
gr.Examples(
|
| 108 |
+
examples = examples,
|
| 109 |
+
fn = infer,
|
| 110 |
+
inputs = [prompt],
|
| 111 |
+
outputs = [result, seed],
|
| 112 |
+
cache_examples="lazy"
|
| 113 |
+
)
|
| 114 |
|
|
|
|
| 115 |
gr.on(
|
| 116 |
triggers=[run_button.click, prompt.submit],
|
| 117 |
+
fn = infer,
|
| 118 |
+
inputs = [prompt, seed, randomize_seed, width, height, num_inference_steps],
|
| 119 |
+
outputs = [result, seed]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
)
|
| 121 |
|
| 122 |
+
demo.launch()
|
|
|
requirements.txt
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
accelerate
|
| 2 |
-
diffusers
|
| 3 |
invisible_watermark
|
| 4 |
torch
|
| 5 |
-
transformers
|
| 6 |
-
xformers
|
|
|
|
|
|
| 1 |
accelerate
|
| 2 |
+
git+https://github.com/huggingface/diffusers.git
|
| 3 |
invisible_watermark
|
| 4 |
torch
|
| 5 |
+
transformers==4.42.4
|
| 6 |
+
xformers
|
| 7 |
+
sentencepiece
|