Spanicin commited on
Commit
5a2e984
·
verified ·
1 Parent(s): 0d09370

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -0
app.py CHANGED
@@ -251,6 +251,51 @@ def status():
251
  "output_image": image_base64
252
  })
253
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
254
  if __name__ == '__main__':
255
  app.run(debug=True,host='0.0.0.0')
256
 
 
251
  "output_image": image_base64
252
  })
253
 
254
+
255
+ # Gradio interface function
256
+ def gradio_infer(input_image, upscale_factor, num_inference_steps, controlnet_conditioning_scale, randomize_seed):
257
+ # Convert the image to Base64 for internal processing
258
+ buffered = io.BytesIO()
259
+ input_image.save(buffered, format="JPEG")
260
+
261
+ # Prepare parameters
262
+ seed = random.randint(0, MAX_SEED) if randomize_seed else 42
263
+
264
+ # Create a unique process ID for this request
265
+ process_id = str(random.randint(1000, 9999))
266
+
267
+ # Set the status to 'in_progress'
268
+ app.config['image_outputs'][process_id] = None
269
+
270
+ # Run the inference in a separate thread
271
+ executor.submit(run_inference, process_id, input_image, upscale_factor, seed, num_inference_steps, controlnet_conditioning_scale)
272
+
273
+ # Wait for the processing to complete and return the output image
274
+ while app.config['image_outputs'][process_id] is None:
275
+ pass # Polling until the process is complete
276
+
277
+ return app.config['image_outputs'][process_id]
278
+
279
+ # Gradio UI setup
280
+ def launch_gradio():
281
+ iface = gr.Interface(
282
+ fn=gradio_infer,
283
+ inputs=[
284
+ gr.inputs.Image(type="pil", label="Input Image"),
285
+ gr.inputs.Slider(1, 8, step=1, default=4, label="Upscale Factor"),
286
+ gr.inputs.Slider(1, 100, step=1, default=28, label="Number of Inference Steps"),
287
+ gr.inputs.Slider(0.0, 2.0, step=0.1, default=0.6, label="ControlNet Conditioning Scale"),
288
+ gr.inputs.Checkbox(default=True, label="Randomize Seed")
289
+ ],
290
+ outputs="image",
291
+ title="Image Upscaling with ControlNet",
292
+ description="Upload an image to upscale it using ControlNet."
293
+ )
294
+ iface.launch()
295
+
296
+ # Start Gradio UI in a separate thread
297
+ threading.Thread(target=launch_gradio).start()
298
+
299
  if __name__ == '__main__':
300
  app.run(debug=True,host='0.0.0.0')
301