-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: save stability artifacts to DIAL file storage (#34)
- Loading branch information
Showing
16 changed files
with
155 additions
and
53 deletions.
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
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,70 @@ | ||
import base64 | ||
import hashlib | ||
import io | ||
from typing import TypedDict | ||
|
||
import aiohttp | ||
|
||
from aidial_adapter_bedrock.utils.log_config import bedrock_logger as log | ||
|
||
|
||
class FileMetadata(TypedDict): | ||
name: str | ||
type: str | ||
path: str | ||
contentLength: int | ||
contentType: str | ||
|
||
|
||
class FileStorage: | ||
base_url: str | ||
api_key: str | ||
|
||
def __init__(self, dial_url: str, base_dir: str, api_key: str): | ||
self.base_url = f"{dial_url}/v1/files/{base_dir}" | ||
self.api_key = api_key | ||
|
||
def auth_headers(self) -> dict[str, str]: | ||
return {"api-key": self.api_key} | ||
|
||
@staticmethod | ||
def to_form_data( | ||
filename: str, content_type: str, content: bytes | ||
) -> aiohttp.FormData: | ||
data = aiohttp.FormData() | ||
data.add_field( | ||
"file", | ||
io.BytesIO(content), | ||
filename=filename, | ||
content_type=content_type, | ||
) | ||
return data | ||
|
||
async def upload( | ||
self, filename: str, content_type: str, content: bytes | ||
) -> FileMetadata: | ||
async with aiohttp.ClientSession() as session: | ||
data = FileStorage.to_form_data(filename, content_type, content) | ||
async with session.post( | ||
self.base_url, | ||
data=data, | ||
headers=self.auth_headers(), | ||
) as response: | ||
response.raise_for_status() | ||
meta = await response.json() | ||
log.debug( | ||
f"Uploaded file: path={self.base_url}, file={filename}, metadata={meta}" | ||
) | ||
return meta | ||
|
||
|
||
def _hash_digest(string: str) -> str: | ||
return hashlib.sha256(string.encode()).hexdigest() | ||
|
||
|
||
async def upload_base64_file( | ||
storage: FileStorage, data: str, content_type: str | ||
) -> FileMetadata: | ||
filename = _hash_digest(data) | ||
content: bytes = base64.b64decode(data) | ||
return await storage.upload(filename, content_type, content) |
File renamed without changes.
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