Spaces:
Running
Running
File size: 3,647 Bytes
5de9fd6 61272e2 d4c9123 61272e2 5de9fd6 61272e2 5de9fd6 11c0789 5de9fd6 61272e2 1c7207f 61272e2 11c0789 1c7207f 11c0789 1c7207f 61272e2 11c0789 61272e2 11c0789 61272e2 11c0789 5de9fd6 61272e2 d4c9123 61272e2 d4c9123 11c0789 d4c9123 11c0789 61272e2 d4c9123 5de9fd6 d4c9123 61272e2 ad1cd01 8ec9729 ad1cd01 d4c9123 1c7207f ad1cd01 1c7207f ad1cd01 d4c9123 1c7207f 8ec9729 d4c9123 8ec9729 ad1cd01 5de9fd6 d4c9123 5de9fd6 11c0789 ad1cd01 5de9fd6 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
import gradio as gr
import pandas as pd
from PIL import Image
import os
import sys
# Determine the absolute path to preferences.csv
current_dir = os.path.dirname(os.path.abspath(__file__))
csv_path = os.path.join(current_dir, 'preferences.csv')
# Check if preferences.csv exists
if not os.path.isfile(csv_path):
print(f"Error: 'preferences.csv' not found at {csv_path}")
sys.exit(1)
# Load metadata
try:
df = pd.read_csv(csv_path)
except Exception as e:
print(f"Error reading 'preferences.csv': {e}")
sys.exit(1)
# Function to display image pairs
def show_image_pair(pair_id):
try:
record = df[df['pair_id'] == pair_id].iloc[0]
except IndexError:
return None, None, "Invalid Pair ID.", "", "", "", "", "", ""
img1_path = os.path.join(current_dir, record['image1'])
img2_path = os.path.join(current_dir, record['image2'])
# Check if images exist
if not os.path.isfile(img1_path):
return None, None, "Image 1 not found.", "", "", "", "", "", ""
if not os.path.isfile(img2_path):
return None, None, "Image 2 not found.", "", "", "", "", "", ""
try:
img1 = Image.open(img1_path)
except Exception as e:
img1 = None
print(f"Error opening Image 1: {e}")
try:
img2 = Image.open(img2_path)
except Exception as e:
img2 = None
print(f"Error opening Image 2: {e}")
return img1, img2, record['prompt'], record['label1'], record['label1_score'], record['label2'], record['label2_score'], record['label3'], record['label3_score']
# Create dropdown options
pair_ids = df['pair_id'].tolist()
# Define Gradio Interface
with gr.Blocks() as demo:
gr.Markdown("# MID-Space Dataset Viewer")
# Pair Selection Row
with gr.Row():
with gr.Column(scale=1):
pair_id_input = gr.Dropdown(label="Select Pair ID", choices=pair_ids, value=pair_ids[0])
with gr.Column(scale=1):
show_button = gr.Button("Show Image Pair")
# Images Row
with gr.Row():
with gr.Column(scale=1):
img1 = gr.Image(label="Image 1")
with gr.Column(scale=1):
img2 = gr.Image(label="Image 2")
# Label 1 and Label 1 Score Row
with gr.Row():
with gr.Column(scale=2):
label1 = gr.Textbox(label="Label 1", interactive=False, lines=1)
with gr.Column(scale=1):
label1_score = gr.Textbox(label="Label 1 Score", interactive=False, lines=1)
# Label 2 and Label 2 Score Row
with gr.Row():
with gr.Column(scale=2):
label2 = gr.Textbox(label="Label 2", interactive=False, lines=1)
with gr.Column(scale=1):
label2_score = gr.Textbox(label="Label 2 Score", interactive=False, lines=1)
# Label 3 and Label 3 Score Row
with gr.Row():
with gr.Column(scale=2):
label3 = gr.Textbox(label="Label 3", interactive=False, lines=1)
with gr.Column(scale=1):
label3_score = gr.Textbox(label="Label 3 Score", interactive=False, lines=1)
# Prompt Row
with gr.Row():
with gr.Column(scale=3):
prompt = gr.Textbox(label="Prompt", interactive=False, lines=2)
# Define Button Click Action
show_button.click(
show_image_pair,
inputs=[pair_id_input],
outputs=[
img1,
img2,
prompt,
label1,
label1_score,
label2,
label2_score,
label3,
label3_score
]
)
if __name__ == "__main__":
demo.launch()
|