Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -2,18 +2,76 @@ import gradio as gr
|
|
2 |
import pandas as pd
|
3 |
from PIL import Image
|
4 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
# Load metadata
|
7 |
-
|
|
|
|
|
|
|
|
|
8 |
|
9 |
-
# Function to display image pairs
|
10 |
def show_image_pair(pair_id):
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
# Create dropdown options
|
19 |
pair_ids = df['pair_id'].tolist()
|
@@ -21,29 +79,95 @@ pair_ids = df['pair_id'].tolist()
|
|
21 |
# Define Gradio Interface
|
22 |
with gr.Blocks() as demo:
|
23 |
gr.Markdown("# MID-Space Dataset Viewer")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
with gr.Row():
|
25 |
with gr.Column():
|
26 |
-
|
27 |
-
|
|
|
|
|
|
|
28 |
with gr.Column():
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
label1_score = gr.Number(label="Label 1 Score", interactive=False)
|
36 |
-
with gr.Row():
|
37 |
-
label2 = gr.Textbox(label="Label 2", interactive=False)
|
38 |
-
label2_score = gr.Number(label="Label 2 Score", interactive=False)
|
39 |
with gr.Row():
|
40 |
-
|
41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
show_button.click(
|
44 |
-
show_image_pair,
|
45 |
-
inputs=[pair_id_input],
|
46 |
-
outputs=[img1, img2, prompt,
|
|
|
47 |
)
|
48 |
|
49 |
if __name__ == "__main__":
|
|
|
2 |
import pandas as pd
|
3 |
from PIL import Image
|
4 |
import os
|
5 |
+
import sys
|
6 |
+
|
7 |
+
# Determine the absolute path to preferences.csv
|
8 |
+
current_dir = os.path.dirname(os.path.abspath(__file__))
|
9 |
+
csv_path = os.path.join(current_dir, 'preferences.csv')
|
10 |
+
|
11 |
+
# Check if preferences.csv exists
|
12 |
+
if not os.path.isfile(csv_path):
|
13 |
+
print(f"Error: 'preferences.csv' not found at {csv_path}")
|
14 |
+
sys.exit(1)
|
15 |
|
16 |
# Load metadata
|
17 |
+
try:
|
18 |
+
df = pd.read_csv(csv_path)
|
19 |
+
except Exception as e:
|
20 |
+
print(f"Error reading 'preferences.csv': {e}")
|
21 |
+
sys.exit(1)
|
22 |
|
23 |
+
# Function to display image pairs and metadata
|
24 |
def show_image_pair(pair_id):
|
25 |
+
try:
|
26 |
+
record = df[df['pair_id'] == pair_id].iloc[0]
|
27 |
+
except IndexError:
|
28 |
+
return {
|
29 |
+
"images": [None, None],
|
30 |
+
"prompt": "Invalid Pair ID.",
|
31 |
+
"labels": {
|
32 |
+
"Label 1": {"name": "", "score": 0.0},
|
33 |
+
"Label 2": {"name": "", "score": 0.0},
|
34 |
+
"Label 3": {"name": "", "score": 0.0},
|
35 |
+
}
|
36 |
+
}
|
37 |
+
|
38 |
+
img1_path = os.path.join(current_dir, record['image1'])
|
39 |
+
img2_path = os.path.join(current_dir, record['image2'])
|
40 |
+
|
41 |
+
# Initialize default response
|
42 |
+
response = {
|
43 |
+
"images": [None, None],
|
44 |
+
"prompt": record.get('prompt', ''),
|
45 |
+
"labels": {
|
46 |
+
"Label 1": {"name": record.get('label1', ''), "score": record.get('label1_score', 0.0)},
|
47 |
+
"Label 2": {"name": record.get('label2', ''), "score": record.get('label2_score', 0.0)},
|
48 |
+
"Label 3": {"name": record.get('label3', ''), "score": record.get('label3_score', 0.0)},
|
49 |
+
}
|
50 |
+
}
|
51 |
+
|
52 |
+
# Load Image 1
|
53 |
+
if os.path.isfile(img1_path):
|
54 |
+
try:
|
55 |
+
img1 = Image.open(img1_path)
|
56 |
+
response["images"][0] = img1
|
57 |
+
except Exception as e:
|
58 |
+
print(f"Error opening Image 1: {e}")
|
59 |
+
response["images"][0] = None
|
60 |
+
else:
|
61 |
+
response["prompt"] = "Image 1 not found."
|
62 |
+
|
63 |
+
# Load Image 2
|
64 |
+
if os.path.isfile(img2_path):
|
65 |
+
try:
|
66 |
+
img2 = Image.open(img2_path)
|
67 |
+
response["images"][1] = img2
|
68 |
+
except Exception as e:
|
69 |
+
print(f"Error opening Image 2: {e}")
|
70 |
+
response["images"][1] = None
|
71 |
+
else:
|
72 |
+
response["prompt"] = "Image 2 not found."
|
73 |
+
|
74 |
+
return response
|
75 |
|
76 |
# Create dropdown options
|
77 |
pair_ids = df['pair_id'].tolist()
|
|
|
79 |
# Define Gradio Interface
|
80 |
with gr.Blocks() as demo:
|
81 |
gr.Markdown("# MID-Space Dataset Viewer")
|
82 |
+
|
83 |
+
with gr.Row():
|
84 |
+
pair_id_input = gr.Dropdown(
|
85 |
+
label="Select Pair ID",
|
86 |
+
choices=pair_ids,
|
87 |
+
value=pair_ids[0],
|
88 |
+
interactive=True
|
89 |
+
)
|
90 |
+
show_button = gr.Button("Show Image Pair")
|
91 |
+
|
92 |
with gr.Row():
|
93 |
with gr.Column():
|
94 |
+
img1 = gr.Image(
|
95 |
+
label="Image 1",
|
96 |
+
interactive=False,
|
97 |
+
elem_id="image1"
|
98 |
+
)
|
99 |
with gr.Column():
|
100 |
+
img2 = gr.Image(
|
101 |
+
label="Image 2",
|
102 |
+
interactive=False,
|
103 |
+
elem_id="image2"
|
104 |
+
)
|
105 |
+
|
|
|
|
|
|
|
|
|
106 |
with gr.Row():
|
107 |
+
prompt = gr.Textbox(
|
108 |
+
label="Prompt",
|
109 |
+
interactive=False,
|
110 |
+
lines=2,
|
111 |
+
placeholder="Select a Pair ID to view details."
|
112 |
+
)
|
113 |
+
|
114 |
+
with gr.Accordion("Labels", open=False):
|
115 |
+
with gr.Row():
|
116 |
+
label1_name = gr.Textbox(
|
117 |
+
label="Label 1",
|
118 |
+
interactive=False,
|
119 |
+
placeholder="Label 1"
|
120 |
+
)
|
121 |
+
label1_score = gr.Number(
|
122 |
+
label="Label 1 Score",
|
123 |
+
interactive=False,
|
124 |
+
precision=2,
|
125 |
+
placeholder="0.0"
|
126 |
+
)
|
127 |
+
with gr.Row():
|
128 |
+
label2_name = gr.Textbox(
|
129 |
+
label="Label 2",
|
130 |
+
interactive=False,
|
131 |
+
placeholder="Label 2"
|
132 |
+
)
|
133 |
+
label2_score = gr.Number(
|
134 |
+
label="Label 2 Score",
|
135 |
+
interactive=False,
|
136 |
+
precision=2,
|
137 |
+
placeholder="0.0"
|
138 |
+
)
|
139 |
+
with gr.Row():
|
140 |
+
label3_name = gr.Textbox(
|
141 |
+
label="Label 3",
|
142 |
+
interactive=False,
|
143 |
+
placeholder="Label 3"
|
144 |
+
)
|
145 |
+
label3_score = gr.Number(
|
146 |
+
label="Label 3 Score",
|
147 |
+
interactive=False,
|
148 |
+
precision=2,
|
149 |
+
placeholder="0.0"
|
150 |
+
)
|
151 |
+
|
152 |
+
# Define the callback function for the button
|
153 |
+
def update_viewer(response):
|
154 |
+
return {
|
155 |
+
img1: response["images"][0],
|
156 |
+
img2: response["images"][1],
|
157 |
+
prompt: response["prompt"],
|
158 |
+
label1_name: response["labels"]["Label 1"]["name"],
|
159 |
+
label1_score: response["labels"]["Label 1"]["score"],
|
160 |
+
label2_name: response["labels"]["Label 2"]["name"],
|
161 |
+
label2_score: response["labels"]["Label 2"]["score"],
|
162 |
+
label3_name: response["labels"]["Label 3"]["name"],
|
163 |
+
label3_score: response["labels"]["Label 3"]["score"],
|
164 |
+
}
|
165 |
|
166 |
show_button.click(
|
167 |
+
show_image_pair,
|
168 |
+
inputs=[pair_id_input],
|
169 |
+
outputs=[img1, img2, prompt, label1_name, label1_score, label2_name, label2_score, label3_name, label3_score],
|
170 |
+
postprocess=update_viewer
|
171 |
)
|
172 |
|
173 |
if __name__ == "__main__":
|