Skip to content

Commit

Permalink
Fix api info for File component (#4153)
Browse files Browse the repository at this point in the history
* Fix output file type info

* Fix example inputs

* CHANGELOG

* Update client

* Add test

* Bump version

* Bump min client version
  • Loading branch information
freddyaboulton authored May 10, 2023
1 parent ccdaac1 commit 3227a9b
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

- Records username when flagging by [@abidlabs](https://github.com/abidlabs) in [PR 4135](https://github.com/gradio-app/gradio/pull/4135)
- Fix website build issue by [@aliabd](https://github.com/aliabd) in [PR 4142](https://github.com/gradio-app/gradio/pull/4142)
- Fix lang agnostic type info for `gr.File(file_count='multiple')` output components by [@freddyaboulton](https://github.com/freddyaboulton) in [PR 4153](https://github.com/gradio-app/gradio/pull/4153)


## Documentation Changes:

Expand Down
29 changes: 29 additions & 0 deletions client/python/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,35 @@

No changes to highlight.

## Bug Fixes:
- Fix example inputs for `gr.File(file_count='multiple')` output components by [@freddyaboulton](https://github.com/freddyaboulton) in [PR 4153](https://github.com/gradio-app/gradio/pull/4153)

## Documentation Changes:

No changes to highlight.

## Testing and Infrastructure Changes:

No changes to highlight.

## Breaking Changes:

No changes to highlight.

## Full Changelog:

No changes to highlight.

## Contributors Shoutout:

No changes to highlight.

# 0.2.2

## New Features:

No changes to highlight.

## Bug Fixes:

- Only send request to `/info` route if demo version is above `3.28.3` by [@freddyaboulton](https://github.com/freddyaboulton) in [PR 4109](https://github.com/gradio-app/gradio/pull/4109)
Expand Down
4 changes: 2 additions & 2 deletions client/python/gradio_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,9 +403,9 @@ def view_api(
else:
api_info_url = urllib.parse.urljoin(self.src, utils.RAW_API_INFO_URL)

# Versions of Gradio older than 3.28.3 returned format of the API info
# Versions of Gradio older than 3.29.0 returned format of the API info
# from the /info endpoint
if version.parse(self.config.get("version", "2.0")) > version.Version("3.28.3"):
if version.parse(self.config.get("version", "2.0")) > version.Version("3.29.0"):
r = requests.get(api_info_url, headers=self.headers)
if r.ok:
info = r.json()
Expand Down
11 changes: 11 additions & 0 deletions client/python/gradio_client/serializing.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,11 +231,22 @@ def api_info(self) -> dict[str, dict | bool]:
return self._single_file_api_info()

def example_inputs(self) -> dict[str, Any]:
return self._single_file_example_inputs()

def _single_file_example_inputs(self) -> dict[str, Any]:
return {
"raw": {"is_file": False, "data": media_data.BASE64_FILE},
"serialized": "https://github.com/gradio-app/gradio/raw/main/test/test_files/sample_file.pdf",
}

def _multiple_file_example_inputs(self) -> dict[str, Any]:
return {
"raw": [{"is_file": False, "data": media_data.BASE64_FILE}],
"serialized": [
"https://github.com/gradio-app/gradio/raw/main/test/test_files/sample_file.pdf"
],
}

def _serialize_single(
self, x: str | FileData | None, load_dir: str | Path = ""
) -> FileData | None:
Expand Down
2 changes: 1 addition & 1 deletion client/python/gradio_client/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.2.2
0.2.3
9 changes: 9 additions & 0 deletions client/python/test/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -740,18 +740,27 @@ def test_file_io(self, file_io_demo):
info = client.view_api(return_format="dict")
inputs = info["named_endpoints"]["/predict"]["parameters"]
outputs = info["named_endpoints"]["/predict"]["returns"]

assert inputs[0]["type"]["type"] == "array"
assert inputs[0]["python_type"] == {
"type": "List[str]",
"description": "List of filepath(s) or URL(s) to files",
}
# Will change to list when we do the next gradio release
assert isinstance(inputs[0]["example_input"], str)

assert inputs[1]["python_type"] == {
"type": "str",
"description": "filepath or URL to file",
}
assert isinstance(inputs[1]["example_input"], str)

assert outputs[0]["python_type"] == {
"type": "List[str]",
"description": "List of filepath(s) or URL(s) to files",
}
assert outputs[0]["type"]["type"] == "array"

assert outputs[1]["python_type"] == {
"type": "str",
"description": "filepath or URL to file",
Expand Down
9 changes: 7 additions & 2 deletions gradio/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -550,8 +550,13 @@ def get_api_info(config: dict, serialize: bool = True):
continue
label = component["props"].get("label", f"value_{o}")
serializer = serializing.COMPONENT_MAPPING[type]()
assert isinstance(serializer, serializing.Serializable)
info = serializer.api_info()
if component.get("api_info") and after_new_format:
info = component["api_info"]
example = component["example_inputs"]["serialized"]
else:
assert isinstance(serializer, serializing.Serializable)
info = serializer.api_info()
example = serializer.example_inputs()["raw"]
python_info = info["info"]
if serialize and info["serialized_info"]:
python_info = serializer.serialized_info()
Expand Down
6 changes: 6 additions & 0 deletions gradio/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -2797,6 +2797,12 @@ def serialized_info(self):
else:
return self._multiple_file_serialized_info()

def example_inputs(self) -> dict[str, Any]:
if self.file_count == "single":
return self._single_file_example_inputs()
else:
return self._multiple_file_example_inputs()


@document("style")
class Dataframe(Changeable, Selectable, IOComponent, JSONSerializable):
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ aiohttp
altair>=4.2.0
fastapi
ffmpy
gradio_client>=0.2.1
gradio_client>=0.2.3
httpx
huggingface_hub>=0.13.0
Jinja2
Expand Down

0 comments on commit 3227a9b

Please sign in to comment.