Skip to content
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
8 changes: 6 additions & 2 deletions jigsawstack/vision.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,17 +154,21 @@ class VOCRParams(TypedDict):
"""
High fidelity word-level bounding boxes within complex documents. Default: false.
"""
return_bounds: NotRequired[bool]
"""
Include line and word level bounding box coordinates. When false, the coordinates are omitted but the text and confidence are still returned. Default: true.
"""


class Word(TypedDict):
text: str
bounds: BoundingBox
bounds: NotRequired[BoundingBox] # omitted when return_bounds is false
confidence: float


class Line(TypedDict):
text: str
bounds: BoundingBox
bounds: NotRequired[BoundingBox] # omitted when return_bounds is false
average_confidence: float
words: List[Word]

Expand Down
32 changes: 8 additions & 24 deletions tests/test_audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,26 +131,20 @@
class TestAudioSync:
"""Test synchronous audio speech-to-text methods"""

@pytest.mark.parametrize(
"test_case", TEST_CASES, ids=[tc["name"] for tc in TEST_CASES]
)
@pytest.mark.parametrize("test_case", TEST_CASES, ids=[tc["name"] for tc in TEST_CASES])
def test_speech_to_text(self, test_case):
"""Test synchronous speech-to-text with various inputs"""
try:
if test_case.get("blob"):
# Download audio content
blob_content = requests.get(test_case["blob"]).content
result = jigsaw.audio.speech_to_text(
blob_content, test_case.get("options", {})
)
result = jigsaw.audio.speech_to_text(blob_content, test_case.get("options", {}))
else:
# Use params directly
result = jigsaw.audio.speech_to_text(test_case["params"])
# Verify response structure
assert result["success"]
assert result.get("text", None) is not None and isinstance(
result["text"], str
)
assert result.get("text", None) is not None and isinstance(result["text"], str)

# Check for chunks
if result.get("chunks", None):
Expand All @@ -172,9 +166,7 @@ def test_speech_to_text_webhook(self, test_case):
if test_case.get("blob"):
# Download audio content
blob_content = requests.get(test_case["blob"]).content
result = jigsaw.audio.speech_to_text(
blob_content, test_case.get("options", {})
)
result = jigsaw.audio.speech_to_text(blob_content, test_case.get("options", {}))
else:
# Use params directly
result = jigsaw.audio.speech_to_text(test_case["params"])
Expand All @@ -189,9 +181,7 @@ def test_speech_to_text_webhook(self, test_case):
class TestAudioAsync:
"""Test asynchronous audio speech-to-text methods"""

@pytest.mark.parametrize(
"test_case", TEST_CASES, ids=[tc["name"] for tc in TEST_CASES]
)
@pytest.mark.parametrize("test_case", TEST_CASES, ids=[tc["name"] for tc in TEST_CASES])
@pytest.mark.asyncio
async def test_speech_to_text_async(self, test_case):
"""Test asynchronous speech-to-text with various inputs"""
Expand All @@ -208,9 +198,7 @@ async def test_speech_to_text_async(self, test_case):

# Verify response structure
assert result["success"]
assert result.get("text", None) is not None and isinstance(
result["text"], str
)
assert result.get("text", None) is not None and isinstance(result["text"], str)

# Check for chunks
if result.get("chunks", None):
Expand All @@ -220,9 +208,7 @@ async def test_speech_to_text_async(self, test_case):
if result.get("speakers", None):
assert isinstance(result["speakers"], list)
except JigsawStackError as e:
pytest.fail(
f"Unexpected JigsawStackError in async {test_case['name']}: {e}"
)
pytest.fail(f"Unexpected JigsawStackError in async {test_case['name']}: {e}")

@pytest.mark.parametrize(
"test_case", WEBHOOK_TEST_CASES, ids=[tc["name"] for tc in WEBHOOK_TEST_CASES]
Expand All @@ -248,6 +234,4 @@ async def test_speech_to_text_webhook_async(self, test_case):

except JigsawStackError as e:
# Webhook URLs might fail if invalid
print(
f"Expected possible error for async webhook test {test_case['name']}: {e}"
)
print(f"Expected possible error for async webhook test {test_case['name']}: {e}")
8 changes: 4 additions & 4 deletions tests/test_embedding.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@
headers={"x-jigsaw-skip-cache": "true"},
)

