Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,33 +1,37 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
| 3 |
import random
|
|
|
|
| 4 |
from diffusers import DiffusionPipeline
|
| 5 |
import torch
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
device = "cpu"
|
| 9 |
|
| 10 |
-
#
|
|
|
|
|
|
|
|
|
|
| 11 |
model_repo_id = "KingNish/Realtime-FLUX"
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
# Load the
|
| 17 |
-
pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype).to(device)
|
| 18 |
|
| 19 |
-
# Constants
|
| 20 |
MAX_SEED = np.iinfo(np.int32).max
|
| 21 |
-
MAX_IMAGE_SIZE =
|
| 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.manual_seed(seed)
|
| 29 |
|
| 30 |
-
|
|
|
|
| 31 |
image = pipe(
|
| 32 |
prompt=prompt,
|
| 33 |
negative_prompt=negative_prompt,
|
|
@@ -40,14 +44,12 @@ def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance
|
|
| 40 |
|
| 41 |
return image, seed
|
| 42 |
|
| 43 |
-
# Sample prompts
|
| 44 |
examples = [
|
| 45 |
-
"Astronaut in a jungle, cold color palette, detailed, 8k",
|
| 46 |
-
"
|
| 47 |
-
"A
|
| 48 |
]
|
| 49 |
|
| 50 |
-
# Gradio Interface
|
| 51 |
css = """
|
| 52 |
#col-container {
|
| 53 |
margin: 0 auto;
|
|
@@ -56,84 +58,26 @@ css = """
|
|
| 56 |
"""
|
| 57 |
|
| 58 |
with gr.Blocks(css=css) as demo:
|
| 59 |
-
|
| 60 |
with gr.Column(elem_id="col-container"):
|
| 61 |
-
gr.Markdown("#
|
| 62 |
-
|
| 63 |
with gr.Row():
|
| 64 |
-
prompt = gr.
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
max_lines=1,
|
| 68 |
-
placeholder="Describe the image you want to generate...",
|
| 69 |
-
container=False,
|
| 70 |
-
)
|
| 71 |
-
|
| 72 |
-
run_button = gr.Button("Generate")
|
| 73 |
-
|
| 74 |
-
result = gr.Image(label="Generated Image", show_label=False)
|
| 75 |
|
| 76 |
with gr.Accordion("Advanced Settings", open=False):
|
| 77 |
-
negative_prompt = gr.
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
placeholder="Enter a negative prompt (optional)",
|
| 81 |
-
visible=True,
|
| 82 |
-
)
|
| 83 |
-
|
| 84 |
-
seed = gr.Slider(
|
| 85 |
-
label="Seed",
|
| 86 |
-
minimum=0,
|
| 87 |
-
maximum=MAX_SEED,
|
| 88 |
-
step=1,
|
| 89 |
-
value=0,
|
| 90 |
-
)
|
| 91 |
-
|
| 92 |
-
randomize_seed = gr.Checkbox(label="Randomize Seed", value=True)
|
| 93 |
-
|
| 94 |
with gr.Row():
|
| 95 |
-
width = gr.Slider(
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
step=32,
|
| 100 |
-
value=384, # Optimal for CPU
|
| 101 |
-
)
|
| 102 |
-
height = gr.Slider(
|
| 103 |
-
label="Height",
|
| 104 |
-
minimum=256,
|
| 105 |
-
maximum=MAX_IMAGE_SIZE,
|
| 106 |
-
step=32,
|
| 107 |
-
value=384, # Optimal for CPU
|
| 108 |
-
)
|
| 109 |
-
|
| 110 |
-
with gr.Row():
|
| 111 |
-
guidance_scale = gr.Slider(
|
| 112 |
-
label="Guidance Scale",
|
| 113 |
-
minimum=0.0,
|
| 114 |
-
maximum=10.0,
|
| 115 |
-
step=0.5,
|
| 116 |
-
value=7.5,
|
| 117 |
-
)
|
| 118 |
-
|
| 119 |
-
num_inference_steps = gr.Slider(
|
| 120 |
-
label="Inference Steps",
|
| 121 |
-
minimum=1,
|
| 122 |
-
maximum=25, # Keep lower for CPU performance
|
| 123 |
-
step=1,
|
| 124 |
-
value=15, # Reasonable default for CPU
|
| 125 |
-
)
|
| 126 |
|
| 127 |
-
gr.Examples(
|
| 128 |
-
examples=examples,
|
| 129 |
-
inputs=[prompt]
|
| 130 |
-
)
|
| 131 |
|
| 132 |
-
gr.on(
|
| 133 |
-
triggers=[run_button.click, prompt.submit],
|
| 134 |
-
fn=infer,
|
| 135 |
-
inputs=[prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
|
| 136 |
-
outputs=[result, seed]
|
| 137 |
-
)
|
| 138 |
|
| 139 |
demo.queue().launch()
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
| 3 |
import random
|
| 4 |
+
import os # Import os to access secrets
|
| 5 |
from diffusers import DiffusionPipeline
|
| 6 |
import torch
|
| 7 |
|
| 8 |
+
# Set device
|
| 9 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 10 |
|
| 11 |
+
# Use Hugging Face secret from your space environment
|
| 12 |
+
access_token = os.getenv('HUGGINGFACE_TOKEN')
|
| 13 |
+
|
| 14 |
+
# Replace with the correct model repo ID
|
| 15 |
model_repo_id = "KingNish/Realtime-FLUX"
|
| 16 |
|
| 17 |
+
# Set the torch dtype based on availability of CUDA
|
| 18 |
+
if torch.cuda.is_available():
|
| 19 |
+
torch_dtype = torch.float16
|
| 20 |
+
else:
|
| 21 |
+
torch_dtype = torch.float32
|
| 22 |
|
| 23 |
+
# Load the pipeline with the access token
|
| 24 |
+
pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype, use_auth_token=access_token).to(device)
|
| 25 |
|
|
|
|
| 26 |
MAX_SEED = np.iinfo(np.int32).max
|
| 27 |
+
MAX_IMAGE_SIZE = 1024
|
| 28 |
|
| 29 |
def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps, progress=gr.Progress(track_tqdm=True)):
|
|
|
|
| 30 |
if randomize_seed:
|
| 31 |
seed = random.randint(0, MAX_SEED)
|
|
|
|
|
|
|
| 32 |
|
| 33 |
+
generator = torch.Generator().manual_seed(seed)
|
| 34 |
+
|
| 35 |
image = pipe(
|
| 36 |
prompt=prompt,
|
| 37 |
negative_prompt=negative_prompt,
|
|
|
|
| 44 |
|
| 45 |
return image, seed
|
| 46 |
|
|
|
|
| 47 |
examples = [
|
| 48 |
+
"Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
|
| 49 |
+
"An astronaut riding a green horse",
|
| 50 |
+
"A delicious ceviche cheesecake slice",
|
| 51 |
]
|
| 52 |
|
|
|
|
| 53 |
css = """
|
| 54 |
#col-container {
|
| 55 |
margin: 0 auto;
|
|
|
|
| 58 |
"""
|
| 59 |
|
| 60 |
with gr.Blocks(css=css) as demo:
|
|
|
|
| 61 |
with gr.Column(elem_id="col-container"):
|
| 62 |
+
gr.Markdown("# Text-to-Image Gradio Template")
|
|
|
|
| 63 |
with gr.Row():
|
| 64 |
+
prompt = gr.Text(label="Prompt", show_label=False, max_lines=1, placeholder="Enter your prompt", container=False)
|
| 65 |
+
run_button = gr.Button("Run", scale=0)
|
| 66 |
+
result = gr.Image(label="Result", show_label=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
with gr.Accordion("Advanced Settings", open=False):
|
| 69 |
+
negative_prompt = gr.Text(label="Negative prompt", max_lines=1, placeholder="Enter a negative prompt", visible=False)
|
| 70 |
+
seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
|
| 71 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
with gr.Row():
|
| 73 |
+
width = gr.Slider(label="Width", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=1024)
|
| 74 |
+
height = gr.Slider(label="Height", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=1024)
|
| 75 |
+
guidance_scale = gr.Slider(label="Guidance scale", minimum=0.0, maximum=10.0, step=0.1, value=7.5)
|
| 76 |
+
num_inference_steps = gr.Slider(label="Number of inference steps", minimum=1, maximum=50, step=1, value=25)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
+
gr.Examples(examples=examples, inputs=[prompt])
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
+
gr.on(run_button.click, infer, inputs=[prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps], outputs=[result, seed])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
demo.queue().launch()
|
| 83 |
+
|