prithivMLmods commited on
Commit
e96545c
1 Parent(s): 98fa4fe

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -285
app.py DELETED
@@ -1,285 +0,0 @@
1
- import os
2
- import random
3
- import uuid
4
- from typing import Tuple
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
-
12
- def save_image(img):
13
- unique_name = str(uuid.uuid4()) + ".png"
14
- img.save(unique_name)
15
- return unique_name
16
-
17
- def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
18
- if randomize_seed:
19
- seed = random.randint(0, MAX_SEED)
20
- return seed
21
-
22
- MAX_SEED = np.iinfo(np.int32).max
23
- USE_TORCH_COMPILE = 0
24
- ENABLE_CPU_OFFLOAD = 0
25
-
26
- if torch.cuda.is_available():
27
- pipe = StableDiffusionXLPipeline.from_pretrained(
28
- "SG161222/RealVisXL_V4.0_Lightning",
29
- torch_dtype=torch.float16,
30
- use_safetensors=True,
31
- )
32
- pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
33
-
34
- LORA_OPTIONS = {
35
- "Realism (face/character)👦🏻": ("prithivMLmods/Canopus-Realism-LoRA", "Canopus-Realism-LoRA.safetensors", "rlms"),
36
- "Pixar (art/toons)🙀": ("prithivMLmods/Canopus-Pixar-Art", "Canopus-Pixar-Art.safetensors", "pixar"),
37
- "Photoshoot (camera/film)📸": ("prithivMLmods/Canopus-Photo-Shoot-Mini-LoRA", "Canopus-Photo-Shoot-Mini-LoRA.safetensors", "photo"),
38
- "Clothing (hoodies/pant/shirts)👔": ("prithivMLmods/Canopus-Clothing-Adp-LoRA", "Canopus-Dress-Clothing-LoRA.safetensors", "clth"),
39
- "Interior Architecture (house/hotel)🏠": ("prithivMLmods/Canopus-Interior-Architecture-0.1", "Canopus-Interior-Architecture-0.1δ.safetensors", "arch"),
40
- "Fashion Product (wearing/usable)👜": ("prithivMLmods/Canopus-Fashion-Product-Dilation", "Canopus-Fashion-Product-Dilation.safetensors", "fashion"),
41
- "Minimalistic Image (minimal/detailed)🏞️": ("prithivMLmods/Pegasi-Minimalist-Image-Style", "Pegasi-Minimalist-Image-Style.safetensors", "minimalist"),
42
- "Modern Clothing (trend/new)👕": ("prithivMLmods/Canopus-Modern-Clothing-Design", "Canopus-Modern-Clothing-Design.safetensors", "mdrnclth"),
43
- "Animaliea (farm/wild)🫎": ("prithivMLmods/Canopus-Animaliea-Artism", "Canopus-Animaliea-Artism.safetensors", "Animaliea"),
44
- "Liquid Wallpaper (minimal/illustration)🖼️": ("prithivMLmods/Canopus-Liquid-Wallpaper-Art", "Canopus-Liquid-Wallpaper-Minimalize-LoRA.safetensors", "liquid"),
45
- "Canes Cars (realistic/futurecars)🚘": ("prithivMLmods/Canes-Cars-Model-LoRA", "Canes-Cars-Model-LoRA.safetensors", "car"),
46
- "Pencil Art (characteristic/creative)✏️": ("prithivMLmods/Canopus-Pencil-Art-LoRA", "Canopus-Pencil-Art-LoRA.safetensors", "Pencil Art"),
47
- "Art Minimalistic (paint/semireal)🎨": ("prithivMLmods/Canopus-Art-Medium-LoRA", "Canopus-Art-Medium-LoRA.safetensors", "mdm"),
48
-
49
- }
50
-
51
- for model_name, weight_name, adapter_name in LORA_OPTIONS.values():
52
- pipe.load_lora_weights(model_name, weight_name=weight_name, adapter_name=adapter_name)
53
- pipe.to("cuda")
54
-
55
- style_list = [
56
- {
57
- "name": "3840 x 2160",
58
- "prompt": "hyper-realistic 8K image of {prompt}. ultra-detailed, lifelike, high-resolution, sharp, vibrant colors, photorealistic",
59
- "negative_prompt": "cartoonish, low resolution, blurry, simplistic, abstract, deformed, ugly",
60
- },
61
- {
62
- "name": "2560 x 1440",
63
- "prompt": "hyper-realistic 4K image of {prompt}. ultra-detailed, lifelike, high-resolution, sharp, vibrant colors, photorealistic",
64
- "negative_prompt": "cartoonish, low resolution, blurry, simplistic, abstract, deformed, ugly",
65
- },
66
- {
67
- "name": "HD+",
68
- "prompt": "hyper-realistic 2K image of {prompt}. ultra-detailed, lifelike, high-resolution, sharp, vibrant colors, photorealistic",
69
- "negative_prompt": "cartoonish, low resolution, blurry, simplistic, abstract, deformed, ugly",
70
- },
71
- {
72
- "name": "Style Zero",
73
- "prompt": "{prompt}",
74
- "negative_prompt": "",
75
- },
76
- ]
77
-
78
- styles = {k["name"]: (k["prompt"], k["negative_prompt"]) for k in style_list}
79
-
80
- DEFAULT_STYLE_NAME = "3840 x 2160"
81
- STYLE_NAMES = list(styles.keys())
82
-
83
- def apply_style(style_name: str, positive: str, negative: str = "") -> Tuple[str, str]:
84
- if style_name in styles:
85
- p, n = styles.get(style_name, styles[DEFAULT_STYLE_NAME])
86
- else:
87
- p, n = styles[DEFAULT_STYLE_NAME]
88
-
89
- if not negative:
90
- negative = ""
91
- return p.replace("{prompt}", positive), n + negative
92
-
93
- @spaces.GPU(duration=180, enable_queue=True)
94
- def generate(
95
- prompt: str,
96
- negative_prompt: str = "",
97
- use_negative_prompt: bool = False,
98
- seed: int = 0,
99
- width: int = 1024,
100
- height: int = 1024,
101
- guidance_scale: float = 3,
102
- randomize_seed: bool = False,
103
- style_name: str = DEFAULT_STYLE_NAME,
104
- lora_model: str = "Realism (face/character)👦🏻",
105
- progress=gr.Progress(track_tqdm=True),
106
- ):
107
- seed = int(randomize_seed_fn(seed, randomize_seed))
108
-
109
- positive_prompt, effective_negative_prompt = apply_style(style_name, prompt, negative_prompt)
110
-
111
- if not use_negative_prompt:
112
- effective_negative_prompt = "" # type: ignore
113
-
114
- model_name, weight_name, adapter_name = LORA_OPTIONS[lora_model]
115
- pipe.set_adapters(adapter_name)
116
-
117
- images = pipe(
118
- prompt=positive_prompt,
119
- negative_prompt=effective_negative_prompt,
120
- width=width,
121
- height=height,
122
- guidance_scale=guidance_scale,
123
- num_inference_steps=20,
124
- num_images_per_prompt=1,
125
- cross_attention_kwargs={"scale": 0.65},
126
- output_type="pil",
127
- ).images
128
- image_paths = [save_image(img) for img in images]
129
- return image_paths, seed
130
-
131
- examples = [
132
- "realism, man in the style of dark beige and brown, uhd image, youthful protagonists, nonrepresentational",
133
- "pixar, a young man with light brown wavy hair and light brown eyes sitting in an armchair and looking directly at the camera, pixar style, disney pixar, office background",
134
- "hoodie, front view, capture a urban style, superman hoodie, technical materials, fabric small point label on text blue theory, with a raised collar, fabric is a light yellow, low angle to capture the hoodies form and detailing, f/5.6 to focus on the hoodies craftsmanship, solid grey background, studio light setting, with batman logo.",
135
- ]
136
-
137
-
138
- css = '''
139
- .gradio-container{max-width: 888px !important}
140
- h1{text-align:center}
141
- .submit-btn {
142
- background-color: #ecde2c !important;
143
- color: white !important;
144
- }
145
- .submit-btn:hover {
146
- background-color: #ffec00 !important;
147
- }
148
- '''
149
-
150
- def load_predefined_images():
151
- predefined_images = [
152
- "assets/1.png",
153
- "assets/2.png",
154
- "assets/3.png",
155
- "assets/4.png",
156
- "assets/5.png",
157
- "assets/6.png",
158
- "assets/7.png",
159
- "assets/8.png",
160
- "assets/9.png",
161
- ]
162
- return predefined_images
163
-
164
- with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
165
- with gr.Row():
166
- with gr.Column(scale=1):
167
- prompt = gr.Text(
168
- label="Prompt",
169
- show_label=False,
170
- max_lines=1,
171
- placeholder="Enter your prompt with resp. tag!",
172
- container=False,
173
- )
174
- run_button = gr.Button("Generate as (1024 x 1024)🎃", scale=0, elem_classes="submit-btn")
175
-
176
- with gr.Row(visible=True):
177
- model_choice = gr.Dropdown(
178
- label="LoRA Selection",
179
- choices=list(LORA_OPTIONS.keys()),
180
- value="Realism (face/character)👦🏻")
181
-
182
- with gr.Accordion("Advanced options", open=True):
183
- use_negative_prompt = gr.Checkbox(label="Use negative prompt", value=True, visible=True)
184
- negative_prompt = gr.Text(
185
- label="Negative prompt",
186
- lines=4,
187
- max_lines=6,
188
- 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",
189
- placeholder="Enter a negative prompt",
190
- visible=True,
191
- )
192
- with gr.Row():
193
- seed = gr.Slider(
194
- label="Seed",
195
- minimum=0,
196
- maximum=MAX_SEED,
197
- step=1,
198
- value=0,
199
- visible=True
200
- )
201
- randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
202
-
203
- with gr.Row(visible=True):
204
- width = gr.Slider(
205
- label="Width",
206
- minimum=512,
207
- maximum=2048,
208
- step=8,
209
- value=1024,
210
- )
211
- height = gr.Slider(
212
- label="Height",
213
- minimum=512,
214
- maximum=2048,
215
- step=8,
216
- value=1024,
217
- )
218
-
219
- guidance_scale = gr.Slider(
220
- label="Guidance Scale",
221
- minimum=0.1,
222
- maximum=20.0,
223
- step=0.1,
224
- value=3.0,
225
- )
226
-
227
- style_selection = gr.Radio(
228
- show_label=True,
229
- container=True,
230
- interactive=True,
231
- choices=STYLE_NAMES,
232
- value=DEFAULT_STYLE_NAME,
233
- label="Quality Style",
234
- )
235
-
236
- with gr.Column(scale=2):
237
- result = gr.Gallery(label="Result", columns=1, preview=True, show_label=False)
238
-
239
- gr.Examples(
240
- examples=examples,
241
- inputs=prompt,
242
- outputs=[result, seed],
243
- fn=generate,
244
- cache_examples=False,
245
- )
246
-
247
- predefined_gallery = gr.Gallery(
248
- label="Image Gallery",
249
- columns=3,
250
- show_label=False,
251
- value=load_predefined_images()
252
- )
253
-
254
- use_negative_prompt.change(
255
- fn=lambda x: gr.update(visible=x),
256
- inputs=use_negative_prompt,
257
- outputs=negative_prompt,
258
- api_name=False,
259
- )
260
-
261
- gr.on(
262
- triggers=[
263
- prompt.submit,
264
- negative_prompt.submit,
265
- run_button.click,
266
- ],
267
- fn=generate,
268
- inputs=[
269
- prompt,
270
- negative_prompt,
271
- use_negative_prompt,
272
- seed,
273
- width,
274
- height,
275
- guidance_scale,
276
- randomize_seed,
277
- style_selection,
278
- model_choice,
279
- ],
280
- outputs=[result, seed],
281
- api_name="run",
282
- )
283
-
284
- if __name__ == "__main__":
285
- demo.queue(max_size=30).launch()