Spaces:
mashroo
/
Running on Zero

YoussefAnso commited on
Commit
39dcad5
·
1 Parent(s): 1f1e8f3

Refactor Gradio interface to encapsulate image processing logic within a dedicated function, enhancing error handling for image uploads and streamlining event chaining for improved clarity.

Browse files
Files changed (1) hide show
  1. app.py +23 -15
app.py CHANGED
@@ -256,24 +256,32 @@ with gr.Blocks() as demo:
256
  )
257
  gr.Markdown("Note: Ensure that the input image is correctly pre-processed into a grey background, otherwise the results will be unpredictable.")
258
 
259
- # Chain the events directly
 
 
 
 
 
260
  text_button.click(
261
- fn=lambda x: gr.Error("No image uploaded!") if x is None else x,
262
- inputs=[image_input],
263
- outputs=[image_input]
264
- ).then(
265
- fn=preprocess_image,
266
- inputs=[image_input, background_choice, foreground_ratio, back_groud_color],
267
- outputs=[processed_image]
268
- ).then(
269
- fn=gen_image,
270
- inputs=[processed_image, seed, guidance_scale, step],
271
- outputs=[image_output, xyz_output, output_model]
 
 
 
 
272
  )
273
 
274
- # Launch the interface
275
  demo.launch(
276
  show_error=True,
277
- max_threads=1,
278
- share=True
279
  )
 
256
  )
257
  gr.Markdown("Note: Ensure that the input image is correctly pre-processed into a grey background, otherwise the results will be unpredictable.")
258
 
259
+ def generate(image, bg_choice, fg_ratio, bg_color, seed_val, guidance, steps):
260
+ if image is None:
261
+ raise gr.Error("No image uploaded!")
262
+ processed = preprocess_image(image, bg_choice, fg_ratio, bg_color)
263
+ return gen_image(processed, seed_val, guidance, steps)
264
+
265
  text_button.click(
266
+ fn=generate,
267
+ inputs=[
268
+ image_input,
269
+ background_choice,
270
+ foreground_ratio,
271
+ back_groud_color,
272
+ seed,
273
+ guidance_scale,
274
+ step
275
+ ],
276
+ outputs=[
277
+ image_output,
278
+ xyz_output,
279
+ output_model
280
+ ]
281
  )
282
 
283
+ # Launch the interface without share parameter since we're on Hugging Face Spaces
284
  demo.launch(
285
  show_error=True,
286
+ max_threads=1
 
287
  )