Spanicin commited on
Commit
bd26dca
·
verified ·
1 Parent(s): a5a4f93

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -38
app.py CHANGED
@@ -254,51 +254,80 @@ def status():
254
  })
255
 
256
 
257
- # Gradio interface function
258
- def gradio_infer(input_image, upscale_factor, num_inference_steps, controlnet_conditioning_scale, randomize_seed):
259
- # Convert the image to Base64 for internal processing
260
- buffered = io.BytesIO()
261
- input_image.save(buffered, format="JPEG")
262
-
263
- # Prepare parameters
264
- seed = random.randint(0, MAX_SEED) if randomize_seed else 42
 
 
 
 
 
 
 
 
265
 
266
- # Create a unique process ID for this request
267
- process_id = str(random.randint(1000, 9999))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
268
 
269
- # Set the status to 'in_progress'
270
- app.config['image_outputs'][process_id] = None
271
 
272
- # Run the inference in a separate thread
273
- executor.submit(run_inference, process_id, input_image, upscale_factor, seed, num_inference_steps, controlnet_conditioning_scale)
274
 
275
- # Wait for the processing to complete and return the output image
276
- while app.config['image_outputs'][process_id] is None:
277
- pass # Polling until the process is complete
278
-
279
- return app.config['image_outputs'][process_id]
280
-
281
- # Gradio UI setup
282
- def launch_gradio():
283
- iface = gr.Interface(
284
- fn=gradio_infer,
285
- inputs=[
286
- gr.inputs.Image(type="pil", label="Input Image"),
287
- gr.inputs.Slider(1, 8, step=1, default=4, label="Upscale Factor"),
288
- gr.inputs.Slider(1, 100, step=1, default=28, label="Number of Inference Steps"),
289
- gr.inputs.Slider(0.0, 2.0, step=0.1, default=0.6, label="ControlNet Conditioning Scale"),
290
- gr.inputs.Checkbox(default=True, label="Randomize Seed")
291
- ],
292
- outputs="image",
293
- title="Image Upscaling with ControlNet",
294
- description="Upload an image to upscale it using ControlNet."
295
  )
296
- iface.launch()
297
 
298
- # Start Gradio UI in a separate thread
299
- threading.Thread(target=launch_gradio).start()
 
 
300
 
301
  if __name__ == '__main__':
302
- app.run(debug=True,host='0.0.0.0')
 
 
 
 
303
 
304
 
 
254
  })
255
 
256
 
257
+ # Gradio Blocks setup
258
+ css = """
259
+ #col-container {
260
+ max-width: 800px;
261
+ }
262
+ """
263
+
264
+ with gr.Blocks(css=css) as demo:
265
+ gr.Markdown(
266
+ """
267
+ # ⚡ Flux.1-dev Upscaler ControlNet ⚡
268
+ This is an interactive demo of [Flux.1-dev Upscaler ControlNet](https://huggingface.co/jasperai/Flux.1-dev-Controlnet-Upscaler) taking as input a low resolution image to generate a high resolution image.
269
+ Currently running on {device}.
270
+ *Note*: Even though the model can handle higher resolution images, due to GPU memory constraints, this demo was limited to a generated output not exceeding a pixel budget of 1024x1024. If the requested size exceeds that limit, the input will be first resized keeping the aspect ratio such that the output of the controlNet model does not exceed the allocated pixel budget. The output is then resized to the targeted shape using a simple resizing. This may explain some artifacts for high resolution input. To address this, run the demo locally or consider implementing a tiling strategy. Happy upscaling! 🚀
271
+ """.format(device=device)
272
+ )
273
 
274
+ with gr.Row():
275
+ run_button = gr.Button(value="Run")
276
+
277
+ with gr.Row():
278
+ with gr.Column(scale=4):
279
+ input_im = gr.Image(label="Input Image", type="pil")
280
+ with gr.Column(scale=1):
281
+ num_inference_steps = gr.Slider(
282
+ label="Number of Inference Steps",
283
+ minimum=8,
284
+ maximum=50,
285
+ step=1,
286
+ value=28,
287
+ )
288
+ upscale_factor = gr.Slider(
289
+ label="Upscale Factor",
290
+ minimum=1,
291
+ maximum=4,
292
+ step=1,
293
+ value=4,
294
+ )
295
+ controlnet_conditioning_scale = gr.Slider(
296
+ label="Controlnet Conditioning Scale",
297
+ minimum=0.1,
298
+ maximum=1.5,
299
+ step=0.1,
300
+ value=0.6,
301
+ )
302
+ seed = gr.Slider(
303
+ label="Seed",
304
+ minimum=0,
305
+ maximum=MAX_SEED,
306
+ step=1,
307
+ value=42,
308
+ )
309
 
310
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
 
311
 
312
+ with gr.Row():
313
+ result = gr.Image(label="Output Image", type="pil", interactive=False)
314
 
315
+ gr.Button("Run").click(
316
+ fn=gr_infer,
317
+ inputs=[seed, randomize_seed, input_im, num_inference_steps, upscale_factor, controlnet_conditioning_scale],
318
+ outputs=result
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
319
  )
 
320
 
321
+ gr.Markdown("**Disclaimer:**")
322
+ gr.Markdown(
323
+ "This demo is only for research purposes. Jasper cannot be held responsible for the generation of NSFW (Not Safe For Work) content through the use of this demo. Users are solely responsible for any content they create, and it is their obligation to ensure that it adheres to appropriate and ethical standards."
324
+ )
325
 
326
  if __name__ == '__main__':
327
+ demo.queue().launch(share=False, show_api=False)
328
+ app.run(host='0.0.0.0', port=5000)
329
+
330
+ # if __name__ == '__main__':
331
+ # app.run(debug=True,host='0.0.0.0')
332
 
333