SAMPLE_TEXT = "The quick brown fox jumps over the lazy dog. This is a sample text for embedding generation."
SAMPLE_TEXT = (
"The quick brown fox jumps over the lazy dog. This is a sample text for embedding generation."
)
SAMPLE_IMAGE_URL = "https://images.unsplash.com/photo-1542931287-023b922fa89b?q=80&w=2574&auto=format&fit=crop&ixlib=rb-4.1.0&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D"
SAMPLE_AUDIO_URL = "https://jigsawstack.com/preview/stt-example.wav"
SAMPLE_PDF_URL = (
"https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf"
)
SAMPLE_PDF_URL = "https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf"

# Test cases for Embedding V2
EMBEDDING_V2_TEST_CASES = [
Expand Down
8 changes: 2 additions & 6 deletions tests/test_file_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,7 @@ class TestFileStoreAsync:
async def test_file_upload_async(self, test_case):
"""Test asynchronous file upload with various options"""
try:
result = await async_jigsaw.store.upload(
test_case["file"], test_case["options"]
)
result = await async_jigsaw.store.upload(test_case["file"], test_case["options"])

print(f"Async upload test {test_case['name']}: {result}")
assert result.get("key") is not None
Expand All @@ -147,9 +145,7 @@ async def test_file_upload_async(self, test_case):
self.uploaded_keys.append(result["key"])

except JigsawStackError as e:
pytest.fail(
f"Unexpected JigsawStackError in async {test_case['name']}: {e}"
)
pytest.fail(f"Unexpected JigsawStackError in async {test_case['name']}: {e}")

@pytest.mark.asyncio
async def test_file_get_async(self):
Expand Down
8 changes: 4 additions & 4 deletions tests/test_object_detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
headers={"x-jigsaw-skip-cache": "true"},
)

IMAGE_URL = "https://rogilvkqloanxtvjfrkm.supabase.co/storage/v1/object/public/demo/Collabo%201080x842.jpg"
IMAGE_URL = (
"https://rogilvkqloanxtvjfrkm.supabase.co/storage/v1/object/public/demo/Collabo%201080x842.jpg"
)

