Spaces:
Running
Running
File size: 2,949 Bytes
5de9fd6 61272e2 5de9fd6 61272e2 5de9fd6 11c0789 5de9fd6 61272e2 11c0789 61272e2 11c0789 61272e2 11c0789 61272e2 11c0789 61272e2 11c0789 5de9fd6 61272e2 11c0789 61272e2 5de9fd6 11c0789 61272e2 5de9fd6 11c0789 61272e2 8ec9729 11c0789 8ec9729 11c0789 5de9fd6 11c0789 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 |
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.", "", 0.0, "", 0.0, "", 0.0
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.", "", 0.0, "", 0.0, "", 0.0
if not os.path.isfile(img2_path):
return None, None, "Image 2 not found.", "", 0.0, "", 0.0, "", 0.0
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")
with gr.Row():
with gr.Column():
pair_id_input = gr.Dropdown(label="Select Pair ID", choices=pair_ids, value=pair_ids[0])
show_button = gr.Button("Show Image Pair")
# New Row for side-by-side images
with gr.Row():
img1 = gr.Image(label="Image 1")
img2 = gr.Image(label="Image 2")
with gr.Row():
prompt = gr.Textbox(label="Prompt", interactive=False)
with gr.Row():
label1 = gr.Textbox(label="Label 1", interactive=False)
label1_score = gr.Number(label="Label 1 Score", interactive=False)
with gr.Row():
label2 = gr.Textbox(label="Label 2", interactive=False)
label2_score = gr.Number(label="Label 2 Score", interactive=False)
with gr.Row():
label3 = gr.Textbox(label="Label 3", interactive=False)
label3_score = gr.Number(label="Label 3 Score", interactive=False)
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()
|