Spaces:
Running
on
Zero
Running
on
Zero
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,155 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from diffusers import StableDiffusionXLPipeline
|
3 |
+
import gradio as gr
|
4 |
+
import random
|
5 |
+
import transformers
|
6 |
+
import numpy as np
|
7 |
+
from transformers import T5Tokenizer, T5ForConditionalGeneration
|
8 |
+
import spaces
|
9 |
+
|
10 |
+
|
11 |
+
if torch.cuda.is_available():
|
12 |
+
device = "cuda"
|
13 |
+
print("Using GPU")
|
14 |
+
else:
|
15 |
+
device = "cpu"
|
16 |
+
print("Using CPU")
|
17 |
+
|
18 |
+
|
19 |
+
MAX_SEED = np.iinfo(np.int32).max
|
20 |
+
|
21 |
+
|
22 |
+
# Initialize the pipeline and download the sdxl flash model
|
23 |
+
pipe = StableDiffusionXLPipeline.from_pretrained("sd-community/sdxl-flash", torch_dtype=torch.float16)
|
24 |
+
pipe.to(device)
|
25 |
+
|
26 |
+
# Ensure sampler uses "trailing" timesteps.
|
27 |
+
pipe.scheduler = DPMSolverSinglestepScheduler.from_config(pipe.scheduler.config, timestep_spacing="trailing")
|
28 |
+
|
29 |
+
# superprompt-v1
|
30 |
+
tokenizer = T5Tokenizer.from_pretrained("roborovski/superprompt-v1")
|
31 |
+
model = T5ForConditionalGeneration.from_pretrained("roborovski/superprompt-v1", device_map="auto", torch_dtype="auto")
|
32 |
+
model.to(device)
|
33 |
+
|
34 |
+
# toggle visibility the enhanced prompt output
|
35 |
+
def update_visibility(enhance_prompt):
|
36 |
+
return gr.update(visible=enhance_prompt)
|
37 |
+
|
38 |
+
|
39 |
+
# Define the image generation function
|
40 |
+
@spaces.GPU(duration=60)
|
41 |
+
def generate_image(prompt, enhance_prompt, negative_prompt, num_inference_steps, height, width, guidance_scale, seed, num_images_per_prompt, progress=gr.Progress(track_tqdm=True)):
|
42 |
+
if seed == 0:
|
43 |
+
seed = random.randint(1, 2**32-1)
|
44 |
+
|
45 |
+
if enhance_prompt:
|
46 |
+
transformers.set_seed(seed)
|
47 |
+
|
48 |
+
input_text = f"Expand the following prompt to add more detail: {prompt}"
|
49 |
+
input_ids = tokenizer(input_text, return_tensors="pt").input_ids.to(device)
|
50 |
+
|
51 |
+
outputs = model.generate(
|
52 |
+
input_ids,
|
53 |
+
max_new_tokens=512,
|
54 |
+
repetition_penalty=1.2,
|
55 |
+
do_sample=True,
|
56 |
+
temperature=0.7,
|
57 |
+
top_p=1,
|
58 |
+
top_k=50
|
59 |
+
)
|
60 |
+
prompt = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
61 |
+
|
62 |
+
generator = torch.Generator().manual_seed(seed)
|
63 |
+
|
64 |
+
output = pipe(
|
65 |
+
prompt=prompt,
|
66 |
+
negative_prompt=negative_prompt,
|
67 |
+
num_inference_steps=num_inference_steps,
|
68 |
+
height=height,
|
69 |
+
width=width,
|
70 |
+
guidance_scale=guidance_scale,
|
71 |
+
generator=generator,
|
72 |
+
num_images_per_prompt=num_images_per_prompt
|
73 |
+
).images
|
74 |
+
|
75 |
+
return output, prompt
|
76 |
+
|
77 |
+
|
78 |
+
|
79 |
+
# Create the Gradio interface
|
80 |
+
|
81 |
+
examples = [
|
82 |
+
["A white car racing fast to the moon.", True],
|
83 |
+
["A woman in a red dress singing on top of a building.", True],
|
84 |
+
["An astrounat on mars in a futuristic cyborg suit.", True],
|
85 |
+
]
|
86 |
+
|
87 |
+
css = '''
|
88 |
+
.gradio-container{max-width: 1000px !important}
|
89 |
+
h1{text-align:center}
|
90 |
+
'''
|
91 |
+
with gr.Blocks(css=css) as demo:
|
92 |
+
with gr.Row():
|
93 |
+
with gr.Column():
|
94 |
+
gr.HTML(
|
95 |
+
"""
|
96 |
+
<h1 style='text-align: center'>
|
97 |
+
SDXL Flash
|
98 |
+
</h1>
|
99 |
+
"""
|
100 |
+
)
|
101 |
+
gr.HTML(
|
102 |
+
"""
|
103 |
+
Made by <a href='https://linktr.ee/Nick088' target='_blank'>Nick088</a>
|
104 |
+
<br> <a href="https://discord.gg/osai"> <img src="https://img.shields.io/discord/1198701940511617164?color=%23738ADB&label=Discord&style=for-the-badge" alt="Discord"> </a>
|
105 |
+
"""
|
106 |
+
)
|
107 |
+
with gr.Group():
|
108 |
+
with gr.Column():
|
109 |
+
prompt = gr.Textbox(label="Prompt", info="Describe the image you want", placeholder="A cat...")
|
110 |
+
enhance_prompt = gr.Checkbox(label="Prompt Enhancement with SuperPrompt-v1", value=True)
|
111 |
+
run_button = gr.Button("Run")
|
112 |
+
result = gr.Gallery(label="Generated AI Images", elem_id="gallery")
|
113 |
+
better_prompt = gr.Textbox(label="Enhanced Prompt", info="The output of your enhanced prompt used for the Image Generation", visible=True)
|
114 |
+
enhance_prompt.change(fn=update_visibility, inputs=enhance_prompt, outputs=better_prompt)
|
115 |
+
with gr.Accordion("Advanced options", open=False):
|
116 |
+
with gr.Row():
|
117 |
+
negative_prompt = gr.Textbox(label="Negative Prompt", info="Describe what you don't want in the image", value="deformed, distorted, disfigured, poorly drawn, bad anatomy, incorrect anatomy, extra limb, missing limb, floating limbs, mutated hands and fingers, disconnected limbs, mutation, mutated, ugly, disgusting, blurry, amputation", placeholder="Ugly, bad anatomy...")
|
118 |
+
with gr.Row():
|
119 |
+
num_inference_steps = gr.Slider(label="Number of Inference Steps", info="The number of denoising steps of the image. More denoising steps usually lead to a higher quality image at the cost of slower inference", minimum=1, maximum=50, value=25, step=1)
|
120 |
+
guidance_scale = gr.Slider(label="Guidance Scale", info="Controls how much the image generation process follows the text prompt. Higher values make the image stick more closely to the input text.", minimum=0.0, maximum=10.0, value=7.5, step=0.1)
|
121 |
+
with gr.Row():
|
122 |
+
width = gr.Slider(label="Width", info="Width of the Image", minimum=256, maximum=1344, step=32, value=1024)
|
123 |
+
height = gr.Slider(label="Height", info="Height of the Image", minimum=256, maximum=1344, step=32, value=1024)
|
124 |
+
with gr.Row():
|
125 |
+
seed = gr.Slider(value=42, minimum=0, maximum=MAX_SEED, step=1, label="Seed", info="A starting point to initiate the generation process, put 0 for a random one")
|
126 |
+
num_images_per_prompt = gr.Slider(label="Images Per Prompt", info="Number of Images to generate with the settings",minimum=1, maximum=4, step=1, value=2)
|
127 |
+
|
128 |
+
gr.Examples(
|
129 |
+
examples=examples,
|
130 |
+
inputs=[prompt, enhance_prompt],
|
131 |
+
outputs=[result, better_prompt],
|
132 |
+
fn=generate_image,
|
133 |
+
)
|
134 |
+
|
135 |
+
gr.on(
|
136 |
+
triggers=[
|
137 |
+
prompt.submit,
|
138 |
+
run_button.click,
|
139 |
+
],
|
140 |
+
fn=generate_image,
|
141 |
+
inputs=[
|
142 |
+
prompt,
|
143 |
+
enhance_prompt,
|
144 |
+
negative_prompt,
|
145 |
+
num_inference_steps,
|
146 |
+
width,
|
147 |
+
height,
|
148 |
+
guidance_scale,
|
149 |
+
seed,
|
150 |
+
num_images_per_prompt,
|
151 |
+
],
|
152 |
+
outputs=[result, better_prompt],
|
153 |
+
)
|
154 |
+
|
155 |
+
demo.queue().launch(share = False)
|