Repository: gradio-app/gradio

Issue:
Interfaces which provide partial examples will crash on Spaces
### Describe the bug

If you define an interface with a partial list of examples, then the Interface will crash on start-up if you try to run it on spaces since cache_examples=True by default but not all inputs have been provided.

### Is there an existing issue for this?

- [X] I have searched the existing issues

### Reproduction

```python
import os
import gradio as gr

os.environ['SYSTEM'] = "spaces"

def repeat_text(text, times):
    return text * times

gr.Interface(repeat_text,
             inputs=[gr.Textbox(), gr.Number(precision=0)],
             outputs=gr.Textbox(),
             examples=[["hello"], ["world"]]).launch()
```

Then try to run it

### Screenshot

-

### Logs

```shell
Caching examples at: '/Users/freddy/sources/scratch/gradio_cached_examples/16/log.csv'
Traceback (most recent call last):
  File "multiplier.py", line 9, in <module>
    gr.Interface(repeat_text,
  File "/Users/freddy/sources/gradio/gradio/interface.py", line 626, in __init__
    self.examples_handler = Examples(
  File "/Users/freddy/sources/gradio/gradio/examples.py", line 56, in create_examples
    utils.synchronize_async(examples_obj.create)
  File "/Users/freddy/sources/gradio/gradio/utils.py", line 364, in synchronize_async
    return fsspec.asyn.sync(fsspec.asyn.get_loop(), func, *args, **kwargs)
  File "/Users/freddy/miniconda3/envs/gradio/lib/python3.8/site-packages/fsspec/asyn.py", line 96, in sync
    raise return_result
  File "/Users/freddy/miniconda3/envs/gradio/lib/python3.8/site-packages/fsspec/asyn.py", line 53, in _runner
    result[0] = await coro
  File "/Users/freddy/sources/gradio/gradio/examples.py", line 208, in create
    await self.cache_interface_examples()
  File "/Users/freddy/sources/gradio/gradio/examples.py", line 246, in cache_interface_examples
    prediction = await self.predict_example(example_id)
  File "/Users/freddy/sources/gradio/gradio/examples.py", line 263, in predict_example
    predictions = await anyio.to_thread.run_sync(self.fn, *processed_input)
  File "/Users/freddy/miniconda3/envs/gradio/lib/python3.8/site-packages/anyio/to_thread.py", line 31, in run_sync
    return await get_asynclib().run_sync_in_worker_thread(
  File "/Users/freddy/miniconda3/envs/gradio/lib/python3.8/site-packages/anyio/_backends/_asyncio.py", line 937, in run_sync_in_worker_thread
    return await future
  File "/Users/freddy/miniconda3/envs/gradio/lib/python3.8/site-packages/anyio/_backends/_asyncio.py", line 867, in run
    result = context.run(func, *args)
TypeError: repeat_text() missing 1 required positional argument: 'times'
```

Relevant forum discussion: https://discuss.huggingface.co/t/problem-showing-mp4-examples-on-spaces/22801/8


### System Info

```shell
3.2
```


### Severity

serious, but I can work around it


Discussion:

Resolving patch:
diff --git a/demo/fake_gan/run.py b/demo/fake_gan/run.py
--- a/demo/fake_gan/run.py
+++ b/demo/fake_gan/run.py
@@ -2,7 +2,6 @@
 # python demo/fake_gan/run.py
 import os
 import random
-import time
 
 import gradio as gr
 
diff --git a/gradio/examples.py b/gradio/examples.py
--- a/gradio/examples.py
+++ b/gradio/examples.py
@@ -187,6 +187,20 @@ def __init__(
             [ex for (ex, keep) in zip(example, input_has_examples) if keep]
             for example in self.processed_examples
         ]
+        if cache_examples:
+            for ex in non_none_examples:
+                if (
+                    len([sample for sample in ex if sample is not None])
+                    != self.inputs_with_examples
+                ):
+                    warnings.warn(
+                        "Examples are being cached but not all input components have "
+                        "example values. This may result in an exception being thrown by "
+                        "your function. If you do get an error while caching examples, make "
+                        "sure all of your inputs have example values for all of your examples "
+                        "or you provide default values for those particular parameters in your function."
+                    )
+                    break
 
         self.dataset = Dataset(
             components=inputs_with_examples,


Test patch:
diff --git a/test/test_examples.py b/test/test_examples.py
--- a/test/test_examples.py
+++ b/test/test_examples.py
@@ -1,4 +1,5 @@
 import os
+from unittest.mock import patch
 
 import pytest
 
@@ -89,3 +90,62 @@ async def test_caching(self):
         prediction = await io.examples_handler.load_from_cache(1)
         io.close()
         assert prediction[0] == "Hello Dunya"
+
+
+def test_raise_helpful_error_message_if_providing_partial_examples(tmp_path):
+    def foo(a, b):
+        return a + b
+
+    with patch("gradio.examples.CACHED_FOLDER", tmp_path):
+        with pytest.warns(
+            UserWarning,
+            match="^Examples are being cached but not all input components have",
+        ):
+            with pytest.raises(Exception):
+                gr.Interface(
+                    foo,
+                    inputs=["text", "text"],
+                    outputs=["text"],
+                    examples=[["foo"], ["bar"]],
+                    cache_examples=True,
+                )
+
+        with pytest.warns(
+            UserWarning,
+            match="^Examples are being cached but not all input components have",
+        ):
+            with pytest.raises(Exception):
+                gr.Interface(
+                    foo,
+                    inputs=["text", "text"],
+                    outputs=["text"],
+                    examples=[["foo", "bar"], ["bar", None]],
+                    cache_examples=True,
+                )
+
+        def foo_no_exception(a, b=2):
+            return a * b
+
+        gr.Interface(
+            foo_no_exception,
+            inputs=["text", "number"],
+            outputs=["text"],
+            examples=[["foo"], ["bar"]],
+            cache_examples=True,
+        )
+
+        def many_missing(a, b, c):
+            return a * b
+
+        with pytest.warns(
+            UserWarning,
+            match="^Examples are being cached but not all input components have",
+        ):
+            with pytest.raises(Exception):
+                gr.Interface(
+                    many_missing,
+                    inputs=["text", "number", "number"],
+                    outputs=["text"],
+                    examples=[["foo", None, None], ["bar", 2, 3]],
+                    cache_examples=True,
+                )