Using images in a survey

This notebook provides sample code for using images with an EDSL survey.

EDSL is an open-source libary for simulating surveys, experiments and other research with AI agents and large language models. Before running the code below, please ensure that you have installed the EDSL library and either activated remote inference from your Coop account or stored API keys for the language models that you want to use with EDSL. Please also see our documentation page for tips and tutorials on getting started using EDSL.

Scenarios

A Scenario is a dictionary containing a key/value pair that is used to add data or content to questions in an EDSL survey. Scenarios allow you create variations and versions of questions efficiently, and with data or content from different sources.

EDSL provides a variety of methods for automatically generating scenarios from PDFs, CSVs, docs, tables, lists, dicts – and images. In the steps below we demonstrate how to create a scenario for an image and use it in a survey.

Learn more about working with scenarios.

Creating a scenario

We start by creating a Scenario for an image:

[1]:
from edsl import Scenario
[2]:
# Code for generating from a local file:
#
# s = Scenario.from_image("logo.png")

We can use the example scenario, which is the EP logo:

[3]:
s = Scenario.example(has_image = True)

The parameter that we will add to questions in order to include the image:

[4]:
s.keys()
[4]:
['logo']

Creating questions using the image

Next we construct questions with the image scenario. Note that we use a {{ placeholder }} for the scenario key. We also demonstrate how to pipe an answer into a follow-on question:

[5]:
from edsl import QuestionFreeText, Survey
[6]:
q = QuestionFreeText(
    question_name = "identify_image",
    question_text = "What is this a picture of: {{ logo }}?"
)
[7]:
q_follow_up = QuestionFreeText(
    question_name = "id_animal",
    question_text = """
    You previously examined a logo, with the following conclusion: {{ identify_image.answer }}.
    What animal did you see?
    """,
)
[8]:
survey = Survey(questions = [q, q_follow_up])

We use the scenario by adding it to the survey when we run it, as with any other scenarios:

[9]:
results = survey.by(s).run()
[10]:
results.select("identify_image", "id_animal").print(format="rich")
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━┓
┃ answer                                                                                         answer          ┃
┃ .identify_image                                                                                .id_animal      ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━┩
│ This image is a visual pun or a play on words. It combines the mathematical notation for       I saw a parrot. │
│ "expected value," denoted by \( \mathbb{E} \left[ \cdot \right] \), with an image of a                         │
│ parrot. The expected value notation typically contains a random variable inside the brackets,                  │
│ but here, the parrot is placed inside. This creates a humorous interpretation where the                        │
│ expected value is a "parrot," playing on the similarity in the sounds of "parrot" and                          │
│ "parenthesis."                                                                                                 │
└───────────────────────────────────────────────────────────────────────────────────────────────┴─────────────────┘

Wrong answer, but a decent guess!