Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -38,7 +38,6 @@ else:
|
|
| 38 |
torch_dtype = torch.float32
|
| 39 |
|
| 40 |
pipe = FLitePipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype)
|
| 41 |
-
# pipe.enable_model_cpu_offload() # For less memory consumption
|
| 42 |
pipe.to(device)
|
| 43 |
pipe.vae.enable_slicing()
|
| 44 |
pipe.vae.enable_tiling()
|
|
@@ -46,7 +45,6 @@ pipe.vae.enable_tiling()
|
|
| 46 |
MAX_SEED = np.iinfo(np.int32).max
|
| 47 |
MAX_IMAGE_SIZE = 1600
|
| 48 |
|
| 49 |
-
# Predefined resolutions
|
| 50 |
RESOLUTIONS = {
|
| 51 |
"horizontal": [
|
| 52 |
{"width": 1344, "height": 896, "label": "1344×896"},
|
|
@@ -66,33 +64,20 @@ RESOLUTIONS = {
|
|
| 66 |
]
|
| 67 |
}
|
| 68 |
|
| 69 |
-
# Default resolution
|
| 70 |
DEFAULT_RESOLUTION = {"width": 1024, "height": 1024, "label": "1024×1024"}
|
| 71 |
|
| 72 |
-
# Create flattened options for the dropdown
|
| 73 |
resolution_options = []
|
| 74 |
for category, resolutions in RESOLUTIONS.items():
|
| 75 |
-
resolution_options.append([f"{category.capitalize()}", None]) #
|
| 76 |
for res in resolutions:
|
| 77 |
resolution_options.append([f" {res['label']}", f"{category}:{res['width']}:{res['height']}"])
|
| 78 |
|
| 79 |
def enrich_prompt_with_gemini(prompt, max_tokens=1024):
|
| 80 |
-
"""
|
| 81 |
-
Enrich a prompt using Google's Gemini API.
|
| 82 |
-
|
| 83 |
-
Args:
|
| 84 |
-
prompt: The original prompt to enrich
|
| 85 |
-
max_tokens: Maximum number of tokens for the response
|
| 86 |
-
|
| 87 |
-
Returns:
|
| 88 |
-
tuple: (enriched_prompt, error_message)
|
| 89 |
-
"""
|
| 90 |
try:
|
| 91 |
if not os.getenv("GEMINI_API_KEY"):
|
| 92 |
-
return None, "GEMINI_API_KEY not found in environment variables.
|
| 93 |
|
| 94 |
model = genai.GenerativeModel('gemini-1.5-flash')
|
| 95 |
-
|
| 96 |
enrichment_prompt = f"""
|
| 97 |
You are a prompt enhancer for image generation.
|
| 98 |
Take the following basic prompt and make it longer, very descriptive, and detailed.
|
|
@@ -102,26 +87,20 @@ def enrich_prompt_with_gemini(prompt, max_tokens=1024):
|
|
| 102 |
|
| 103 |
Enhanced prompt:
|
| 104 |
"""
|
| 105 |
-
|
| 106 |
response = model.generate_content(enrichment_prompt, generation_config={
|
| 107 |
"max_output_tokens": max_tokens,
|
| 108 |
"temperature": 1,
|
| 109 |
})
|
| 110 |
-
|
| 111 |
enriched_prompt = response.text.strip()
|
| 112 |
return enriched_prompt, None
|
| 113 |
-
|
| 114 |
except Exception as e:
|
| 115 |
error_message = f"Error enriching prompt: {str(e)}"
|
| 116 |
logging.error(error_message)
|
| 117 |
return None, error_message
|
| 118 |
|
| 119 |
-
# Function to update width and height based on selected resolution
|
| 120 |
def update_resolution(resolution_value):
|
| 121 |
-
"""Updates width and height based on selected resolution value"""
|
| 122 |
if not resolution_value:
|
| 123 |
return DEFAULT_RESOLUTION["width"], DEFAULT_RESOLUTION["height"]
|
| 124 |
-
|
| 125 |
try:
|
| 126 |
category, width, height = resolution_value.split(":")
|
| 127 |
return int(width), int(height)
|
|
@@ -144,14 +123,13 @@ def infer(
|
|
| 144 |
):
|
| 145 |
enriched_prompt_str = None
|
| 146 |
error_message_str = None
|
| 147 |
-
generation_prompt = prompt
|
| 148 |
|
| 149 |
if use_prompt_enrichment and gemini_available:
|
| 150 |
enriched_prompt_str, error_message_str = enrich_prompt_with_gemini(prompt)
|
| 151 |
if enriched_prompt_str:
|
| 152 |
-
generation_prompt = enriched_prompt_str
|
| 153 |
-
|
| 154 |
-
|
| 155 |
if randomize_seed:
|
| 156 |
seed = random.randint(0, MAX_SEED)
|
| 157 |
|
|
@@ -168,7 +146,6 @@ def infer(
|
|
| 168 |
apg_config=APGConfig(enabled=enable_apg)
|
| 169 |
).images[0]
|
| 170 |
|
| 171 |
-
# Prepare Gradio updates for the enriched prompt display
|
| 172 |
enriched_prompt_display_update = gr.update(visible=False)
|
| 173 |
enriched_prompt_text_update = gr.update(value="")
|
| 174 |
enrichment_error_update = gr.update(visible=False, value="")
|
|
@@ -183,10 +160,10 @@ def infer(
|
|
| 183 |
return image, seed, enriched_prompt_display_update, enriched_prompt_text_update, enrichment_error_update
|
| 184 |
|
| 185 |
examples = [
|
| 186 |
-
["A photorealistic 3D render of a charming, mischievous young boy
|
| 187 |
-
["Two white swans with long necks
|
| 188 |
-
["
|
| 189 |
-
["A captivating photo
|
| 190 |
]
|
| 191 |
|
| 192 |
css = """
|
|
@@ -195,15 +172,15 @@ css = """
|
|
| 195 |
max-width: 1024px;
|
| 196 |
}
|
| 197 |
.prompt-row > .gr-form {
|
| 198 |
-
gap: 0.5rem !important;
|
| 199 |
-
align-items: center;
|
| 200 |
}
|
| 201 |
"""
|
| 202 |
|
| 203 |
with gr.Blocks(css=css, theme="ParityError/Interstellar") as demo:
|
| 204 |
with gr.Column(elem_id="col-container"):
|
| 205 |
gr.Markdown(f" # {model_name} Text-to-Image Demo")
|
| 206 |
-
|
| 207 |
with gr.Row(elem_classes="prompt-row"):
|
| 208 |
prompt = gr.Text(
|
| 209 |
label="Prompt",
|
|
@@ -211,22 +188,18 @@ with gr.Blocks(css=css, theme="ParityError/Interstellar") as demo:
|
|
| 211 |
max_lines=1,
|
| 212 |
placeholder="Enter your prompt",
|
| 213 |
container=False,
|
| 214 |
-
scale=6
|
| 215 |
)
|
| 216 |
-
|
| 217 |
use_prompt_enrichment = gr.Checkbox(
|
| 218 |
label="Enrich",
|
| 219 |
-
value=True
|
| 220 |
-
visible=
|
| 221 |
-
scale=1, # Give checkbox some space
|
| 222 |
-
min_width=100 # Ensure label isn't cut off
|
| 223 |
)
|
| 224 |
-
|
| 225 |
run_button = gr.Button("Run", scale=1, variant="primary", min_width=100)
|
| 226 |
-
|
| 227 |
result = gr.Image(label="Result", show_label=False)
|
| 228 |
|
| 229 |
-
# Enriched prompt display (outside Advanced Settings)
|
| 230 |
enriched_prompt_display = gr.Accordion("Enriched Prompt", open=False, visible=False)
|
| 231 |
with enriched_prompt_display:
|
| 232 |
enriched_prompt_text = gr.Textbox(
|
|
@@ -253,7 +226,8 @@ with gr.Blocks(css=css, theme="ParityError/Interstellar") as demo:
|
|
| 253 |
resolution_dropdown = gr.Dropdown(
|
| 254 |
label="Resolution",
|
| 255 |
choices=resolution_options,
|
| 256 |
-
|
|
|
|
| 257 |
type="value"
|
| 258 |
)
|
| 259 |
|
|
@@ -264,15 +238,16 @@ with gr.Blocks(css=css, theme="ParityError/Interstellar") as demo:
|
|
| 264 |
minimum=256,
|
| 265 |
maximum=MAX_IMAGE_SIZE,
|
| 266 |
step=32,
|
| 267 |
-
|
|
|
|
| 268 |
)
|
| 269 |
-
|
| 270 |
height = gr.Slider(
|
| 271 |
label="Height",
|
| 272 |
minimum=256,
|
| 273 |
maximum=MAX_IMAGE_SIZE,
|
| 274 |
step=32,
|
| 275 |
-
|
|
|
|
| 276 |
)
|
| 277 |
|
| 278 |
seed = gr.Slider(
|
|
@@ -291,43 +266,37 @@ with gr.Blocks(css=css, theme="ParityError/Interstellar") as demo:
|
|
| 291 |
minimum=0.0,
|
| 292 |
maximum=15.0,
|
| 293 |
step=0.1,
|
| 294 |
-
value=6,
|
| 295 |
)
|
| 296 |
enable_apg = gr.Checkbox(
|
| 297 |
label="Enable APG",
|
| 298 |
value=True,
|
| 299 |
)
|
| 300 |
|
|
|
|
| 301 |
num_inference_steps = gr.Slider(
|
| 302 |
label="Number of inference steps",
|
| 303 |
minimum=1,
|
| 304 |
maximum=50,
|
| 305 |
step=1,
|
| 306 |
-
value=
|
| 307 |
)
|
| 308 |
|
| 309 |
-
#
|
| 310 |
max_length = 180
|
| 311 |
|
| 312 |
-
# Function to handle example clicks - sets prompt and disables enrichment
|
| 313 |
def set_example_and_disable_enrichment(example, current_checkbox_value):
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
gr.Examples(
|
| 318 |
examples=examples,
|
| 319 |
-
# Add use_prompt_enrichment to inputs
|
| 320 |
inputs=[prompt, use_prompt_enrichment],
|
| 321 |
outputs=[prompt, use_prompt_enrichment],
|
| 322 |
-
fn=set_example_and_disable_enrichment,
|
| 323 |
-
# Need to adjust example_labels to access the prompt string within the sublist
|
| 324 |
example_labels=[ex[0][:max_length] + "..." if len(ex[0]) > max_length else ex[0] for ex in examples]
|
| 325 |
)
|
| 326 |
-
|
| 327 |
-
# Add link to model card
|
| 328 |
gr.Markdown(f"[{model_name} Model Card and Weights](https://huggingface.co/{model_repo_id})")
|
| 329 |
|
| 330 |
-
# Update width and height when resolution dropdown changes
|
| 331 |
resolution_dropdown.change(
|
| 332 |
fn=update_resolution,
|
| 333 |
inputs=resolution_dropdown,
|
|
@@ -354,4 +323,4 @@ with gr.Blocks(css=css, theme="ParityError/Interstellar") as demo:
|
|
| 354 |
|
| 355 |
|
| 356 |
if __name__ == "__main__":
|
| 357 |
-
demo.launch()
|
|
|
|
| 38 |
torch_dtype = torch.float32
|
| 39 |
|
| 40 |
pipe = FLitePipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype)
|
|
|
|
| 41 |
pipe.to(device)
|
| 42 |
pipe.vae.enable_slicing()
|
| 43 |
pipe.vae.enable_tiling()
|
|
|
|
| 45 |
MAX_SEED = np.iinfo(np.int32).max
|
| 46 |
MAX_IMAGE_SIZE = 1600
|
| 47 |
|
|
|
|
| 48 |
RESOLUTIONS = {
|
| 49 |
"horizontal": [
|
| 50 |
{"width": 1344, "height": 896, "label": "1344×896"},
|
|
|
|
| 64 |
]
|
| 65 |
}
|
| 66 |
|
|
|
|
| 67 |
DEFAULT_RESOLUTION = {"width": 1024, "height": 1024, "label": "1024×1024"}
|
| 68 |
|
|
|
|
| 69 |
resolution_options = []
|
| 70 |
for category, resolutions in RESOLUTIONS.items():
|
| 71 |
+
resolution_options.append([f"{category.capitalize()}", None]) # 카테고리 헤더용
|
| 72 |
for res in resolutions:
|
| 73 |
resolution_options.append([f" {res['label']}", f"{category}:{res['width']}:{res['height']}"])
|
| 74 |
|
| 75 |
def enrich_prompt_with_gemini(prompt, max_tokens=1024):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
try:
|
| 77 |
if not os.getenv("GEMINI_API_KEY"):
|
| 78 |
+
return None, "GEMINI_API_KEY not found in environment variables."
|
| 79 |
|
| 80 |
model = genai.GenerativeModel('gemini-1.5-flash')
|
|
|
|
| 81 |
enrichment_prompt = f"""
|
| 82 |
You are a prompt enhancer for image generation.
|
| 83 |
Take the following basic prompt and make it longer, very descriptive, and detailed.
|
|
|
|
| 87 |
|
| 88 |
Enhanced prompt:
|
| 89 |
"""
|
|
|
|
| 90 |
response = model.generate_content(enrichment_prompt, generation_config={
|
| 91 |
"max_output_tokens": max_tokens,
|
| 92 |
"temperature": 1,
|
| 93 |
})
|
|
|
|
| 94 |
enriched_prompt = response.text.strip()
|
| 95 |
return enriched_prompt, None
|
|
|
|
| 96 |
except Exception as e:
|
| 97 |
error_message = f"Error enriching prompt: {str(e)}"
|
| 98 |
logging.error(error_message)
|
| 99 |
return None, error_message
|
| 100 |
|
|
|
|
| 101 |
def update_resolution(resolution_value):
|
|
|
|
| 102 |
if not resolution_value:
|
| 103 |
return DEFAULT_RESOLUTION["width"], DEFAULT_RESOLUTION["height"]
|
|
|
|
| 104 |
try:
|
| 105 |
category, width, height = resolution_value.split(":")
|
| 106 |
return int(width), int(height)
|
|
|
|
| 123 |
):
|
| 124 |
enriched_prompt_str = None
|
| 125 |
error_message_str = None
|
| 126 |
+
generation_prompt = prompt
|
| 127 |
|
| 128 |
if use_prompt_enrichment and gemini_available:
|
| 129 |
enriched_prompt_str, error_message_str = enrich_prompt_with_gemini(prompt)
|
| 130 |
if enriched_prompt_str:
|
| 131 |
+
generation_prompt = enriched_prompt_str
|
| 132 |
+
|
|
|
|
| 133 |
if randomize_seed:
|
| 134 |
seed = random.randint(0, MAX_SEED)
|
| 135 |
|
|
|
|
| 146 |
apg_config=APGConfig(enabled=enable_apg)
|
| 147 |
).images[0]
|
| 148 |
|
|
|
|
| 149 |
enriched_prompt_display_update = gr.update(visible=False)
|
| 150 |
enriched_prompt_text_update = gr.update(value="")
|
| 151 |
enrichment_error_update = gr.update(visible=False, value="")
|
|
|
|
| 160 |
return image, seed, enriched_prompt_display_update, enriched_prompt_text_update, enrichment_error_update
|
| 161 |
|
| 162 |
examples = [
|
| 163 |
+
["A photorealistic 3D render of a charming, mischievous young boy ... donkey ears ...", None],
|
| 164 |
+
["Two white swans with long necks ... heart shape ... calm water ...", None],
|
| 165 |
+
["An awe-inspiring landscape of a pristine mountain lake ...", None],
|
| 166 |
+
["A captivating photo of a stunning blonde woman ... bohemian style ...", None],
|
| 167 |
]
|
| 168 |
|
| 169 |
css = """
|
|
|
|
| 172 |
max-width: 1024px;
|
| 173 |
}
|
| 174 |
.prompt-row > .gr-form {
|
| 175 |
+
gap: 0.5rem !important;
|
| 176 |
+
align-items: center;
|
| 177 |
}
|
| 178 |
"""
|
| 179 |
|
| 180 |
with gr.Blocks(css=css, theme="ParityError/Interstellar") as demo:
|
| 181 |
with gr.Column(elem_id="col-container"):
|
| 182 |
gr.Markdown(f" # {model_name} Text-to-Image Demo")
|
| 183 |
+
|
| 184 |
with gr.Row(elem_classes="prompt-row"):
|
| 185 |
prompt = gr.Text(
|
| 186 |
label="Prompt",
|
|
|
|
| 188 |
max_lines=1,
|
| 189 |
placeholder="Enter your prompt",
|
| 190 |
container=False,
|
| 191 |
+
scale=6
|
| 192 |
)
|
| 193 |
+
# 1) Enrich 기본값 True / 표시 안 함(visible=False)
|
| 194 |
use_prompt_enrichment = gr.Checkbox(
|
| 195 |
label="Enrich",
|
| 196 |
+
value=True, # default True
|
| 197 |
+
visible=False # Hide from UI
|
|
|
|
|
|
|
| 198 |
)
|
|
|
|
| 199 |
run_button = gr.Button("Run", scale=1, variant="primary", min_width=100)
|
| 200 |
+
|
| 201 |
result = gr.Image(label="Result", show_label=False)
|
| 202 |
|
|
|
|
| 203 |
enriched_prompt_display = gr.Accordion("Enriched Prompt", open=False, visible=False)
|
| 204 |
with enriched_prompt_display:
|
| 205 |
enriched_prompt_text = gr.Textbox(
|
|
|
|
| 226 |
resolution_dropdown = gr.Dropdown(
|
| 227 |
label="Resolution",
|
| 228 |
choices=resolution_options,
|
| 229 |
+
# 2) 디폴트로 1600x896(수평 최대 크기)
|
| 230 |
+
value="horizontal:1600:896",
|
| 231 |
type="value"
|
| 232 |
)
|
| 233 |
|
|
|
|
| 238 |
minimum=256,
|
| 239 |
maximum=MAX_IMAGE_SIZE,
|
| 240 |
step=32,
|
| 241 |
+
# set default to 1600
|
| 242 |
+
value=1600,
|
| 243 |
)
|
|
|
|
| 244 |
height = gr.Slider(
|
| 245 |
label="Height",
|
| 246 |
minimum=256,
|
| 247 |
maximum=MAX_IMAGE_SIZE,
|
| 248 |
step=32,
|
| 249 |
+
# set default to 896
|
| 250 |
+
value=896,
|
| 251 |
)
|
| 252 |
|
| 253 |
seed = gr.Slider(
|
|
|
|
| 266 |
minimum=0.0,
|
| 267 |
maximum=15.0,
|
| 268 |
step=0.1,
|
| 269 |
+
value=6, # Keep default
|
| 270 |
)
|
| 271 |
enable_apg = gr.Checkbox(
|
| 272 |
label="Enable APG",
|
| 273 |
value=True,
|
| 274 |
)
|
| 275 |
|
| 276 |
+
# 3) num_inference_steps를 50으로 (최대값)
|
| 277 |
num_inference_steps = gr.Slider(
|
| 278 |
label="Number of inference steps",
|
| 279 |
minimum=1,
|
| 280 |
maximum=50,
|
| 281 |
step=1,
|
| 282 |
+
value=50, # "highest quality" default
|
| 283 |
)
|
| 284 |
|
| 285 |
+
# 제한된 길이
|
| 286 |
max_length = 180
|
| 287 |
|
|
|
|
| 288 |
def set_example_and_disable_enrichment(example, current_checkbox_value):
|
| 289 |
+
return example, gr.update(value=False)
|
| 290 |
+
|
|
|
|
| 291 |
gr.Examples(
|
| 292 |
examples=examples,
|
|
|
|
| 293 |
inputs=[prompt, use_prompt_enrichment],
|
| 294 |
outputs=[prompt, use_prompt_enrichment],
|
| 295 |
+
fn=set_example_and_disable_enrichment,
|
|
|
|
| 296 |
example_labels=[ex[0][:max_length] + "..." if len(ex[0]) > max_length else ex[0] for ex in examples]
|
| 297 |
)
|
|
|
|
|
|
|
| 298 |
gr.Markdown(f"[{model_name} Model Card and Weights](https://huggingface.co/{model_repo_id})")
|
| 299 |
|
|
|
|
| 300 |
resolution_dropdown.change(
|
| 301 |
fn=update_resolution,
|
| 302 |
inputs=resolution_dropdown,
|
|
|
|
| 323 |
|
| 324 |
|
| 325 |
if __name__ == "__main__":
|
| 326 |
+
demo.launch()
|