clement-bonnet commited on
Commit
808cfce
·
1 Parent(s): 0dcdf8e

feat: radio button

Browse files
Files changed (1) hide show
  1. app.py +36 -35
app.py CHANGED
@@ -1,11 +1,19 @@
1
- from functools import partial
2
-
3
  import gradio as gr
4
  from PIL import Image
5
 
6
  from inference import generate_image
7
 
8
 
 
 
 
 
 
 
 
 
 
 
9
  def process_coord_click(image_idx: int, evt: gr.SelectData) -> Image.Image:
10
  """
11
  Process the click event on the coordinate selector
@@ -16,53 +24,43 @@ def process_coord_click(image_idx: int, evt: gr.SelectData) -> Image.Image:
16
  return generate_image(image_idx, x, y)
17
 
18
 
19
- def process_image_select(evt: gr.SelectData, idx: int) -> tuple[int, str]:
20
- """
21
- Process the reference image selection
22
- Returns the selected image index and corresponding heatmap
23
- """
24
- return idx, f"imgs/heatmap_{idx}.png"
25
-
26
-
27
  with gr.Blocks() as demo:
28
  gr.Markdown(
29
  """
30
  # Interactive Image Generation
31
- Click on a reference image to select it, then click on the coordinate selector to generate a new image.
32
  """
33
  )
34
 
35
  with gr.Row():
36
- # Left column: Interactive reference images
37
  with gr.Column(scale=1):
38
  # State variable to track selected image index
39
  selected_idx = gr.State(value=0)
40
 
41
- # Two separate Image components for reference images
42
- with gr.Column():
43
- image_0 = gr.Image(
44
- value="imgs/pattern_0.png",
45
- label="Task 1",
46
- show_label=False,
47
- interactive=True,
48
- height=300,
49
- width=450,
50
- )
51
- image_1 = gr.Image(
52
- value="imgs/pattern_1.png",
53
- label="Task 2",
54
- show_label=False,
55
- interactive=True,
56
- height=300,
57
- width=450,
58
- )
59
 
60
  # Right column: Coordinate selector and output image
61
  with gr.Column(scale=1):
62
  # Coordinate selector with dynamic background
63
  coord_selector = gr.Image(
64
- value="imgs/heatmap_0.png", # Initial background
65
- label="Click to select (x, y) coordinates",
66
  show_label=True,
67
  interactive=True,
68
  height=400,
@@ -72,9 +70,12 @@ with gr.Blocks() as demo:
72
  # Generated image output
73
  output_image = gr.Image(label="Generated Output", height=400, width=400)
74
 
75
- # Handle image selection for each reference image
76
- image_0.select(partial(process_image_select, idx=0), outputs=[selected_idx, coord_selector])
77
- image_1.select(partial(process_image_select, idx=1), outputs=[selected_idx, coord_selector])
 
 
 
78
 
79
  # Handle coordinate selection
80
  coord_selector.select(
 
 
 
1
  import gradio as gr
2
  from PIL import Image
3
 
4
  from inference import generate_image
5
 
6
 
7
+ def update_reference_image(choice: int) -> tuple[str, int, str]:
8
+ """
9
+ Update the reference image display based on radio button selection
10
+ Returns the image path, selected index, and corresponding heatmap
11
+ """
12
+ image_path = f"imgs/pattern_{choice}.png"
13
+ heatmap_path = f"imgs/heatmap_{choice}.png"
14
+ return image_path, choice, heatmap_path
15
+
16
+
17
  def process_coord_click(image_idx: int, evt: gr.SelectData) -> Image.Image:
18
  """
19
  Process the click event on the coordinate selector
 
24
  return generate_image(image_idx, x, y)
25
 
26
 
 
 
 
 
 
 
 
 
27
  with gr.Blocks() as demo:
28
  gr.Markdown(
29
  """
30
  # Interactive Image Generation
31
+ Select a task using the radio buttons, then click on the coordinate selector to generate a new image.
32
  """
33
  )
34
 
35
  with gr.Row():
36
+ # Left column: Radio selection and reference image
37
  with gr.Column(scale=1):
38
  # State variable to track selected image index
39
  selected_idx = gr.State(value=0)
40
 
41
+ # Radio buttons for task selection
42
+ task_select = gr.Radio(
43
+ choices=["Task 1", "Task 2"],
44
+ value="Task 1",
45
+ label="Select Task",
46
+ interactive=True,
47
+ )
48
+
49
+ # Single reference image component that updates based on selection
50
+ reference_image = gr.Image(
51
+ value="imgs/pattern_0.png",
52
+ show_label=False,
53
+ interactive=False, # No need for click interaction now
54
+ height=300,
55
+ width=450,
56
+ )
 
 
57
 
58
  # Right column: Coordinate selector and output image
59
  with gr.Column(scale=1):
60
  # Coordinate selector with dynamic background
61
  coord_selector = gr.Image(
62
+ value="imgs/heatmap_0.png",
63
+ label="Click to select (x, y) coordinates in the latent space",
64
  show_label=True,
65
  interactive=True,
66
  height=400,
 
70
  # Generated image output
71
  output_image = gr.Image(label="Generated Output", height=400, width=400)
72
 
73
+ # Handle radio button selection
74
+ task_select.change(
75
+ fn=lambda x: update_reference_image(0 if x == "Task 1" else 1),
76
+ inputs=[task_select],
77
+ outputs=[reference_image, selected_idx, coord_selector],
78
+ )
79
 
80
  # Handle coordinate selection
81
  coord_selector.select(