clement-bonnet commited on
Commit
9ef0f25
·
1 Parent(s): 999b913

feat: app test

Browse files
Files changed (1) hide show
  1. app.py +69 -120
app.py CHANGED
@@ -1,135 +1,84 @@
1
- # import gradio as gr
2
- # import numpy as np
3
- # from PIL import Image
4
-
5
- # from inference import generate_image
6
-
7
-
8
- # # Create a square image for the coordinate selector
9
- # def create_selector_image():
10
- # # Create a white square with black border
11
- # size = 400
12
- # border = 2
13
- # img = np.ones((size, size, 3), dtype=np.uint8) * 255
14
- # # Add black border
15
- # img[:border, :] = 0 # top
16
- # img[-border:, :] = 0 # bottom
17
- # img[:, :border] = 0 # left
18
- # img[:, -border:] = 0 # right
19
- # return Image.fromarray(img)
20
-
21
-
22
- # def process_click(image_idx: int, x: int, y: int) -> tuple[Image.Image, str]:
23
- # """
24
- # Process the click event on the coordinate selector
25
- # """
26
- # try:
27
- # # Normalize coordinates to [0, 1]
28
- # x_norm, y_norm = x / 400, y / 400 # Divide by image size (400x400)
29
-
30
- # # Debug message
31
- # debug_msg = f"Processing: image_idx={image_idx}, coordinates=({x_norm:.3f}, {y_norm:.3f})"
32
- # print(debug_msg)
33
-
34
- # # Generate image using the model
35
- # generated_img = generate_image(image_idx, x_norm, y_norm)
36
- # return generated_img, debug_msg
37
- # except Exception as e:
38
- # error_msg = f"Error: {str(e)}"
39
- # print(error_msg)
40
- # return None, error_msg
41
-
42
-
43
- # with gr.Blocks() as demo:
44
- # gr.Markdown(
45
- # """
46
- # # Interactive Image Generation
47
- # Choose a reference image and click on the coordinate selector to generate a new image.
48
- # """
49
- # )
50
-
51
- # with gr.Row():
52
- # # Left column: Reference images and coordinate selector
53
- # with gr.Column(scale=1):
54
- # # Radio buttons for image selection
55
- # image_idx = gr.Radio(
56
- # choices=list(range(2)), value=0, label="Select Reference Image", type="index"
57
- # )
58
-
59
- # # Display reference images
60
- # gallery = gr.Gallery(
61
- # value=["imgs/pattern_1.png", "imgs/pattern_2.png"],
62
- # columns=2,
63
- # rows=1,
64
- # height=500,
65
- # label="Different Tasks",
66
- # )
67
-
68
- # # Coordinate selector
69
- # coord_selector = gr.Image(
70
- # value=create_selector_image(),
71
- # label="Click to select (x, y) coordinates",
72
- # show_label=True,
73
- # interactive=True,
74
- # height=400,
75
- # width=400,
76
- # )
77
-
78
- # # Right column: Generated image and debug info
79
- # with gr.Column(scale=1):
80
- # output_image = gr.Image(label="Generated Image", height=400)
81
- # debug_text = gr.Textbox(label="Debug Info", interactive=False)
82
-
83
- # # Handle click events using click instead of select
84
- # coord_selector.click(
85
- # fn=process_click,
86
- # inputs=[image_idx, coord_selector], # coord_selector will provide x, y coordinates
87
- # outputs=[output_image, debug_text],
88
- # )
89
-
90
- # if __name__ == "__main__":
91
- # print("Starting Gradio app...")
92
- # demo.launch(debug=True)
93
-
94
-
95
  import gradio as gr
96
  import numpy as np
97
  from PIL import Image
98
 
 
99
 
100
- def create_white_square(size=400):
101
- # Create a white square image
102
- print("Creating white square")
103
- return np.full((size, size, 3), 255, dtype=np.uint8)
104
 
 
 
 
 
 
 
 
 
 
 
 
 
105
 
