Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -79,117 +79,58 @@ def enhance_prompt(user_prompt):
|
|
| 79 |
except Exception as e:
|
| 80 |
enhanced = f"Error enhancing prompt: {str(e)}"
|
| 81 |
return enhanced
|
| 82 |
-
# ---------- End Prompt Enhancement Setup ----------
|
| 83 |
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
# Use the enhanced prompt for image generation
|
| 95 |
-
for img in pipe.flux_pipe_call_that_returns_an_iterable_of_images(
|
| 96 |
-
prompt=enhanced_prompt,
|
| 97 |
-
guidance_scale=guidance_scale,
|
| 98 |
-
num_inference_steps=num_inference_steps,
|
| 99 |
-
width=width,
|
| 100 |
-
height=height,
|
| 101 |
generator=generator,
|
| 102 |
output_type="pil",
|
| 103 |
good_vae=good_vae,
|
| 104 |
-
)
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
margin: 0 auto;
|
| 110 |
-
max-width: 520px;
|
| 111 |
-
}
|
| 112 |
-
"""
|
| 113 |
|
| 114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
-
with gr.
|
| 117 |
-
gr.
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
with gr.Row():
|
| 122 |
-
prompt = gr.Text(
|
| 123 |
-
label="Prompt",
|
| 124 |
-
show_label=False,
|
| 125 |
-
max_lines=1,
|
| 126 |
-
placeholder="Enter your prompt",
|
| 127 |
-
container=False,
|
| 128 |
-
)
|
| 129 |
-
run_button = gr.Button("Run", scale=0)
|
| 130 |
-
|
| 131 |
-
result = gr.Image(label="Result", show_label=False)
|
| 132 |
-
|
| 133 |
-
with gr.Accordion("Advanced Settings", open=False):
|
| 134 |
-
seed = gr.Slider(
|
| 135 |
-
label="Seed",
|
| 136 |
-
minimum=0,
|
| 137 |
-
maximum=MAX_SEED,
|
| 138 |
-
step=1,
|
| 139 |
-
value=0,
|
| 140 |
-
)
|
| 141 |
-
|
| 142 |
-
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
| 143 |
-
|
| 144 |
-
with gr.Row():
|
| 145 |
-
width = gr.Slider(
|
| 146 |
-
label="Width",
|
| 147 |
-
minimum=256,
|
| 148 |
-
maximum=MAX_IMAGE_SIZE,
|
| 149 |
-
step=32,
|
| 150 |
-
value=1024,
|
| 151 |
-
)
|
| 152 |
-
height = gr.Slider(
|
| 153 |
-
label="Height",
|
| 154 |
-
minimum=256,
|
| 155 |
-
maximum=MAX_IMAGE_SIZE,
|
| 156 |
-
step=32,
|
| 157 |
-
value=1024,
|
| 158 |
-
)
|
| 159 |
-
|
| 160 |
-
with gr.Row():
|
| 161 |
-
guidance_scale = gr.Slider(
|
| 162 |
-
label="Guidance Scale",
|
| 163 |
-
minimum=1,
|
| 164 |
-
maximum=15,
|
| 165 |
-
step=0.1,
|
| 166 |
-
value=3.5,
|
| 167 |
-
)
|
| 168 |
-
num_inference_steps = gr.Slider(
|
| 169 |
-
label="Number of inference steps",
|
| 170 |
-
minimum=1,
|
| 171 |
-
maximum=50,
|
| 172 |
-
step=1,
|
| 173 |
-
value=28,
|
| 174 |
-
)
|
| 175 |
-
|
| 176 |
-
gr.Examples(
|
| 177 |
-
examples=[
|
| 178 |
-
"a tiny astronaut hatching from an egg on the moon",
|
| 179 |
-
"a cat holding a sign that says hello world",
|
| 180 |
-
"an anime illustration of a wiener schnitzel",
|
| 181 |
-
],
|
| 182 |
-
fn=infer,
|
| 183 |
-
inputs=[prompt],
|
| 184 |
-
outputs=[result, seed],
|
| 185 |
-
cache_examples="lazy"
|
| 186 |
)
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 194 |
|
| 195 |
-
|
|
|
|
|
|
| 79 |
except Exception as e:
|
| 80 |
enhanced = f"Error enhancing prompt: {str(e)}"
|
| 81 |
return enhanced
|
|
|
|
| 82 |
|
| 83 |
+
def generate_image(prompt):
|
| 84 |
+
"""Generates an image using the provided (possibly edited) prompt."""
|
| 85 |
+
try:
|
| 86 |
+
generator = torch.Generator(device).manual_seed(42)
|
| 87 |
+
image = pipe(
|
| 88 |
+
prompt=prompt,
|
| 89 |
+
guidance_scale=3.5,
|
| 90 |
+
num_inference_steps=50,
|
| 91 |
+
width=1024,
|
| 92 |
+
height=1024,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
generator=generator,
|
| 94 |
output_type="pil",
|
| 95 |
good_vae=good_vae,
|
| 96 |
+
).images[0]
|
| 97 |
+
except Exception as e:
|
| 98 |
+
image = None
|
| 99 |
+
print(e)
|
| 100 |
+
return image
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
+
# --- Gradio Interface ---
|
| 103 |
+
with gr.Blocks(css=".gradio-container {background-color: #f9f9f9; padding: 20px;}") as demo:
|
| 104 |
+
gr.Markdown("# 2-Step Image Generator")
|
| 105 |
+
gr.Markdown(
|
| 106 |
+
"### Step 1: Prompt Enhancement\n"
|
| 107 |
+
"Enter your original prompt below and click **Enhance Prompt**. "
|
| 108 |
+
"The system will generate a detailed version of your prompt. You can modify the enhanced prompt before generating the image."
|
| 109 |
+
)
|
| 110 |
|
| 111 |
+
with gr.Row():
|
| 112 |
+
original_prompt = gr.Textbox(
|
| 113 |
+
label="Your Original Prompt",
|
| 114 |
+
placeholder="Describe your idea here...",
|
| 115 |
+
lines=3
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
)
|
| 117 |
+
enhance_button = gr.Button("Enhance Prompt")
|
| 118 |
+
|
| 119 |
+
enhanced_prompt_box = gr.Textbox(
|
| 120 |
+
label="Enhanced Prompt (Editable)",
|
| 121 |
+
placeholder="The enhanced prompt will appear here...",
|
| 122 |
+
lines=3
|
| 123 |
)
|
| 124 |
+
|
| 125 |
+
enhance_button.click(fn=enhance_prompt, inputs=original_prompt, outputs=enhanced_prompt_box)
|
| 126 |
+
|
| 127 |
+
gr.Markdown("### Step 2: Image Generation\n"
|
| 128 |
+
"Review and modify the enhanced prompt if necessary. Once you're ready, click **Generate Image** to create your image.")
|
| 129 |
+
|
| 130 |
+
generate_button = gr.Button("Generate Image")
|
| 131 |
+
image_output = gr.Image(label="Generated Image")
|
| 132 |
+
|
| 133 |
+
generate_button.click(fn=generate_image, inputs=enhanced_prompt_box, outputs=image_output)
|
| 134 |
|
| 135 |
+
if __name__ == "__main__":
|
| 136 |
+
demo.launch(share=True)
|