Cache view_api info in server and python client (#7888)

* Cache view_api info

* add changeset

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
This commit is contained in:
Freddy Boulton 2024-03-29 10:17:42 -07:00 committed by GitHub
parent 7bbc3b62bf
commit 946487cf8e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 38 additions and 6 deletions

View File

@ -0,0 +1,6 @@
---
"gradio": patch
"gradio_client": patch
---
fix:Cache view_api info in server and python client

View File

@ -169,7 +169,7 @@ class Client:
self.upload_url = urllib.parse.urljoin(self.src, utils.UPLOAD_URL)
self.reset_url = urllib.parse.urljoin(self.src, utils.RESET_URL)
self.app_version = version.parse(self.config.get("version", "2.0"))
self._info = None
self._info = self._get_api_info()
self.session_hash = str(uuid.uuid4())
endpoint_class = (
@ -611,8 +611,6 @@ class Client:
}
"""
if not self._info:
self._info = self._get_api_info()
num_named_endpoints = len(self._info["named_endpoints"])
num_unnamed_endpoints = len(self._info["unnamed_endpoints"])
if num_named_endpoints == 0 and all_endpoints is None:
@ -1000,6 +998,7 @@ class Endpoint:
self.api_name: str | Literal[False] | None = (
"/" + api_name if isinstance(api_name, str) else api_name
)
self._info = self.client._info
self.protocol = protocol
self.input_component_types = [
self._get_component_type(id_) for id_ in dependency["inputs"]
@ -1029,8 +1028,6 @@ class Endpoint:
)
def _get_parameters_info(self) -> list[ParameterInfo] | None:
if not self.client._info:
self._info = self.client._get_api_info()
if self.api_name in self._info["named_endpoints"]:
return self._info["named_endpoints"][self.api_name]["parameters"]
return None

View File

@ -428,3 +428,20 @@ def long_response_with_info():
None,
gr.Textbox(label="Output"),
)
@pytest.fixture
def many_endpoint_demo():
with gr.Blocks() as demo:
def noop(x):
return x
n_elements = 1000
for _ in range(n_elements):
msg2 = gr.Textbox()
msg2.submit(noop, msg2, msg2)
butn2 = gr.Button()
butn2.click(noop, msg2, msg2)
return demo

View File

@ -74,6 +74,14 @@ class TestClientInitialization:
)
assert {"authorization": "Bearer abcde"}.items() <= client.headers.items()
def test_many_endpoint_demo_loads_quickly(self, many_endpoint_demo):
import datetime
start = datetime.datetime.now()
with connect(many_endpoint_demo):
pass
assert (datetime.datetime.now() - start).seconds < 5
class TestClientPredictions:
@pytest.mark.flaky

View File

@ -163,6 +163,7 @@ class App(FastAPI):
self.change_event: None | threading.Event = None
self._asyncio_tasks: list[asyncio.Task] = []
self.auth_dependency = auth_dependency
self.api_info = None
# Allow user to manually set `docs_url` and `redoc_url`
# when instantiating an App; when they're not set, disable docs and redoc.
kwargs.setdefault("docs_url", None)
@ -391,7 +392,10 @@ class App(FastAPI):
@app.get("/info/", dependencies=[Depends(login_check)])
@app.get("/info", dependencies=[Depends(login_check)])
def api_info():
return app.get_blocks().get_api_info() # type: ignore
# The api info is set in create_app
if not app.api_info:
app.api_info = app.get_blocks().get_api_info()
return app.api_info
@app.get("/config/", dependencies=[Depends(login_check)])
@app.get("/config", dependencies=[Depends(login_check)])