multimodalart's picture
multimodalart HF Staff
Restore safe (working) variant: img2 column survives
25c34c3 verified
Raw
History Blame Contribute Delete
2.73 kB
"""Minimal repro isolating how gr.Examples handles None in optional image columns.
Hypothesis under test
---------------------
gr.Examples drops any INPUT COLUMN that is None in *every* example row (it builds
an `input_has_examples` mask, keeps a column only if at least one row is non-None,
then filters the value rows to match). When a whole column is dropped, the click
handler that loads an example back into the inputs returns fewer values than there
are output components -> ValueError: "didn't return enough output values".
Claim: a lone None in a *surviving* column (one that has >=1 non-None value across
the rows) is fine. Only an *entirely*-None column breaks.
This Space demonstrates the SAFE case (option 2). The optional second image column
has a real image in row 1, so the column survives; row 2's None loads cleanly.
To reproduce the FAILURE, swap in the BROKEN_EXAMPLES block below (both rows None
for img2) and redeploy -> you'll get the ValueError on example click / cache warm.
"""
import gradio as gr
SAFE_EXAMPLES = [
# [label, img1, img2] -- img2 column survives because row 1 fills it.
["both images", "red.png", "green.png"],
["only first image (img2=None, but column survives)", "blue.png", None],
]
# BROKEN_EXAMPLES -- uncomment to reproduce the crash. img2 is None in EVERY row,
# so gr.Examples drops the img2 column and the loader returns 2 values for 3
# components -> "didn't return enough output values (needed: 3, returned: 2)".
# BROKEN_EXAMPLES = [
# ["only first image", "red.png", None],
# ["only first image", "blue.png", None],
# ]
def describe(label, img1, img2):
n = sum(x is not None for x in (img1, img2))
return f"label={label!r} | images provided: {n} | img1={'set' if img1 is not None else 'None'} | img2={'set' if img2 is not None else 'None'}"
with gr.Blocks() as demo:
gr.Markdown(
"# gr.Examples None-column repro\n"
"Click each example. Both load without error because the `img2` column "
"has at least one real image (row 1), so Gradio keeps the column and the "
"`None` in row 2 loads fine. This is **option 2** from the fix guidance."
)
label = gr.Textbox(label="label")
with gr.Row():
img1 = gr.Image(label="image 1 (required)", type="filepath")
img2 = gr.Image(label="image 2 (optional)", type="filepath")
out = gr.Textbox(label="result")
run = gr.Button("Describe", variant="primary")
gr.Examples(
examples=SAFE_EXAMPLES,
inputs=[label, img1, img2],
outputs=out,
fn=describe,
cache_examples=True,
)
run.click(describe, inputs=[label, img1, img2], outputs=out)
if __name__ == "__main__":
demo.launch()