Skip to content

feat/file-handling #44

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
merged 12 commits into from
May 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions jigsawstack/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def __init__(
api_key=api_key,
api_url=api_url,
disable_request_logging=disable_request_logging,
).translate
)

self.prompt_engine = PromptEngine(
api_key=api_key,
Expand Down Expand Up @@ -209,7 +209,7 @@ def __init__(
api_key=api_key,
api_url=api_url,
disable_request_logging=disable_request_logging,
).translate
)

self.prompt_engine = AsyncPromptEngine(
api_key=api_key,
Expand Down
65 changes: 58 additions & 7 deletions jigsawstack/audio.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from typing import Any, Dict, List, cast, Union, Optional
from typing import Any, Dict, List, cast, Union, Optional, overload
from typing_extensions import NotRequired, TypedDict
from .request import Request, RequestConfig
from .async_request import AsyncRequest, AsyncRequestConfig
from ._config import ClientConfig
from typing import Any, Dict, List, cast
from typing_extensions import NotRequired, TypedDict
from .custom_typing import SupportedAccents
from .helpers import build_path


class TextToSpeechParams(TypedDict):
Expand All @@ -29,6 +30,7 @@ class SpeechToTextParams(TypedDict):
by_speaker: NotRequired[bool]
webhook_url: NotRequired[str]
batch_size: NotRequired[int]
chunk_duration: NotRequired[int]


class ChunkParams(TypedDict):
Expand Down Expand Up @@ -63,16 +65,41 @@ def __init__(
disable_request_logging=disable_request_logging,
)

def speech_to_text(self, params: SpeechToTextParams) -> SpeechToTextResponse:
path = "/ai/transcribe"
@overload
def speech_to_text(self, params: SpeechToTextParams) -> SpeechToTextResponse: ...
@overload
def speech_to_text(self, file: bytes, options: Optional[SpeechToTextParams] = None) -> SpeechToTextResponse: ...

def speech_to_text(
self,
blob: Union[SpeechToTextParams, bytes],
options: Optional[SpeechToTextParams] = None,
) -> SpeechToTextResponse:
if isinstance(blob, dict): # If params is provided as a dict, we assume it's the first argument
resp = Request(
config=self.config,
path="/ai/transcribe",
params=cast(Dict[Any, Any], blob),
verb="post",
).perform_with_content()
return resp

options = options or {}
path = build_path(base_path="/ai/transcribe", params=options)
content_type = options.get("content_type", "application/octet-stream")
headers = {"Content-Type": content_type}

resp = Request(
config=self.config,
path=path,
params=cast(Dict[Any, Any], params),
params=options,
data=blob,
headers=headers,
verb="post",
).perform_with_content()
return resp


def text_to_speech(self, params: TextToSpeechParams) -> TextToSpeechResponse:
path = "/ai/tts"
resp = Request(
Expand Down Expand Up @@ -110,12 +137,36 @@ def __init__(
disable_request_logging=disable_request_logging,
)

async def speech_to_text(self, params: SpeechToTextParams) -> SpeechToTextResponse:
path = "/ai/transcribe"
@overload
async def speech_to_text(self, params: SpeechToTextParams) -> SpeechToTextResponse: ...
@overload
async def speech_to_text(self, file: bytes, options: Optional[SpeechToTextParams] = None) -> SpeechToTextResponse: ...

async def speech_to_text(
self,
blob: Union[SpeechToTextParams, bytes],
options: Optional[SpeechToTextParams] = None,
) -> SpeechToTextResponse:
if isinstance(blob, dict):
resp = await AsyncRequest(
config=self.config,
path="/ai/transcribe",
params=cast(Dict[Any, Any], blob),
verb="post",
).perform_with_content()
return resp

options = options or {}
path = build_path(base_path="/ai/transcribe", params=options)
content_type = options.get("content_type", "application/octet-stream")
headers = {"Content-Type": content_type}

resp = await AsyncRequest(
config=self.config,
path=path,
params=cast(Dict[Any, Any], params),
params=options,
data=blob,
headers=headers,
verb="post",
).perform_with_content()
return resp
Expand Down
65 changes: 58 additions & 7 deletions jigsawstack/embedding.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from typing import Any, Dict, List, Union, cast, Literal
from typing import Any, Dict, List, Union, cast, Literal, overload
from typing_extensions import NotRequired, TypedDict
from .request import Request, RequestConfig
from .async_request import AsyncRequest
from typing import List, Union
from ._config import ClientConfig
from .helpers import build_path


class EmbeddingParams(TypedDict):
Expand Down Expand Up @@ -38,12 +39,37 @@ def __init__(
disable_request_logging=disable_request_logging,
)

def execute(self, params: EmbeddingParams) -> EmbeddingResponse:
path = "/embedding"
@overload
def execute(self, params: EmbeddingParams) -> EmbeddingResponse: ...
@overload
def execute(self, file: bytes, options: EmbeddingParams = None) -> EmbeddingResponse: ...

def execute(
self,
blob: Union[EmbeddingParams, bytes],
options: EmbeddingParams = None,
) -> EmbeddingResponse:
path="/embedding"
if isinstance(blob, dict):
resp = Request(
config=self.config,
path=path,
params=cast(Dict[Any, Any], blob),
verb="post",
).perform_with_content()
return resp

options = options or {}
path = build_path(base_path=path, params=options)
content_type = options.get("content_type", "application/octet-stream")
_headers = {"Content-Type": content_type}

resp = Request(
config=self.config,
path=path,
params=cast(Dict[Any, Any], params),
params=options,
data=blob,
headers=_headers,
verb="post",
).perform_with_content()
return resp
Expand All @@ -66,12 +92,37 @@ def __init__(
disable_request_logging=disable_request_logging,
)

async def execute(self, params: EmbeddingParams) -> EmbeddingResponse:
path = "/embedding"
@overload
async def execute(self, params: EmbeddingParams) -> EmbeddingResponse: ...
@overload
async def execute(self, file: bytes, options: EmbeddingParams = None) -> EmbeddingResponse: ...

async def execute(
self,
blob: Union[EmbeddingParams, bytes],
options: EmbeddingParams = None,
) -> EmbeddingResponse:
path="/embedding"
if isinstance(blob, dict):
resp = await AsyncRequest(
config=self.config,
path=path,
params=cast(Dict[Any, Any], blob),
verb="post",
).perform_with_content()
return resp

options = options or {}
path = build_path(base_path=path, params=options)
content_type = options.get("content_type", "application/octet-stream")
_headers = {"Content-Type": content_type}

resp = await AsyncRequest(
config=self.config,
path=path,
params=cast(Dict[Any, Any], params),
params=options,
data=blob,
headers=_headers,
verb="post",
).perform_with_content()
return resp
2 changes: 1 addition & 1 deletion jigsawstack/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def build_path(base_path: str, params: Optional[Dict[str, Union[str, int, bool]]
return base_path

#remove None values from the parameters
filtered_params = {k: v for k, v in params.items() if v is not None}
filtered_params = { k: str(v).lower() if isinstance(v, bool) else v for k, v in params.items() if v is not None}

#encode the parameters
return f"{base_path}?{urlencode(filtered_params)}" if filtered_params else base_path
Expand Down
12 changes: 10 additions & 2 deletions jigsawstack/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ class FileUploadParams(TypedDict):
overwrite: NotRequired[bool]
key: NotRequired[str]
content_type: NotRequired[str]
temp_public_url: NotRequired[bool]

class FileUploadResponse(TypedDict):
key: str
url: str
size: int
temp_public_url: NotRequired[str] # Optional, only if temp_public_url is set to True in params


class Store(ClientConfig):

Expand All @@ -30,7 +38,7 @@ def __init__(
disable_request_logging=disable_request_logging,
)

def upload(self, file: bytes, options: Union[FileUploadParams, None] = None) -> Any:
def upload(self, file: bytes, options: Union[FileUploadParams, None] = None) -> FileUploadResponse:
if options is None:
options = {}

Expand Down Expand Up @@ -87,7 +95,7 @@ def __init__(
)


async def upload(self, file: bytes, options: Union[FileUploadParams, None] = None) -> Any:
async def upload(self, file: bytes, options: Union[FileUploadParams, None] = None) -> FileUploadResponse:
if options is None:
options = {}

Expand Down
Loading