forked from openml-labs/server-demo
-
Notifications
You must be signed in to change notification settings - Fork 7
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
Feature/get actual data from AI Assets #175
Merged
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
14b2fc9
ResourceAIAssetRouter inheriting RessourceRouter with polymorphism of…
jsmatias 15b34d8
Generic feature to get the actual data from the AI assets.
jsmatias 7836882
Resolved comments regarding the changes to retrieve the content from …
jsmatias bd201eb
Added pytest.mak.skip to huggingface/test_dataset_uploader.py::test_h…
jsmatias 07d92d9
Added pytest.mak.skip to another test to ignore an error for now.
jsmatias 3938c47
Resolved comment on the absence of a encoding format. Content-Type wo…
jsmatias 464475c
Resolved conflict with pytest.mark.skip uploader/huggingface/test..
jsmatias be5d071
Minor change
jsmatias 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 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 |
---|---|---|
@@ -0,0 +1,102 @@ | ||
from fastapi.responses import Response | ||
from fastapi import APIRouter, HTTPException, status | ||
import requests | ||
from sqlalchemy.engine import Engine | ||
|
||
from database.model.ai_asset.ai_asset import AIAsset | ||
|
||
from .resource_router import ResourceRouter, _wrap_as_http_exception | ||
|
||
|
||
class ResourceAIAssetRouter(ResourceRouter): | ||
def create(self, engine: Engine, url_prefix: str) -> APIRouter: | ||
version = "v1" | ||
default_kwargs = { | ||
"response_model_exclude_none": True, | ||
"deprecated": False, | ||
"tags": [self.resource_name_plural], | ||
} | ||
|
||
router = super().create(engine, url_prefix) | ||
|
||
router.add_api_route( | ||
path=f"{url_prefix}/{self.resource_name_plural}/{version}/{{identifier}}/content", | ||
endpoint=self.get_resource_content_func(engine, default=True), | ||
name=self.resource_name, | ||
response_model=str, | ||
**default_kwargs, | ||
) | ||
|
||
router.add_api_route( | ||
path=f"{url_prefix}/{self.resource_name_plural}/{version}/{{identifier}}/content/" | ||
f"{{distribution_idx}}", | ||
endpoint=self.get_resource_content_func(engine, default=False), | ||
name=self.resource_name, | ||
response_model=str, | ||
**default_kwargs, | ||
) | ||
|
||
return router | ||
|
||
def get_resource_content_func(self, engine: Engine, default: bool): | ||
""" | ||
Returns a function to download the content from resources. | ||
This function returns a function (instead of being that function directly) because the | ||
docstring and the variables are dynamic, and used in Swagger. | ||
""" | ||
|
||
def get_resource_content(identifier: str, distribution_idx: int, default: bool = False): | ||
f"""Retrieve a distribution of the content for {self.resource_name} | ||
identified by its identifier.""" | ||
|
||
metadata: AIAsset = self.get_resource( | ||
engine=engine, identifier=identifier, schema="aiod", platform=None | ||
) # type: ignore | ||
|
||
distributions = metadata.distribution | ||
if not distributions: | ||
raise HTTPException( | ||
status_code=status.HTTP_404_NOT_FOUND, detail="Distribution not found." | ||
) | ||
elif default and (len(distributions) > 1): | ||
raise HTTPException( | ||
status_code=status.HTTP_409_CONFLICT, | ||
detail=( | ||
"Multiple distributions encountered. " | ||
"Use another endpoint indicating the distribution index `distribution_idx` " | ||
"at the end of the url for a especific distribution.", | ||
), | ||
) | ||
elif distribution_idx >= len(distributions): | ||
raise HTTPException( | ||
status_code=status.HTTP_400_BAD_REQUEST, | ||
detail="Distribution index out of range.", | ||
) | ||
|
||
try: | ||
url = distributions[distribution_idx].content_url | ||
encoding_format = distributions[distribution_idx].encoding_format | ||
filename = distributions[distribution_idx].name | ||
|
||
response = requests.get(url) | ||
content = response.content | ||
headers = { | ||
"Content-Disposition": ( | ||
"attachment; " f"filename={filename or url.split('/')[-1]}" | ||
), | ||
"Content-Type": f"{encoding_format or 'unknown'}", | ||
} | ||
return Response(content=content, headers=headers) | ||
|
||
except Exception as exc: | ||
raise _wrap_as_http_exception(exc) | ||
|
||
def get_resource_content_default(identifier: str): | ||
f"""Retrieve the first distribution (index 0 as default) of the content | ||
for a {self.resource_name} identified by its identifier.""" | ||
return get_resource_content(identifier=identifier, distribution_idx=0, default=True) | ||
|
||
if default: | ||
return get_resource_content_default | ||
|
||
return get_resource_content |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
row1,row2,row3 | ||
1,2,3 |
This file contains 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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
col1;col2;col3 | ||
1;2;3 |
Empty file.
Oops, something went wrong.
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.
"unknown" is not a valid mimetype.
https://stackoverflow.com/questions/1176022/unknown-file-type-mime suggests to remove the content-type in this case, that sounds like a good approach to me!