Not logged in!

File

Send and receive files using the File class.
Introduction

The File class deals with receiving or sending files via upload or download

Contents

File.Download

File.Download allows you to send files for a user to download. This component should only be used if there is no native support for outputting/viewing a file via the other existing components. If/when passing output_filenames, we recommend to use the correct file extension for the optimal user experience.

Example Applications:

  • Providing a download for video/audio files

Example Usage
1import feather as ftr
2from my_model import my_video_translation_model
3
4def init():
5    uploader = ftr.File.Upload(types=["video"], title="Upload video to translate it into another language")
6    language_select = ftr.List.SelectOne(items=["French", "Chinese", "Spanish"], style="dropdown",
7                                    title="Select language to translate video to")
8    return uploader, language_select
9            
10def run_translation(uploader, language_select):
11    vidoes = uploader.get()
12    # videos = [
13    #     {"name": "video1.mp4", "data": byte-stream},
14    #     {"name": "video2.mp4", "data": byte-stream}
15    # ] 
16
17    to_language = language_select.get_selected() # "French"
18    output_videos = my_video_translation_model(videos, to_language) # [video1-data, video2-data]
19    output_videos = convert_to_byte_stream_data(output_videos)
20    output_filenames = [x["name"] for x in videos]
21
22    #################### CONSTRUCTOR ####################
23    return ftr.File.Download(files=output_videos, output_filenames=output_filenames)
24    #####################################################
25
26if __name__ == "__main__":
27    bundle = ftr.bundle(code_files=[__file__, "my_model.py"], model_files=["outputs/model.ckpt"])
28    ftr.build(name="Video Translation Model", init=init, steps=[run_translation], file_bundle=bundle)

Constructor:
  • files: ListType[Union[bytes, str, Any]] - a list files (of type byte, string or any) for a user to download. Use byte stream data for binary objects such as videos or audio.
  • output_filenames: Optional[ListType[str]] = None - an optional list of filenames to be shown for each of the files to download. We recommend to set this with the correct file extension for optimal user experience.
  • title: Optional[str] = None - a title semantically attached to the component
  • description: Optional[str] = None - a description semantically attached to the component

Attributes:
  • files - get the files passed into this component

Component Playground:
Arg NameValue
files
output_filenames
title
description
ftr.File.Download(
    files=[
    {
        "name": "test1",
        "data": "AAAAI...uMTAy"
    },
    {
        "name": "test2",
        "data": "aGVsb...ybGQh"
    }
], 
    output_filenames=["informative_video_filename.mp4", "informative_text_filename.txt"], 
    title="Click to download your files", 
    description="Non-informative descriptions are useless 👍")
File.Upload

File.Upload allows you receive files from a user.

Example Applications:

  • Receiving image files for an image classifier
  • Receiving text files/documents for a question answering model

Example Usage
1import feather as ftr
2from my_model import my_image_classifier
3
4def init():
5    #################### CONSTRUCTOR ####################
6    return ftr.File.Upload(types=["images"], max_files=3)
7    #####################################################
8      
9def run_my_model(uploader):
10    #################### ACCESSOR ####################
11    images = uploader.get_image_files()
12    ##################################################
13    # images = [
14    #     {"name": "image1.jpg", "data": np.ndarray},
15    #     {"name": "image2.png", "data": np.ndarray}
16    # ] 
17
18    classifications = my_image_classifier(images) # ["cat", "dog"]
19    return ftr.Image.View(images, classifications)
20
21if __name__ == "__main__":
22    bundle = ftr.bundle(code_files=[__file__, "my_model.py"], model_files=["outputs/model.ckpt"])
23    ftr.build(name="Image Classification Model", init=init, steps=[run_my_model], file_bundle=bundle)

Constructor:
  • types: ListType[str] - a list of accepted file types (see supportedFileTypes) that a user can upload.
  • max_files: int = 5 - the maximum amount of files a user can upload. Values over 5 will be capped at 5.
  • title: Optional[str] = None - a title semantically attached to the component
  • description: Optional[str] = None - a description semantically attached to the component

Accessors:
  • get_files() -> ListType[GenericFileType] - gets the user uploaded files as a list of GenericFileType.
  • get_only_filedata() -> ListType[Union[str, bytes]] - gets the user uploaded data and returns only the data (i.e. a list of the uploaded data, without filenames).
  • get_image_files(self) -> ListType[ImageInputObj]- gets a list of image files a user has uploaded. Thedata field in the returned object/dictionary is of type np.ndarray. See ImageInputObjfor more information
  • get_image_only_filedata(self) -> ListType[np.ndarray] - gets a list of only image data a user has uploaded (i.e. without filenames)
  • get_text_files(self) -> ListType[DocumentInputObj]- gets a list of text files a user has uploaded. Thedata field in the returned object/dictionary is of type str. Typically used for document-like or tabular data. See DocumentInputObjfor more information
  • get_text_only_filedata(self) -> ListType[str] - gets a list of only string/text data a user has uploaded (i.e. without filenames)
  • get(format="raw", return_only_filedata=False) - see below
get() (usage of .get() is not recommended)
def get(format="raw", return_only_filedata=False):
    if format == "raw":
        if return_only_filedata:
            return get_only_filedata()
        return get_files()
    if format == "images":
        if return_only_filedata:
            return get_image_only_filedata()
        return get_image_files()
    if format == "text":
        if return_only_filedata:
            return get_text_only_filedata()
        return get_text_files()

Component Playground:
Arg NameValue
types
max_files
title
description
ftr.File.Upload(
    types=["images", ".txt", "video"], 
    max_files=5, 
    title="Upload your files", 
    description="Get a rating of pets cuteness!")
File specific types
# N.B. "ListType" in our documentation refers to the standard Python List type
  # (so as not to be confused with our 'List' class)

supportedFileTypes = ["images", "video", "audio", ".csv", ".gif", ".jpg",
        ".json", ".mp3", ".mp4", ".mpeg", ".png", ".tsv", ".txt"]

class GenericFileType():
    name: str
    data: Union[str, bytes]

class ImageInputObj():
    name: str
    data: np.ndarray
    
class DocumentInputObj():
    name: str
    data: str