from datasets import load_dataset import gradio as gr import os import random wmtis = load_dataset("nlphuji/wmtis")['test'] print(f"Loaded WMTIS, first example:") print(wmtis[0]) dataset_size = len(wmtis) - 1 IMAGE = 'image' IMAGE_DESIGNER = 'image_designer' DESIGNER_EXPLANATION = 'designer_explanation' CROWD_CAPTIONS = 'crowd_captions' CROWD_EXPLANATIONS = 'crowd_explanations' CROWD_UNDERSPECIFIED_CAPTIONS = 'crowd_underspecified_captions' # CROWD_NEGATIVE_EXPLANATIONS = 'crowd_negative_explanations' QA = 'question_answering_pairs' IMAGE_ID = 'image_id' left_side_columns = [IMAGE] # left_side_columns = [IMAGE, DESIGNER_EXPLANATION, IMAGE_DESIGNER, IMAGE_ID] right_side_columns = [x for x in wmtis.features.keys() if x not in left_side_columns and x not in [QA]] enumerate_cols = [CROWD_CAPTIONS, CROWD_EXPLANATIONS, CROWD_UNDERSPECIFIED_CAPTIONS] emoji_to_label = {IMAGE: '🖼️, 📷, 🌄', IMAGE_DESIGNER: '🎨, 🧑‍🎨, 💻', DESIGNER_EXPLANATION: '💡, 🤔, 🧑‍🎨', CROWD_CAPTIONS: '👥, 💬, 📝', CROWD_EXPLANATIONS: '👥, 💡, 🤔', CROWD_UNDERSPECIFIED_CAPTIONS: '👥, 💬, 👎', QA: '❓, 🤔, 💡', IMAGE_ID: '🔍, 📄, 💾'} def func(index): example = wmtis[index] values = get_instance_values(example) return values def get_instance_values(example): values = [] for k in left_side_columns + right_side_columns: if k in enumerate_cols: value = list_to_string(example[k]) elif k == QA: qa_list = [f"Q: {x[0]} A: {x[1]}" for x in example[k]] value = list_to_string(qa_list) else: value = example[k] values.append(value) return values def list_to_string(lst): return '\n'.join(['{}. {}'.format(i+1, item) for i, item in enumerate(lst)]) demo = gr.Blocks() with demo: gr.Markdown("# Slide to iterate WMTIS") with gr.Column(): slider = gr.Slider(minimum=0, maximum=dataset_size, step=1, label='index') with gr.Row(): # index = random.choice(range(0, dataset_size)) index = slider.label example = wmtis[index] instance_values = get_instance_values(example) with gr.Column(): # image_input = gr.Image(value=wmtis[index]["image"]) inputs_left = [] assert len(left_side_columns) == len( instance_values[:len(left_side_columns)]) # excluding the image & designer for key, value in zip(left_side_columns, instance_values[:len(left_side_columns)]): if key == IMAGE: input_k = gr.Image(value=wmtis[index]["image"], label=f"Image {emoji_to_label[key]}") else: label = key.capitalize().replace("_", " ") input_k = gr.Textbox(value=value, label=f"{label} {emoji_to_label[key]}") inputs_left.append(input_k) with gr.Column(): text_inputs_right = [] assert len(right_side_columns) == len(instance_values[len(left_side_columns):]) # excluding the image & designer for key, value in zip(right_side_columns, instance_values[len(left_side_columns):]): label = key.capitalize().replace("_", " ") text_input_k = gr.Textbox(value=value, label=f"{label} {emoji_to_label[key]}") text_inputs_right.append(text_input_k) slider.change(func, inputs=[slider], outputs=inputs_left + text_inputs_right) demo.launch()