Spaces:
Running
on
Zero
Running
on
Zero
prithivMLmods
commited on
Commit
•
0a329ff
1
Parent(s):
6e9d8c5
Rename last-commit/demo.txt to last-commit/doc.txt
Browse files- last-commit/demo.txt +0 -0
- last-commit/doc.txt +320 -0
last-commit/demo.txt
DELETED
File without changes
|
last-commit/doc.txt
ADDED
@@ -0,0 +1,320 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# TEXT 2 IMAGE PLAYGROUND Documentation
|
2 |
+
|
3 |
+
## Overview
|
4 |
+
|
5 |
+
TEXT 2 IMAGE PLAYGROUND is a Gradio-based web application designed to generate images from text prompts using advanced AI models. It offers various model options, customization parameters, and a user-friendly interface for an enhanced user experience.
|
6 |
+
|
7 |
+
## Features
|
8 |
+
|
9 |
+
- **Model Selection**: Choose from multiple AI models to generate images with different styles and qualities.
|
10 |
+
- **Custom Prompts**: Input text prompts to define the content and style of the generated images.
|
11 |
+
- **Negative Prompts**: Use negative prompts to avoid unwanted elements in the images.
|
12 |
+
- **Image Customization**: Adjust parameters like seed, width, height, guidance scale, and number of inference steps.
|
13 |
+
- **Random Seed Generation**: Enable random seed generation for varied outputs.
|
14 |
+
- **Image Gallery**: View a gallery of predefined images for inspiration.
|
15 |
+
|
16 |
+
## Interface
|
17 |
+
|
18 |
+
### Description
|
19 |
+
|
20 |
+
```markdown
|
21 |
+
## TEXT 2 IMAGE PLAYGROUND 🥠
|
22 |
+
```
|
23 |
+
|
24 |
+
### CSS
|
25 |
+
|
26 |
+
```css
|
27 |
+
.gradio-container {
|
28 |
+
max-width: 690px !important;
|
29 |
+
}
|
30 |
+
h1 {
|
31 |
+
text-align: center;
|
32 |
+
}
|
33 |
+
footer {
|
34 |
+
visibility: hidden;
|
35 |
+
}
|
36 |
+
```
|
37 |
+
|
38 |
+
### JavaScript
|
39 |
+
|
40 |
+
```javascript
|
41 |
+
function refresh() {
|
42 |
+
const url = new URL(window.location);
|
43 |
+
if (url.searchParams.get('__theme') !== 'dark') {
|
44 |
+
url.searchParams.set('__theme', 'dark');
|
45 |
+
window.location.href = url.href;
|
46 |
+
}
|
47 |
+
}
|
48 |
+
```
|
49 |
+
|
50 |
+
### Examples
|
51 |
+
|
52 |
+
Predefined text prompts for quick testing:
|
53 |
+
|
54 |
+
- 3d image, cute girl, in the style of Pixar...
|
55 |
+
- Chocolate dripping from a donut against a yellow background...
|
56 |
+
- Illustration of A starry night camp in the mountains...
|
57 |
+
- Man in brown leather jacket posing for camera...
|
58 |
+
- Commercial photography, giant burger...
|
59 |
+
|
60 |
+
## Model Options
|
61 |
+
|
62 |
+
```python
|
63 |
+
MODEL_OPTIONS = {
|
64 |
+
"Realism : V4.0_Lightning🔥": "SG161222/RealVisXL_V4.0_Lightning",
|
65 |
+
"Detailed/SOTA : Mobius🚀": "Corcelio/mobius",
|
66 |
+
"Anime : Cagliostrolab🍺": "cagliostrolab/animagine-xl-3.1"
|
67 |
+
}
|
68 |
+
```
|
69 |
+
|
70 |
+
## Configuration
|
71 |
+
|
72 |
+
Environment variables and configurations:
|
73 |
+
|
74 |
+
```python
|
75 |
+
MAX_IMAGE_SIZE = int(os.getenv("MAX_IMAGE_SIZE", "4096"))
|
76 |
+
USE_TORCH_COMPILE = os.getenv("USE_TORCH_COMPILE", "0") == "1"
|
77 |
+
ENABLE_CPU_OFFLOAD = os.getenv("ENABLE_CPU_OFFLOAD", "0") == "1"
|
78 |
+
BATCH_SIZE = int(os.getenv("BATCH_SIZE", "1"))
|
79 |
+
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
80 |
+
```
|
81 |
+
|
82 |
+
## Model Loading and Preparation
|
83 |
+
|
84 |
+
Function to load and prepare models:
|
85 |
+
|
86 |
+
```python
|
87 |
+
def load_and_prepare_model(model_id):
|
88 |
+
pipe = StableDiffusionXLPipeline.from_pretrained(
|
89 |
+
model_id,
|
90 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
91 |
+
use_safetensors=True,
|
92 |
+
add_watermarker=False,
|
93 |
+
).to(device)
|
94 |
+
pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
|
95 |
+
|
96 |
+
if USE_TORCH_COMPILE:
|
97 |
+
pipe.compile()
|
98 |
+
|
99 |
+
if ENABLE_CPU_OFFLOAD:
|
100 |
+
pipe.enable_model_cpu_offload()
|
101 |
+
|
102 |
+
return pipe
|
103 |
+
|
104 |
+
models = {key: load_and_prepare_model(value) for key, value in MODEL_OPTIONS.items()}
|
105 |
+
```
|
106 |
+
|
107 |
+
## Image Generation
|
108 |
+
|
109 |
+
Function to generate images based on user inputs:
|
110 |
+
|
111 |
+
```python
|
112 |
+
@spaces.GPU(duration=60, enable_queue=True)
|
113 |
+
def generate(
|
114 |
+
model_choice: str,
|
115 |
+
prompt: str,
|
116 |
+
negative_prompt: str = "",
|
117 |
+
use_negative_prompt: bool = False,
|
118 |
+
seed: int = 1,
|
119 |
+
width: int = 1024,
|
120 |
+
height: int = 1024,
|
121 |
+
guidance_scale: float = 3,
|
122 |
+
num_inference_steps: int = 25,
|
123 |
+
randomize_seed: bool = False,
|
124 |
+
use_resolution_binning: bool = True,
|
125 |
+
num_images: int = 1,
|
126 |
+
progress=gr.Progress(track_tqdm=True),
|
127 |
+
):
|
128 |
+
global models
|
129 |
+
pipe = models[model_choice]
|
130 |
+
|
131 |
+
seed = int(randomize_seed_fn(seed, randomize_seed))
|
132 |
+
generator = torch.Generator(device=device).manual_seed(seed)
|
133 |
+
|
134 |
+
options = {
|
135 |
+
"prompt": [prompt] * num_images,
|
136 |
+
"negative_prompt": [negative_prompt] * num_images if use_negative_prompt else None,
|
137 |
+
"width": width,
|
138 |
+
"height": height,
|
139 |
+
"guidance_scale": guidance_scale,
|
140 |
+
"num_inference_steps": num_inference_steps,
|
141 |
+
"generator": generator,
|
142 |
+
"output_type": "pil",
|
143 |
+
}
|
144 |
+
|
145 |
+
if use_resolution_binning:
|
146 |
+
options["use_resolution_binning"] = True
|
147 |
+
|
148 |
+
images = []
|
149 |
+
for i in range(0, num_images, BATCH_SIZE):
|
150 |
+
batch_options = options.copy()
|
151 |
+
batch_options["prompt"] = options["prompt"][i:i+BATCH_SIZE]
|
152 |
+
if "negative_prompt" in batch_options:
|
153 |
+
batch_options["negative_prompt"] = options["negative_prompt"][i:i+BATCH_SIZE]
|
154 |
+
images.extend(pipe(**batch_options).images)
|
155 |
+
|
156 |
+
image_paths = [save_image(img) for img in images]
|
157 |
+
return image_paths, seed
|
158 |
+
```
|
159 |
+
|
160 |
+
## Load Predefined Images
|
161 |
+
|
162 |
+
Function to load predefined images for the gallery:
|
163 |
+
|
164 |
+
```python
|
165 |
+
def load_predefined_images():
|
166 |
+
predefined_images = [
|
167 |
+
"assets/1.png",
|
168 |
+
"assets/2.png",
|
169 |
+
"assets/3.png",
|
170 |
+
"assets/4.png",
|
171 |
+
"assets/5.png",
|
172 |
+
"assets/6.png",
|
173 |
+
"assets/7.png",
|
174 |
+
"assets/8.png",
|
175 |
+
"assets/9.png",
|
176 |
+
"assets/10.png",
|
177 |
+
"assets/11.png",
|
178 |
+
"assets/12.png",
|
179 |
+
]
|
180 |
+
return predefined_images
|
181 |
+
```
|
182 |
+
|
183 |
+
## Gradio Interface
|
184 |
+
|
185 |
+
Creating the Gradio interface:
|
186 |
+
|
187 |
+
```python
|
188 |
+
with gr.Blocks(css=css, theme="bethecloud/storj_theme", js=js_func) as demo:
|
189 |
+
gr.Markdown(DESCRIPTIONx)
|
190 |
+
with gr.Row():
|
191 |
+
prompt = gr.Text(
|
192 |
+
label="Prompt",
|
193 |
+
show_label=False,
|
194 |
+
max_lines=1,
|
195 |
+
placeholder="Enter your prompt",
|
196 |
+
value="Chocolate dripping from a donut against a yellow background, in the style of brocore, hyper-realistic oil --ar 2:3 --q 2 --s 750 --v 5 --ar 2:3 --q 2 --s 750 --v 5",
|
197 |
+
container=False,
|
198 |
+
)
|
199 |
+
run_button = gr.Button("Run🚀", scale=0)
|
200 |
+
result = gr.Gallery(label="Result", columns=1, show_label=False)
|
201 |
+
|
202 |
+
with gr.Row():
|
203 |
+
model_choice = gr.Dropdown(
|
204 |
+
label="Model Selection ☑️",
|
205 |
+
choices=list(MODEL_OPTIONS.keys()),
|
206 |
+
value="Realism : V4.0_Lightning🔥"
|
207 |
+
)
|
208 |
+
|
209 |
+
with gr.Accordion("Advanced options", open=True):
|
210 |
+
num_images = gr.Slider(
|
211 |
+
label="Number of Images",
|
212 |
+
minimum=1,
|
213 |
+
maximum=1,
|
214 |
+
step=1,
|
215 |
+
value=1,
|
216 |
+
)
|
217 |
+
with gr.Row():
|
218 |
+
with gr.Column(scale=1):
|
219 |
+
use_negative_prompt = gr.Checkbox(label="Use negative prompt", value=True)
|
220 |
+
negative_prompt = gr.Text(
|
221 |
+
label="Negative prompt",
|
222 |
+
max_lines=5,
|
223 |
+
lines=4,
|
224 |
+
placeholder="Enter a negative prompt",
|
225 |
+
value="(deformed, distorted, disfigured:1.3), poorly drawn, bad anatomy, wrong anatomy, extra limb, missing limb, floating limbs, (mutated hands and fingers:1.4), disconnected limbs, mutation, mutated, ugly, disgusting, blurry, amputation",
|
226 |
+
visible=True,
|
227 |
+
)
|
228 |
+
seed = gr.Slider(
|
229 |
+
label="Seed",
|
230 |
+
minimum=0,
|
231 |
+
maximum=MAX_SEED,
|
232 |
+
step=1,
|
233 |
+
value=0,
|
234 |
+
)
|
235 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
236 |
+
with gr.Row():
|
237 |
+
width = gr.Slider(
|
238 |
+
label="Width",
|
239 |
+
minimum=512,
|
240 |
+
maximum=MAX_IMAGE_SIZE,
|
241 |
+
step=64,
|
242 |
+
value=1024,
|
243 |
+
)
|
244 |
+
height = gr.Slider(
|
245 |
+
label="Height",
|
246 |
+
minimum=512,
|
247 |
+
maximum=MAX_IMAGE_SIZE,
|
248 |
+
step=64,
|
249 |
+
value=1024,
|
250 |
+
)
|
251 |
+
with gr.Row():
|
252 |
+
guidance_scale = gr.Slider(
|
253 |
+
label="Guidance Scale",
|
254 |
+
minimum=0.1,
|
255 |
+
maximum=6,
|
256 |
+
step=0.1,
|
257 |
+
value=3.0,
|
258 |
+
)
|
259 |
+
num_inference_steps = gr.Slider(
|
260 |
+
label="Number of inference steps",
|
261 |
+
minimum=1,
|
262 |
+
maximum=35,
|
263 |
+
step=1,
|
264 |
+
value=20,
|
265 |
+
)
|
266 |
+
|
267 |
+
gr.Examples(
|
268 |
+
examples=examples,
|
269 |
+
inputs=prompt,
|
270 |
+
cache_examples=False
|
271 |
+
)
|
272 |
+
|
273 |
+
use_negative_prompt.change(
|
274 |
+
fn=lambda x: gr.update(visible=x),
|
275 |
+
inputs=use_negative_prompt,
|
276 |
+
outputs=negative_prompt,
|
277 |
+
api_name=False,
|
278 |
+
)
|
279 |
+
|
280 |
+
gr.on(
|
281 |
+
triggers=[
|
282 |
+
prompt.submit,
|
283 |
+
negative_prompt.submit,
|
284 |
+
run_button.click,
|
285 |
+
],
|
286 |
+
fn=generate,
|
287 |
+
inputs=[
|
288 |
+
model_choice,
|
289 |
+
prompt,
|
290 |
+
negative_prompt,
|
291 |
+
use_negative_prompt,
|
292 |
+
seed,
|
293 |
+
width,
|
294 |
+
height,
|
295 |
+
guidance_scale,
|
296 |
+
num_inference_steps,
|
297 |
+
randomize_seed,
|
298 |
+
num_images
|
299 |
+
],
|
300 |
+
outputs=[result, seed],
|
301 |
+
api_name="run",
|
302 |
+
)
|
303 |
+
|
304 |
+
with gr.Column(scale=3):
|
305 |
+
gr.Markdown("### Image Gallery")
|
306 |
+
predefined_gallery = gr.Gallery(label="Image Gallery", columns=4, show_label=False, value=load
|
307 |
+
|
308 |
+
_predefined_images())
|
309 |
+
|
310 |
+
if __name__ == "__main__":
|
311 |
+
demo.queue(max_size=40).launch(show_api=False)
|
312 |
+
```
|
313 |
+
|
314 |
+
## Running the Application
|
315 |
+
|
316 |
+
To run the application, simply execute the script. The interface will launch and be accessible via a web browser.
|
317 |
+
|
318 |
+
```sh
|
319 |
+
python app.py
|
320 |
+
```
|