rsdmu commited on
Commit
11c0789
·
verified ·
1 Parent(s): 8ec9729

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -112
app.py CHANGED
@@ -20,58 +20,35 @@ 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()
@@ -81,87 +58,34 @@ 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
- img1 = gr.Image(
94
- label="Image 1",
95
- interactive=False
96
- )
97
- img2 = gr.Image(
98
- label="Image 2",
99
- interactive=False
100
- )
101
 
102
  with gr.Row():
103
- prompt = gr.Textbox(
104
- label="Prompt",
105
- interactive=False,
106
- lines=2,
107
- placeholder="Select a Pair ID to view details."
108
- )
109
 
110
  with gr.Row():
111
- label1 = gr.Textbox(
112
- label="Label 1",
113
- interactive=False
114
- )
115
- label1_score = gr.Number(
116
- label="Label 1 Score",
117
- interactive=False,
118
- precision=2,
119
- value=0.0 # ✅ Correct usage
120
- )
121
 
122
  with gr.Row():
123
- label2 = gr.Textbox(
124
- label="Label 2",
125
- interactive=False
126
- )
127
- label2_score = gr.Number(
128
- label="Label 2 Score",
129
- interactive=False,
130
- precision=2,
131
- value=0.0 # ✅ Correct usage
132
- )
133
-
134
- with gr.Row():
135
- label3 = gr.Textbox(
136
- label="Label 3",
137
- interactive=False
138
- )
139
- label3_score = gr.Number(
140
- label="Label 3 Score",
141
- interactive=False,
142
- precision=2,
143
- value=0.0 # ✅ Correct usage
144
- )
145
-
146
- # Define the callback function for the button
147
- def update_viewer(response):
148
- return {
149
- img1: response["images"][0],
150
- img2: response["images"][1],
151
- prompt: response["prompt"],
152
- label1: response["labels"]["Label 1"]["name"],
153
- label1_score: response["labels"]["Label 1"]["score"],
154
- label2: response["labels"]["Label 2"]["name"],
155
- label2_score: response["labels"]["Label 2"]["score"],
156
- label3: response["labels"]["Label 3"]["name"],
157
- label3_score: response["labels"]["Label 3"]["score"],
158
- }
159
 
160
  show_button.click(
161
- show_image_pair,
162
- inputs=[pair_id_input],
163
- outputs=[img1, img2, prompt, label1, label1_score, label2, label2_score, label3, label3_score],
164
- postprocess=update_viewer
165
  )
166
 
167
  if __name__ == "__main__":
 
20
  print(f"Error reading 'preferences.csv': {e}")
21
  sys.exit(1)
22
 
23
+ # Function to display image pairs
24
  def show_image_pair(pair_id):
25
  try:
26
  record = df[df['pair_id'] == pair_id].iloc[0]
27
  except IndexError:
28
+ return None, None, "Invalid Pair ID.", "", 0.0, "", 0.0, "", 0.0
 
 
 
 
 
 
 
 
29
 
30
  img1_path = os.path.join(current_dir, record['image1'])
31
  img2_path = os.path.join(current_dir, record['image2'])
32
 
33
+ # Check if images exist
34
+ if not os.path.isfile(img1_path):
35
+ return None, None, "Image 1 not found.", "", 0.0, "", 0.0, "", 0.0
36
+ if not os.path.isfile(img2_path):
37
+ return None, None, "Image 2 not found.", "", 0.0, "", 0.0, "", 0.0
 
 
 
 
 
38
 
39
+ try:
40
+ img1 = Image.open(img1_path)
41
+ except Exception as e:
42
+ img1 = None
43
+ print(f"Error opening Image 1: {e}")
 
 
 
 
 
44
 
45
+ try:
46
+ img2 = Image.open(img2_path)
47
+ except Exception as e:
48
+ img2 = None
49
+ print(f"Error opening Image 2: {e}")
 
 
 
 
 
50
 
51
+ return img1, img2, record['prompt'], record['label1'], record['label1_score'], record['label2'], record['label2_score'], record['label3'], record['label3_score']
52
 
53
  # Create dropdown options
54
  pair_ids = df['pair_id'].tolist()
 
58
  gr.Markdown("# MID-Space Dataset Viewer")
59
 
60
  with gr.Row():
61
+ with gr.Column():
62
+ pair_id_input = gr.Dropdown(label="Select Pair ID", choices=pair_ids, value=pair_ids[0])
63
+ show_button = gr.Button("Show Image Pair")
64
+
65
+ # New Row for side-by-side images
66
+ with gr.Row():
67
+ img1 = gr.Image(label="Image 1")
68
+ img2 = gr.Image(label="Image 2")
69
 
70
  with gr.Row():
71
+ prompt = gr.Textbox(label="Prompt", interactive=False)
 
 
 
 
 
 
 
72
 
73
  with gr.Row():
74
+ label1 = gr.Textbox(label="Label 1", interactive=False)
75
+ label1_score = gr.Number(label="Label 1 Score", interactive=False)
 
 
 
 
76
 
77
  with gr.Row():
78
+ label2 = gr.Textbox(label="Label 2", interactive=False)
79
+ label2_score = gr.Number(label="Label 2 Score", interactive=False)
 
 
 
 
 
 
 
 
80
 
81
  with gr.Row():
82
+ label3 = gr.Textbox(label="Label 3", interactive=False)
83
+ label3_score = gr.Number(label="Label 3 Score", interactive=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84
 
85
  show_button.click(
86
+ show_image_pair,
87
+ inputs=[pair_id_input],
88
+ outputs=[img1, img2, prompt, label1, label1_score, label2, label2_score, label3, label3_score]
 
89
  )
90
 
91
  if __name__ == "__main__":