prithivMLmods commited on
Commit
75761b4
·
verified ·
1 Parent(s): 66bfacb

Delete file.txt

Browse files
Files changed (1) hide show
  1. file.txt +0 -285
file.txt DELETED
@@ -1,285 +0,0 @@
1
- #!/usr/bin/env python
2
- import os
3
- import random
4
- import uuid
5
- import gradio as gr
6
- import numpy as np
7
- from PIL import Image
8
- import spaces
9
- import torch
10
- from diffusers import StableDiffusionXLPipeline, EulerAncestralDiscreteScheduler
11
- from typing import Tuple
12
-
13
- css = '''
14
- .gradio-container{max-width: 570px !important}
15
- h1{text-align:center}
16
- footer {
17
- visibility: hidden
18
- }
19
- '''
20
-
21
- DESCRIPTIONXX = """
22
- ## REALVISXL V5 + LIGHTNING ⚡
23
- """
24
-
25
- examples = [
26
- "Illustration of A starry night camp in the mountains, 4k, cinematic --ar 85:128 --v 6.0 --style raw",
27
- "A delicious ceviche cheesecake slice, 4k, octane render, ray tracing, Ultra-High-Definition"
28
- ]
29
-
30
- MODEL_OPTIONS = {
31
- "REALVISXL V5.0": "SG161222/RealVisXL_V5.0",
32
- # "LIGHTNING V5.0": "SG161222/RealVisXL_V5.0_Lightning",
33
- }
34
-
35
- MAX_IMAGE_SIZE = int(os.getenv("MAX_IMAGE_SIZE", "4096"))
36
- USE_TORCH_COMPILE = os.getenv("USE_TORCH_COMPILE", "0") == "1"
37
- ENABLE_CPU_OFFLOAD = os.getenv("ENABLE_CPU_OFFLOAD", "0") == "1"
38
- BATCH_SIZE = int(os.getenv("BATCH_SIZE", "1"))
39
-
40
- device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
41
-
42
- style_list = [
43
- {
44
- "name": "3840 x 2160",
45
- "prompt": "hyper-realistic 8K image of {prompt}. ultra-detailed, lifelike, high-resolution, sharp, vibrant colors, photorealistic",
46
- "negative_prompt": "cartoonish, low resolution, blurry, simplistic, abstract, deformed, ugly",
47
- },
48
- {
49
- "name": "2560 x 1440",
50
- "prompt": "hyper-realistic 4K image of {prompt}. ultra-detailed, lifelike, high-resolution, sharp, vibrant colors, photorealistic",
51
- "negative_prompt": "cartoonish, low resolution, blurry, simplistic, abstract, deformed, ugly",
52
- },
53
- {
54
- "name": "HD+",
55
- "prompt": "hyper-realistic 2K image of {prompt}. ultra-detailed, lifelike, high-resolution, sharp, vibrant colors, photorealistic",
56
- "negative_prompt": "cartoonish, low resolution, blurry, simplistic, abstract, deformed, ugly",
57
- },
58
- {
59
- "name": "Style Zero",
60
- "prompt": "{prompt}",
61
- "negative_prompt": "",
62
- },
63
- ]
64
-
65
- styles = {k["name"]: (k["prompt"], k["negative_prompt"]) for k in style_list}
66
- DEFAULT_STYLE_NAME = "3840 x 2160"
67
- STYLE_NAMES = list(styles.keys())
68
-
69
- def apply_style(style_name: str, positive: str, negative: str = "") -> Tuple[str, str]:
70
- if style_name in styles:
71
- p, n = styles.get(style_name, styles[DEFAULT_STYLE_NAME])
72
- else:
73
- p, n = styles[DEFAULT_STYLE_NAME]
74
-
75
- if not negative:
76
- negative = ""
77
- return p.replace("{prompt}", positive), n + negative
78
-
79
- def load_and_prepare_model(model_id):
80
- pipe = StableDiffusionXLPipeline.from_pretrained(
81
- model_id,
82
- torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
83
- use_safetensors=True,
84
- add_watermarker=False,
85
- ).to(device)
86
- pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
87
-
88
- if USE_TORCH_COMPILE:
89
- pipe.compile()
90
-
91
- if ENABLE_CPU_OFFLOAD:
92
- pipe.enable_model_cpu_offload()
93
-
94
- return pipe
95
-
96
- # Preload and compile both models
97
- models = {key: load_and_prepare_model(value) for key, value in MODEL_OPTIONS.items()}
98
-
99
- MAX_SEED = np.iinfo(np.int32).max
100
-
101
- def save_image(img):
102
- unique_name = str(uuid.uuid4()) + ".png"
103
- img.save(unique_name)
104
- return unique_name
105
-
106
- def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
107
- if randomize_seed:
108
- seed = random.randint(0, MAX_SEED)
109
- return seed
110
-
111
- @spaces.GPU(duration=60, enable_queue=True)
112
- def generate(
113
- model_choice: str,
114
- prompt: str,
115
- negative_prompt: str = "",
116
- use_negative_prompt: bool = False,
117
- style_selection: str = DEFAULT_STYLE_NAME,
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
- prompt, negative_prompt = apply_style(style_selection, prompt, negative_prompt)
135
-
136
- options = {
137
- "prompt": [prompt] * num_images,
138
- "negative_prompt": [negative_prompt] * num_images if use_negative_prompt else None,
139
- "width": width,
140
- "height": height,
141
- "guidance_scale": guidance_scale,
142
- "num_inference_steps": num_inference_steps,
143
- "generator": generator,
144
- "output_type": "pil",
145
- }
146
-
147
- if use_resolution_binning:
148
- options["use_resolution_binning"] = True
149
-
150
- images = []
151
- for i in range(0, num_images, BATCH_SIZE):
152
- batch_options = options.copy()
153
- batch_options["prompt"] = options["prompt"][i:i+BATCH_SIZE]
154
- if "negative_prompt" in batch_options:
155
- batch_options["negative_prompt"] = options["negative_prompt"][i:i+BATCH_SIZE]
156
- images.extend(pipe(**batch_options).images)
157
-
158
- image_paths = [save_image(img) for img in images]
159
- return image_paths, seed
160
-
161
- with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
162
- gr.Markdown(DESCRIPTIONXX)
163
- with gr.Row():
164
- prompt = gr.Text(
165
- label="Prompt",
166
- show_label=False,
167
- max_lines=1,
168
- placeholder="Enter your prompt",
169
- container=False,
170
- )
171
- run_button = gr.Button("Run", scale=0)
172
- result = gr.Gallery(label="Result", columns=1, show_label=False)
173
-
174
- with gr.Row():
175
- model_choice = gr.Dropdown(
176
- label="Model Selection🔻",
177
- choices=list(MODEL_OPTIONS.keys()),
178
- value="REALVISXL V5.0"
179
- )
180
-
181
- with gr.Accordion("Advanced options", open=False, visible=True):
182
- style_selection = gr.Radio(
183
- show_label=True,
184
- container=True,
185
- interactive=True,
186
- choices=STYLE_NAMES,
187
- value=DEFAULT_STYLE_NAME,
188
- label="Quality Style",
189
- )
190
- num_images = gr.Slider(
191
- label="Number of Images",
192
- minimum=1,
193
- maximum=5,
194
- step=1,
195
- value=1,
196
- )
197
- with gr.Row():
198
- with gr.Column(scale=1):
199
- use_negative_prompt = gr.Checkbox(label="Use negative prompt", value=True)
200
- negative_prompt = gr.Text(
201
- label="Negative prompt",
202
- max_lines=5,
203
- lines=4,
204
- placeholder="Enter a negative prompt",
205
- 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",
206
- visible=True,
207
- )
208
- seed = gr.Slider(
209
- label="Seed",
210
- minimum=0,
211
- maximum=MAX_SEED,
212
- step=1,
213
- value=0,
214
- )
215
- randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
216
- with gr.Row():
217
- width = gr.Slider(
218
- label="Width",
219
- minimum=512,
220
- maximum=MAX_IMAGE_SIZE,
221
- step=64,
222
- value=1024,
223
- )
224
- height = gr.Slider(
225
- label="Height",
226
- minimum=512,
227
- maximum=MAX_IMAGE_SIZE,
228
- step=64,
229
- value=1024,
230
- )
231
- with gr.Row():
232
- guidance_scale = gr.Slider(
233
- label="Guidance Scale",
234
- minimum=0.1,
235
- maximum=6,
236
- step=0.1,
237
- value=3.0,
238
- )
239
- num_inference_steps = gr.Slider(
240
- label="Number of inference steps",
241
- minimum=1,
242
- maximum=60,
243
- step=1,
244
- value=32,
245
- )
246
-
247
- gr.Examples(
248
- examples=examples,
249
- inputs=prompt,
250
- cache_examples=False
251
- )
252
-
253
- use_negative_prompt.change(
254
- fn=lambda x: gr.update(visible=x),
255
- inputs=use_negative_prompt,
256
- outputs=negative_prompt,
257
- api_name=False,
258
- )
259
-
260
- gr.on(
261
- triggers=[
262
- prompt.submit,
263
- negative_prompt.submit,
264
- run_button.click,
265
- ],
266
- fn=generate,
267
- inputs=[
268
- model_choice,
269
- prompt,
270
- negative_prompt,
271
- use_negative_prompt,
272
- style_selection,
273
- seed,
274
- width,
275
- height,
276
- guidance_scale,
277
- num_inference_steps,
278
- randomize_seed,
279
- gr.State(value=True),
280
- num_images,
281
- ],
282
- outputs=[result, seed],
283
- )
284
-
285
- demo.queue(concurrency_count=3).launch(debug=True)