| 
					
				 | 
			
			
				@@ -7,6 +7,9 @@ import google.generativeai as genai 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import uuid 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+from google.generativeai.types import HarmCategory, HarmBlockThreshold 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+from common import Common 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 from common.feishu_data import Material 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -51,8 +54,13 @@ class VideoAnalyzer: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     async def upload_video(self, save_path, mime_type = None): 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         """上传视频文件并获取视频文件对象""" 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        self.video_file = genai.upload_file(save_path, mime_type=mime_type) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        await self._wait_for_processing() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        try: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            self.video_file = genai.upload_file(save_path, mime_type=mime_type) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            await self._wait_for_processing() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        except Exception as e: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            Common.logger("ai").info(f'上传视频文件并获取视频文件对象失败异常信息{e}') 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            self.video_file.delete() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            print( f"上传视频文件并获取视频文件对象失败:{e}" ) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     async def _wait_for_processing(self): 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         """等待视频文件处理完成""" 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -64,10 +72,6 @@ class VideoAnalyzer: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     async def create_cache(self): 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         generation_config = { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            "temperature": 1, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            "top_p": 0.95, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            "top_k": 64, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            "max_output_tokens": 8192, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             "response_mime_type": "application/json" 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         """创建缓存内容,并返回生成模型""" 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -75,11 +79,15 @@ class VideoAnalyzer: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         model = genai.GenerativeModel( 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             model_name="gemini-1.5-flash", 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             generation_config=generation_config, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            safety_settings={ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                HarmCategory.HARM_CATEGORY_HATE_SPEECH: HarmBlockThreshold.BLOCK_NONE, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         ) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         return model 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     async def analyze_video(self, model, questions, sample_data): 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        try: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             chat_session = model.start_chat(history=[]) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             message_content = { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 "parts": [ 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -92,6 +100,10 @@ class VideoAnalyzer: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             response = chat_session.send_message( message_content ) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             self.video_file.delete() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             return response 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        except Exception as e: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            Common.logger("ai").info(f'创建缓存模型异常信息{e}') 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            self.video_file.delete() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            print( f"视频处理失败:{e}" ) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     def video_duration(self, filename): 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         cap = cv2.VideoCapture( filename ) 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -128,6 +140,8 @@ async def main(video_path, api_key, prompt, mark): 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             await analyzer.upload_video(save_path) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             # 创建缓存模型 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             model =await analyzer.create_cache() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            print("创建缓存模型") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             sample_data = { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 "一、基础信息": { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                     "视觉/音乐/文字": "", 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -174,7 +188,8 @@ async def main(video_path, api_key, prompt, mark): 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 print(f"重试第 {attempt} 次...") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             else: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 print( "达到最大重试次数,处理失败" ) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                return f"视频分析处理失败:{e}" 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                Common.logger( "ai" ).info( f'视频分析处理失败异常信息{e}' ) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                return f"视频分析处理失败:{e}", None 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 if __name__ == "__main__": 
			 |