Skip to content

Commit

Permalink
Allow textbox / number submits to trigger Interface submit (#4090)
Browse files Browse the repository at this point in the history
* changes

* changes

* changes

* changes

* changes

---------

Co-authored-by: Abubakar Abid <abubakar@huggingface.co>
  • Loading branch information
2 people authored and dawoodkhan82 committed Jun 2, 2023
1 parent 7546a4e commit 32c37e2
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 18 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ No changes to highlight.

## Full Changelog:

- Allow users to submit with enter in Interfaces with textbox / number inputs [@aliabid94](https://github.com/aliabid94) in [PR 4090](https://github.com/gradio-app/gradio/pull/4090).
- Updates gradio's requirements.txt to requires uvicorn>=0.14.0 by [@abidlabs](https://github.com/abidlabs) in [PR 4086](https://github.com/gradio-app/gradio/pull/4086)
- Updates some error messaging by [@abidlabs](https://github.com/abidlabs) in [PR 4086](https://github.com/gradio-app/gradio/pull/4086)
- Renames simplified Chinese translation file from `zh-cn.json` to `zh-CN.json` by [@abidlabs](https://github.com/abidlabs) in [PR 4086](https://github.com/gradio-app/gradio/pull/4086)


## Contributors Shoutout:

No changes to highlight.
Expand Down
46 changes: 33 additions & 13 deletions gradio/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
get_component_instance,
)
from gradio.data_classes import InterfaceTypes
from gradio.events import Changeable, Streamable
from gradio.events import Changeable, Streamable, Submittable
from gradio.flagging import CSVLogger, FlaggingCallback, FlagMethod
from gradio.layouts import Column, Row, Tab, Tabs
from gradio.pipelines import load_from_pipeline
Expand Down Expand Up @@ -625,17 +625,37 @@ def fn(*args):
] + [Button.update(visible=True), Button.update(visible=False)]

extra_output = [submit_btn, stop_btn]
pred = submit_btn.click(
fn,
self.input_components,
self.output_components + extra_output,
api_name="predict",
scroll_to_output=True,
preprocess=not (self.api_mode),
postprocess=not (self.api_mode),
batch=self.batch,
max_batch_size=self.max_batch_size,
)
triggers = [submit_btn.click] + [
component.submit
for component in self.input_components
if isinstance(component, Submittable)
]
predict_events = []
for i, trigger in enumerate(triggers):
predict_events.append(
trigger(
fn,
self.input_components,
self.output_components + extra_output,
api_name="predict" if i == 0 else None,
scroll_to_output=True,
preprocess=not (self.api_mode),
postprocess=not (self.api_mode),
batch=self.batch,
max_batch_size=self.max_batch_size,
)
)
if stop_btn:
trigger(
lambda: (
submit_btn.update(visible=False),
stop_btn.update(visible=True),
),
inputs=None,
outputs=[submit_btn, stop_btn],
queue=False,
)

if stop_btn:
submit_btn.click(
lambda: (
Expand All @@ -653,7 +673,7 @@ def fn(*args):
),
inputs=None,
outputs=[submit_btn, stop_btn],
cancels=[pred],
cancels=predict_events,
queue=False,
)

Expand Down
8 changes: 4 additions & 4 deletions test/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,10 +327,10 @@ def concatenate(str1, str2):
app, _, _ = io.launch(prevent_thread_lock=True)
client = TestClient(app)

response = client.post("/api/predict/", json={"fn_index": 5, "data": [0]})
response = client.post("/api/predict/", json={"fn_index": 7, "data": [0]})
assert response.json()["data"] == ["Hello,"]

response = client.post("/api/predict/", json={"fn_index": 5, "data": [1]})
response = client.post("/api/predict/", json={"fn_index": 7, "data": [1]})
assert response.json()["data"] == ["Michael"]

def test_end_to_end_cache_examples(self):
Expand All @@ -348,8 +348,8 @@ def concatenate(str1, str2):
app, _, _ = io.launch(prevent_thread_lock=True)
client = TestClient(app)

response = client.post("/api/predict/", json={"fn_index": 5, "data": [0]})
response = client.post("/api/predict/", json={"fn_index": 7, "data": [0]})
assert response.json()["data"] == ["Hello,", "World", "Hello, World"]

response = client.post("/api/predict/", json={"fn_index": 5, "data": [1]})
response = client.post("/api/predict/", json={"fn_index": 7, "data": [1]})
assert response.json()["data"] == ["Michael", "Jordan", "Michael Jordan"]

0 comments on commit 32c37e2

Please sign in to comment.