|
@@ -201,94 +201,98 @@ def analyze_video(model: genai.GenerativeModel, google_file: File, prompt: str,
|
|
|
|
|
|
|
|
|
def run():
|
|
|
- if not API_KEY:
|
|
|
- logger.error('[+] 请在环境变量中新增 API_KEY')
|
|
|
- return
|
|
|
- if not TASK_TYPE:
|
|
|
- logger.error('[+] 请在环境变量中新增 TASK_TYPE, 可选值: recommend | top')
|
|
|
- return
|
|
|
- genai.configure(api_key=API_KEY)
|
|
|
+ video_path = None
|
|
|
+ try:
|
|
|
+ if not API_KEY:
|
|
|
+ logger.error('[+] 请在环境变量中新增 API_KEY')
|
|
|
+ return
|
|
|
+ if not TASK_TYPE:
|
|
|
+ logger.error('[+] 请在环境变量中新增 TASK_TYPE, 可选值: recommend | top')
|
|
|
+ return
|
|
|
+ genai.configure(api_key=API_KEY)
|
|
|
|
|
|
- redis_task = get_redis_task(task_type=TASK_TYPE)
|
|
|
- if not redis_task:
|
|
|
- time.sleep(10)
|
|
|
- return
|
|
|
- redis_task = json.loads(redis_task)
|
|
|
- mark, prompt = Material.ad_feishu_list()
|
|
|
+ redis_task = get_redis_task(task_type=TASK_TYPE)
|
|
|
+ if not redis_task:
|
|
|
+ time.sleep(10)
|
|
|
+ return
|
|
|
+ redis_task = json.loads(redis_task)
|
|
|
+ mark, prompt = Material.ad_feishu_list()
|
|
|
|
|
|
- # video_duration = get_video_duration(video_link=redis_task[3])
|
|
|
- # if not video_duration:
|
|
|
- # AliyunLogger.logging( str( redis_task[0] ), redis_task[1], redis_task['video_path'], "",
|
|
|
- # redis_task['type'], redis_task['partition'], "[+] 获取视频时长失败, 跳过任务" )
|
|
|
- # logger.error('[+] 获取视频时长失败, 跳过任务')
|
|
|
- # return
|
|
|
- # elif video_duration >= 600:
|
|
|
- # AliyunLogger.logging( str( redis_task['video_id'] ), redis_task['title'], redis_task['video_path'], "",
|
|
|
- # redis_task['type'], redis_task['partition'], "[+] 视频时长超过10分钟, 跳过任务" )
|
|
|
- # logger.error('[+] 视频时长超过10分钟, 跳过任务')
|
|
|
- # return
|
|
|
+ # video_duration = get_video_duration(video_link=redis_task[3])
|
|
|
+ # if not video_duration:
|
|
|
+ # AliyunLogger.logging( str( redis_task[0] ), redis_task[1], redis_task['video_path'], "",
|
|
|
+ # redis_task['type'], redis_task['partition'], "[+] 获取视频时长失败, 跳过任务" )
|
|
|
+ # logger.error('[+] 获取视频时长失败, 跳过任务')
|
|
|
+ # return
|
|
|
+ # elif video_duration >= 600:
|
|
|
+ # AliyunLogger.logging( str( redis_task['video_id'] ), redis_task['title'], redis_task['video_path'], "",
|
|
|
+ # redis_task['type'], redis_task['partition'], "[+] 视频时长超过10分钟, 跳过任务" )
|
|
|
+ # logger.error('[+] 视频时长超过10分钟, 跳过任务')
|
|
|
+ # return
|
|
|
|
|
|
- video_path = download_video(video_link=redis_task['material_address'])
|
|
|
- if not video_path:
|
|
|
- AliyunLogger.ad_logging( str(redis_task['ad_id']),
|
|
|
- redis_task['creative_code'],
|
|
|
- redis_task['creative_title'],
|
|
|
- redis_task['material_address'],
|
|
|
- redis_task['click_button_text'],
|
|
|
- redis_task['creative_logo_address'],
|
|
|
- redis_task['update_time'],
|
|
|
- "[+] 视频下载失败, 跳过任务" )
|
|
|
- logger.error(f'[+] 视频下载失败, 跳过任务')
|
|
|
- if os.path.exists(video_path):
|
|
|
- os.remove(video_path)
|
|
|
- logger.info(f"文件已删除: {video_path}")
|
|
|
- return
|
|
|
+ video_path = download_video(video_link=redis_task['material_address'])
|
|
|
+ if not video_path:
|
|
|
+ AliyunLogger.ad_logging( str(redis_task['ad_id']),
|
|
|
+ redis_task['creative_code'],
|
|
|
+ redis_task['creative_title'],
|
|
|
+ redis_task['material_address'],
|
|
|
+ redis_task['click_button_text'],
|
|
|
+ redis_task['creative_logo_address'],
|
|
|
+ redis_task['update_time'],
|
|
|
+ "[+] 视频下载失败, 跳过任务" )
|
|
|
+ logger.error(f'[+] 视频下载失败, 跳过任务')
|
|
|
+ if os.path.exists(video_path):
|
|
|
+ os.remove(video_path)
|
|
|
+ logger.info(f"文件已删除: {video_path}")
|
|
|
+ return
|
|
|
|
|
|
- google_file, google_file_state = upload_video(video_path=video_path, redis_task=redis_task)
|
|
|
- if not google_file_state:
|
|
|
- return
|
|
|
- elif google_file_state != 'ACTIVE':
|
|
|
- logger.error('[+] 视频上传状态不为 ACTIVE, 跳过任务')
|
|
|
- genai.delete_file(google_file)
|
|
|
- if os.path.exists(video_path):
|
|
|
- os.remove(video_path)
|
|
|
- logger.info(f"文件已删除: {video_path}")
|
|
|
- return
|
|
|
+ google_file, google_file_state = upload_video(video_path=video_path, redis_task=redis_task)
|
|
|
+ if not google_file_state:
|
|
|
+ return
|
|
|
+ elif google_file_state != 'ACTIVE':
|
|
|
+ logger.error('[+] 视频上传状态不为 ACTIVE, 跳过任务')
|
|
|
+ genai.delete_file(google_file)
|
|
|
+ if os.path.exists(video_path):
|
|
|
+ os.remove(video_path)
|
|
|
+ logger.info(f"文件已删除: {video_path}")
|
|
|
+ return
|
|
|
|
|
|
- model = create_model_cache(redis_task=redis_task)
|
|
|
- if isinstance(model, str):
|
|
|
- logger.error('[+] 创建模型失败, 跳过任务')
|
|
|
- genai.delete_file(google_file)
|
|
|
+ model = create_model_cache(redis_task=redis_task)
|
|
|
+ if isinstance(model, str):
|
|
|
+ logger.error('[+] 创建模型失败, 跳过任务')
|
|
|
+ genai.delete_file(google_file)
|
|
|
+ if os.path.exists(video_path):
|
|
|
+ os.remove(video_path)
|
|
|
+ logger.info(f"文件已删除: {video_path}")
|
|
|
+ return
|
|
|
+
|
|
|
+ response = analyze_video(model=model, google_file=google_file, prompt=prompt, redis_task=redis_task)
|
|
|
+ if isinstance(response, str):
|
|
|
+ logger.error('[+] 获取模型响应失败, 跳过任务')
|
|
|
+ genai.delete_file(google_file)
|
|
|
+ if os.path.exists(video_path):
|
|
|
+ os.remove(video_path)
|
|
|
+ logger.info(f"文件已删除: {video_path}")
|
|
|
+ return
|
|
|
+
|
|
|
+ text = response.text.strip()
|
|
|
+ cleaned_text = text.replace("```json", '').replace("```", '').strip()
|
|
|
+ AliyunLogger.ad_logging(str(redis_task['ad_id']),
|
|
|
+ redis_task['creative_code'],
|
|
|
+ redis_task['creative_title'],
|
|
|
+ redis_task['material_address'],
|
|
|
+ redis_task['click_button_text'],
|
|
|
+ redis_task['creative_logo_address'],
|
|
|
+ redis_task['update_time'],
|
|
|
+ str(cleaned_text))
|
|
|
+ logger.info(f'[+] 模型响应结果: {text}')
|
|
|
if os.path.exists(video_path):
|
|
|
os.remove(video_path)
|
|
|
logger.info(f"文件已删除: {video_path}")
|
|
|
- return
|
|
|
-
|
|
|
- response = analyze_video(model=model, google_file=google_file, prompt=prompt, redis_task=redis_task)
|
|
|
- if isinstance(response, str):
|
|
|
- logger.error('[+] 获取模型响应失败, 跳过任务')
|
|
|
genai.delete_file(google_file)
|
|
|
+ except KeyboardInterrupt:
|
|
|
if os.path.exists(video_path):
|
|
|
os.remove(video_path)
|
|
|
- logger.info(f"文件已删除: {video_path}")
|
|
|
- return
|
|
|
-
|
|
|
- text = response.text.strip()
|
|
|
- cleaned_text = text.replace("```json", '').replace("```", '').strip()
|
|
|
- AliyunLogger.ad_logging(str(redis_task['ad_id']),
|
|
|
- redis_task['creative_code'],
|
|
|
- redis_task['creative_title'],
|
|
|
- redis_task['material_address'],
|
|
|
- redis_task['click_button_text'],
|
|
|
- redis_task['creative_logo_address'],
|
|
|
- redis_task['update_time'],
|
|
|
- str(cleaned_text))
|
|
|
- logger.info(f'[+] 模型响应结果: {text}')
|
|
|
- if os.path.exists(video_path):
|
|
|
- os.remove(video_path)
|
|
|
- logger.info(f"文件已删除: {video_path}")
|
|
|
- genai.delete_file(google_file)
|
|
|
-
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
logger.info(f'[+] 任务已启动 -> API_KEY: {API_KEY}, TASK_TYPE: {TASK_TYPE}')
|