zhangyong 6 kuukautta sitten
vanhempi
commit
4a11ee6383
2 muutettua tiedostoa jossa 28 lisäystä ja 7 poistoa
  1. 22 7
      google_ai/generativeai_video.py
  2. 6 0
      job_video_processing.py

+ 22 - 7
google_ai/generativeai_video.py

@@ -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__":

+ 6 - 0
job_video_processing.py

@@ -12,10 +12,16 @@ def video_ai_task_start():
             try:
                 redis_task_list = ['task:video_ai_top', 'task:video_ai_recommend'] * 3
                 futures = []
+                start_time = time.time()  # 记录开始时间
+
                 for redis_task in redis_task_list:
                     futures.append(executor.submit( process_video_ai, redis_task))
                     time.sleep(1)  # 每秒提交一个任务
                 wait( futures )  # 等待所有任务完成
+                end_time = time.time()  # 记录结束时间
+
+                total_time = end_time - start_time  # 计算总耗时
+                print( f"6个线程完成任务总耗时:{total_time:.2f}秒" )
             except Exception as e:
                 print(f"异常信息{e}")
                 time.sleep(3)