Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -89,12 +89,12 @@ def generate_image(
|
|
| 89 |
).images[0]
|
| 90 |
|
| 91 |
print(neg_json_prompt)
|
| 92 |
-
return image, seed, json_prompt, json.dumps(neg_json_prompt), gr.update(visible=True)
|
| 93 |
|
| 94 |
@spaces.GPU(duration=300)
|
| 95 |
def refine_prompt(
|
| 96 |
refine_instruction,
|
| 97 |
-
|
| 98 |
negative_prompt="",
|
| 99 |
seed=42,
|
| 100 |
randomize_seed=False,
|
|
@@ -107,10 +107,17 @@ def refine_prompt(
|
|
| 107 |
seed = random.randint(0, MAX_SEED)
|
| 108 |
|
| 109 |
with torch.inference_mode():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
json_prompt_str = (
|
| 111 |
-
json.dumps(
|
| 112 |
-
if isinstance(
|
| 113 |
-
else str(
|
| 114 |
)
|
| 115 |
output = vlm_pipe(json_prompt=json_prompt_str, prompt=refine_instruction)
|
| 116 |
json_prompt = output.values["json_prompt"]
|
|
@@ -131,7 +138,7 @@ def refine_prompt(
|
|
| 131 |
).images[0]
|
| 132 |
|
| 133 |
print(neg_json_prompt)
|
| 134 |
-
return image, seed, json_prompt, json.dumps(neg_json_prompt)
|
| 135 |
|
| 136 |
|
| 137 |
css = """
|
|
@@ -155,33 +162,60 @@ with gr.Blocks(css=css, theme=gr.themes.Soft(primary_hue="violet")) as demo:
|
|
| 155 |
[[non-commercial license]()] [[arxiv](https://arxiv.org/)] [[model](https://huggingface.co/briaai/FIBO)] [[code](https://github.com/Bria-AI/FIBO)]
|
| 156 |
""" , elem_id="app-title")
|
| 157 |
|
|
|
|
|
|
|
|
|
|
| 158 |
with gr.Row(elem_id="col-container"):
|
| 159 |
with gr.Column(scale=1):
|
| 160 |
|
| 161 |
-
with gr.
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 181 |
|
| 182 |
-
# Refine button - initially hidden
|
| 183 |
-
refine_btn = gr.Button("Refine Existing Image", variant="primary", visible=False)
|
| 184 |
-
|
| 185 |
with gr.Accordion("Advanced Settings", open=False):
|
| 186 |
with gr.Row():
|
| 187 |
seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
|
|
@@ -195,7 +229,7 @@ with gr.Blocks(css=css, theme=gr.themes.Soft(primary_hue="violet")) as demo:
|
|
| 195 |
width = gr.Slider(label="Width", minimum=832, maximum=1344, step=64, value=1024)
|
| 196 |
with gr.Row():
|
| 197 |
negative_prompt = gr.Textbox(label="negative prompt")
|
| 198 |
-
negative_prompt_json = gr.
|
| 199 |
|
| 200 |
with gr.Column(scale=1):
|
| 201 |
result = gr.Image(label="output")
|
|
@@ -225,24 +259,28 @@ with gr.Blocks(css=css, theme=gr.themes.Soft(primary_hue="violet")) as demo:
|
|
| 225 |
guidance_scale,
|
| 226 |
num_inference_steps,
|
| 227 |
],
|
| 228 |
-
outputs=[result, seed, prompt_in_json, negative_prompt_json,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 229 |
)
|
| 230 |
|
| 231 |
-
# Refine image
|
| 232 |
refine_btn.click(
|
| 233 |
fn=refine_prompt,
|
| 234 |
inputs=[
|
| 235 |
-
|
| 236 |
-
|
| 237 |
negative_prompt,
|
| 238 |
-
|
| 239 |
-
|
| 240 |
width,
|
| 241 |
height,
|
| 242 |
guidance_scale,
|
| 243 |
num_inference_steps,
|
| 244 |
],
|
| 245 |
-
outputs=[result,
|
| 246 |
)
|
| 247 |
|
| 248 |
demo.queue().launch()
|
|
|
|
| 89 |
).images[0]
|
| 90 |
|
| 91 |
print(neg_json_prompt)
|
| 92 |
+
return image, seed, json_prompt, json.dumps(neg_json_prompt), gr.update(visible=True), image, json_prompt, seed, seed
|
| 93 |
|
| 94 |
@spaces.GPU(duration=300)
|
| 95 |
def refine_prompt(
|
| 96 |
refine_instruction,
|
| 97 |
+
refine_json,
|
| 98 |
negative_prompt="",
|
| 99 |
seed=42,
|
| 100 |
randomize_seed=False,
|
|
|
|
| 107 |
seed = random.randint(0, MAX_SEED)
|
| 108 |
|
| 109 |
with torch.inference_mode():
|
| 110 |
+
# Parse the JSON string if it's a string
|
| 111 |
+
if isinstance(refine_json, str):
|
| 112 |
+
try:
|
| 113 |
+
refine_json = json.loads(refine_json) if refine_json else {}
|
| 114 |
+
except:
|
| 115 |
+
refine_json = {}
|
| 116 |
+
|
| 117 |
json_prompt_str = (
|
| 118 |
+
json.dumps(refine_json)
|
| 119 |
+
if isinstance(refine_json, (dict, list))
|
| 120 |
+
else str(refine_json)
|
| 121 |
)
|
| 122 |
output = vlm_pipe(json_prompt=json_prompt_str, prompt=refine_instruction)
|
| 123 |
json_prompt = output.values["json_prompt"]
|
|
|
|
| 138 |
).images[0]
|
| 139 |
|
| 140 |
print(neg_json_prompt)
|
| 141 |
+
return image, seed, json_prompt, json.dumps(neg_json_prompt), image, json_prompt, seed
|
| 142 |
|
| 143 |
|
| 144 |
css = """
|
|
|
|
| 162 |
[[non-commercial license]()] [[arxiv](https://arxiv.org/)] [[model](https://huggingface.co/briaai/FIBO)] [[code](https://github.com/Bria-AI/FIBO)]
|
| 163 |
""" , elem_id="app-title")
|
| 164 |
|
| 165 |
+
# State to store the last used seed
|
| 166 |
+
last_seed_state = gr.State(value=0)
|
| 167 |
+
|
| 168 |
with gr.Row(elem_id="col-container"):
|
| 169 |
with gr.Column(scale=1):
|
| 170 |
|
| 171 |
+
with gr.Tabs() as tabs:
|
| 172 |
+
with gr.Tab("Generate", id="generate_tab") as tab_generate:
|
| 173 |
+
with gr.Accordion("Inspire from Image", open=False) as inspire_accordion:
|
| 174 |
+
prompt_inspire_image = gr.Image(
|
| 175 |
+
label="Inspiration Image",
|
| 176 |
+
type="pil",
|
| 177 |
+
)
|
| 178 |
+
|
| 179 |
+
prompt_generate = gr.Textbox(
|
| 180 |
+
label="Prompt",
|
| 181 |
+
placeholder="a man holding a goose screaming",
|
| 182 |
+
lines=3
|
| 183 |
+
)
|
| 184 |
+
|
| 185 |
+
prompt_in_json = gr.Code(
|
| 186 |
+
label="Structured JSON Prompt (editable)",
|
| 187 |
+
language="json",
|
| 188 |
+
value="",
|
| 189 |
+
lines=10
|
| 190 |
+
)
|
| 191 |
+
|
| 192 |
+
with gr.Row():
|
| 193 |
+
generate_json_btn = gr.Button("Generate JSON Prompt", variant="secondary")
|
| 194 |
+
generate_image_btn = gr.Button("Generate Image", variant="primary")
|
| 195 |
+
|
| 196 |
+
with gr.Tab("Refine", id="refine_tab", visible=False) as tab_refine:
|
| 197 |
+
previous_result = gr.Image(label="Previous Generation", interactive=False)
|
| 198 |
+
|
| 199 |
+
refine_instruction = gr.Textbox(
|
| 200 |
+
label="Refinement instruction",
|
| 201 |
+
placeholder="make the cat white",
|
| 202 |
+
lines=2,
|
| 203 |
+
info="Describe the changes you want to make"
|
| 204 |
+
)
|
| 205 |
+
|
| 206 |
+
refine_json = gr.Code(
|
| 207 |
+
label="Structured JSON Prompt (editable)",
|
| 208 |
+
language="json",
|
| 209 |
+
value="",
|
| 210 |
+
lines=10
|
| 211 |
+
)
|
| 212 |
+
|
| 213 |
+
with gr.Row():
|
| 214 |
+
refine_seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
|
| 215 |
+
refine_randomize_seed = gr.Checkbox(label="Randomize seed", value=False, info="Keep off for consistent refinements")
|
| 216 |
+
|
| 217 |
+
refine_btn = gr.Button("Refine Image", variant="primary")
|
| 218 |
|
|
|
|
|
|
|
|
|
|
| 219 |
with gr.Accordion("Advanced Settings", open=False):
|
| 220 |
with gr.Row():
|
| 221 |
seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
|
|
|
|
| 229 |
width = gr.Slider(label="Width", minimum=832, maximum=1344, step=64, value=1024)
|
| 230 |
with gr.Row():
|
| 231 |
negative_prompt = gr.Textbox(label="negative prompt")
|
| 232 |
+
negative_prompt_json = gr.Code(label="json negative prompt", language="json")
|
| 233 |
|
| 234 |
with gr.Column(scale=1):
|
| 235 |
result = gr.Image(label="output")
|
|
|
|
| 259 |
guidance_scale,
|
| 260 |
num_inference_steps,
|
| 261 |
],
|
| 262 |
+
outputs=[result, seed, prompt_in_json, negative_prompt_json, tab_refine, previous_result, refine_json, last_seed_state],
|
| 263 |
+
).then(
|
| 264 |
+
fn=lambda s: s, # Copy seed to refine tab
|
| 265 |
+
inputs=[last_seed_state],
|
| 266 |
+
outputs=[refine_seed]
|
| 267 |
)
|
| 268 |
|
| 269 |
+
# Refine image with dedicated seed controls
|
| 270 |
refine_btn.click(
|
| 271 |
fn=refine_prompt,
|
| 272 |
inputs=[
|
| 273 |
+
refine_instruction,
|
| 274 |
+
refine_json,
|
| 275 |
negative_prompt,
|
| 276 |
+
refine_seed,
|
| 277 |
+
refine_randomize_seed,
|
| 278 |
width,
|
| 279 |
height,
|
| 280 |
guidance_scale,
|
| 281 |
num_inference_steps,
|
| 282 |
],
|
| 283 |
+
outputs=[result, refine_seed, refine_json, negative_prompt_json, previous_result, refine_json, last_seed_state],
|
| 284 |
)
|
| 285 |
|
| 286 |
demo.queue().launch()
|