-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feature: Data Serializer #2956
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
Merged
feature: Data Serializer #2956
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ea7e9a9
feature: Data Serializer capabile of supporting image audio files
jeniyat ecb4800
added exception handling
jeniyat 1cb9de6
updated doc string
jeniyat d79af25
Merge branch 'aws:dev' into jeniyat/data-serializer
jeniyat e2936b2
update exception handling
jeniyat 7e4893c
update exception handling
jeniyat 8368584
updated doc string
jeniyat bde9af2
udpated try/except
jeniyat fe0bf46
Merge branch 'aws:dev' into jeniyat/data-serializer
jeniyat File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,7 +18,6 @@ | |
import csv | ||
import io | ||
import json | ||
|
||
import numpy as np | ||
from six import with_metaclass | ||
|
||
|
@@ -357,3 +356,38 @@ def serialize(self, data): | |
return data.read() | ||
|
||
raise ValueError("Unable to handle input format: %s" % type(data)) | ||
|
||
|
||
class DataSerializer(SimpleBaseSerializer): | ||
"""Serialize data in any file by extracting raw bytes from the file.""" | ||
|
||
def __init__(self, content_type="file-path/raw-bytes"): | ||
"""Initialize a ``DataSerializer`` instance. | ||
|
||
Args: | ||
content_type (str): The MIME type to signal to the inference endpoint when sending | ||
request data (default: "file-path/raw-bytes"). | ||
""" | ||
super(DataSerializer, self).__init__(content_type=content_type) | ||
|
||
def serialize(self, data): | ||
"""Serialize file data to a raw bytes. | ||
|
||
Args: | ||
data (object): Data to be serialized. The data can be a string, | ||
representing file-path or the raw bytes from a file. | ||
Returns: | ||
raw-bytes: The data serialized as a raw-bytes from the input. | ||
""" | ||
if isinstance(data, str): | ||
try: | ||
dataFile = open(data, "rb") | ||
except Exception: | ||
raise ValueError(f"{data} is not a valid file-path.") | ||
dataFileInfo = dataFile.read() | ||
dataFile.close() | ||
return dataFileInfo | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you please move this in try block |
||
if isinstance(data, bytes): | ||
return data | ||
|
||
raise ValueError(f"Object of type {type(data)} is not Data serializable.") |
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lets generalise the exception and not conclude
not a valid file-path
for better debugging.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated in the current revision.