Completely hide api page if show_api=False (#5219)

* Add code

* add changeset

* type check

* CHANGELOG

* Update dirty-snails-love.md

* Add test

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
This commit is contained in:
Freddy Boulton 2023-08-15 10:07:53 -04:00 committed by GitHub
parent 640397075d
commit e8fd4e4ec6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 22 additions and 7 deletions

View File

@ -0,0 +1,6 @@
---
"@gradio/app": minor
"gradio": minor
---
feat:Add `api_name` parameter to `gr.Interface`. Additionally, completely hide api page if show_api=False

View File

@ -9,7 +9,7 @@ import json
import os
import warnings
import weakref
from typing import TYPE_CHECKING, Any, Callable
from typing import TYPE_CHECKING, Any, Callable, Literal
from gradio_client.documentation import document, set_documentation_group
@ -143,6 +143,7 @@ class Interface(Blocks):
analytics_enabled: bool | None = None,
batch: bool = False,
max_batch_size: int = 4,
api_name: str | Literal[False] | None = "predict",
_api_mode: bool = False,
allow_duplication: bool = False,
**kwargs,
@ -171,6 +172,7 @@ class Interface(Blocks):
analytics_enabled: Whether to allow basic telemetry. If None, will use GRADIO_ANALYTICS_ENABLED environment variable if defined, or default to True.
batch: If True, then the function should process a batch of inputs, meaning that it should accept a list of input values for each parameter. The lists should be of equal length (and be up to length `max_batch_size`). The function is then *required* to return a tuple of lists (even if there is only 1 output component), with each list in the tuple corresponding to one output component.
max_batch_size: Maximum number of inputs to batch together if this is called from the queue (only relevant if batch=True)
api_name: Defines how the endpoint appears in the API docs. Can be a string, None, or False. If False or None, the endpoint will not be exposed in the api docs. If set to a string, the endpoint will be exposed in the api docs with the given name. Default value is "predict".
allow_duplication: If True, then will show a 'Duplicate Spaces' button on Hugging Face Spaces.
"""
super().__init__(
@ -181,6 +183,7 @@ class Interface(Blocks):
theme=theme,
**kwargs,
)
self.api_name: str | Literal[False] | None = api_name
if isinstance(fn, list):
raise DeprecationWarning(
@ -620,7 +623,7 @@ class Interface(Blocks):
self.fn,
None,
self.output_components,
api_name="predict",
api_name=self.api_name,
preprocess=not (self.api_mode),
postprocess=not (self.api_mode),
batch=self.batch,
@ -633,7 +636,7 @@ class Interface(Blocks):
self.fn,
self.input_components,
self.output_components,
api_name="predict",
api_name=self.api_name,
preprocess=not (self.api_mode),
postprocess=not (self.api_mode),
)
@ -643,7 +646,7 @@ class Interface(Blocks):
self.fn,
self.input_components,
self.output_components,
api_name="predict",
api_name=self.api_name,
preprocess=not (self.api_mode),
postprocess=not (self.api_mode),
)
@ -678,7 +681,7 @@ class Interface(Blocks):
self.fn,
self.input_components,
self.output_components,
api_name="predict" if i == 0 else None,
api_name=self.api_name if i == 0 else None,
scroll_to_output=True,
preprocess=not (self.api_mode),
postprocess=not (self.api_mode),
@ -708,7 +711,7 @@ class Interface(Blocks):
fn,
self.input_components,
self.output_components,
api_name="predict" if i == 0 else None,
api_name=self.api_name if i == 0 else None,
scroll_to_output=True,
preprocess=not (self.api_mode),
postprocess=not (self.api_mode),

View File

@ -78,7 +78,7 @@
});
let params = new URLSearchParams(window.location.search);
let api_docs_visible = params.get("view") === "api";
let api_docs_visible = params.get("view") === "api" && show_api;
function set_api_docs_visible(visible: boolean): void {
api_docs_visible = visible;
let params = new URLSearchParams(window.location.search);

View File

@ -179,6 +179,12 @@ class TestInterface:
assert len(api_info["named_endpoints"]) == 1
assert len(api_info["unnamed_endpoints"]) == 0
def test_api_name(self):
io = Interface(lambda x: x, "textbox", "textbox", api_name="echo")
assert next(
(d for d in io.config["dependencies"] if d["api_name"] == "echo"), None
)
class TestTabbedInterface:
def test_tabbed_interface_config_matches_manual_tab(self):