maheshpd commited on
Commit
2f30b71
ยท
verified ยท
1 Parent(s): 523d602

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +133 -146
app.py CHANGED
@@ -1,154 +1,141 @@
1
  import gradio as gr
2
- import numpy as np
3
- import random
4
 
5
- # import spaces #[uncomment to use ZeroGPU]
6
- from diffusers import DiffusionPipeline
7
- import torch
 
 
8
 
9
  device = "cuda" if torch.cuda.is_available() else "cpu"
10
- model_repo_id = "stabilityai/sdxl-turbo" # Replace to the model you would like to use
11
-
12
- if torch.cuda.is_available():
13
- torch_dtype = torch.float16
14
- else:
15
- torch_dtype = torch.float32
16
-
17
- pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype)
18
- pipe = pipe.to(device)
19
-
20
- MAX_SEED = np.iinfo(np.int32).max
21
- MAX_IMAGE_SIZE = 1024
22
-
23
-
24
- # @spaces.GPU #[uncomment to use ZeroGPU]
25
- def infer(
26
- prompt,
27
- negative_prompt,
28
- seed,
29
- randomize_seed,
30
- width,
31
- height,
32
- guidance_scale,
33
- num_inference_steps,
34
- progress=gr.Progress(track_tqdm=True),
35
- ):
36
- if randomize_seed:
37
- seed = random.randint(0, MAX_SEED)
38
-
39
- generator = torch.Generator().manual_seed(seed)
40
-
41
- image = pipe(
42
- prompt=prompt,
43
- negative_prompt=negative_prompt,
44
- guidance_scale=guidance_scale,
45
- num_inference_steps=num_inference_steps,
46
- width=width,
47
- height=height,
48
- generator=generator,
49
- ).images[0]
50
-
51
- return image, seed
52
-
53
-
54
- examples = [
55
- "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
56
- "An astronaut riding a green horse",
57
- "A delicious ceviche cheesecake slice",
58
- ]
59
 
60
- css = """
61
- #col-container {
62
- margin: 0 auto;
63
- max-width: 640px;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
  }
65
- """
66
-
67
- with gr.Blocks(css=css) as demo:
68
- with gr.Column(elem_id="col-container"):
69
- gr.Markdown(" # Text-to-Image Gradio Template")
70
-
71
- with gr.Row():
72
- prompt = gr.Text(
73
- label="Prompt",
74
- show_label=False,
75
- max_lines=1,
76
- placeholder="Enter your prompt",
77
- container=False,
78
- )
79
-
80
- run_button = gr.Button("Run", scale=0, variant="primary")
81
-
82
- result = gr.Image(label="Result", show_label=False)
83
-
84
- with gr.Accordion("Advanced Settings", open=False):
85
- negative_prompt = gr.Text(
86
- label="Negative prompt",
87
- max_lines=1,
88
- placeholder="Enter a negative prompt",
89
- visible=False,
90
- )
91
-
92
- seed = gr.Slider(
93
- label="Seed",
94
- minimum=0,
95
- maximum=MAX_SEED,
96
- step=1,
97
- value=0,
98
- )
99
-
100
- randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
101
-
102
- with gr.Row():
103
- width = gr.Slider(
104
- label="Width",
105
- minimum=256,
106
- maximum=MAX_IMAGE_SIZE,
107
- step=32,
108
- value=1024, # Replace with defaults that work for your model
109
- )
110
-
111
- height = gr.Slider(
112
- label="Height",
113
- minimum=256,
114
- maximum=MAX_IMAGE_SIZE,
115
- step=32,
116
- value=1024, # Replace with defaults that work for your model
117
- )
118
-
119
- with gr.Row():
120
- guidance_scale = gr.Slider(
121
- label="Guidance scale",
122
- minimum=0.0,
123
- maximum=10.0,
124
- step=0.1,
125
- value=0.0, # Replace with defaults that work for your model
126
- )
127
-
128
- num_inference_steps = gr.Slider(
129
- label="Number of inference steps",
130
- minimum=1,
131
- maximum=50,
132
- step=1,
133
- value=2, # Replace with defaults that work for your model
134
- )
135
-
136
- gr.Examples(examples=examples, inputs=[prompt])
137
- gr.on(
138
- triggers=[run_button.click, prompt.submit],
139
- fn=infer,
140
- inputs=[
141
- prompt,
142
- negative_prompt,
143
- seed,
144
- randomize_seed,
145
- width,
146
- height,
147
- guidance_scale,
148
- num_inference_steps,
149
- ],
150
- outputs=[result, seed],
151
  )
