Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix request serialization for fastapi /docs #8530

Merged
merged 4 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()