Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -46,6 +46,23 @@ decrypted_token = get_hf_token("gAAAAABn3GfShExoJd50nau3B5ZJNiQ9dRD1ACO3XXMwVaIQ
|
|
| 46 |
login(token=decrypted_token)
|
| 47 |
groq_client = Groq(api_key="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")
|
| 48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
def enhance_prompt(user_prompt):
|
| 50 |
"""Enhances the given prompt using Groq and returns the refined prompt."""
|
| 51 |
try:
|
|
@@ -61,7 +78,7 @@ def enhance_prompt(user_prompt):
|
|
| 61 |
"aperture, lens, and shot type. Use clear, visual descriptions. "
|
| 62 |
"Prompt Structure: Prompt should only contain keywords, no phrases. "
|
| 63 |
"[relation to background] [background]. [Details of background] "
|
| 64 |
-
"[Interactions with color and lighting]. (\"Taken on:\"/\"Drawn with:\")[Specific traits of style]
|
| 65 |
"Include details on medium, subject (clothing, hairstyle, pose, etc.), background, "
|
| 66 |
"colors, lighting, style traits, influences, technique, and camera settings if applicable."
|
| 67 |
),
|
|
@@ -80,57 +97,97 @@ def enhance_prompt(user_prompt):
|
|
| 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 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
with gr.
|
|
|
|
|
|
|
|
|
|
| 112 |
original_prompt = gr.Textbox(
|
| 113 |
-
label="
|
| 114 |
-
placeholder="
|
| 115 |
-
lines=
|
| 116 |
)
|
| 117 |
enhance_button = gr.Button("Enhance Prompt")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
|
|
|
|
|
|
| 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 |
-
|
| 136 |
-
demo.launch(share=True)
|
|
|
|
| 46 |
login(token=decrypted_token)
|
| 47 |
groq_client = Groq(api_key="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")
|
| 48 |
|
| 49 |
+
@spaces.GPU(duration=75)
|
| 50 |
+
def infer(prompt, seed=42, randomize_seed=False, width=1024, height=1024, guidance_scale=3.5, num_inference_steps=28, progress=gr.Progress(track_tqdm=True)):
|
| 51 |
+
if randomize_seed:
|
| 52 |
+
seed = random.randint(0, MAX_SEED)
|
| 53 |
+
generator = torch.Generator().manual_seed(seed)
|
| 54 |
+
for img in pipe.flux_pipe_call_that_returns_an_iterable_of_images(
|
| 55 |
+
prompt=prompt,
|
| 56 |
+
guidance_scale=guidance_scale,
|
| 57 |
+
num_inference_steps=num_inference_steps,
|
| 58 |
+
width=width,
|
| 59 |
+
height=height,
|
| 60 |
+
generator=generator,
|
| 61 |
+
output_type="pil",
|
| 62 |
+
good_vae=good_vae,
|
| 63 |
+
):
|
| 64 |
+
yield img, seed
|
| 65 |
+
|
| 66 |
def enhance_prompt(user_prompt):
|
| 67 |
"""Enhances the given prompt using Groq and returns the refined prompt."""
|
| 68 |
try:
|
|
|
|
| 78 |
"aperture, lens, and shot type. Use clear, visual descriptions. "
|
| 79 |
"Prompt Structure: Prompt should only contain keywords, no phrases. "
|
| 80 |
"[relation to background] [background]. [Details of background] "
|
| 81 |
+
"[Interactions with color and lighting]. (\"Taken on:\"/\"Drawn with:\")[Specific traits of style] "
|
| 82 |
"Include details on medium, subject (clothing, hairstyle, pose, etc.), background, "
|
| 83 |
"colors, lighting, style traits, influences, technique, and camera settings if applicable."
|
| 84 |
),
|
|
|
|
| 97 |
enhanced = f"Error enhancing prompt: {str(e)}"
|
| 98 |
return enhanced
|
| 99 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
# --- Gradio Interface ---
|
| 101 |
+
css = """
|
| 102 |
+
#col-container {
|
| 103 |
+
margin: 0 auto;
|
| 104 |
+
max-width: 520px;
|
| 105 |
+
}
|
| 106 |
+
"""
|
| 107 |
+
|
| 108 |
+
with gr.Blocks(css=css) as demo:
|
| 109 |
+
with gr.Column(elem_id="col-container"):
|
| 110 |
+
gr.Markdown("# FLUX.1 [dev] with Prompt Enhancement")
|
| 111 |
+
gr.Markdown("### Step 1: Enhance Your Prompt")
|
| 112 |
+
# Original prompt input and enhancement button
|
| 113 |
original_prompt = gr.Textbox(
|
| 114 |
+
label="Original Prompt",
|
| 115 |
+
placeholder="Enter your idea here...",
|
| 116 |
+
lines=2
|
| 117 |
)
|
| 118 |
enhance_button = gr.Button("Enhance Prompt")
|
| 119 |
+
# Editable textbox that will hold the enhanced prompt
|
| 120 |
+
enhanced_prompt = gr.Textbox(
|
| 121 |
+
label="Enhanced Prompt (Editable)",
|
| 122 |
+
placeholder="The enhanced prompt will appear here...",
|
| 123 |
+
lines=2
|
| 124 |
+
)
|
| 125 |
+
# When clicked, this button calls the enhance_prompt function.
|
| 126 |
+
enhance_button.click(fn=enhance_prompt, inputs=original_prompt, outputs=enhanced_prompt)
|
| 127 |
+
|
| 128 |
+
gr.Markdown("### Step 2: Generate Image Using Enhanced Prompt")
|
| 129 |
+
with gr.Row():
|
| 130 |
+
run_button = gr.Button("Generate Image", scale=0)
|
| 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 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
| 142 |
+
with gr.Row():
|
| 143 |
+
width = gr.Slider(
|
| 144 |
+
label="Width",
|
| 145 |
+
minimum=256,
|
| 146 |
+
maximum=MAX_IMAGE_SIZE,
|
| 147 |
+
step=32,
|
| 148 |
+
value=1024,
|
| 149 |
+
)
|
| 150 |
+
height = gr.Slider(
|
| 151 |
+
label="Height",
|
| 152 |
+
minimum=256,
|
| 153 |
+
maximum=MAX_IMAGE_SIZE,
|
| 154 |
+
step=32,
|
| 155 |
+
value=1024,
|
| 156 |
+
)
|
| 157 |
+
with gr.Row():
|
| 158 |
+
guidance_scale = gr.Slider(
|
| 159 |
+
label="Guidance Scale",
|
| 160 |
+
minimum=1,
|
| 161 |
+
maximum=15,
|
| 162 |
+
step=0.1,
|
| 163 |
+
value=3.5,
|
| 164 |
+
)
|
| 165 |
+
num_inference_steps = gr.Slider(
|
| 166 |
+
label="Number of inference steps",
|
| 167 |
+
minimum=1,
|
| 168 |
+
maximum=50,
|
| 169 |
+
step=1,
|
| 170 |
+
value=28,
|
| 171 |
+
)
|
| 172 |
+
|
| 173 |
+
gr.Examples(
|
| 174 |
+
examples=[
|
| 175 |
+
"a tiny astronaut hatching from an egg on the moon",
|
| 176 |
+
"a cat holding a sign that says hello world",
|
| 177 |
+
"an anime illustration of a wiener schnitzel",
|
| 178 |
+
],
|
| 179 |
+
fn=infer,
|
| 180 |
+
inputs=enhanced_prompt, # Uses the enhanced prompt
|
| 181 |
+
outputs=[result, seed],
|
| 182 |
+
cache_examples="lazy"
|
| 183 |
+
)
|
| 184 |
|
| 185 |
+
# Trigger the original image generation code using the enhanced prompt.
|
| 186 |
+
gr.on(
|
| 187 |
+
triggers=[run_button.click, enhanced_prompt.submit],
|
| 188 |
+
fn=infer,
|
| 189 |
+
inputs=[enhanced_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
|
| 190 |
+
outputs=[result, seed]
|
| 191 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 192 |
|
| 193 |
+
demo.launch(share=True)
|
|
|