TEST_CASES = [
{
Expand Down Expand Up @@ -114,9 +116,7 @@ def test_object_detection(self, test_case):
if test_case.get("blob"):
# Download blob content
blob_content = requests.get(test_case["blob"]).content
result = jigsaw.vision.object_detection(
blob_content, test_case.get("options", {})
)
result = jigsaw.vision.object_detection(blob_content, test_case.get("options", {}))
else:
# Use params directly
result = jigsaw.vision.object_detection(test_case["params"])
Expand Down
8 changes: 2 additions & 6 deletions tests/test_prediction.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ def generate_dates(start_date, num_days):
{
"name": "seasonal_pattern",
"params": {
"dataset": [
{"date": dates[i], "value": 100 + (50 * (i % 7))} for i in range(21)
],
"dataset": [{"date": dates[i], "value": 100 + (50 * (i % 7))} for i in range(21)],
"steps": 7,
},
},
Expand All @@ -75,9 +73,7 @@ def generate_dates(start_date, num_days):
{
"name": "large_dataset_prediction",
"params": {
"dataset": [
{"date": dates[i], "value": 1000 + (i * 20)} for i in range(30)
],
"dataset": [{"date": dates[i], "value": 1000 + (i * 20)} for i in range(30)],
"steps": 10,
},
},
Expand Down
8 changes: 2 additions & 6 deletions tests/test_translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,7 @@ def test_translate_text(self, test_case):
# Check if the response structure matches the input
if isinstance(test_case["params"]["text"], list):
assert isinstance(result["translated_text"], list)
assert len(result["translated_text"]) == len(
test_case["params"]["text"]
)
assert len(result["translated_text"]) == len(test_case["params"]["text"])
else:
assert isinstance(result["translated_text"], str)

Expand All @@ -123,9 +121,7 @@ async def test_translate_text_async(self, test_case):
# Check if the response structure matches the input
if isinstance(test_case["params"]["text"], list):
assert isinstance(result["translated_text"], list)
assert len(result["translated_text"]) == len(
test_case["params"]["text"]
)
assert len(result["translated_text"]) == len(test_case["params"]["text"])
else:
assert isinstance(result["translated_text"], str)

Expand Down
8 changes: 2 additions & 6 deletions tests/test_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@
)

# Sample URLs for NSFW testing
SAFE_IMAGE_URL = (
"https://images.unsplash.com/photo-1506905925346-21bda4d32df4?q=80&w=2070"
)
SAFE_IMAGE_URL = "https://images.unsplash.com/photo-1506905925346-21bda4d32df4?q=80&w=2070"
POTENTIALLY_NSFW_URL = "https://images.unsplash.com/photo-1512310604669-443f26c35f52?q=80&w=868&auto=format&fit=crop&ixlib=rb-4.1.0&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D"

# Profanity Test Cases
Expand Down Expand Up @@ -240,9 +238,7 @@ async def test_nsfw_check_blob_async(self, test_case):
try:
# Download blob content
blob_content = requests.get(test_case["blob_url"]).content
result = await async_jigsaw.validate.nsfw(
blob_content, test_case["options"]
)
result = await async_jigsaw.validate.nsfw(blob_content, test_case["options"])

assert result["success"]
assert "nsfw" in result
Expand Down
36 changes: 12 additions & 24 deletions tests/test_vocr.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,7 @@ def test_vocr(self, test_case):
except JigsawStackError as e:
pytest.fail(f"Unexpected JigsawStackError in {test_case['name']}: {e}")

@pytest.mark.parametrize(
"test_case", pdf_test_cases, ids=[tc["name"] for tc in pdf_test_cases]
)
@pytest.mark.parametrize("test_case", pdf_test_cases, ids=[tc["name"] for tc in pdf_test_cases])
def test_vocr_pdf(self, test_case):
"""Test synchronous VOCR with PDF inputs"""
try:
Expand All @@ -177,15 +175,13 @@ def test_vocr_pdf(self, test_case):
assert "context" in result
assert "total_pages" in result

if test_case.get("params", {}).get("page_range") or test_case.get(
"options", {}
).get("page_range"):
if test_case.get("params", {}).get("page_range") or test_case.get("options", {}).get(
"page_range"
):
assert "page_range" in result
assert isinstance(result["page_range"], list)

logger.info(
f"Test {test_case['name']}: total_pages={result.get('total_pages')}"
)
logger.info(f"Test {test_case['name']}: total_pages={result.get('total_pages')}")

except JigsawStackError as e:
pytest.fail(f"Unexpected JigsawStackError in {test_case['name']}: {e}")
Expand All @@ -207,9 +203,7 @@ async def test_vocr_async(self, test_case):
if test_case.get("blob"):
# Download blob content
blob_content = requests.get(test_case["blob"]).content
result = await async_jigsaw.vision.vocr(
blob_content, test_case.get("options", {})
)
result = await async_jigsaw.vision.vocr(blob_content, test_case.get("options", {}))
else:
# Use params directly
result = await async_jigsaw.vision.vocr(test_case["params"])
Expand All @@ -236,19 +230,15 @@ async def test_vocr_async(self, test_case):
except JigsawStackError as e:
pytest.fail(f"Unexpected JigsawStackError in {test_case['name']}: {e}")

@pytest.mark.parametrize(
"test_case", pdf_test_cases, ids=[tc["name"] for tc in pdf_test_cases]
)
@pytest.mark.parametrize("test_case", pdf_test_cases, ids=[tc["name"] for tc in pdf_test_cases])
@pytest.mark.asyncio
async def test_vocr_pdf_async(self, test_case):
"""Test asynchronous VOCR with PDF inputs"""
try:
if test_case.get("blob"):
# Download blob content
blob_content = requests.get(test_case["blob"]).content
result = await async_jigsaw.vision.vocr(
blob_content, test_case.get("options", {})
)
result = await async_jigsaw.vision.vocr(blob_content, test_case.get("options", {}))
else:
# Use params directly
result = await async_jigsaw.vision.vocr(test_case["params"])
Expand All @@ -262,15 +252,13 @@ async def test_vocr_pdf_async(self, test_case):
assert "total_pages" in result # PDF specific

# Check if page_range is in response when requested
if test_case.get("params", {}).get("page_range") or test_case.get(
"options", {}
).get("page_range"):
if test_case.get("params", {}).get("page_range") or test_case.get("options", {}).get(
"page_range"
):
assert "page_range" in result
assert isinstance(result["page_range"], list)

logger.info(
f"Test {test_case['name']}: total_pages={result.get('total_pages')}"
)
logger.info(f"Test {test_case['name']}: total_pages={result.get('total_pages')}")

except JigsawStackError as e:
pytest.fail(f"Unexpected JigsawStackError in {test_case['name']}: {e}")
Loading