Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,87 +1,94 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
| 3 |
import random
|
| 4 |
-
#import spaces #[uncomment to use ZeroGPU]
|
| 5 |
from diffusers import DiffusionPipeline
|
| 6 |
import torch
|
| 7 |
|
|
|
|
| 8 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 9 |
-
model_repo_id = "stabilityai/sdxl-turbo" #Replace to the model you would like to use
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
else:
|
| 14 |
-
torch_dtype = torch.float32
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype)
|
| 17 |
pipe = pipe.to(device)
|
| 18 |
|
|
|
|
| 19 |
MAX_SEED = np.iinfo(np.int32).max
|
| 20 |
MAX_IMAGE_SIZE = 1024
|
| 21 |
|
| 22 |
-
|
| 23 |
def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps, progress=gr.Progress(track_tqdm=True)):
|
| 24 |
-
|
|
|
|
| 25 |
if randomize_seed:
|
| 26 |
seed = random.randint(0, MAX_SEED)
|
| 27 |
-
|
|
|
|
| 28 |
generator = torch.Generator().manual_seed(seed)
|
| 29 |
|
|
|
|
| 30 |
image = pipe(
|
| 31 |
-
prompt
|
| 32 |
-
negative_prompt
|
| 33 |
-
guidance_scale
|
| 34 |
-
num_inference_steps
|
| 35 |
-
width
|
| 36 |
-
height
|
| 37 |
-
generator
|
| 38 |
).images[0]
|
| 39 |
|
| 40 |
return image, seed
|
| 41 |
|
|
|
|
| 42 |
examples = [
|
| 43 |
-
"
|
| 44 |
-
"
|
| 45 |
-
"A
|
| 46 |
]
|
| 47 |
|
| 48 |
-
|
|
|
|
| 49 |
#col-container {
|
| 50 |
margin: 0 auto;
|
| 51 |
max-width: 640px;
|
| 52 |
}
|
| 53 |
"""
|
| 54 |
|
|
|
|
| 55 |
with gr.Blocks(css=css) as demo:
|
| 56 |
|
| 57 |
with gr.Column(elem_id="col-container"):
|
| 58 |
gr.Markdown(f"""
|
| 59 |
-
# Text-to-Image
|
| 60 |
""")
|
| 61 |
|
|
|
|
| 62 |
with gr.Row():
|
| 63 |
-
|
| 64 |
prompt = gr.Text(
|
| 65 |
label="Prompt",
|
| 66 |
show_label=False,
|
| 67 |
max_lines=1,
|
| 68 |
-
placeholder="Enter your prompt",
|
| 69 |
container=False,
|
| 70 |
)
|
| 71 |
-
|
| 72 |
-
run_button = gr.Button("Run", scale=0)
|
| 73 |
|
| 74 |
-
|
|
|
|
| 75 |
|
|
|
|
| 76 |
with gr.Accordion("Advanced Settings", open=False):
|
| 77 |
-
|
| 78 |
negative_prompt = gr.Text(
|
| 79 |
-
label="Negative
|
| 80 |
max_lines=1,
|
| 81 |
-
placeholder="Enter a negative prompt",
|
| 82 |
visible=False,
|
| 83 |
)
|
| 84 |
-
|
| 85 |
seed = gr.Slider(
|
| 86 |
label="Seed",
|
| 87 |
minimum=0,
|
|
@@ -89,54 +96,55 @@ with gr.Blocks(css=css) as demo:
|
|
| 89 |
step=1,
|
| 90 |
value=0,
|
| 91 |
)
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
with gr.Row():
|
| 96 |
-
|
| 97 |
width = gr.Slider(
|
| 98 |
label="Width",
|
| 99 |
minimum=256,
|
| 100 |
maximum=MAX_IMAGE_SIZE,
|
| 101 |
step=32,
|
| 102 |
-
value=
|
| 103 |
)
|
| 104 |
-
|
| 105 |
height = gr.Slider(
|
| 106 |
label="Height",
|
| 107 |
minimum=256,
|
| 108 |
maximum=MAX_IMAGE_SIZE,
|
| 109 |
step=32,
|
| 110 |
-
value=
|
| 111 |
)
|
| 112 |
|
|
|
|
| 113 |
with gr.Row():
|
| 114 |
-
|
| 115 |
guidance_scale = gr.Slider(
|
| 116 |
-
label="Guidance
|
| 117 |
minimum=0.0,
|
| 118 |
maximum=10.0,
|
| 119 |
step=0.1,
|
| 120 |
-
value=
|
| 121 |
)
|
| 122 |
-
|
| 123 |
num_inference_steps = gr.Slider(
|
| 124 |
-
label="Number of
|
| 125 |
minimum=1,
|
| 126 |
maximum=50,
|
| 127 |
step=1,
|
| 128 |
-
value=
|
| 129 |
)
|
| 130 |
-
|
|
|
|
| 131 |
gr.Examples(
|
| 132 |
-
examples
|
| 133 |
-
inputs
|
| 134 |
)
|
|
|
|
|
|
|
| 135 |
gr.on(
|
| 136 |
triggers=[run_button.click, prompt.submit],
|
| 137 |
-
fn
|
| 138 |
-
inputs
|
| 139 |
-
outputs
|
| 140 |
)
|
| 141 |
|
| 142 |
-
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
| 3 |
import random
|
|
|
|
| 4 |
from diffusers import DiffusionPipeline
|
| 5 |
import torch
|
| 6 |
|
| 7 |
+
# Check if CUDA is available
|
| 8 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
|
|
| 9 |
|
| 10 |
+
# Define the model you want to use (black-forest-labs/FLUX.1-dev)
|
| 11 |
+
model_repo_id = "black-forest-labs/FLUX.1-dev"
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
# Set the appropriate torch dtype depending on the available hardware
|
| 14 |
+
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
|
| 15 |
+
|
| 16 |
+
# Load the model
|
| 17 |
pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype)
|
| 18 |
pipe = pipe.to(device)
|
| 19 |
|
| 20 |
+
# Constants for seed and image size
|
| 21 |
MAX_SEED = np.iinfo(np.int32).max
|
| 22 |
MAX_IMAGE_SIZE = 1024
|
| 23 |
|
| 24 |
+
# Function to perform inference using the model
|
| 25 |
def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps, progress=gr.Progress(track_tqdm=True)):
|
| 26 |
+
|
| 27 |
+
# If randomize seed is checked, generate a random seed
|
| 28 |
if randomize_seed:
|
| 29 |
seed = random.randint(0, MAX_SEED)
|
| 30 |
+
|
| 31 |
+
# Set the seed for reproducibility
|
| 32 |
generator = torch.Generator().manual_seed(seed)
|
| 33 |
|
| 34 |
+
# Generate the image based on the prompt
|
| 35 |
image = pipe(
|
| 36 |
+
prompt=prompt,
|
| 37 |
+
negative_prompt=negative_prompt,
|
| 38 |
+
guidance_scale=guidance_scale,
|
| 39 |
+
num_inference_steps=num_inference_steps,
|
| 40 |
+
width=width,
|
| 41 |
+
height=height,
|
| 42 |
+
generator=generator
|
| 43 |
).images[0]
|
| 44 |
|
| 45 |
return image, seed
|
| 46 |
|
| 47 |
+
# Example prompts for testing the model
|
| 48 |
examples = [
|
| 49 |
+
"A futuristic city skyline at sunset",
|
| 50 |
+
"A cat riding a bicycle in space",
|
| 51 |
+
"A surreal painting of a dreamlike forest",
|
| 52 |
]
|
| 53 |
|
| 54 |
+
# Custom CSS for styling the UI
|
| 55 |
+
css = """
|
| 56 |
#col-container {
|
| 57 |
margin: 0 auto;
|
| 58 |
max-width: 640px;
|
| 59 |
}
|
| 60 |
"""
|
| 61 |
|
| 62 |
+
# Create the Gradio interface
|
| 63 |
with gr.Blocks(css=css) as demo:
|
| 64 |
|
| 65 |
with gr.Column(elem_id="col-container"):
|
| 66 |
gr.Markdown(f"""
|
| 67 |
+
# Text-to-Image Generation using FLUX.1-dev Model
|
| 68 |
""")
|
| 69 |
|
| 70 |
+
# Input row for prompt and run button
|
| 71 |
with gr.Row():
|
|
|
|
| 72 |
prompt = gr.Text(
|
| 73 |
label="Prompt",
|
| 74 |
show_label=False,
|
| 75 |
max_lines=1,
|
| 76 |
+
placeholder="Enter your text prompt",
|
| 77 |
container=False,
|
| 78 |
)
|
| 79 |
+
run_button = gr.Button("Generate Image", scale=0)
|
|
|
|
| 80 |
|
| 81 |
+
# Output for the generated image
|
| 82 |
+
result = gr.Image(label="Generated Image", show_label=False)
|
| 83 |
|
| 84 |
+
# Accordion for advanced settings
|
| 85 |
with gr.Accordion("Advanced Settings", open=False):
|
|
|
|
| 86 |
negative_prompt = gr.Text(
|
| 87 |
+
label="Negative Prompt",
|
| 88 |
max_lines=1,
|
| 89 |
+
placeholder="Enter a negative prompt (optional)",
|
| 90 |
visible=False,
|
| 91 |
)
|
|
|
|
| 92 |
seed = gr.Slider(
|
| 93 |
label="Seed",
|
| 94 |
minimum=0,
|
|
|
|
| 96 |
step=1,
|
| 97 |
value=0,
|
| 98 |
)
|
| 99 |
+
randomize_seed = gr.Checkbox(label="Randomize Seed", value=True)
|
| 100 |
+
|
| 101 |
+
# Sliders for width and height of the output image
|
| 102 |
with gr.Row():
|
|
|
|
| 103 |
width = gr.Slider(
|
| 104 |
label="Width",
|
| 105 |
minimum=256,
|
| 106 |
maximum=MAX_IMAGE_SIZE,
|
| 107 |
step=32,
|
| 108 |
+
value=512, # Default width value
|
| 109 |
)
|
|
|
|
| 110 |
height = gr.Slider(
|
| 111 |
label="Height",
|
| 112 |
minimum=256,
|
| 113 |
maximum=MAX_IMAGE_SIZE,
|
| 114 |
step=32,
|
| 115 |
+
value=512, # Default height value
|
| 116 |
)
|
| 117 |
|
| 118 |
+
# Sliders for guidance scale and number of inference steps
|
| 119 |
with gr.Row():
|
|
|
|
| 120 |
guidance_scale = gr.Slider(
|
| 121 |
+
label="Guidance Scale",
|
| 122 |
minimum=0.0,
|
| 123 |
maximum=10.0,
|
| 124 |
step=0.1,
|
| 125 |
+
value=7.5, # Default value that works for most models
|
| 126 |
)
|
|
|
|
| 127 |
num_inference_steps = gr.Slider(
|
| 128 |
+
label="Number of Inference Steps",
|
| 129 |
minimum=1,
|
| 130 |
maximum=50,
|
| 131 |
step=1,
|
| 132 |
+
value=25, # Default number of steps for better image quality
|
| 133 |
)
|
| 134 |
+
|
| 135 |
+
# Example inputs for quick testing
|
| 136 |
gr.Examples(
|
| 137 |
+
examples=examples,
|
| 138 |
+
inputs=[prompt]
|
| 139 |
)
|
| 140 |
+
|
| 141 |
+
# Set up triggers for image generation when prompt is submitted or button is clicked
|
| 142 |
gr.on(
|
| 143 |
triggers=[run_button.click, prompt.submit],
|
| 144 |
+
fn=infer,
|
| 145 |
+
inputs=[prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
|
| 146 |
+
outputs=[result, seed]
|
| 147 |
)
|
| 148 |
|
| 149 |
+
# Launch the Gradio app
|
| 150 |
+
demo.queue().launch()
|