Skip to content

Commit 16895d1

Browse files
Update samples and tests to pass content range as plain strings directly
Co-authored-by: changjian-wang <15209050+changjian-wang@users.noreply.github.com>
1 parent 43492d2 commit 16895d1

6 files changed

Lines changed: 142 additions & 90 deletions

File tree

sdk/contentunderstanding/azure-ai-contentunderstanding/samples/async_samples/sample_analyze_binary_async.py

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -136,24 +136,55 @@ async def main() -> None:
136136
# [END analyze_binary_with_combined_content_range]
137137

138138
# [START analyze_binary_with_raw_content_range]
139-
# You can also pass a range string directly to the ContentRange constructor.
140-
# This is equivalent to using the factory methods and is useful for dynamically
141-
# constructed or user-supplied ranges.
142-
# Analyze pages 1-3, page 5, and pages 9 onward using a raw range string.
143-
# This is equivalent to: ContentRange.combine(ContentRange.pages(1, 3), ContentRange.page(5), ContentRange.pages_from(9))
144-
print("\nAnalyzing with raw ContentRange string '1-3,5,9-'...")
145-
raw_range_poller = await client.begin_analyze_binary(
139+
# You can also pass content range as a plain string directly, without using
140+
# ContentRange factory methods. The wire format is the same as what the factory methods produce.
141+
142+
# "1-3" — pages 1 through 3 (equivalent to ContentRange.pages(1, 3))
143+
print("\nAnalyzing pages 1-3 with raw string '1-3'...")
144+
raw_pages_poller = await client.begin_analyze_binary(
145+
analyzer_id="prebuilt-documentSearch",
146+
binary_input=multi_page_bytes,
147+
content_range="1-3",
148+
)
149+
raw_pages_result: AnalysisResult = await raw_pages_poller.result()
150+
151+
if isinstance(raw_pages_result.contents[0], DocumentContent):
152+
raw_pages_doc = raw_pages_result.contents[0]
153+
print(
154+
f"Raw '1-3' returned pages"
155+
f" {raw_pages_doc.start_page_number} - {raw_pages_doc.end_page_number}"
156+
)
157+
158+
# "9-" — pages 9 onward (equivalent to ContentRange.pages_from(9))
159+
print("\nAnalyzing pages 9 onward with raw string '9-'...")
160+
raw_from_poller = await client.begin_analyze_binary(
161+
analyzer_id="prebuilt-documentSearch",
162+
binary_input=multi_page_bytes,
163+
content_range="9-",
164+
)
165+
raw_from_result: AnalysisResult = await raw_from_poller.result()
166+
167+
if isinstance(raw_from_result.contents[0], DocumentContent):
168+
raw_from_doc = raw_from_result.contents[0]
169+
print(
170+
f"Raw '9-' returned pages"
171+
f" {raw_from_doc.start_page_number} - {raw_from_doc.end_page_number}"
172+
)
173+
174+
# "1-3,5,9-" — combined ranges (equivalent to ContentRange.combine(pages(1,3), page(5), pages_from(9)))
175+
print("\nAnalyzing combined pages (1-3, 5, 9-) with raw string '1-3,5,9-'...")
176+
raw_combine_poller = await client.begin_analyze_binary(
146177
analyzer_id="prebuilt-documentSearch",
147178
binary_input=multi_page_bytes,
148-
content_range=ContentRange("1-3,5,9-"),
179+
content_range="1-3,5,9-",
149180
)
150-
raw_range_result: AnalysisResult = await raw_range_poller.result()
181+
raw_combine_result: AnalysisResult = await raw_combine_poller.result()
151182

152-
if isinstance(raw_range_result.contents[0], DocumentContent):
153-
raw_doc = raw_range_result.contents[0]
183+
if isinstance(raw_combine_result.contents[0], DocumentContent):
184+
raw_combine_doc = raw_combine_result.contents[0]
154185
print(
155-
f"Raw ContentRange analysis returned pages"
156-
f" {raw_doc.start_page_number} - {raw_doc.end_page_number}"
186+
f"Raw '1-3,5,9-' returned pages"
187+
f" {raw_combine_doc.start_page_number} - {raw_combine_doc.end_page_number}"
157188
)
158189
# [END analyze_binary_with_raw_content_range]
159190

sdk/contentunderstanding/azure-ai-contentunderstanding/samples/async_samples/sample_analyze_url_async.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -262,18 +262,16 @@ async def main() -> None:
262262
# [END analyze_video_url_with_additional_content_ranges]
263263

264264
# [START analyze_video_url_with_raw_content_range]
265-
# You can also pass a range string directly to the ContentRange constructor.
266-
# Time ranges use milliseconds on the wire. This is useful for dynamically
267-
# constructed or user-supplied ranges.
268-
# Analyze the first 5 seconds using a raw range string (milliseconds).
269-
# This is equivalent to: ContentRange.time_range(timedelta(0), timedelta(seconds=5))
270-
print("\nAnalyzing first 5 seconds of video with raw ContentRange string '0-5000'...")
265+
# You can also pass content range as a plain string directly for video time ranges.
266+
# Time ranges use milliseconds. This is useful for dynamically constructed or user-supplied ranges.
267+
# "0-5000" — analyze the first 5 seconds (equivalent to ContentRange.time_range(timedelta(0), timedelta(seconds=5)))
268+
print("\nAnalyzing first 5 seconds of video with raw string '0-5000'...")
271269
raw_video_range_poller = await client.begin_analyze(
272270
analyzer_id="prebuilt-videoSearch",
273271
inputs=[
274272
AnalysisInput(
275273
url=video_url,
276-
content_range=str(ContentRange("0-5000")),
274+
content_range="0-5000",
277275
)
278276
],
279277
)
@@ -282,7 +280,7 @@ async def main() -> None:
282280
for raw_media in raw_video_range_result.contents:
283281
raw_video_content = cast(AudioVisualContent, raw_media)
284282
print(
285-
f"Raw ContentRange segment:"
283+
f"Raw '0-5000' segment:"
286284
f" {raw_video_content.start_time_ms} ms - {raw_video_content.end_time_ms} ms"
287285
)
288286
# [END analyze_video_url_with_raw_content_range]
@@ -398,24 +396,24 @@ async def main() -> None:
398396
# [END analyze_audio_url_with_additional_content_ranges]
399397

400398
# [START analyze_audio_url_with_raw_content_range]
401-
# You can also pass a range string directly for audio time ranges.
402-
# Analyze audio from 5 seconds onward using a raw range string (milliseconds).
403-
# This is equivalent to: ContentRange.time_range_from(timedelta(seconds=5))
404-
print("\nAnalyzing audio from 5 seconds onward with raw ContentRange string '5000-'...")
399+
# You can also pass content range as a plain string directly for audio time ranges.
400+
# Time ranges use milliseconds. This is useful for dynamically constructed or user-supplied ranges.
401+
# "5000-" — analyze from 5 seconds onward (equivalent to ContentRange.time_range_from(timedelta(seconds=5)))
402+
print("\nAnalyzing audio from 5 seconds onward with raw string '5000-'...")
405403
raw_audio_range_poller = await client.begin_analyze(
406404
analyzer_id="prebuilt-audioSearch",
407405
inputs=[
408406
AnalysisInput(
409407
url=audio_url,
410-
content_range=str(ContentRange("5000-")),
408+
content_range="5000-",
411409
)
412410
],
413411
)
414412
raw_audio_range_result = await raw_audio_range_poller.result()
415413

416414
raw_audio_content = cast(AudioVisualContent, raw_audio_range_result.contents[0])
417415
print(
418-
f"Raw ContentRange audio analysis:"
416+
f"Raw '5000-' audio analysis:"
419417
f" {raw_audio_content.start_time_ms} ms onward"
420418
)
421419
# [END analyze_audio_url_with_raw_content_range]

sdk/contentunderstanding/azure-ai-contentunderstanding/samples/sample_analyze_binary.py

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -128,22 +128,47 @@ def main() -> None:
128128
# [END analyze_binary_with_combined_content_range]
129129

130130
# [START analyze_binary_with_raw_content_range]
131-
# You can also pass a range string directly to the ContentRange constructor.
132-
# This is equivalent to using the factory methods and is useful for dynamically
133-
# constructed or user-supplied ranges.
134-
# Analyze pages 1-3, page 5, and pages 9 onward using a raw range string.
135-
# This is equivalent to: ContentRange.combine(ContentRange.pages(1, 3), ContentRange.page(5), ContentRange.pages_from(9))
136-
print("\nAnalyzing with raw ContentRange string '1-3,5,9-'...")
137-
raw_range_poller = client.begin_analyze_binary(
131+
# You can also pass content range as a plain string directly, without using
132+
# ContentRange factory methods. The wire format is the same as what the factory methods produce.
133+
134+
# "1-3" — pages 1 through 3 (equivalent to ContentRange.pages(1, 3))
135+
print("\nAnalyzing pages 1-3 with raw string '1-3'...")
136+
raw_pages_poller = client.begin_analyze_binary(
137+
analyzer_id="prebuilt-documentSearch",
138+
binary_input=multi_page_bytes,
139+
content_range="1-3",
140+
)
141+
raw_pages_result: AnalysisResult = raw_pages_poller.result()
142+
143+
if isinstance(raw_pages_result.contents[0], DocumentContent):
144+
raw_pages_doc = raw_pages_result.contents[0]
145+
print(f"Raw '1-3' returned pages {raw_pages_doc.start_page_number} - {raw_pages_doc.end_page_number}")
146+
147+
# "9-" — pages 9 onward (equivalent to ContentRange.pages_from(9))
148+
print("\nAnalyzing pages 9 onward with raw string '9-'...")
149+
raw_from_poller = client.begin_analyze_binary(
150+
analyzer_id="prebuilt-documentSearch",
151+
binary_input=multi_page_bytes,
152+
content_range="9-",
153+
)
154+
raw_from_result: AnalysisResult = raw_from_poller.result()
155+
156+
if isinstance(raw_from_result.contents[0], DocumentContent):
157+
raw_from_doc = raw_from_result.contents[0]
158+
print(f"Raw '9-' returned pages {raw_from_doc.start_page_number} - {raw_from_doc.end_page_number}")
159+
160+
# "1-3,5,9-" — combined ranges (equivalent to ContentRange.combine(pages(1,3), page(5), pages_from(9)))
161+
print("\nAnalyzing combined pages (1-3, 5, 9-) with raw string '1-3,5,9-'...")
162+
raw_combine_poller = client.begin_analyze_binary(
138163
analyzer_id="prebuilt-documentSearch",
139164
binary_input=multi_page_bytes,
140-
content_range=ContentRange("1-3,5,9-"),
165+
content_range="1-3,5,9-",
141166
)
142-
raw_range_result: AnalysisResult = raw_range_poller.result()
167+
raw_combine_result: AnalysisResult = raw_combine_poller.result()
143168

144-
if isinstance(raw_range_result.contents[0], DocumentContent):
145-
raw_doc = raw_range_result.contents[0]
146-
print(f"Raw ContentRange analysis returned pages {raw_doc.start_page_number} - {raw_doc.end_page_number}")
169+
if isinstance(raw_combine_result.contents[0], DocumentContent):
170+
raw_combine_doc = raw_combine_result.contents[0]
171+
print(f"Raw '1-3,5,9-' returned pages {raw_combine_doc.start_page_number} - {raw_combine_doc.end_page_number}")
147172
# [END analyze_binary_with_raw_content_range]
148173

149174
# [START extract_markdown]

sdk/contentunderstanding/azure-ai-contentunderstanding/samples/sample_analyze_url.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -258,18 +258,16 @@ def main() -> None:
258258
# [END analyze_video_url_with_additional_content_ranges]
259259

260260
# [START analyze_video_url_with_raw_content_range]
261-
# You can also pass a range string directly to the ContentRange constructor.
262-
# Time ranges use milliseconds on the wire. This is useful for dynamically
263-
# constructed or user-supplied ranges.
264-
# Analyze the first 5 seconds using a raw range string (milliseconds).
265-
# This is equivalent to: ContentRange.time_range(timedelta(0), timedelta(seconds=5))
266-
print("\nAnalyzing first 5 seconds of video with raw ContentRange string '0-5000'...")
261+
# You can also pass content range as a plain string directly for video time ranges.
262+
# Time ranges use milliseconds. This is useful for dynamically constructed or user-supplied ranges.
263+
# "0-5000" — analyze the first 5 seconds (equivalent to ContentRange.time_range(timedelta(0), timedelta(seconds=5)))
264+
print("\nAnalyzing first 5 seconds of video with raw string '0-5000'...")
267265
raw_video_range_poller = client.begin_analyze(
268266
analyzer_id="prebuilt-videoSearch",
269267
inputs=[
270268
AnalysisInput(
271269
url=video_url,
272-
content_range=str(ContentRange("0-5000")),
270+
content_range="0-5000",
273271
)
274272
],
275273
)
@@ -278,7 +276,7 @@ def main() -> None:
278276
for raw_media in raw_video_range_result.contents:
279277
raw_video_content = cast(AudioVisualContent, raw_media)
280278
print(
281-
f"Raw ContentRange segment:"
279+
f"Raw '0-5000' segment:"
282280
f" {raw_video_content.start_time_ms} ms - {raw_video_content.end_time_ms} ms"
283281
)
284282
# [END analyze_video_url_with_raw_content_range]
@@ -391,24 +389,24 @@ def main() -> None:
391389
# [END analyze_audio_url_with_additional_content_ranges]
392390

393391
# [START analyze_audio_url_with_raw_content_range]
394-
# You can also pass a range string directly for audio time ranges.
395-
# Analyze audio from 5 seconds onward using a raw range string (milliseconds).
396-
# This is equivalent to: ContentRange.time_range_from(timedelta(seconds=5))
397-
print("\nAnalyzing audio from 5 seconds onward with raw ContentRange string '5000-'...")
392+
# You can also pass content range as a plain string directly for audio time ranges.
393+
# Time ranges use milliseconds. This is useful for dynamically constructed or user-supplied ranges.
394+
# "5000-" — analyze from 5 seconds onward (equivalent to ContentRange.time_range_from(timedelta(seconds=5)))
395+
print("\nAnalyzing audio from 5 seconds onward with raw string '5000-'...")
398396
raw_audio_range_poller = client.begin_analyze(
399397
analyzer_id="prebuilt-audioSearch",
400398
inputs=[
401399
AnalysisInput(
402400
url=audio_url,
403-
content_range=str(ContentRange("5000-")),
401+
content_range="5000-",
404402
)
405403
],
406404
)
407405
raw_audio_range_result = raw_audio_range_poller.result()
408406

409407
raw_audio_content = cast(AudioVisualContent, raw_audio_range_result.contents[0])
410408
print(
411-
f"Raw ContentRange audio analysis:"
409+
f"Raw '5000-' audio analysis:"
412410
f" {raw_audio_content.start_time_ms} ms onward"
413411
)
414412
# [END analyze_audio_url_with_raw_content_range]

0 commit comments

Comments
 (0)