multimodalart HF Staff commited on
Commit
b3b072d
·
verified ·
1 Parent(s): 9a5449e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -37
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
- prompt_in_json,
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(prompt_in_json)
112
- if isinstance(prompt_in_json, (dict, list))
113
- else str(prompt_in_json)
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.Accordion("Inspire from Image Reference", open=False) as inspire_accordion:
162
- prompt_inspire_image = gr.Image(
163
- label="Inspiration Image",
164
- type="pil",
165
- )
166
-
167
- prompt_generate = gr.Textbox(
168
- label="Prompt",
169
- placeholder="a man holding a goose screaming",
170
- lines=3
171
- )
172
-
173
- prompt_in_json = gr.JSON(
174
- label="Structured JSON Prompt (editable)",
175
- value=None
176
- )
177
-
178
- with gr.Row():
179
- generate_json_btn = gr.Button("Generate JSON Prompt", variant="secondary")
180
- generate_image_btn = gr.Button("Generate New Image", variant="primary")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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.JSON(label="json negative prompt")
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, refine_btn],
 
 
 
 
229
  )
230
 
231
- # Refine image (reuses the main prompt box)
232
  refine_btn.click(
233
  fn=refine_prompt,
234
  inputs=[
235
- prompt_generate, # Reuse the main prompt box
236
- prompt_in_json,
237
  negative_prompt,
238
- seed,
239
- randomize_seed,
240
  width,
241
  height,
242
  guidance_scale,
243
  num_inference_steps,
244
  ],
245
- outputs=[result, seed, prompt_in_json, negative_prompt_json],
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()