[docs]classVideoCaptioningChain(Chain):""" Video Captioning Chain. """llm:BaseLanguageModelassemblyai_key:strprompt:Optional[PromptTemplate]=Noneverbose:bool=Trueuse_logging:Optional[bool]=Trueframe_skip:int=-1image_delta_threshold:int=3000000closed_caption_char_limit:int=20closed_caption_similarity_threshold:int=80use_unclustered_video_models:bool=Falsemodel_config=ConfigDict(arbitrary_types_allowed=True,extra="allow",)@propertydefinput_keys(self)->List[str]:return["video_file_path"]@propertydefoutput_keys(self)->List[str]:return["srt"]def_call(self,inputs:Dict[str,Any],run_manager:Optional[CallbackManagerForChainRun]=None,)->Dict[str,str]:if"video_file_path"notininputs:raiseValueError("Missing 'video_file_path' in inputs for video captioning.")video_file_path=inputs["video_file_path"]nl="\n"run_manager.on_text("Loading processors..."+nl)ifself.use_loggingandrun_managerelseNoneaudio_processor=AudioProcessor(api_key=self.assemblyai_key)image_processor=ImageProcessor(frame_skip=self.frame_skip,threshold=self.image_delta_threshold)caption_processor=CaptionProcessor(llm=self.llm,verbose=self.verbose,similarity_threshold=self.closed_caption_similarity_threshold,use_unclustered_models=self.use_unclustered_video_models,)combine_processor=CombineProcessor(llm=self.llm,verbose=self.verbose,char_limit=self.closed_caption_char_limit,)srt_processor=SRTProcessor()run_manager.on_text("Finished loading processors."+nl+"Generating subtitles from audio..."+nl)ifself.use_loggingandrun_managerelseNone# Get models for speech to text subtitlesaudio_models=audio_processor.process(video_file_path,run_manager)run_manager.on_text("Finished generating subtitles:"+nl+f"{nl.join(str(obj)forobjinaudio_models)}"+nl+"Generating closed captions from video..."+nl)ifself.use_loggingandrun_managerelseNone# Get models for image frame descriptionimage_models=image_processor.process(video_file_path,run_manager)run_manager.on_text("Finished generating closed captions:"+nl+f"{nl.join(str(obj)forobjinimage_models)}"+nl+"Refining closed captions..."+nl)ifself.use_loggingandrun_managerelseNone# Get models for video event closed-captionsvideo_models=caption_processor.process(image_models,run_manager)run_manager.on_text("Finished refining closed captions:"+nl+f"{nl.join(str(obj)forobjinvideo_models)}"+nl+"Combining subtitles with closed captions..."+nl)ifself.use_loggingandrun_managerelseNone# Combine the subtitle models with the closed-caption modelscaption_models=combine_processor.process(video_models,audio_models,run_manager)run_manager.on_text("Finished combining subtitles with closed captions:"+nl+f"{nl.join(str(obj)forobjincaption_models)}"+nl+"Generating SRT file..."+nl)ifself.use_loggingandrun_managerelseNone# Convert the combined model to SRT formatsrt_content=srt_processor.process(caption_models)run_manager.on_text("Finished generating srt file."+nl)ifself.use_loggingandrun_managerelseNonereturn{"srt":srt_content}@propertydef_chain_type(self)->str:return"video_captioning_chain"