Skip to content

Commit 4f429d7

Browse files
ploeberjhazenaai
authored andcommitted
fix(python/sdk): Fix README for sentiment analysis example (#2472)
GitOrigin-RevId: c2d97c625499679492c8d3c9083a39bc74211c83
1 parent d12a626 commit 4f429d7

11 files changed

+178
-486
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ for sentiment_result in transcript.sentiment_analysis:
460460
print(sentiment_result.text)
461461
print(sentiment_result.sentiment) # POSITIVE, NEUTRAL, or NEGATIVE
462462
print(sentiment_result.confidence)
463-
print(f"Timestamp: {sentiment_result.timestamp.start} - {sentiment_result.timestamp.end}")
463+
print(f"Timestamp: {sentiment_result.start} - {sentiment_result.end}")
464464
```
465465

466466
If `speaker_labels` is also enabled, then each sentiment analysis result will also include a `speaker` field.

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
setup(
99
name="assemblyai",
10-
version="0.19.0",
10+
version="0.18.0",
1111
description="AssemblyAI Python SDK",
1212
author="AssemblyAI",
1313
author_email="[email protected]",

tests/unit/test_auto_chapters.py

Lines changed: 4 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
import json
2-
from typing import Any, Dict, Tuple
3-
41
import factory
5-
import httpx
62
import pytest
73
from pytest_httpx import HTTPXMock
84

5+
import tests.unit.unit_test_utils as unit_test_utils
96
import assemblyai as aai
10-
from assemblyai.api import ENDPOINT_TRANSCRIPT
117
from tests.unit import factories
128

139
aai.settings.api_key = "test"
@@ -17,65 +13,14 @@ class AutoChaptersResponseFactory(factories.TranscriptCompletedResponseFactory):
1713
chapters = factory.List([factory.SubFactory(factories.ChapterFactory)])
1814

1915

20-
def __submit_mock_request(
21-
httpx_mock: HTTPXMock,
22-
mock_response: Dict[str, Any],
23-
config: aai.TranscriptionConfig,
24-
) -> Tuple[Dict[str, Any], aai.Transcript]:
25-
"""
26-
Helper function to abstract mock transcriber calls with given `TranscriptionConfig`,
27-
and perform some common assertions.
28-
"""
29-
30-
mock_transcript_id = mock_response.get("id", "mock_id")
31-
32-
# Mock initial submission response (transcript is processing)
33-
mock_processing_response = factories.generate_dict_factory(
34-
factories.TranscriptProcessingResponseFactory
35-
)()
36-
37-
httpx_mock.add_response(
38-
url=f"{aai.settings.base_url}{ENDPOINT_TRANSCRIPT}",
39-
status_code=httpx.codes.OK,
40-
method="POST",
41-
json={
42-
**mock_processing_response,
43-
"id": mock_transcript_id, # inject ID from main mock response
44-
},
45-
)
46-
47-
# Mock polling-for-completeness response, with completed transcript
48-
httpx_mock.add_response(
49-
url=f"{aai.settings.base_url}{ENDPOINT_TRANSCRIPT}/{mock_transcript_id}",
50-
status_code=httpx.codes.OK,
51-
method="GET",
52-
json=mock_response,
53-
)
54-
55-
# == Make API request via SDK ==
56-
transcript = aai.Transcriber().transcribe(
57-
data="https://example.org/audio.wav",
58-
config=config,
59-
)
60-
61-
# Check that submission and polling requests were made
62-
assert len(httpx_mock.get_requests()) == 2
63-
64-
# Extract body of initial submission request
65-
request = httpx_mock.get_requests()[0]
66-
request_body = json.loads(request.content.decode())
67-
68-
return request_body, transcript
69-
70-
7116
def test_auto_chapters_fails_without_punctuation(httpx_mock: HTTPXMock):
7217
"""
7318
Tests whether the SDK raises an error before making a request
7419
if `auto_chapters` is enabled and `punctuation` is disabled
7520
"""
7621

7722
with pytest.raises(ValueError) as error:
78-
__submit_mock_request(
23+
unit_test_utils.submit_mock_transcription_request(
7924
httpx_mock,
8025
mock_response={}, # response doesn't matter, since it shouldn't occur
8126
config=aai.TranscriptionConfig(
@@ -98,7 +43,7 @@ def test_auto_chapters_disabled_by_default(httpx_mock: HTTPXMock):
9843
Tests that excluding `auto_chapters` from the `TranscriptionConfig` will
9944
result in the default behavior of it being excluded from the request body
10045
"""
101-
request_body, transcript = __submit_mock_request(
46+
request_body, transcript = unit_test_utils.submit_mock_transcription_request(
10247
httpx_mock,
10348
mock_response=factories.generate_dict_factory(
10449
factories.TranscriptCompletedResponseFactory
@@ -116,7 +61,7 @@ def test_auto_chapters_enabled(httpx_mock: HTTPXMock):
11661
response is properly parsed into a `Transcript` object
11762
"""
11863
mock_response = factories.generate_dict_factory(AutoChaptersResponseFactory)()
119-
request_body, transcript = __submit_mock_request(
64+
request_body, transcript = unit_test_utils.submit_mock_transcription_request(
12065
httpx_mock,
12166
mock_response=mock_response,
12267
config=aai.TranscriptionConfig(auto_chapters=True),

tests/unit/test_auto_highlights.py

Lines changed: 3 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
import json
2-
from typing import Any, Dict, Tuple
3-
41
import factory
5-
import httpx
62
from pytest_httpx import HTTPXMock
73

4+
import tests.unit.unit_test_utils as unit_test_utils
85
import assemblyai as aai
9-
from assemblyai.api import ENDPOINT_TRANSCRIPT
106
from tests.unit import factories
117

128
aai.settings.api_key = "test"
@@ -36,63 +32,12 @@ class AutohighlightTranscriptResponseFactory(
3632
auto_highlights_result = factory.SubFactory(AutohighlightResponseFactory)
3733

3834

39-
def __submit_mock_request(
40-
httpx_mock: HTTPXMock,
41-
mock_response: Dict[str, Any],
42-
config: aai.TranscriptionConfig,
43-
) -> Tuple[Dict[str, Any], aai.Transcript]:
44-
"""
45-
Helper function to abstract mock transcriber calls with given `TranscriptionConfig`,
46-
and perform some common assertions.
47-
"""
48-
49-
mock_transcript_id = mock_response.get("id", "mock_id")
50-
51-
# Mock initial submission response (transcript is processing)
52-
mock_processing_response = factories.generate_dict_factory(
53-
factories.TranscriptProcessingResponseFactory
54-
)()
55-
56-
httpx_mock.add_response(
57-
url=f"{aai.settings.base_url}{ENDPOINT_TRANSCRIPT}",
58-
status_code=httpx.codes.OK,
59-
method="POST",
60-
json={
61-
**mock_processing_response,
62-
"id": mock_transcript_id, # inject ID from main mock response
63-
},
64-
)
65-
66-
# Mock polling-for-completeness response, with completed transcript
67-
httpx_mock.add_response(
68-
url=f"{aai.settings.base_url}{ENDPOINT_TRANSCRIPT}/{mock_transcript_id}",
69-
status_code=httpx.codes.OK,
70-
method="GET",
71-
json=mock_response,
72-
)
73-
74-
# == Make API request via SDK ==
75-
transcript = aai.Transcriber().transcribe(
76-
data="https://example.org/audio.wav",
77-
config=config,
78-
)
79-
80-
# Check that submission and polling requests were made
81-
assert len(httpx_mock.get_requests()) == 2
82-
83-
# Extract body of initial submission request
84-
request = httpx_mock.get_requests()[0]
85-
request_body = json.loads(request.content.decode())
86-
87-
return request_body, transcript
88-
89-
9035
def test_auto_highlights_disabled_by_default(httpx_mock: HTTPXMock):
9136
"""
9237
Tests that excluding `auto_highlights` from the `TranscriptionConfig` will
9338
result in the default behavior of it being excluded from the request body
9439
"""
95-
request_body, transcript = __submit_mock_request(
40+
request_body, transcript = unit_test_utils.submit_mock_transcription_request(
9641
httpx_mock,
9742
mock_response=factories.generate_dict_factory(
9843
factories.TranscriptCompletedResponseFactory
@@ -112,7 +57,7 @@ def test_auto_highlights_enabled(httpx_mock: HTTPXMock):
11257
mock_response = factories.generate_dict_factory(
11358
AutohighlightTranscriptResponseFactory
11459
)()
115-
request_body, transcript = __submit_mock_request(
60+
request_body, transcript = unit_test_utils.submit_mock_transcription_request(
11661
httpx_mock,
11762
mock_response=mock_response,
11863
config=aai.TranscriptionConfig(auto_highlights=True),

tests/unit/test_content_safety.py

Lines changed: 5 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
import json
21
import random
3-
from typing import Any, Dict, Tuple
42

53
import factory
6-
import httpx
74
import pytest
85
from pytest_httpx import HTTPXMock
96

7+
import tests.unit.unit_test_utils as unit_test_utils
108
import assemblyai as aai
11-
from assemblyai.api import ENDPOINT_TRANSCRIPT
129
from tests.unit import factories
1310

1411
aai.settings.api_key = "test"
@@ -69,63 +66,12 @@ class ContentSafetyTranscriptResponseFactory(
6966
content_safety_labels = factory.SubFactory(ContentSafetyResponseFactory)
7067

7168

72-
def __submit_mock_request(
73-
httpx_mock: HTTPXMock,
74-
mock_response: Dict[str, Any],
75-
config: aai.TranscriptionConfig,
76-
) -> Tuple[Dict[str, Any], aai.Transcript]:
77-
"""
78-
Helper function to abstract mock transcriber calls with given `TranscriptionConfig`,
79-
and perform some common assertions.
80-
"""
81-
82-
mock_transcript_id = mock_response.get("id", "mock_id")
83-
84-
# Mock initial submission response (transcript is processing)
85-
mock_processing_response = factories.generate_dict_factory(
86-
factories.TranscriptProcessingResponseFactory
87-
)()
88-
89-
httpx_mock.add_response(
90-
url=f"{aai.settings.base_url}{ENDPOINT_TRANSCRIPT}",
91-
status_code=httpx.codes.OK,
92-
method="POST",
93-
json={
94-
**mock_processing_response,
95-
"id": mock_transcript_id, # inject ID from main mock response
96-
},
97-
)
98-
99-
# Mock polling-for-completeness response, with completed transcript
100-
httpx_mock.add_response(
101-
url=f"{aai.settings.base_url}{ENDPOINT_TRANSCRIPT}/{mock_transcript_id}",
102-
status_code=httpx.codes.OK,
103-
method="GET",
104-
json=mock_response,
105-
)
106-
107-
# == Make API request via SDK ==
108-
transcript = aai.Transcriber().transcribe(
109-
data="https://example.org/audio.wav",
110-
config=config,
111-
)
112-
113-
# Check that submission and polling requests were made
114-
assert len(httpx_mock.get_requests()) == 2
115-
116-
# Extract body of initial submission request
117-
request = httpx_mock.get_requests()[0]
118-
request_body = json.loads(request.content.decode())
119-
120-
return request_body, transcript
121-
122-
12369
def test_content_safety_disabled_by_default(httpx_mock: HTTPXMock):
12470
"""
12571
Tests that excluding `content_safety` from the `TranscriptionConfig` will
12672
result in the default behavior of it being excluded from the request body
12773
"""
128-
request_body, transcript = __submit_mock_request(
74+
request_body, transcript = unit_test_utils.submit_mock_transcription_request(
12975
httpx_mock,
13076
mock_response=factories.generate_dict_factory(
13177
factories.TranscriptCompletedResponseFactory
@@ -145,7 +91,7 @@ def test_content_safety_enabled(httpx_mock: HTTPXMock):
14591
mock_response = factories.generate_dict_factory(
14692
ContentSafetyTranscriptResponseFactory
14793
)()
148-
request_body, transcript = __submit_mock_request(
94+
request_body, transcript = unit_test_utils.submit_mock_transcription_request(
14995
httpx_mock,
15096
mock_response=mock_response,
15197
config=aai.TranscriptionConfig(content_safety=True),
@@ -248,7 +194,7 @@ def test_content_safety_with_confidence_threshold(httpx_mock: HTTPXMock):
248194
and will be included in the request body
249195
"""
250196
confidence = 40
251-
request, _ = __submit_mock_request(
197+
request, _ = unit_test_utils.submit_mock_transcription_request(
252198
httpx_mock,
253199
mock_response={}, # Response doesn't matter here; we're just testing the request body
254200
config=aai.TranscriptionConfig(
@@ -269,7 +215,7 @@ def test_content_safety_with_invalid_confidence_threshold(
269215
an exception to be raised before the request is sent
270216
"""
271217
with pytest.raises(ValueError) as error:
272-
__submit_mock_request(
218+
unit_test_utils.submit_mock_transcription_request(
273219
httpx_mock,
274220
mock_response={}, # We don't expect to produce a response
275221
config=aai.TranscriptionConfig(

0 commit comments

Comments
 (0)