Jangai commited on
Commit
4da9241
·
verified ·
1 Parent(s): 2fd0f3a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -12
app.py CHANGED
@@ -6,24 +6,26 @@ import logging
6
  logging.basicConfig(level=logging.DEBUG)
7
 
8
  def display_sketch(sketch):
9
- try:
10
- image_data = np.array(sketch) # Convert the dict data to a numpy array
11
- logging.debug(f"Image data type: {type(image_data)}") # Log the type of image data
12
- logging.debug(f"Image data shape: {image_data.shape}") # Log the shape of image data
 
 
13
 
14
- # Plot the image
15
- fig, ax = plt.subplots()
16
- ax.imshow(image_data, cmap='gray')
17
  plt.axis('off')
18
  plt.savefig("/mnt/data/output.png", bbox_inches='tight')
19
  return "/mnt/data/output.png"
20
- except Exception as e:
21
- logging.error(f"Error processing sketch: {e}")
22
- return str(e)
 
23
 
24
  with gr.Blocks() as demo:
25
- sketchpad = gr.Sketchpad(label="Draw Something", tool="sketch")
26
  output_image = gr.Image(label="Your Sketch")
27
- sketchpad.submit(display_sketch, inputs=sketchpad, outputs=output_image)
 
28
 
29
  demo.launch()
 
6
  logging.basicConfig(level=logging.DEBUG)
7
 
8
  def display_sketch(sketch):
9
+ logging.debug(f"Received sketch data: {sketch}")
10
+
11
+ if isinstance(sketch, dict) and "image" in sketch:
12
+ image_data = sketch["image"]
13
+ logging.debug(f"Image data type: {type(image_data)}")
14
+ logging.debug(f"Image data shape: {np.array(image_data).shape}")
15
 
16
+ plt.imshow(image_data, cmap='gray')
 
 
17
  plt.axis('off')
18
  plt.savefig("/mnt/data/output.png", bbox_inches='tight')
19
  return "/mnt/data/output.png"
20
+ else:
21
+ error_message = f"Unexpected sketch data format: {type(sketch)}"
22
+ logging.error(error_message)
23
+ return error_message
24
 
25
  with gr.Blocks() as demo:
26
+ sketchpad = gr.Sketchpad(label="Draw Something")
27
  output_image = gr.Image(label="Your Sketch")
28
+ submit_btn = gr.Button("Submit")
29
+ submit_btn.click(display_sketch, inputs=sketchpad, outputs=output_image)
30
 
31
  demo.launch()