152
 
153
- if __name__ == "__main__":
154
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ import torch, random
3
+ from diffusers import StableDiffusionXLPipeline, StableDiffusionXLImg2ImgPipeline, StableDiffusionXLInpaintPipeline, StableDiffusionLatentUpscalePipeline
4
 
5
+ # ---------------------------
6
+ # HARD CHOICES (donโ€™t change)
7
+ # ---------------------------
8
+ BASE_MODEL = "Lykon/dreamshaper-xl-1-0" # High-quality SDXL finetune
9
+ UPSCALE_MODEL = "stabilityai/sdxl-upscaler" # x4 upscaler
10
 
11
  device = "cuda" if torch.cuda.is_available() else "cpu"
12
+ dtype = torch.float16 if device == "cuda" else torch.float32
13
+
14
+ # Load base text2img pipeline
15
+ pipe = StableDiffusionXLPipeline.from_pretrained(
16
+ BASE_MODEL, torch_dtype=dtype, use_safetensors=True
17
+ )
18
+ pipe.to(device)
19
+
20
+ # Load upscaler (x4)
21
+ upscale_pipe = StableDiffusionLatentUpscalePipeline.from_pretrained(
22
+ UPSCALE_MODEL, torch_dtype=dtype, use_safetensors=True
23
+ ).to(device)
24
+
25
+ # ---------------------------
26
+ # Presets & helpers
27
+ # ---------------------------
28
+ ASPECTS = {
29
+ "1:1 (Square | 4096x4096)": (4096, 4096),
30
+ "16:9 (UHD | 3840x2160)": (3840, 2160),
31
+ "9:16 (Phone Tall | 2160x3840)": (2160, 3840), # Zedge/phone
32
+ "4:5 (Portrait | 4000x5000)": (4000, 5000), # Adobe Stock favorite
33
+ "3:4 (Poster | 3000x4000)": (3000, 4000),
34
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
 
36
+ STYLE_PRESETS = {
37
+ "Realistic Photo": {
38
+ "pos": "ultra-detailed realistic photo, natural lighting, sharp focus, DSLR depth of field, film grain subtle",
39
+ "neg": "blurry, lowres, artifact, extra fingers, deformed, watermark, text, logo, signature"
40
+ },
41
+ "3D Render": {
42
+ "pos": "high poly 3D render, global illumination, octane style, cinematic light, clean materials",
43
+ "neg": "lowpoly, noisy, aliased, bad shading, watermark, text, logo"
44
+ },
45
+ "Flat Illustration": {
46
+ "pos": "flat vector illustration, minimal shapes, smooth gradients, simple background, clean lines",
47
+ "neg": "noise, gradients banding, messy, sketchy, text"
48
+ },
49
+ "Minimalist": {
50
+ "pos": "minimalist composition, lots of negative space, clean background, simple geometric forms",
51
+ "neg": "cluttered, busy, noisy, text"
52
+ },
53
+ "Neon Wallpaper": {
54
+ "pos": "neon glow, vibrant colors, high contrast, dark background, crisp edges, perfect symmetry",
55
+ "neg": "dull, washed out, blurry, text, watermark"
56
+ }
57
  }
58
+
59
+ def seed_or_random(seed):
60
+ if seed in [None, "", 0]:
61
+ return random.randint(1, 2**31 - 1)
62
+ return int(seed)
63
+
64
+ def generate(prompt, style, aspect_key, steps, guidance, batch, seed, add_negatives):
65
+ if not prompt or len(prompt.strip()) < 3:
66
+ return [None]*batch, f"โ— Please enter a prompt."
67
+ w, h = ASPECTS[aspect_key]
68
+ s = seed_or_random(seed)
69
+ pos = prompt
70
+ neg = ""
71
+
72
+ # apply style preset
73
+ if style in STYLE_PRESETS:
74
+ pos = f"{STYLE_PRESETS[style]['pos']}, {prompt}"
75
+ if add_negatives:
76
+ neg = STYLE_PRESETS[style]['neg']
77
+
78
+ generator = torch.Generator(device=device).manual_seed(s)
79
+
80
+ images = pipe(
81
+ prompt=pos,
82
+ negative_prompt=neg,
83
+ guidance_scale=float(guidance),
84
+ num_inference_steps=int(steps),
85
+ width=int(w),
86
+ height=int(h),
87
+ num_images_per_prompt=int(batch),
88
+ generator=generator
89
+ ).images
90
+
91
+ # Simple quality guard: remove any None
92
+ images = [im for im in images if im is not None]
93
+ return images, f"โœ… Done. Seed: {s} | {aspect_key} | {style}"
94
+
95
+ def upscale(image, steps_up, guidance_up):
96
+ if image is None:
97
+ return None, "โ— Generate an image first."
98
+ # Latent upscaler expects an image and prompt; prompt can be empty for neutral upscale
99
+ result = upscale_pipe(
100
+ prompt="high quality, detailed, clean edges, no artifacts",
101
+ image=image,
102
+ guidance_scale=float(guidance_up),
103
+ num_inference_steps=int(steps_up)
104
+ ).images[0]
105
+ return result, "โœ… Upscaled x4"
106
+
107
+ with gr.Blocks(title="My Personal MidJourney-Style Tool") as demo:
108
+ gr.Markdown(
109
+ "# ๐Ÿ”ฎ Personal MidJourney-Style Tool\n"
110
+ "Optimized for **Freepik / Adobe Stock / Zedge**.\n"
111
+ "- Choose a **style** โ†’ set **aspect ratio** โ†’ **Generate** (x4 grid) โ†’ **Upscale x4** for stock.\n"
112
+ "- Tip: Use descriptive nouns, materials, lighting, and mood."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
113
  )
114
 
115
+ with gr.Row():
116
+ with gr.Column(scale=1):
117
+ prompt = gr.Textbox(label="Prompt (describe what you want)", placeholder="e.g., futuristic city skyline at dusk, reflective water, birds, cinematic")
118
+ style = gr.Dropdown(choices=list(STYLE_PRESETS.keys()), value="Realistic Photo", label="Style preset")
119
+ aspect = gr.Dropdown(choices=list(ASPECTS.keys()), value="16:9 (UHD | 3840x2160)", label="Aspect Ratio")
120
+ steps = gr.Slider(5, 50, value=28, step=1, label="Steps (quality โ†‘ but slower)")
121
+ guidance = gr.Slider(1.0, 12.0, value=6.5, step=0.5, label="Prompt Strength (CFG)")
122
+ batch = gr.Slider(1, 4, value=4, step=1, label="Batch (number of images)")
123
+ seed = gr.Number(value=0, label="Seed (0 = random)")
124
+ add_negs = gr.Checkbox(value=True, label="Add smart negative prompts")
125
+ gen_btn = gr.Button("๐Ÿš€ Generate")
126
+
127
+ with gr.Column(scale=1):
128
+ gallery = gr.Gallery(label="Results", show_label=True, columns=2, rows=2, height=600)
129
+ status = gr.Markdown()
130
+
131
+ gr.Markdown("### Upscale (pick the best image in gallery and paste below)")
132
+ to_upscale = gr.Image(type="pil", label="Image to upscale (paste or upload one of the results)")
133
+ steps_up = gr.Slider(5, 30, value=15, step=1, label="Upscale Steps")
134
+ guidance_up = gr.Slider(1.0, 10.0, value=5.0, step=0.5, label="Upscale Guidance")
135
+ up_btn = gr.Button("โฌ†๏ธ Upscale x4")
136
+ up_out = gr.Image(type="pil", label="Upscaled Output")
137
+
138
+ gen_btn.click(generate, [prompt, style, aspect, steps, guidance, batch, seed, add_negs], [gallery, status])
139
+ up_btn.click(upscale, [to_upscale, steps_up, guidance_up], [up_out, status])
140
+
141
+ demo.launch()