|  | import os | 
					
						
						|  | import gradio as gr | 
					
						
						|  | from PIL import Image | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | BASE_PATH = "demo_images" | 
					
						
						|  | IMAGES_DIR = os.path.join(BASE_PATH, "paths_images") | 
					
						
						|  | STRIPS_DIR = os.path.join(BASE_PATH, "paths_strips") | 
					
						
						|  | ORIGINAL_IMAGE = os.path.join(BASE_PATH, "original_image.jpg") | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | dipole_options = sorted( | 
					
						
						|  | [f"Path {i}" for i in range(len(os.listdir(IMAGES_DIR)))] | 
					
						
						|  | ) | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | def edit_image(attribute: str, slider_value: float): | 
					
						
						|  | index = dipole_options.index(attribute) | 
					
						
						|  | image_dir = os.path.join(IMAGES_DIR, f"path_{index:03d}") | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | num_frames = len(os.listdir(image_dir)) | 
					
						
						|  | frame_index = int(slider_value * (num_frames - 1)) | 
					
						
						|  | frame_path = os.path.join(image_dir, f"{frame_index:06d}.jpg") | 
					
						
						|  |  | 
					
						
						|  | frame_image = Image.open(frame_path) | 
					
						
						|  | return frame_image | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | def display_original_image(): | 
					
						
						|  | return Image.open(ORIGINAL_IMAGE) | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | def build_interface(): | 
					
						
						|  | with gr.Row(): | 
					
						
						|  | gr.Markdown("# ContraCLIP Predefined Editing Demo") | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | original_image_box = gr.Image(label="Original Image", type="pil") | 
					
						
						|  | original_image_box.update(value=display_original_image()) | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | demo = gr.Interface( | 
					
						
						|  | fn=edit_image, | 
					
						
						|  | inputs=[ | 
					
						
						|  | gr.Dropdown(dipole_options, label="Select Attribute"), | 
					
						
						|  | gr.Slider(0, 1, step=0.1, label="Attribute Strength"), | 
					
						
						|  | ], | 
					
						
						|  | outputs=gr.Image(type="pil"), | 
					
						
						|  | live=True, | 
					
						
						|  | title="Predefined ContraCLIP Image Editing", | 
					
						
						|  | description="Select an attribute and adjust its strength to view the edited image." | 
					
						
						|  | ) | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | demo.launch() | 
					
						
						|  |  | 
					
						
						|  | if __name__ == "__main__": | 
					
						
						|  | build_interface() | 
					
						
						|  |  |