Skip to content

Commit

Permalink
fix issue 6873: File with file_count='directory' bug (#6874)
Browse files Browse the repository at this point in the history
* fix issue 6873: File with file_count='directory' bug

* add changeset

* fixes

---------

Co-authored-by: Joshua Wilson <uqjwil54@uq.edu.au>
Co-authored-by: Abubakar Abid <abubakar@huggingface.co>
Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
  • Loading branch information
4 people committed Dec 26, 2023
1 parent f416da8 commit 31c2316
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 27 deletions.
5 changes: 5 additions & 0 deletions .changeset/nasty-eagles-do.md
@@ -0,0 +1,5 @@
---
"gradio": patch
---

fix:fix issue 6873: File with file_count='directory' bug
16 changes: 3 additions & 13 deletions gradio/components/file.py
Expand Up @@ -5,28 +5,18 @@
import tempfile
import warnings
from pathlib import Path
from typing import Any, Callable, List, Literal
from typing import Any, Callable, Literal

from gradio_client.documentation import document, set_documentation_group

from gradio.components.base import Component
from gradio.data_classes import FileData, GradioRootModel
from gradio.data_classes import FileData, ListFiles
from gradio.events import Events
from gradio.utils import NamedString

set_documentation_group("component")


class ListFiles(GradioRootModel):
root: List[FileData]

def __getitem__(self, index):
return self.root[index]

def __iter__(self):
return iter(self.root)


@document()
class File(Component):
"""
Expand Down Expand Up @@ -79,7 +69,7 @@ def __init__(
render: If False, component will not render be rendered in the Blocks context. Should be used if the intention is to assign event listeners now but render the component later.
"""
self.file_count = file_count
if self.file_count == "multiple":
if self.file_count in ["multiple", "directory"]:
self.data_model = ListFiles
else:
self.data_model = FileData
Expand Down
16 changes: 3 additions & 13 deletions gradio/components/upload_button.py
Expand Up @@ -5,28 +5,18 @@
import tempfile
import warnings
from pathlib import Path
from typing import Any, Callable, List, Literal
from typing import Any, Callable, Literal

from gradio_client.documentation import document, set_documentation_group

from gradio.components.base import Component
from gradio.data_classes import FileData, GradioRootModel
from gradio.data_classes import FileData, ListFiles
from gradio.events import Events
from gradio.utils import NamedString

set_documentation_group("component")


class ListFiles(GradioRootModel):
root: List[FileData]

def __getitem__(self, index):
return self.root[index]

def __iter__(self):
return iter(self.root)


@document()
class UploadButton(Component):
"""
Expand Down Expand Up @@ -95,7 +85,7 @@ def __init__(
raise ValueError(
f"Parameter file_types must be a list. Received {file_types.__class__.__name__}"
)
if self.file_count == "multiple":
if self.file_count in ["multiple", "directory"]:
self.data_model = ListFiles
else:
self.data_model = FileData
Expand Down
10 changes: 10 additions & 0 deletions gradio/data_classes.py
Expand Up @@ -204,3 +204,13 @@ def is_file_data(cls, obj: Any):
except (TypeError, ValidationError):
return False
return False


class ListFiles(GradioRootModel):
root: List[FileData]

def __getitem__(self, index):
return self.root[index]

def __iter__(self):
return iter(self.root)
18 changes: 17 additions & 1 deletion test/test_components.py
Expand Up @@ -34,7 +34,7 @@
from gradio.components.file_explorer import FileExplorerData
from gradio.components.image_editor import EditorData
from gradio.components.video import VideoData
from gradio.data_classes import FileData
from gradio.data_classes import FileData, ListFiles

os.environ["GRADIO_ANALYTICS_ENABLED"] = "False"

Expand Down Expand Up @@ -991,6 +991,14 @@ def test_component_functions(self):
output2 = file_input.postprocess("test/test_files/sample_file.pdf")
assert output1 == output2

def test_preprocess_with_multiple_files(self):
file_data = FileData(path=media_data.BASE64_FILE["path"])
list_file_data = ListFiles(root=[file_data, file_data])
file_input = gr.File(file_count="directory")
output = file_input.preprocess(list_file_data)
assert isinstance(output, list)
assert isinstance(output[0], str)

def test_file_type_must_be_list(self):
with pytest.raises(
ValueError, match="Parameter file_types must be a list. Received str"
Expand Down Expand Up @@ -1044,6 +1052,14 @@ def test_raises_if_file_types_is_not_list(self):
):
gr.UploadButton(file_types=2)

def test_preprocess_with_multiple_files(self):
file_data = FileData(path=media_data.BASE64_FILE["path"])
list_file_data = ListFiles(root=[file_data, file_data])
upload_input = gr.UploadButton(file_count="directory")
output = upload_input.preprocess(list_file_data)
assert isinstance(output, list)
assert isinstance(output[0], str)


class TestDataframe:
def test_component_functions(self):
Expand Down

0 comments on commit 31c2316

Please sign in to comment.