Skip to content

Commit c0f98a8

Browse files
Hugo Cordoba LealHugo Cordoba Leal
authored andcommitted
ADD TextualSummarization function
1 parent 09e1f65 commit c0f98a8

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

API-Samples/Python/VideoIndexerClient/VideoIndexerClient.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,3 +398,70 @@ def get_player_widget_url_async(self, video_id:str) -> None:
398398

399399
url = response.url
400400
print(f'Got the player widget URL: {url}')
401+
402+
def get_textual_summary(self, video_id:str, deployment_name:str, lenght:str, style:str, timeout_sec:Optional[int]=None) -> None:
403+
'''
404+
Calls the TextualSummarization API. First a call is made with the POST method, to create the video summary:
405+
(https://api-portal.videoindexer.ai/api-details#api=Operations&operation=Create-Video-Summary)
406+
Then, a call is made with the get method to retrieve the video summary and the function returns it.
407+
(https://api-portal.videoindexer.ai/api-details#api=Operations&operation=Get-Video-Summary)
408+
409+
:param video_id: The video ID
410+
:param deploymentName: The name of the deployment
411+
:param length: The length of the summary
412+
:param style: The style of the summary
413+
'''
414+
415+
self.get_account_async() # if account is not initialized, get it
416+
417+
# POST request to create video summary
418+
url_create = f'{self.consts.ApiEndpoint}/{self.account["location"]}/Accounts/{self.account["properties"]["accountId"]}/' + \
419+
f'Videos/{video_id}/Summaries/Textual?'
420+
params = {
421+
'accessToken': self.vi_access_token,
422+
'deploymentName': deployment_name,
423+
'length': lenght,
424+
'style': style,
425+
'Cache-Control': 'no-cache'
426+
}
427+
428+
try:
429+
response = requests.post(url_create, params=params)
430+
response.raise_for_status()
431+
summary_id = response.json().get('id')
432+
except requests.exceptions.RequestException as e:
433+
print(f"Error creating video summary: {e}")
434+
return
435+
436+
# GET request to get video summary
437+
url_get = f'{self.consts.ApiEndpoint}/{self.account["location"]}/Accounts/{self.account["properties"]["accountId"]}/' + \
438+
f'Videos/{video_id}/Summaries/Textual/{summary_id}'
439+
params = {
440+
'accessToken': self.vi_access_token,
441+
'Cache-Control': 'no-cache'
442+
}
443+
start_time = time.time()
444+
backoff = 10
445+
446+
while True:
447+
try:
448+
response = requests.get(url_get, params=params)
449+
response.raise_for_status()
450+
video_result = response.json()
451+
video_state = video_result.get('state')
452+
except requests.exceptions.RequestException as e:
453+
print(f"Error getting video summary status: {e}")
454+
return
455+
if video_state == 'Processed':
456+
print(f'Here is the textual summary of the video: \n{video_result}')
457+
break
458+
elif video_state == 'Failed':
459+
print(f"Text summary failed for video ID {video_id}.")
460+
break
461+
else:
462+
print(f'Text summary status for the video is {video_state}')
463+
if timeout_sec is not None and time.time() - start_time > timeout_sec:
464+
print(f'The {timeout_sec} seconds timeout was reached. Exiting...')
465+
break
466+
print(f'Waiting {backoff} seconds before trying again....')
467+
time.sleep(backoff)

0 commit comments

Comments
 (0)