Skip to content

Commit ca57831

Browse files
authored
Merge pull request #21 from JigsawStack/check
Improve Request client
2 parents b3a737d + 4ae84a5 commit ca57831

19 files changed

+840
-425
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ jigsawstack.egg-info/
55

66
.env
77
build/
8-
dist/
8+
dist/
9+
/demo

jigsawstack/__init__.py

Lines changed: 82 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
from typing import Union
32
import os
43
from .audio import Audio
@@ -15,11 +14,13 @@
1514
from .geo import Geo
1615
from .prompt_engine import PromptEngine
1716
from .exceptions import JigsawStackError
17+
1818
# from .version import get_version
1919

20+
2021
class JigsawStack:
2122
audio: Audio
22-
vision : Vision
23+
vision: Vision
2324
prediction: Prediction
2425
text_to_sql: SQL
2526
file: Store
@@ -30,19 +31,26 @@ class JigsawStack:
3031
validate: Validate
3132
summary: Summary
3233
search: Search
33-
geo : Geo
34+
geo: Geo
3435
prompt_engine: PromptEngine
3536
api_key: str
3637
api_url: str
38+
disable_request_logging: bool
3739

38-
39-
def __init__(self, api_key: Union[str, None] = None, api_url: Union[str, None] = None) -> None:
40+
def __init__(
41+
self,
42+
api_key: Union[str, None] = None,
43+
api_url: Union[str, None] = None,
44+
disable_request_logging: Union[bool, None] = None,
45+
) -> None:
4046
if api_key is None:
4147
api_key = os.environ.get("JIGSAWSTACK_API_KEY")
42-
48+
4349
if api_key is None:
44-
raise ValueError("The api_key client option must be set either by passing api_key to the client or by setting the JIGSAWSTACK_API_KEY environment variable")
45-
50+
raise ValueError(
51+
"The api_key client option must be set either by passing api_key to the client or by setting the JIGSAWSTACK_API_KEY environment variable"
52+
)
53+
4654
if api_url is None:
4755
api_url = os.environ.get("JIGSAWSTACK_API_URL")
4856
if api_url is None:
@@ -51,20 +59,72 @@ def __init__(self, api_key: Union[str, None] = None, api_url: Union[str, None] =
5159
self.api_key = api_key
5260
self.api_url = api_url
5361

62+
self.audio = Audio(
63+
api_key=api_key,
64+
api_url=api_url,
65+
disable_request_logging=disable_request_logging,
66+
)
67+
self.web = Web(
68+
api_key=api_key,
69+
api_url=api_url,
70+
disable_request_logging=disable_request_logging,
71+
)
72+
self.sentiment = Sentiment(
73+
api_key=api_key,
74+
api_url=api_url,
75+
disable_request_logging=disable_request_logging,
76+
).analyze
77+
self.validate = Validate(
78+
api_key=api_key,
79+
api_url=api_url,
80+
disable_request_logging=disable_request_logging,
81+
)
82+
self.summary = Summary(
83+
api_key=api_key,
84+
api_url=api_url,
85+
disable_request_logging=disable_request_logging,
86+
).summarize
87+
self.vision = Vision(
88+
api_key=api_key,
89+
api_url=api_url,
90+
disable_request_logging=disable_request_logging,
91+
)
92+
self.prediction = Prediction(
93+
api_key=api_key,
94+
api_url=api_url,
95+
disable_request_logging=disable_request_logging,
96+
).predict
97+
self.text_to_sql = SQL(
98+
api_key=api_key,
99+
api_url=api_url,
100+
disable_request_logging=disable_request_logging,
101+
).text_to_sql
102+
self.store = Store(
103+
api_key=api_key,
104+
api_url=api_url,
105+
disable_request_logging=disable_request_logging,
106+
)
107+
self.kv = KV(
108+
api_key=api_key,
109+
api_url=api_url,
110+
disable_request_logging=disable_request_logging,
111+
)
112+
self.translate = Translate(
113+
api_key=api_key,
114+
api_url=api_url,
115+
disable_request_logging=disable_request_logging,
116+
).translate
117+
self.geo = Geo(
118+
api_key=api_key,
119+
api_url=api_url,
120+
disable_request_logging=disable_request_logging,
121+
)
122+
self.prompt_engine = PromptEngine(
123+
api_key=api_key,
124+
api_url=api_url,
125+
disable_request_logging=disable_request_logging,
126+
)
54127

55-
self.audio = Audio(api_key=api_key, api_url=api_url)
56-
self.web = Web(api_key=api_key, api_url=api_url)
57-
self.sentiment = Sentiment(api_key=api_key, api_url=api_url).analyze
58-
self.validate = Validate(api_key=api_key, api_url=api_url)
59-
self.summary = Summary(api_key=api_key, api_url=api_url).summarize
60-
self.vision = Vision(api_key=api_key, api_url=api_url)
61-
self.prediction = Prediction(api_key=api_key, api_url=api_url).predict
62-
self.text_to_sql = SQL(api_key=api_key, api_url=api_url).text_to_sql
63-
self.store = Store(api_key=api_key, api_url=api_url)
64-
self.kv = KV(api_key=api_key, api_url=api_url)
65-
self.translate = Translate(api_key=api_key, api_url=api_url).translate
66-
self.geo = Geo(api_key=api_key, api_url=api_url)
67-
self.prompt_engine = PromptEngine(api_key=api_key, api_url=api_url)
68128

69129
# Create a global instance of the Web class
70-
__all__ = ["JigsawStack", "Search", "JigsawStackError"]
130+
__all__ = ["JigsawStack", "Search", "JigsawStackError"]

jigsawstack/_config.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
1+
from typing import Union
2+
3+
14
class ClientConfig:
5+
base_url: str
6+
api_key: str
7+
disable_request_logging: Union[bool, None] = None
28

3-
base_url:str
4-
api_key:str
5-
6-
def __init__(self, api_key: str, api_url: str):
9+
def __init__(
10+
self,
11+
api_key: str,
12+
api_url: str,
13+
disable_request_logging: Union[bool, None] = None,
14+
):
715
self.api_key = api_key
8-
self.api_url = api_url
16+
self.api_url = api_url
17+
self.disable_request_logging = disable_request_logging

jigsawstack/audio.py

Lines changed: 47 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,81 @@
1-
from typing import Any, Dict, List, cast
1+
from typing import Any, Dict, List, cast, Union
22
from typing_extensions import NotRequired, TypedDict
3-
from .request import Request
3+
from .request import Request, RequestConfig
44
from ._config import ClientConfig
55
from typing import Any, Dict, List, cast
66
from typing_extensions import NotRequired, TypedDict
77

88

99
class TextToSpeechParams(TypedDict):
10-
text:str
10+
text: str
1111
accent: NotRequired[str]
1212
speaker_clone_url: NotRequired[str]
1313
speaker_clone_file_store_key: NotRequired[str]
1414

15+
1516
class TextToSpeechResponse(TypedDict):
16-
success:bool
17+
success: bool
1718
text: str
18-
chunks : List[object]
19+
chunks: List[object]
20+
1921

2022
class SpeechToTextParams(TypedDict):
21-
url:str
22-
file_store_key : NotRequired[str]
23-
language : NotRequired[str]
24-
translate : NotRequired[bool]
25-
by_speaker : NotRequired[bool]
26-
webhook_url : NotRequired[str]
23+
url: str
24+
file_store_key: NotRequired[str]
25+
language: NotRequired[str]
26+
translate: NotRequired[bool]
27+
by_speaker: NotRequired[bool]
28+
webhook_url: NotRequired[str]
29+
2730

2831
class SpeechToTextResponse(TypedDict):
29-
success:bool
30-
text : str
31-
chunks : List[object]
32+
success: bool
33+
text: str
34+
chunks: List[object]
3235

3336

3437
class Audio(ClientConfig):
38+
config: RequestConfig
39+
40+
def __init__(
41+
self,
42+
api_key: str,
43+
api_url: str,
44+
disable_request_logging: Union[bool, None] = False,
45+
):
46+
super().__init__(api_key, api_url, disable_request_logging)
47+
self.config = RequestConfig(
48+
api_url=api_url,
49+
api_key=api_key,
50+
disable_request_logging=disable_request_logging,
51+
)
52+
3553
def speech_to_text(self, params: SpeechToTextParams) -> SpeechToTextResponse:
3654
path = "/ai/transcribe"
3755
resp = Request(
38-
api_key=self.api_key,
39-
api_url=self.api_url,
40-
path=path, params=cast(Dict[Any, Any], params), verb="post"
56+
config=self.config,
57+
path=path,
58+
params=cast(Dict[Any, Any], params),
59+
verb="post",
4160
).perform_with_content()
4261
return resp
43-
62+
4463
def text_to_speech(self, params: TextToSpeechParams) -> TextToSpeechResponse:
4564
path = "/ai/tts"
4665
resp = Request(
47-
api_key=self.api_key,
48-
api_url=self.api_url,
49-
path=path, params=cast(Dict[Any, Any], params), verb="post"
66+
config=self.config,
67+
path=path,
68+
params=cast(Dict[Any, Any], params),
69+
verb="post",
5070
).perform_with_content()
5171
return resp
72+
5273
def speaker_voice_accents(self) -> TextToSpeechResponse:
5374
path = "/ai/tts"
5475
resp = Request(
55-
api_key=self.api_key,
56-
api_url=self.api_url,
57-
path=path, params={}, verb="get"
76+
config=self.config,
77+
path=path,
78+
params={},
79+
verb="get",
5880
).perform_with_content()
59-
return resp
81+
return resp

0 commit comments

Comments
 (0)