luojunhui 2 주 전
부모
커밋
fbd5685b58
2개의 변경된 파일133개의 추가작업 그리고 54개의 파일을 삭제
  1. 17 6
      applications/api/google_ai_api.py
  2. 116 48
      coldStartTasks/ai_pipeline/extract_video_best_frame.py

+ 17 - 6
applications/api/google_ai_api.py

@@ -11,8 +11,8 @@ class GoogleAIAPI(object):
     """
     """
     Google 视频内容理解API
     Google 视频内容理解API
     """
     """
-    def __init__(self):
-        self.api_key = 'AIzaSyBAFFL6yHa4kUK-YcuAw8tbiSHQB6oJG34'
+    def __init__(self, api_key):
+        self.api_key = api_key
         self.client = genai.Client(api_key=self.api_key)
         self.client = genai.Client(api_key=self.api_key)
 
 
     def upload_file(self, file_path: str):
     def upload_file(self, file_path: str):
@@ -64,16 +64,27 @@ class GoogleAIAPI(object):
         """
         """
         try:
         try:
             response = self.client.models.generate_content(
             response = self.client.models.generate_content(
-                model='gemini-2.0-flash',
+                model='gemini-1.5-flash',
                 contents=[
                 contents=[
                     video_file,
                     video_file,
                     prompt
                     prompt
                 ]
                 ]
             )
             )
-            return response.text
+            return {
+                'code': 200,
+                'text': response.text
+            }
         except ClientError as e:
         except ClientError as e:
-            print(e.response.status_code)
-            return None
+            return {
+                'code': e.response.status_code,
+                'text': 'resource exhausted'
+            }
+        except Exception as e:
+            return {
+                'code': 500,
+                'text': str(e)
+            }
+
 
 
     def delete_video(self, file_name: str):
     def delete_video(self, file_name: str):
         """
         """

+ 116 - 48
coldStartTasks/ai_pipeline/extract_video_best_frame.py

@@ -22,8 +22,7 @@ from coldStartTasks.ai_pipeline.basic import get_video_cover
 from coldStartTasks.ai_pipeline.basic import normalize_time_str
 from coldStartTasks.ai_pipeline.basic import normalize_time_str
 
 
 const = GoogleVideoUnderstandTaskConst()
 const = GoogleVideoUnderstandTaskConst()
-google_ai = GoogleAIAPI()
-
+google_ai = GoogleAIAPI(api_key='AIzaSyBAFFL6yHa4kUK-YcuAw8tbiSHQB6oJG34')
 
 
 class ExtractVideoBestFrame:
 class ExtractVideoBestFrame:
     """
     """
@@ -33,6 +32,7 @@ class ExtractVideoBestFrame:
     def __init__(self):
     def __init__(self):
         self.db_client = DatabaseConnector(db_config=long_articles_config)
         self.db_client = DatabaseConnector(db_config=long_articles_config)
         self.db_client.connect()
         self.db_client.connect()
