Skip to content

Commit e0e2445

Browse files
Fix state handling for simple call API (#13515)
* Fix call API state session handling * add changeset * Address skip_api inputs in simple call API --------- Co-authored-by: gradio-pr-bot <[email protected]>
1 parent ba4963e commit e0e2445

3 files changed

Lines changed: 55 additions & 1 deletion

File tree

‎.changeset/plenty-lands-obey.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"gradio": patch
3+
---
4+
5+
fix:Fix state handling for simple call API

‎gradio/routes.py‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1315,6 +1315,15 @@ async def predict(
13151315
)
13161316
return ORJSONResponse(output)
13171317

1318+
def prepare_simple_api_data(body: PredictBody, fn: Any) -> None:
1319+
if len(body.data) == len(fn.inputs):
1320+
return
1321+
api_inputs = [block for block in fn.inputs if not block.skip_api]
1322+
if len(body.data) != len(api_inputs):
1323+
return
1324+
data = iter(body.data)
1325+
body.data = [None if block.skip_api else next(data) for block in fn.inputs]
1326+
13181327
@router.post("/call/v2/{api_name}", dependencies=[Depends(login_check)])
13191328
@router.post("/call/v2/{api_name}/", dependencies=[Depends(login_check)])
13201329
async def _(
@@ -1336,6 +1345,7 @@ async def _(
13361345
fn = route_utils.get_fn(
13371346
blocks=app.get_blocks(), api_name=api_name, body=full_body
13381347
)
1348+
prepare_simple_api_data(full_body, fn)
13391349
full_body.fn_index = fn._id
13401350
return await queue_join_helper(full_body, request, username)
13411351

@@ -1351,6 +1361,7 @@ async def simple_predict_post(
13511361
fn = route_utils.get_fn(
13521362
blocks=app.get_blocks(), api_name=api_name, body=full_body
13531363
)
1364+
prepare_simple_api_data(full_body, fn)
13541365
full_body.fn_index = fn._id
13551366
return await queue_join_helper(full_body, request, username)
13561367

@@ -1458,7 +1469,9 @@ def process_msg(message: EventMessage) -> str | None:
14581469
return None
14591470
return f"event: {event}\ndata: {json.dumps(data)}\n\n"
14601471

1461-
return await queue_data_helper(request, event_id, process_msg)
1472+
event = app.get_blocks()._queue.event_ids_to_events.get(event_id)
1473+
session_hash = event.session_hash if event else event_id
1474+
return await queue_data_helper(request, session_hash, process_msg)
14621475

14631476
@router.get("/queue/data", dependencies=[Depends(login_check)])
14641477
async def queue_data(

‎test/test_routes.py‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1978,6 +1978,42 @@ def test_bash_api_multiple_inputs_outputs():
19781978
assert json.dumps([123, "abc"]) in response.text
19791979

19801980

1981+
def test_bash_api_uses_session_hash_for_stateful_calls():
1982+
def increment(message, button_value, count):
1983+
count = (count or 0) + 1
1984+
return f"{message}:{button_value}:{count}", count
1985+
1986+
with gr.Blocks() as demo:
1987+
textbox = gr.Textbox()
1988+
button = gr.Button("Go")
1989+
state = gr.State(0)
1990+
output = gr.Textbox()
1991+
textbox.submit(
1992+
increment, [textbox, button, state], [output, state], api_name="predict"
1993+
)
1994+
1995+
app, _, _ = demo.queue().launch(prevent_thread_lock=True, _frontend=False)
1996+
test_client = TestClient(app)
1997+
1998+
try:
1999+
with test_client:
2000+
for message, expected in [
2001+
("first", "first:None:1"),
2002+
("second", "second:None:2"),
2003+
]:
2004+
submit = test_client.post(
2005+
f"{API_PREFIX}/call/predict",
2006+
json={"data": [message], "session_hash": "stateful-session"},
2007+
)
2008+
event_id = submit.json()["event_id"]
2009+
response = test_client.get(f"{API_PREFIX}/call/predict/{event_id}")
2010+
assert response.status_code == 200
2011+
assert "event: complete\ndata:" in response.text
2012+
assert json.dumps([expected, None]) in response.text
2013+
finally:
2014+
demo.close()
2015+
2016+
19812017
def test_attacker_cannot_change_root_in_config(
19822018
attacker_threads=1, victim_threads=10, max_attempts=30
19832019
):

0 commit comments

Comments
 (0)