Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
STRIPS_DIR = os.path.join(BASE_PATH, "paths_strips")
|
| 9 |
+
ORIGINAL_IMAGE = os.path.join(BASE_PATH, "original_image.jpg")
|
| 10 |
+
|
| 11 |
+
# Predefined dipole options based on folder structure
|
| 12 |
+
dipole_options = sorted(
|
| 13 |
+
[f"Path {i}" for i in range(len(os.listdir(IMAGES_DIR)))]
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
# Display edited images based on user-selected dipole and slider strength
|
| 17 |
+
def edit_image(attribute: str, slider_value: float):
|
| 18 |
+
index = dipole_options.index(attribute)
|
| 19 |
+
image_dir = os.path.join(IMAGES_DIR, f"path_{index:03d}")
|
| 20 |
+
|
| 21 |
+
# Determine frame from slider value
|
| 22 |
+
num_frames = len(os.listdir(image_dir))
|
| 23 |
+
frame_index = int(slider_value * (num_frames - 1))
|
| 24 |
+
frame_path = os.path.join(image_dir, f"{frame_index:06d}.jpg")
|
| 25 |
+
|
| 26 |
+
frame_image = Image.open(frame_path)
|
| 27 |
+
return frame_image
|
| 28 |
+
|
| 29 |
+
# Display the original image
|
| 30 |
+
def display_original_image():
|
| 31 |
+
return Image.open(ORIGINAL_IMAGE)
|
| 32 |
+
|
| 33 |
+
# Gradio Interface
|
| 34 |
+
def build_interface():
|
| 35 |
+
with gr.Row():
|
| 36 |
+
gr.Markdown("# ContraCLIP Predefined Editing Demo")
|
| 37 |
+
|
| 38 |
+
# Original Image Display
|
| 39 |
+
original_image_box = gr.Image(label="Original Image", type="pil")
|
| 40 |
+
original_image_box.update(value=display_original_image())
|
| 41 |
+
|
| 42 |
+
# Demo Interface
|
| 43 |
+
demo = gr.Interface(
|
| 44 |
+
fn=edit_image,
|
| 45 |
+
inputs=[
|
| 46 |
+
gr.Dropdown(dipole_options, label="Select Attribute"),
|
| 47 |
+
gr.Slider(0, 1, step=0.1, label="Attribute Strength"),
|
| 48 |
+
],
|
| 49 |
+
outputs=gr.Image(type="pil"),
|
| 50 |
+
live=True,
|
| 51 |
+
title="Predefined ContraCLIP Image Editing",
|
| 52 |
+
description="Select an attribute and adjust its strength to view the edited image."
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
# Launch the Interface
|
| 56 |
+
demo.launch()
|
| 57 |
+
|
| 58 |
+
if __name__ == "__main__":
|
| 59 |
+
build_interface()
|