# import gradio as gr # import numpy as np # from PIL import Image # from inference import generate_image # # Create a square image for the coordinate selector # def create_selector_image(): # # Create a white square with black border # size = 400 # border = 2 # img = np.ones((size, size, 3), dtype=np.uint8) * 255 # # Add black border # img[:border, :] = 0 # top # img[-border:, :] = 0 # bottom # img[:, :border] = 0 # left # img[:, -border:] = 0 # right # return Image.fromarray(img) # def process_click(image_idx: int, x: int, y: int) -> tuple[Image.Image, str]: # """ # Process the click event on the coordinate selector # """ # try: # # Normalize coordinates to [0, 1] # x_norm, y_norm = x / 400, y / 400 # Divide by image size (400x400) # # Debug message # debug_msg = f"Processing: image_idx={image_idx}, coordinates=({x_norm:.3f}, {y_norm:.3f})" # print(debug_msg) # # Generate image using the model # generated_img = generate_image(image_idx, x_norm, y_norm) # return generated_img, debug_msg # except Exception as e: # error_msg = f"Error: {str(e)}" # print(error_msg) # return None, error_msg # with gr.Blocks() as demo: # gr.Markdown( # """ # # Interactive Image Generation # Choose a reference image and click on the coordinate selector to generate a new image. # """ # ) # with gr.Row(): # # Left column: Reference images and coordinate selector # with gr.Column(scale=1): # # Radio buttons for image selection # image_idx = gr.Radio( # choices=list(range(2)), value=0, label="Select Reference Image", type="index" # ) # # Display reference images # gallery = gr.Gallery( # value=["imgs/pattern_1.png", "imgs/pattern_2.png"], # columns=2, # rows=1, # height=500, # label="Different Tasks", # ) # # Coordinate selector # coord_selector = gr.Image( # value=create_selector_image(), # label="Click to select (x, y) coordinates", # show_label=True, # interactive=True, # height=400, # width=400, # ) # # Right column: Generated image and debug info # with gr.Column(scale=1): # output_image = gr.Image(label="Generated Image", height=400) # debug_text = gr.Textbox(label="Debug Info", interactive=False) # # Handle click events using click instead of select # coord_selector.click( # fn=process_click, # inputs=[image_idx, coord_selector], # coord_selector will provide x, y coordinates # outputs=[output_image, debug_text], # ) # if __name__ == "__main__": # print("Starting Gradio app...") # demo.launch(debug=True) import gradio as gr import numpy as np from PIL import Image def create_white_square(size=400): # Create a white square image print("Creating white square") return np.full((size, size, 3), 255, dtype=np.uint8) def get_click_coordinates(evt: gr.SelectData): # Get click coordinates x, y = evt.index print(f"Clicked at coordinates: x={x}, y={y}") return f"Clicked at coordinates: x={x}, y={y}" # Create the interface with gr.Blocks() as demo: gr.Markdown("## Click Coordinate Detector\nClick anywhere on the white square to see coordinates") # Display the white square image = gr.Image( label="Click on the white square", value=create_white_square(), interactive=True, height=400, width=400, mirror_webcam=False, ) # Display coordinates output_text = gr.Textbox(label="Coordinates") print("oh yeah") # Handle click events image.select(get_click_coordinates, inputs=[], outputs=output_text) # Launch the app if __name__ == "__main__": demo.launch()