prithivMLmods commited on
Commit
2901f8f
·
verified ·
1 Parent(s): f461530

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -233
app.py CHANGED
@@ -1,233 +0,0 @@
1
- import gradio as gr
2
- import numpy as np
3
- import random
4
- import time
5
-
6
- import spaces
7
- from diffusers import DiffusionPipeline
8
- import torch
9
- from PIL import Image
10
-
11
- from gradio.themes.base import Base
12
- from gradio.themes.utils import colors, fonts, sizes
13
- from typing import Iterable
14
-
15
- class Seafoam(Base):
16
- def __init__(
17
- self,
18
- *,
19
- primary_hue: colors.Color | str = colors.emerald,
20
- secondary_hue: colors.Color | str = colors.blue,
21
- neutral_hue: colors.Color | str = colors.gray,
22
- spacing_size: sizes.Size | str = sizes.spacing_md,
23
- radius_size: sizes.Size | str = sizes.radius_md,
24
- text_size: sizes.Size | str = sizes.text_lg,
25
- font: fonts.Font
26
- | str
27
- | Iterable[fonts.Font | str] = (
28
- fonts.GoogleFont("Quicksand"),
29
- "ui-sans-serif",
30
- "sans-serif",
31
- ),
32
- font_mono: fonts.Font
33
- | str
34
- | Iterable[fonts.Font | str] = (
35
- fonts.GoogleFont("IBM Plex Mono"),
36
- "ui-monospace",
37
- "monospace",
38
- ),
39
- ):
40
- super().__init__(
41
- primary_hue=primary_hue,
42
- secondary_hue=secondary_hue,
43
- neutral_hue=neutral_hue,
44
- spacing_size=spacing_size,
45
- radius_size=radius_size,
46
- text_size=text_size,
47
- font=font,
48
- font_mono=font_mono,
49
- )
50
-
51
- seafoam = Seafoam()
52
-
53
- # Device and model setup
54
- device = "cuda" if torch.cuda.is_available() else "cpu"
55
- model_repo_id = "stabilityai/stable-diffusion-3.5-large-turbo"
56
-
57
- torch_dtype = torch.bfloat16 if torch.cuda.is_available() else torch.float32
58
- pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype)
59
- pipe = pipe.to(device)
60
-
61
- pipe.load_lora_weights("prithivMLmods/SD3.5-Turbo-Realism-2.0-LoRA", weight_name="SD3.5-Turbo-Realism-2.0-LoRA.safetensors")
62
- trigger_word = "Turbo Realism"
63
- pipe.fuse_lora(lora_scale=1.0)
64
-
65
- # Constants
66
- MAX_SEED = np.iinfo(np.int32).max
67
- MAX_IMAGE_SIZE = 1024
68
-
69
- # Define styles
70
- style_list = [
71
- {
72
- "name": "3840 x 2160",
73
- "prompt": "hyper-realistic 8K image of {prompt}. ultra-detailed, lifelike, high-resolution, sharp, vibrant colors, photorealistic",
74
- "negative_prompt": "cartoonish, low resolution, blurry, simplistic, abstract, deformed, ugly",
75
- },
76
- {
77
- "name": "2560 x 1440",
78
- "prompt": "hyper-realistic 4K image of {prompt}. ultra-detailed, lifelike, high-resolution, sharp, vibrant colors, photorealistic",
79
- "negative_prompt": "cartoonish, low resolution, blurry, simplistic, abstract, deformed, ugly",
80
- },
81
- {
82
- "name": "HD+",
83
- "prompt": "hyper-realistic 2K image of {prompt}. ultra-detailed, lifelike, high-resolution, sharp, vibrant colors, photorealistic",
84
- "negative_prompt": "cartoonish, low resolution, blurry, simplistic, abstract, deformed, ugly",
85
- },
86
- {
87
- "name": "Style Zero",
88
- "prompt": "{prompt}",
89
- "negative_prompt": "",
90
- },
91
- ]
92
- STYLE_NAMES = [s["name"] for s in style_list]
93
- DEFAULT_STYLE_NAME = STYLE_NAMES[0]
94
-
95
- # Define grid layouts
96
- grid_sizes = {
97
- "2x1": (2, 1),
98
- "1x2": (1, 2),
99
- "2x2": (2, 2),
100
- "2x3": (2, 3),
101
- "3x2": (3, 2),
102
- "1x1": (1, 1),
103
- }
104
-
105
- @spaces.GPU
106
- def infer(
107
- prompt,
108
- negative_prompt="",
109
- seed=42,
110
- randomize_seed=False,
111
- width=1024,
112
- height=1024,
113
- guidance_scale=7.5,
114
- num_inference_steps=10,
115
- style="Style Zero",
116
- grid_size="1x1",
117
- progress=gr.Progress(track_tqdm=True),
118
- ):
119
- if randomize_seed:
120
- seed = random.randint(0, MAX_SEED)
121
- generator = torch.Generator().manual_seed(seed)
122
-
123
- selected_style = next(s for s in style_list if s["name"] == style)
124
- styled_prompt = selected_style["prompt"].format(prompt=prompt)
125
- styled_negative = selected_style["negative_prompt"] or negative_prompt
126
-
127
- grid_x, grid_y = grid_sizes.get(grid_size, (1, 1))
128
- num_images = grid_x * grid_y
129
-
130
- output = pipe(
131
- prompt=styled_prompt,
132
- negative_prompt=styled_negative,
133
- width=width,
134
- height=height,
135
- guidance_scale=guidance_scale,
136
- num_inference_steps=num_inference_steps,
137
- generator=generator,
138
- num_images_per_prompt=num_images,
139
- )
140
-
141
- grid_img = Image.new('RGB', (width * grid_x, height * grid_y))
142
- for i, img in enumerate(output.images[:num_images]):
143
- x = (i % grid_x) * width
144
- y = (i // grid_x) * height
145
- grid_img.paste(img, (x, y))
146
-
147
- return grid_img, seed
148
-
149
- examples = [
150
- "A tiny, cute astronaut emerging from a cracked eggshell on the surface of the moon, surrounded by cosmic dust and distant planets in the starry sky. The scene is illuminated by soft lunar light, showcasing the texture of the moon's surface. Shot in ultra-detailed 4K resolution, with a sci-fi fantasy atmosphere and planetary background",
151
- "A cute, fluffy cat standing upright on its hind legs, holding a hand-drawn sign that says 'Hello World' in bold, playful letters. The background is softly blurred, emphasizing the cat’s detailed fur texture and the colorful sign. Captured in portrait aspect ratio --ar 85:128, using --v 6.0 and --style raw for a semi-realistic, endearing look",
152
- "A photorealistic image of a cold coffee beverage in a glass cup, condensation on the surface, sitting on a wooden café table with shallow depth of field. The background features beautiful bokeh lighting, creating a cozy, blurred café ambiance. Shot in portrait mode --ar 85:128, 4K ultra-resolution, using --style raw5 for authentic textures, --v 6.0.",
153
- "An anime-style food illustration of a golden-brown wiener schnitzel, perfectly fried and crispy, served on a white ceramic plate. Accompanied by fresh parsley garnish and thin lemon slices, with artistic shading and stylized highlights. Captured in an anime illustration format --style raw5, high color saturation, food-themed aesthetic."
154
- ]
155
-
156
- css = '''
157
- .gradio-container {
158
- max-width: 100%;
159
- margin: 0 auto;
160
- }
161
- h1 { text-align: center; }
162
- footer { visibility: hidden; }
163
- '''
164
-
165
- with gr.Blocks(theme=seafoam, css=css) as demo:
166
- gr.Markdown("## T2I SD3.5")
167
-
168
- with gr.Row():
169
- with gr.Column(scale=1):
170
- with gr.Row():
171
- prompt = gr.Text(
172
- show_label=False,
173
- max_lines=1,
174
- placeholder="Enter your prompt",
175
- container=False,
176
- )
177
- run_button = gr.Button("Run", scale=0, variant="primary")
178
-
179
- result = gr.Image(show_label=False, format="png")
180
-
181
- grid_size_selection = gr.Dropdown(
182
- choices=list(grid_sizes.keys()),
183
- value="1x1",
184
- label="Grid Size"
185
- )
186
-
187
- with gr.Accordion("Advanced Settings", open=False):
188
- negative_prompt = gr.Text(
189
- label="Negative prompt",
190
- max_lines=1,
191
- placeholder="Enter a negative prompt",
192
- 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",
193
- )
194
- seed = gr.Slider(0, MAX_SEED, value=0, label="Seed")
195
- randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
196
-
197
- with gr.Row():
198
- width = gr.Slider(512, MAX_IMAGE_SIZE, step=32, value=1024, label="Width")
199
- height = gr.Slider(512, MAX_IMAGE_SIZE, step=32, value=1024, label="Height")
200
-
201
- with gr.Row():
202
- guidance_scale = gr.Slider(0.0, 7.5, step=0.1, value=0.0, label="Guidance scale")
203
- num_inference_steps = gr.Slider(1, 50, step=1, value=10, label="Number of inference steps")
204
-
205
- style_selection = gr.Radio(
206
- choices=STYLE_NAMES,
207
- value=DEFAULT_STYLE_NAME,
208
- label="Quality Style",
209
- )
210
-
211
- with gr.Column(scale=1):
212
- gr.Examples(
213
- examples=examples,
214
- inputs=[prompt],
215
- outputs=[result, seed],
216
- fn=infer,
217
- cache_examples=False,
218
- label="Prompt Examples"
219
- )
220
-
221
- gr.on(
222
- triggers=[run_button.click, prompt.submit],
223
- fn=infer,
224
- inputs=[
225
- prompt, negative_prompt, seed, randomize_seed,
226
- width, height, guidance_scale, num_inference_steps,
227
- style_selection, grid_size_selection
228
- ],
229
- outputs=[result, seed],
230
- )
231
-
232
- if __name__ == "__main__":
233
- demo.launch(ssr_mode=False)