Repository: gradio-app/gradio

Issue:
`gr.Interface.load()` not working if the loaded Space has examples
### Describe the bug

It seems that `gr.Interface.load()` is not able to load a Space if the Space has examples

### Is there an existing issue for this?

- [X] I have searched the existing issues

### Reproduction

```py
import gradio as gr

io = gr.Interface.load("spaces/abidlabs/test-calculator-2")
```

### Screenshot

_No response_

### Logs

```shell
KeyError                                  Traceback (most recent call last)
<ipython-input-9-1a3d663f2b76> in <module>
----> 1 io = gr.Interface.load("spaces/abidlabs/test-calculator-2")

c:\users\islam\dev\gradio-repos\gradio\gradio\interface.py in load(cls, name, src, api_key, alias, **kwargs)
    106             demo.launch()
    107         """
--> 108         return super().load(name=name, src=src, api_key=api_key, alias=alias)
    109 
    110     @classmethod

c:\users\islam\dev\gradio-repos\gradio\gradio\blocks.py in load(self_or_cls, fn, inputs, outputs, name, src, api_key, alias, _js, every, **kwargs)
   1112                     "Blocks.load() requires passing parameters as keyword arguments"
   1113                 )
-> 1114             return external.load_blocks_from_repo(name, src, api_key, alias, **kwargs)
   1115         else:
   1116             return self_or_cls.set_event_trigger(

c:\users\islam\dev\gradio-repos\gradio\gradio\external.py in load_blocks_from_repo(name, src, api_key, alias, **kwargs)
     55     )
     56 
---> 57     blocks: gradio.Blocks = factory_methods[src](name, api_key, alias, **kwargs)
     58     return blocks
     59 

c:\users\islam\dev\gradio-repos\gradio\gradio\external.py in from_spaces(space_name, api_key, alias, **kwargs)
    346         )
    347     else:  # Create a Blocks for Gradio 3.x Spaces
--> 348         return from_spaces_blocks(config, api_key, iframe_url)
    349 
    350 

c:\users\islam\dev\gradio-repos\gradio\gradio\external.py in from_spaces_blocks(config, api_key, iframe_url)
    397         else:
    398             fns.append(None)
--> 399     return gradio.Blocks.from_config(config, fns, iframe_url)
    400 
    401 

c:\users\islam\dev\gradio-repos\gradio\gradio\blocks.py in from_config(cls, config, fns, root_url)
    638 
    639                 for target in targets:
--> 640                     dependency = original_mapping[target].set_event_trigger(
    641                         event_name=trigger, fn=fn, **dependency
    642                     )

KeyError: 2
```


### System Info

```shell
3.9.1
```


### Severity

serious, but I can work around it


Discussion:
I think the problem is related to the "fake event" used in example caching:

Just checked out the config of the upstream calculator space and id 2 corresponds to the fake event:
```
{"targets":[2],"trigger":"fake_event","inputs":[0,1,3],"outputs":[4],"backend_fn":true,"js":null,"queue":null,"api_name":null,"scroll_to_output":false,"show_progress":true,"every":null,"batch":false,"max_batch_size":4,"cancels":[]}
```

Can take a look at a fix

Resolving patch:
diff --git a/gradio/blocks.py b/gradio/blocks.py
--- a/gradio/blocks.py
+++ b/gradio/blocks.py
@@ -622,6 +622,13 @@ def iterate_over_children(children_list):
 
             # add the event triggers
             for dependency, fn in zip(config["dependencies"], fns):
+                # We used to add a "fake_event" to the config to cache examples
+                # without removing it. This was causing bugs in calling gr.Interface.load
+                # We fixed the issue by removing "fake_event" from the config in examples.py
+                # but we still need to skip these events when loading the config to support
+                # older demos
+                if dependency["trigger"] == "fake_event":
+                    continue
                 targets = dependency.pop("targets")
                 trigger = dependency.pop("trigger")
                 dependency.pop("backend_fn")
diff --git a/gradio/examples.py b/gradio/examples.py
--- a/gradio/examples.py
+++ b/gradio/examples.py
@@ -296,6 +296,9 @@ async def cache(self) -> None:
                 if self.batch:
                     output = [value[0] for value in output]
                 cache_logger.flag(output)
+            # Remove the "fake_event" to prevent bugs in loading interfaces from spaces
+            Context.root_block.dependencies.remove(dependency)
+            Context.root_block.fns.pop(fn_index)
 
     async def load_from_cache(self, example_id: int) -> List[Any]:
         """Loads a particular cached example for the interface.


Test patch:
diff --git a/test/test_examples.py b/test/test_examples.py
--- a/test/test_examples.py
+++ b/test/test_examples.py
@@ -179,6 +179,7 @@ async def test_caching_with_dict(self):
         )
         prediction = await io.examples_handler.load_from_cache(0)
         assert prediction == [{"lines": 4, "__type__": "update"}, {"label": "lion"}]
+        assert not any(d["trigger"] == "fake_event" for d in io.config["dependencies"])
 
     def test_raise_helpful_error_message_if_providing_partial_examples(self, tmp_path):
         def foo(a, b):
diff --git a/test/test_external.py b/test/test_external.py
--- a/test/test_external.py
+++ b/test/test_external.py
@@ -274,6 +274,15 @@ def test_root_url(self):
             ]
         )
 
+    def test_interface_with_examples(self):
+        # This demo has the "fake_event" correctly removed
+        demo = gr.Interface.load("spaces/freddyaboulton/calculator")
+        assert demo(2, "add", 3) == 5
+
+        # This demo still has the "fake_event". both should work
+        demo = gr.Interface.load("spaces/abidlabs/test-calculator-2")
+        assert demo(2, "add", 4) == 6
+
 
 def test_get_tabular_examples_replaces_nan_with_str_nan():
     readme = """