Skip to content

Commit b8bbf70

Browse files
chore: formatting.
1 parent 306eeb5 commit b8bbf70

File tree

8 files changed

+57
-83
lines changed

8 files changed

+57
-83
lines changed

jigsawstack/async_request.py

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -184,11 +184,11 @@ def __get_headers(self) -> Dict[str, str]:
184184
h["x-jigsaw-no-request-log"] = "true"
185185

186186
_headers = h.copy()
187-
188-
#don't override Content-Type if using multipart
187+
188+
# don't override Content-Type if using multipart
189189
if self.files and "Content-Type" in self.headers:
190190
self.headers.pop("Content-Type")
191-
191+
192192
_headers.update(self.headers)
193193

194194
return _headers
@@ -251,38 +251,29 @@ async def make_request(
251251
_form_data = None
252252

253253
if verb.lower() in ["get", "delete"]:
254-
#convert params for URL encoding if needed
254+
# convert params for URL encoding if needed
255255
_params = self.__convert_params(params)
256256
elif files:
257257
# for multipart requests - matches request.py behavior
258258
_form_data = aiohttp.FormData()
259-
259+
260260
# add file(s) to form data
261261
for field_name, file_data in files.items():
262262
if isinstance(file_data, bytes):
263263
# just pass the blob without filename
264264
_form_data.add_field(
265-
field_name,
266-
BytesIO(file_data),
267-
content_type="application/octet-stream"
265+
field_name, BytesIO(file_data), content_type="application/octet-stream"
268266
)
269267
elif isinstance(file_data, tuple):
270268
# if tuple format (filename, data, content_type)
271269
filename, content, content_type = file_data
272270
_form_data.add_field(
273-
field_name,
274-
content,
275-
filename=filename,
276-
content_type=content_type
271+
field_name, content, filename=filename, content_type=content_type
277272
)
278-
273+
279274
# add params as 'body' field in multipart form (JSON stringified)
280275
if params and isinstance(params, dict):
281-
_form_data.add_field(
282-
"body",
283-
json.dumps(params),
284-
content_type="application/json"
285-
)
276+
_form_data.add_field("body", json.dumps(params), content_type="application/json")
286277
elif data:
287278
# for binary data without multipart
288279
_data = data

jigsawstack/audio.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ def speech_to_text(
8080
) -> Union[SpeechToTextResponse, SpeechToTextWebhookResponse]:
8181
options = options or {}
8282
path = "/ai/transcribe"
83-
params= options or {}
84-
if isinstance(blob, dict):
83+
params = options or {}
84+
if isinstance(blob, dict):
8585
# URL or file_store_key based request
8686
resp = Request(
8787
config=self.config,
@@ -143,7 +143,7 @@ async def speech_to_text(
143143
verb="post",
144144
).perform_with_content()
145145
return resp
146-
146+
147147
files = {"file": blob}
148148
resp = await AsyncRequest(
149149
config=self.config,

jigsawstack/embedding.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ def execute(
6666
).perform_with_content()
6767
return resp
6868

69-
7069
files = {"file": blob}
7170
resp = Request(
7271
config=self.config,

jigsawstack/request.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ def __get_headers(self) -> Dict[Any, Any]:
158158
"Accept": "application/json",
159159
"x-api-key": f"{self.api_key}",
160160
}
161-
161+
162162
# Only add Content-Type if not using multipart (files)
163163
if not self.files and not self.data:
164164
h["Content-Type"] = "application/json"
@@ -167,11 +167,11 @@ def __get_headers(self) -> Dict[Any, Any]:
167167
h["x-jigsaw-no-request-log"] = "true"
168168

169169
_headers = h.copy()
170-
170+
171171
# Don't override Content-Type if using multipart
172172
if self.files and "Content-Type" in self.headers:
173173
self.headers.pop("Content-Type")
174-
174+
175175
_headers.update(self.headers)
176176

177177
return _headers

jigsawstack/translate.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@ def image(
108108
).perform_with_content()
109109
return resp
110110

111-
112111
files = {"file": blob}
113112
resp = Request(
114113
config=self.config,

jigsawstack/validate.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ def nsfw(
111111
verb="post",
112112
).perform_with_content()
113113
return resp
114-
114+
115115
files = {"file": blob}
116116
resp = Request(
117117
config=self.config,
@@ -198,7 +198,7 @@ async def nsfw(
198198
verb="post",
199199
).perform_with_content()
200200
return resp
201-
201+
202202
files = {"file": blob}
203203
resp = await AsyncRequest(
204204
config=self.config,

jigsawstack/vision.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ class OCRResponse(BaseResponse):
160160
tags: List[str]
161161
has_text: bool
162162
sections: List[object]
163-
total_pages: Optional[int]
163+
total_pages: Optional[int]
164164
page_range: Optional[
165165
List[int]
166166
] # Only available if page_range is set in the request parameters.
@@ -205,7 +205,7 @@ def vocr(
205205
).perform_with_content()
206206
return resp
207207

208-
files ={"file": blob}
208+
files = {"file": blob}
209209
resp = Request(
210210
config=self.config,
211211
path=path,

tests/test_vocr.py

Lines changed: 38 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@
1313
logging.basicConfig(level=logging.INFO)
1414
logger = logging.getLogger(__name__)
1515

16-
jigsaw = jigsawstack.JigsawStack(api_url="http://localhost:3000/api/", api_key=os.getenv("JIGSAWSTACK_API_KEY"))
17-
async_jigsaw = jigsawstack.AsyncJigsawStack(api_url="http://localhost:3000/api/", api_key=os.getenv("JIGSAWSTACK_API_KEY"))
16+
jigsaw = jigsawstack.JigsawStack(
17+
api_url="http://localhost:3000/api/", api_key=os.getenv("JIGSAWSTACK_API_KEY")
18+
)
19+
async_jigsaw = jigsawstack.AsyncJigsawStack(
20+
api_url="http://localhost:3000/api/", api_key=os.getenv("JIGSAWSTACK_API_KEY")
21+
)
1822

1923
IMAGE_URL = "https://jigsawstack.com/preview/vocr-example.jpg"
2024

@@ -46,7 +50,7 @@
4650
"prompt": [
4751
"What is the main heading?",
4852
"Extract any dates mentioned",
49-
"What are the key points?"
53+
"What are the key points?",
5054
]
5155
},
5256
},
@@ -57,25 +61,19 @@
5761
"prompt": {
5862
"title": "Extract the main title",
5963
"content": "What is the main content?",
60-
"metadata": "Extract any metadata or additional information"
64+
"metadata": "Extract any metadata or additional information",
6165
}
6266
},
6367
},
6468
{
6569
"name": "url_with_string_prompt",
66-
"params": {
67-
"url": IMAGE_URL,
68-
"prompt": "Summarize the text content"
69-
},
70+
"params": {"url": IMAGE_URL, "prompt": "Summarize the text content"},
7071
"blob": None,
7172
"options": None,
7273
},
7374
{
7475
"name": "url_with_list_prompt",
75-
"params": {
76-
"url": IMAGE_URL,
77-
"prompt": ["Extract headers", "Extract body text"]
78-
},
76+
"params": {"url": IMAGE_URL, "prompt": ["Extract headers", "Extract body text"]},
7977
"blob": None,
8078
"options": None,
8179
},
@@ -85,31 +83,20 @@
8583
PDF_TEST_CASES = [
8684
{
8785
"name": "pdf_with_page_range",
88-
"params": {
89-
"url": PDF_URL,
90-
"page_range": [1, 3],
91-
"prompt": "Extract text from these pages"
92-
},
86+
"params": {"url": PDF_URL, "page_range": [1, 3], "prompt": "Extract text from these pages"},
9387
"blob": None,
9488
"options": None,
9589
},
9690
{
9791
"name": "pdf_single_page",
98-
"params": {
99-
"url": PDF_URL,
100-
"page_range": [1, 1],
101-
"prompt": "What is on the first page?"
102-
},
92+
"params": {"url": PDF_URL, "page_range": [1, 1], "prompt": "What is on the first page?"},
10393
"blob": None,
10494
"options": None,
10595
},
10696
{
10797
"name": "pdf_blob_with_page_range",
10898
"blob": PDF_URL,
109-
"options": {
110-
"page_range": [1, 3],
111-
"prompt": "what is this about?"
112-
},
99+
"options": {"page_range": [1, 3], "prompt": "what is this about?"},
113100
},
114101
]
115102

@@ -135,7 +122,7 @@ def test_vocr(self, test_case):
135122
result = jigsaw.vision.vocr(test_case["params"])
136123

137124
print(f"Test {test_case['name']}: Success={result.get('success')}")
138-
125+
139126
# Verify response structure
140127
assert result["success"] is True
141128
if "prompt" in (test_case.get("params") or {}):
@@ -147,13 +134,11 @@ def test_vocr(self, test_case):
147134
assert isinstance(result["tags"], list)
148135
assert "sections" in result
149136
assert isinstance(result["sections"], list)
150-
137+
151138
except JigsawStackError as e:
152139
pytest.fail(f"Unexpected JigsawStackError in {test_case['name']}: {e}")
153140

154-
@pytest.mark.parametrize(
155-
"test_case", pdf_test_cases, ids=[tc["name"] for tc in pdf_test_cases]
156-
)
141+
@pytest.mark.parametrize("test_case", pdf_test_cases, ids=[tc["name"] for tc in pdf_test_cases])
157142
def test_vocr_pdf(self, test_case):
158143
"""Test synchronous VOCR with PDF inputs"""
159144
try:
@@ -164,19 +149,21 @@ def test_vocr_pdf(self, test_case):
164149
else:
165150
# Use params directly
166151
result = jigsaw.vision.vocr(test_case["params"])
167-
152+
168153
# Verify response structure
169154
assert result["success"] is True
170155
if "prompt" in (test_case.get("params") or {}):
171156
assert "context" in result
172157
assert "total_pages" in result
173-
174-
if test_case.get("params", {}).get("page_range") or test_case.get("options", {}).get("page_range"):
158+
159+
if test_case.get("params", {}).get("page_range") or test_case.get("options", {}).get(
160+
"page_range"
161+
):
175162
assert "page_range" in result
176163
assert isinstance(result["page_range"], list)
177164

178165
logger.info(f"Test {test_case['name']}: total_pages={result.get('total_pages')}")
179-
166+
180167
except JigsawStackError as e:
181168
pytest.fail(f"Unexpected JigsawStackError in {test_case['name']}: {e}")
182169

@@ -197,15 +184,13 @@ async def test_vocr_async(self, test_case):
197184
if test_case.get("blob"):
198185
# Download blob content
199186
blob_content = requests.get(test_case["blob"]).content
200-
result = await async_jigsaw.vision.vocr(
201-
blob_content, test_case.get("options", {})
202-
)
187+
result = await async_jigsaw.vision.vocr(blob_content, test_case.get("options", {}))
203188
else:
204189
# Use params directly
205190
result = await async_jigsaw.vision.vocr(test_case["params"])
206191

207192
print(f"Test {test_case['name']}: Success={result.get('success')}")
208-
193+
209194
# Verify response structure
210195
assert result["success"] is True
211196
if "prompt" in (test_case.get("params") or {}):
@@ -217,44 +202,44 @@ async def test_vocr_async(self, test_case):
217202
assert isinstance(result["tags"], list)
218203
assert "sections" in result
219204
assert isinstance(result["sections"], list)
220-
205+
221206
# Log some details
222-
logger.info(f"Test {test_case['name']}: has_text={result['has_text']}, tags={result['tags'][:3] if result['tags'] else []}")
223-
207+
logger.info(
208+
f"Test {test_case['name']}: has_text={result['has_text']}, tags={result['tags'][:3] if result['tags'] else []}"
209+
)
210+
224211
except JigsawStackError as e:
225212
pytest.fail(f"Unexpected JigsawStackError in {test_case['name']}: {e}")
226213

227-
@pytest.mark.parametrize(
228-
"test_case", pdf_test_cases, ids=[tc["name"] for tc in pdf_test_cases]
229-
)
214+
@pytest.mark.parametrize("test_case", pdf_test_cases, ids=[tc["name"] for tc in pdf_test_cases])
230215
@pytest.mark.asyncio
231216
async def test_vocr_pdf_async(self, test_case):
232217
"""Test asynchronous VOCR with PDF inputs"""
233218
try:
234219
if test_case.get("blob"):
235220
# Download blob content
236221
blob_content = requests.get(test_case["blob"]).content
237-
result = await async_jigsaw.vision.vocr(
238-
blob_content, test_case.get("options", {})
239-
)
222+
result = await async_jigsaw.vision.vocr(blob_content, test_case.get("options", {}))
240223
else:
241224
# Use params directly
242225
result = await async_jigsaw.vision.vocr(test_case["params"])
243226

244227
print(f"Test {test_case['name']}: Success={result.get('success')}")
245-
228+
246229
# Verify response structure
247230
assert result["success"] is True
248231
if "prompt" in (test_case.get("params") or {}):
249232
assert "context" in result
250233
assert "total_pages" in result # PDF specific
251-
234+
252235
# Check if page_range is in response when requested
253-
if test_case.get("params", {}).get("page_range") or test_case.get("options", {}).get("page_range"):
236+
if test_case.get("params", {}).get("page_range") or test_case.get("options", {}).get(
237+
"page_range"
238+
):
254239
assert "page_range" in result
255240
assert isinstance(result["page_range"], list)
256241

257242
logger.info(f"Test {test_case['name']}: total_pages={result.get('total_pages')}")
258-
243+
259244
except JigsawStackError as e:
260-
pytest.fail(f"Unexpected JigsawStackError in {test_case['name']}: {e}")
245+
pytest.fail(f"Unexpected JigsawStackError in {test_case['name']}: {e}")

0 commit comments

Comments
 (0)