dattarij commited on
Commit
c7eaf55
·
2 Parent(s): 5e94425 64a5374

Merge branch 'main' of https://huggingface.co/spaces/dattarij/disentangled-image-editing-final-project

Browse files
Files changed (1) hide show
  1. app.py +55 -0
app.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ from PIL import Image
4
+
5
+ # Paths for predefined dipole images
6
+ BASE_PATH = "demo_images"
7
+ IMAGES_DIR = os.path.join(BASE_PATH, "paths_images")
8
+ ORIGINAL_IMAGE = os.path.join(BASE_PATH, "original_image.jpg")
9
+
10
+ # Predefined dipole options based on folder structure
11
+ dipole_options = sorted(
12
+ [f"Path {i}" for i in range(len(os.listdir(IMAGES_DIR)))]
13
+ )
14
+
15
+ # Display edited images based on user-selected dipole and slider strength
16
+ def edit_image(attribute: str, slider_value: int):
17
+ index = dipole_options.index(attribute)
18
+ image_dir = os.path.join(IMAGES_DIR, f"path_{index:03d}")
19
+
20
+ # Determine frame from slider value
21
+ frame_path = os.path.join(image_dir, f"{slider_value:06d}.jpg")
22
+
23
+ # Load and return the image
24
+ frame_image = Image.open(frame_path)
25
+ return frame_image
26
+
27
+ # Display the original image
28
+ def display_original_image():
29
+ return Image.open(ORIGINAL_IMAGE)
30
+
31
+ # Gradio Interface
32
+ def build_interface():
33
+ with gr.Row():
34
+ gr.Markdown("# ContraCLIP Predefined Editing Demo")
35
+
36
+ with gr.Row():
37
+ gr.Image(display_original_image(), label="Original Image", type="pil")
38
+
39
+ demo = gr.Interface(
40
+ fn=edit_image,
41
+ inputs=[
42
+ gr.Dropdown(dipole_options, label="Select Attribute"),
43
+ gr.Slider(0, 32, step=1, label="Frame Index"), # Adjust slider range based on actual frames
44
+ ],
45
+ outputs=gr.Image(type="pil"),
46
+ live=True,
47
+ title="Predefined ContraCLIP Image Editing",
48
+ description="Select an attribute and adjust the frame index to view the edited image."
49
+ )
50
+
51
+ # Launch the Interface
52
+ demo.launch()
53
+
54
+ if __name__ == "__main__":
55
+ build_interface()