106
- def get_click_coordinates(evt: gr.SelectData):
107
- # Get click coordinates
108
- x, y = evt.index
109
- print(f"Clicked at coordinates: x={x}, y={y}")
110
- return f"Clicked at coordinates: x={x}, y={y}"
111
 
 
 
 
 
 
 
 
 
 
 
 
112
 
113
- # Create the interface
114
- with gr.Blocks() as demo:
115
- gr.Markdown("## Click Coordinate Detector\nClick anywhere on the white square to see coordinates")
116
 
117
- # Display the white square
118
- image = gr.Image(
119
- label="Click on the white square",
120
- value=create_white_square(),
121
- interactive=True,
122
- height=400,
123
- width=400,
124
- mirror_webcam=False,
125
  )
126
 
127
- # Display coordinates
128
- output_text = gr.Textbox(label="Coordinates")
129
- print("oh yeah")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
130
  # Handle click events
131
- image.select(get_click_coordinates, inputs=[], outputs=output_text)
132
 
133
  # Launch the app
134
- if __name__ == "__main__":
135
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  import numpy as np
3
  from PIL import Image
4
 
5
+ from inference import generate_image
6
 
 
 
 
 
7
 
8
+ # Create a square image for the coordinate selector
9
+ def create_selector_image():
10
+ # Create a white square with black border
11
+ size = 400
12
+ border = 2
13
+ img = np.ones((size, size, 3), dtype=np.uint8) * 255
14
+ # Add black border
15
+ img[:border, :] = 0 # top
16
+ img[-border:, :] = 0 # bottom
17
+ img[:, :border] = 0 # left
18
+ img[:, -border:] = 0 # right
19
+ return Image.fromarray(img)
20
 
 
 
 
 
 
21
 
22
+ def process_click(image_idx: int, evt: gr.SelectData) -> Image.Image:
23
+ """
24
+ Process the click event on the coordinate selector
25
+ """
26
+ # Extract coordinates from click event
27
+ x, y = evt.index[0], evt.index[1]
28
+ # Normalize coordinates to [0, 1]
29
+ x, y = x / 400, y / 400 # Divide by image size (400x400)
30
+ print(f"Clicked at coordinates: ({x:.3f}, {y:.3f})")
31
+ # Generate image using the model
32
+ return generate_image(image_idx, x, y)
33
 
 
 
 
34
 
35
+ with gr.Blocks() as demo:
36
+ gr.Markdown(
37
+ """
38
+ # Interactive Image Generation
39
+ Choose a reference image and click on the coordinate selector to generate a new image.
40
+ """
 
 
41
  )
42
 
43
+ with gr.Row():
44
+ # Left column: Reference images and coordinate selector
45
+ with gr.Column(scale=1):
46
+ # Radio buttons for image selection
47
+ image_idx = gr.Radio(
48
+ choices=list(range(2)),
49
+ value=0,
50
+ label="Select Reference Image",
51
+ type="index",
52
+ )
53
+
54
+ # Display reference images
55
+ gallery = gr.Gallery(
56
+ value=[
57
+ "imgs/pattern_1.png",
58
+ "imgs/pattern_2.png",
59
+ ],
60
+ columns=2,
61
+ rows=2,
62
+ height=300,
63
+ label="Reference Images",
64
+ )
65
+
66
+ # Coordinate selector (now using Image component)
67
+ coord_selector = gr.Image(
68
+ value=create_selector_image(),
69
+ label="Click to select (x, y) coordinates",
70
+ show_label=True,
71
+ interactive=True,
72
+ height=400,
73
+ width=400,
74
+ )
75
+
76
+ # Right column: Generated image
77
+ with gr.Column(scale=1):
78
+ output_image = gr.Image(label="Generated Image", height=400, width=400)
79
+
80
  # Handle click events
81
+ coord_selector.select(process_click, inputs=[image_idx], outputs=output_image)
82
 
83
  # Launch the app
84
+ demo.launch()