@@ -398,3 +398,76 @@ 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?deploymentName=gpt-4&length=Short&style=Formal'
420+
421+ params = {
422+ 'accessToken' : self .vi_access_token ,
423+ 'deploymentName' : deployment_name ,
424+ 'length' : lenght ,
425+ 'style' : style ,
426+ 'Cache-Control' : 'no-cache'
427+ }
428+
429+ try :
430+ response = requests .post (url_create , params = params , verify = False )
431+ response .raise_for_status ()
432+ summary_id = response .json ().get ('id' )
433+ except requests .exceptions .RequestException as e :
434+ print (f"Error creating video summary: { e } " )
435+ return
436+
437+ # GET request to get video summary
438+ url_get = f'{ self .consts .ApiEndpoint } /{ self .account ["location" ]} /Accounts/{ self .account ["properties" ]["accountId" ]} /' + \
439+ f'Videos/{ video_id } /Summaries/Textual/{ summary_id } '
440+ params = {
441+ 'accessToken' : self .vi_access_token ,
442+ 'Cache-Control' : 'no-cache'
443+ }
444+
445+ start_time = time .time ()
446+ backoff = 10
447+
448+ while True :
449+ try :
450+
451+ response = requests .get (url_get , params = params , verify = False )
452+ response .raise_for_status ()
453+
454+ video_result = response .json ()
455+ video_state = video_result .get ('state' )
456+ except requests .exceptions .RequestException as e :
457+ print (f"Error getting video summary status: { e } " )
458+ return
459+
460+ if video_state == 'Processed' :
461+ print (f'Here is the textual summary of the video: \n { video_result } ' )
462+ break
463+ elif video_state == 'Failed' :
464+ print (f"Text summary failed for video ID { video_id } ." )
465+ break
466+ else :
467+ print (f'Text summary status for the video is { video_state } ' )
468+ if timeout_sec is not None and time .time () - start_time > timeout_sec :
469+ print (f'The { timeout_sec } seconds timeout was reached. Exiting...' )
470+ break
471+ print (f'Waiting { backoff } seconds before trying again....' )
472+ time .sleep (backoff )
473+ backoff = min (backoff * 2 , 60 )
0 commit comments