ideprado commited on
Commit
0b36562
·
1 Parent(s): c5a017f

preset resolutions

Browse files
Files changed (1) hide show
  1. app.py +94 -24
app.py CHANGED
@@ -1,6 +1,7 @@
1
  import gradio as gr
2
  import numpy as np
3
  import random
 
4
 
5
  import spaces
6
  import torch
@@ -26,10 +27,38 @@ pipe.vae.enable_slicing()
26
  pipe.vae.enable_tiling()
27
 
28
  MAX_SEED = np.iinfo(np.int32).max
29
- MAX_IMAGE_SIZE = 1024
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
 
31
 
32
- @spaces.GPU(duration=500)
33
  def infer(
34
  prompt,
35
  negative_prompt,
@@ -59,10 +88,32 @@ def infer(
59
  return image, seed
60
 
61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
  examples = [
63
- "A photorealistic 3D render of a charming, mischievous young boy with long, floppy donkey ears and a small pink pig nose",
64
- "A landscape photograph showing a serene mountain lake at sunset with reflections in crystal clear water",
65
- "A detailed digital painting of a futuristic cyberpunk city with neon lights and flying vehicles",
 
66
  ]
67
 
68
  css = """
@@ -74,7 +125,7 @@ css = """
74
 
75
  with gr.Blocks(css=css) as demo:
76
  with gr.Column(elem_id="col-container"):
77
- gr.Markdown(" # Pikigen Text-to-Image Demo")
78
 
79
  with gr.Row():
80
  prompt = gr.Text(
@@ -90,6 +141,33 @@ with gr.Blocks(css=css) as demo:
90
  result = gr.Image(label="Result", show_label=False)
91
 
92
  with gr.Accordion("Advanced Settings", open=False):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93
  negative_prompt = gr.Text(
94
  label="Negative prompt",
95
  max_lines=1,
@@ -107,23 +185,6 @@ with gr.Blocks(css=css) as demo:
107
 
108
  randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
109
 
110
- with gr.Row():
111
- width = gr.Slider(
112
- label="Width",
113
- minimum=256,
114
- maximum=MAX_IMAGE_SIZE,
115
- step=32,
116
- value=1024,
117
- )
118
-
119
- height = gr.Slider(
120
- label="Height",
121
- minimum=256,
122
- maximum=MAX_IMAGE_SIZE,
123
- step=32,
124
- value=1024,
125
- )
126
-
127
  with gr.Row():
128
  guidance_scale = gr.Slider(
129
  label="Guidance scale",
@@ -141,7 +202,16 @@ with gr.Blocks(css=css) as demo:
141
  value=30,
142
  )
143
 
144
- gr.Examples(examples=examples, inputs=[prompt])
 
 
 
 
 
 
 
 
 
145
  gr.on(
146
  triggers=[run_button.click, prompt.submit],
147
  fn=infer,
 
1
  import gradio as gr
2
  import numpy as np
3
  import random
4
+ import json
5
 
6
  import spaces
7
  import torch
 
27
  pipe.vae.enable_tiling()
28
 
29
  MAX_SEED = np.iinfo(np.int32).max
30
+ MAX_IMAGE_SIZE = 1600
31
+
32
+ # Predefined resolutions
33
+ RESOLUTIONS = {
34
+ "horizontal": [
35
+ {"width": 1344, "height": 896, "label": "1344×896"},
36
+ {"width": 1152, "height": 768, "label": "1152×768"},
37
+ {"width": 960, "height": 640, "label": "960×640"},
38
+ {"width": 1600, "height": 896, "label": "1600×896"}
39
+ ],
40
+ "vertical": [
41
+ {"width": 896, "height": 1344, "label": "896×1344"},
42
+ {"width": 768, "height": 1152, "label": "768×1152"},
43
+ {"width": 640, "height": 960, "label": "640×960"},
44
+ {"width": 896, "height": 1600, "label": "896×1600"}
45
+ ],
46
+ "square": [
47
+ {"width": 1216, "height": 1216, "label": "1216×1216"},
48
+ {"width": 1024, "height": 1024, "label": "1024×1024"}
49
+ ],
50
+ "default": {"width": 1024, "height": 1024, "label": "1024×1024"}
51
+ }
52
+
53
+ # Create flattened options for the dropdown
54
+ resolution_options = []
55
+ for category, resolutions in RESOLUTIONS.items():
56
+ if category != "default":
57
+ for res in resolutions:
58
+ resolution_options.append(f"{category.capitalize()} - {res['label']}")
59
 
60
 
61
+ @spaces.GPU(duration=120)
62
  def infer(
63
  prompt,
64
  negative_prompt,
 
88
  return image, seed
89
 
90
 
91
+ def update_resolution(resolution_option):
92
+ """Updates width and height based on selected resolution option"""
93
+ if not resolution_option:
94
+ # Use default resolution
95
+ return RESOLUTIONS["default"]["width"], RESOLUTIONS["default"]["height"]
96
+
97
+ # Parse the resolution option format: "Category - WidthxHeight"
98
+ try:
99
+ category, label = resolution_option.split(" - ")
100
+ category = category.lower()
101
+
102
+ for res in RESOLUTIONS[category]:
103
+ if res["label"] == label:
104
+ return res["width"], res["height"]
105
+ except:
106
+ pass
107
+
108
+ # Fallback to default
109
+ return RESOLUTIONS["default"]["width"], RESOLUTIONS["default"]["height"]
110
+
111
+
112
  examples = [
113
+ "A photorealistic 3D render of a charming, mischievous young boy, approximately eight years old, possessing the endearingly unusual features of long, floppy donkey ears that droop playfully over his shoulders and a surprisingly small, pink pig nose that twitches slightly. His eyes, a sparkling, intelligent hazel, are wide with a hint of playful mischief, framed by slightly unruly, sandy-brown hair that falls in tousled waves across his forehead. He's dressed in a simple, slightly oversized, worn denim shirt and patched-up corduroy trousers, hinting at a life spent playing outdoors. The lighting is soft and natural, casting gentle shadows that highlight the texture of his skin – slightly freckled and sun-kissed, suggesting time spent in the sun. His expression is one of curious anticipation, his lips slightly parted as if he’s about to speak or perhaps is listening intently. The background is a subtly blurred pastoral scene, perhaps a sun-dappled meadow with wildflowers, enhancing the overall whimsical and slightly surreal nature of the character. The overall style aims for a blend of realistic rendering with a touch of whimsical cartoonishness, capturing the unique juxtaposition of the boy's human features and his animalistic ears and nose.",
114
+ "Two white swans with long necks, gracefully swimming in a still body of water. The swans are positioned in a heart shape, with their necks intertwined, creating a romantic and elegant scene. The water is calm and reflective, reflecting the soft, golden light of the setting sun. The background is a blur of soft, golden hues, suggesting a peaceful and serene environment. The image is likely a photograph, captured with a shallow depth of field, which emphasizes the swans and creates a sense of intimacy. The soft lighting and the gentle curves of the swans create a sense of tranquility and beauty. The overall mood of the image is one of love, peace, and serenity.",
115
+ """A watercolor painting of the American flag waving in the wind. The flag is painted in a vibrant red, white, and blue, with the stars in the blue field appearing slightly blurred, creating a sense of motion. The red stripes are painted with a slightly textured brushstroke, giving the flag a realistic and weathered look. The flag is positioned diagonally across the image, with the top left corner extending beyond the frame. The background is a simple white, allowing the flag to be the focal point. Below the flag, in bold red letters, is the text "PRESIDENTS DAY," with "21 FEBRUARY" in blue text above it. Below the text, in black, is "UNITED STATES OF AMERICA." The overall style of the image is patriotic and celebratory, with the watercolor technique adding a touch of artistic flair. The image evokes a sense of pride and national unity, making it a fitting tribute to Presidents Day.""",
116
+ "A captivating photo, shot with a shallow depth of field, of a stunning blonde woman with cascading waves of platinum blonde hair that fall past her shoulders, catching the light. Her eyes, a striking shade of emerald green, are intensely focused on something just off-camera, creating a sense of intrigue. Sunlight streams softly onto her face, highlighting the delicate curve of her cheekbones and the subtle freckles scattered across her nose. She's wearing a flowing, bohemian-style maxi dress, the fabric a deep sapphire blue that complements her hair and eyes beautifully. The dress is adorned with intricate embroidery along the neckline and sleeves, adding a touch of elegance. The background is intentionally blurred, suggesting a sun-drenched garden setting with hints of vibrant flowers and lush greenery, drawing the viewer's eye to the woman's captivating features. The overall mood is serene yet captivating, evoking a feeling of summer warmth and quiet contemplation. The image should have a natural, slightly ethereal quality, with soft, diffused lighting that enhances her beauty without harsh shadows.",
117
  ]
118
 
119
  css = """
 
125
 
126
  with gr.Blocks(css=css) as demo:
127
  with gr.Column(elem_id="col-container"):
128
+ gr.Markdown(" # F-lite Text-to-Image Demo")
129
 
130
  with gr.Row():
131
  prompt = gr.Text(
 
141
  result = gr.Image(label="Result", show_label=False)
142
 
143
  with gr.Accordion("Advanced Settings", open=False):
144
+ with gr.Tabs() as resolution_tabs:
145
+ with gr.TabItem("Preset Resolutions"):
146
+ resolution_dropdown = gr.Dropdown(
147
+ label="Select Resolution",
148
+ choices=[""] + resolution_options,
149
+ value="",
150
+ allow_custom_value=False,
151
+ )
152
+
153
+ with gr.TabItem("Custom Resolution"):
154
+ with gr.Row():
155
+ width = gr.Slider(
156
+ label="Width",
157
+ minimum=256,
158
+ maximum=MAX_IMAGE_SIZE,
159
+ step=32,
160
+ value=RESOLUTIONS["default"]["width"],
161
+ )
162
+
163
+ height = gr.Slider(
164
+ label="Height",
165
+ minimum=256,
166
+ maximum=MAX_IMAGE_SIZE,
167
+ step=32,
168
+ value=RESOLUTIONS["default"]["height"],
169
+ )
170
+
171
  negative_prompt = gr.Text(
172
  label="Negative prompt",
173
  max_lines=1,
 
185
 
186
  randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
187
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
188
  with gr.Row():
189
  guidance_scale = gr.Slider(
190
  label="Guidance scale",
 
202
  value=30,
203
  )
204
 
205
+ # Examples should explicitly target only the prompt input
206
+ gr.Examples(examples=examples, inputs=prompt, example_labels=[ex[:120] + "..." if len(ex) > 120 else ex for ex in examples])
207
+
208
+ # Update width and height when resolution is selected from dropdown
209
+ resolution_dropdown.change(
210
+ fn=update_resolution,
211
+ inputs=[resolution_dropdown],
212
+ outputs=[width, height]
213
+ )
214
+
215
  gr.on(
216
  triggers=[run_button.click, prompt.submit],
217
  fn=infer,