+        self.api_status = True
 
 
     def _roll_back_lock_tasks(self, task: str) -> int:
     def _roll_back_lock_tasks(self, task: str) -> int:
         return roll_back_lock_tasks(
         return roll_back_lock_tasks(
@@ -222,6 +222,14 @@ class ExtractVideoBestFrame:
     def upload_video_to_gemini_ai(
     def upload_video_to_gemini_ai(
         self, max_processing_pool_size: int = const.POOL_SIZE
         self, max_processing_pool_size: int = const.POOL_SIZE
     ) -> None:
     ) -> None:
+        if not self.api_status:
+            log(
+                task=const.TASK_NAME,
+                function="upload_video_to_gemini_ai",
+                message="api_status is False, skip this task",
+            )
+            return
+
         # upload video to gemini ai
         # upload video to gemini ai
         roll_back_lock_tasks_count = self._roll_back_lock_tasks(task="upload")
         roll_back_lock_tasks_count = self._roll_back_lock_tasks(task="upload")
         log(
         log(
@@ -305,19 +313,98 @@ class ExtractVideoBestFrame:
 
 
                 case "ACTIVE":
                 case "ACTIVE":
                     # video process successfully
                     # video process successfully
-                    # try:
-                    best_frame_time_ms = google_ai.fetch_info_from_google_ai(
-                        prompt=extract_best_frame_prompt(),
-                        video_file=google_file,
-                    )
-                    print(best_frame_time_ms)
-                    if best_frame_time_ms:
-                        self.set_extract_result(
-                            task_id=task["id"],
-                            file_state="ACTIVE",
-                            best_frame_time_ms=best_frame_time_ms.strip(),
+                    try:
+                        response = google_ai.fetch_info_from_google_ai(
+                            prompt=extract_best_frame_prompt(),
+                            video_file=google_file,
+                        )
+                        code = response['code']
+                        match code:
+                            case 200:
+                                best_frame_time_ms = response['text']
+                                if best_frame_time_ms:
+                                    self.set_extract_result(
+                                        task_id=task["id"],
+                                        file_state="ACTIVE",
+                                        best_frame_time_ms=best_frame_time_ms.strip(),
+                                    )
+                                else:
+                                    update_task_queue_status(
+                                        db_client=self.db_client,
+                                        task_id=task["id"],
+                                        task="extract",
+                                        ori_status=const.PROCESSING_STATUS,
+                                        new_status=const.FAIL_STATUS,
+                                    )
+                                # delete local file and google file
+                                if os.path.exists(video_local_path):
+                                    os.remove(video_local_path)
+
+                                google_ai.delete_video(file_name)
+                                log(
+                                    task=const.TASK_NAME,
+                                    function="extract_best_frame_with_gemini_ai",
+                                    message="video process successfully",
+                                    data={
+                                        "task_id": task["id"],
+                                        "file_name": file_name,
+                                        "state": state,
+                                        "best_frame_time_ms": best_frame_time_ms,
+                                    },
+                                )
+                            case 429:
+                                # resource exhausted
+                                log(
+                                    task=const.TASK_NAME,
+                                    function="extract_best_frame_with_gemini_ai",
+                                    message="resource exhausted",
+                                    data={
+                                        "task_id": task["id"],
+                                        "file_name": file_name,
+                                        "state": state,
+                                        "response": response
+                                    },
+                                )
+                                update_task_queue_status(
+                                    db_client=self.db_client,
+                                    task_id=task["id"],
+                                    task="extract",
+                                    ori_status=const.PROCESSING_STATUS,
+                                    new_status=const.INIT_STATUS
+                                )
+                                return
+                            case 500:
+                                # other error
+                                log(
+                                    task=const.TASK_NAME,
+                                    function="extract_best_frame_with_gemini_ai",
+                                    message="other error",
+                                    data={
+                                        "task_id": task["id"],
+                                        "file_name": file_name,
+                                        "state": state,
+                                        "response": response,
+                                    },
+                                )
+                                update_task_queue_status(
+                                    db_client=self.db_client,
+                                    task_id=task["id"],
+                                    task="extract",
+                                    ori_status=const.PROCESSING_STATUS,
+                                    new_status=const.FAIL_STATUS
+                                )
+
+                    except Exception as e:
+                        log(
+                            task=const.TASK_NAME,
+                            function="extract_best_frame_with_gemini_ai",
+                            message="task_failed_inside_cycle",
+                            data={
+                                "task_id": task["id"],
+                                "track_back": traceback.format_exc(),
+                                "error": str(e),
+                            },
                         )
                         )
-                    else:
                         update_task_queue_status(
                         update_task_queue_status(
                             db_client=self.db_client,
                             db_client=self.db_client,
                             task_id=task["id"],
                             task_id=task["id"],
@@ -325,40 +412,6 @@ class ExtractVideoBestFrame:
                             ori_status=const.PROCESSING_STATUS,
                             ori_status=const.PROCESSING_STATUS,
                             new_status=const.FAIL_STATUS,
                             new_status=const.FAIL_STATUS,
                         )
                         )
-                    # delete local file and google file
-                    if os.path.exists(video_local_path):
-                        os.remove(video_local_path)
-
-                    google_ai.delete_video(file_name)
-                    log(
-                        task=const.TASK_NAME,
-                        function="extract_best_frame_with_gemini_ai",
-                        message="video process successfully",
-                        data={
-                            "task_id": task["id"],
-                            "file_name": file_name,
-                            "state": state,
-                            "best_frame_time_ms": best_frame_time_ms,
-                        },
-                    )
-                    # except Exception as e:
-                    #     log(
-                    #         task=const.TASK_NAME,
-                    #         function="extract_best_frame_with_gemini_ai",
-                    #         message="task_failed_inside_cycle",
-                    #         data={
-                    #             "task_id": task["id"],
-                    #             "track_back": traceback.format_exc(),
-                    #             "error": str(e),
-                    #         },
-                    #     )
-                    #     update_task_queue_status(
-                    #         db_client=self.db_client,
-                    #         task_id=task["id"],
-                    #         task="extract",
-                    #         ori_status=const.PROCESSING_STATUS,
-                    #         new_status=const.FAIL_STATUS,
-                    #     )
 
 
         except Exception as e:
         except Exception as e:
             log(
             log(
@@ -380,6 +433,14 @@ class ExtractVideoBestFrame:
             )
             )
 
 
     def extract_best_frame_with_gemini_ai(self):
     def extract_best_frame_with_gemini_ai(self):
+        if not self.api_status:
+            log(
+                task=const.TASK_NAME,
+                function="extract_best_frame_with_gemini_ai",
+                message="api_status is False, skip this task",
+            )
+            return
+
         # roll back lock tasks
         # roll back lock tasks
         roll_back_lock_tasks_count = self._roll_back_lock_tasks(task="extract")
         roll_back_lock_tasks_count = self._roll_back_lock_tasks(task="extract")
         log(
         log(
@@ -447,6 +508,13 @@ class ExtractVideoBestFrame:
         """
         """
         get cover with best frame
         get cover with best frame
         """
         """
+        if not self.api_status:
+            log(
+                task=const.TASK_NAME,
+                function="extract_cover_with_ffmpeg",
+                message="api_status is False, skip this task",
+            )
+            return
         # roll back lock tasks
         # roll back lock tasks
         roll_back_lock_tasks_count = self._roll_back_lock_tasks(task="get_cover")
         roll_back_lock_tasks_count = self._roll_back_lock_tasks(task="get_cover")
         log(
         log(