Skip to content

Commit

Permalink
Fix request serialization for fastapi /docs (#8530)
Browse files Browse the repository at this point in the history
* Fix request serialization for fastapi `/docs`

* fix issues identified in code review

* unit test

* add changeset

---------

Co-authored-by: freddyaboulton <alfonsoboulton@gmail.com>
Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
  • Loading branch information
3 people committed Jun 11, 2024
1 parent d43d696 commit d429690
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/dull-lizards-suffer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"gradio": patch
---

fix:Fix request serialization for fastapi `/docs`
19 changes: 19 additions & 0 deletions gradio/data_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,25 @@ class PredictBody(BaseModel):
None # dictionary of request headers, query parameters, url, etc. (used to to pass in request for queuing)
)

@classmethod
def __get_pydantic_json_schema__(cls, core_schema, handler):
return {
"title": "PredictBody",
"type": "object",
"properties": {
"session_hash": {"type": "string"},
"event_id": {"type": "string"},
"data": {"type": "array", "items": {"type": "object"}},
"event_data": {"type": "object"},
"fn_index": {"type": "integer"},
"trigger_id": {"type": "integer"},
"simple_format": {"type": "boolean"},
"batched": {"type": "boolean"},
"request": {"type": "object"},
},
"required": ["data"],
}


class ResetBody(BaseModel):
event_id: str
Expand Down
16 changes: 16 additions & 0 deletions test/test_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1320,3 +1320,19 @@ def test_max_file_size_used_in_upload_route(connect):
with open("test/test_files/alphabet.txt", "rb") as f:
r = test_client.post("/upload", files={"files": f})
assert r.status_code == 200


def test_docs_url():
with gr.Blocks() as demo:
num = gr.Number(value=0)
button = gr.Button()
button.click(lambda n: n + 1, [num], [num])

app, _, _ = demo.launch(app_kwargs={"docs_url": "/docs"}, prevent_thread_lock=True)
try:
test_client = TestClient(app)
with test_client:
r = test_client.get("/docs")
assert r.status_code == 200
finally:
demo.close()

0 comments on commit d429690

Please sign in to comment.