|
@@ -1,18 +1,12 @@
|
|
|
import asyncio
|
|
|
import os
|
|
|
import time
|
|
|
-
|
|
|
import cv2
|
|
|
-import aiohttp
|
|
|
import requests
|
|
|
import google.generativeai as genai
|
|
|
-
|
|
|
import uuid
|
|
|
-
|
|
|
from google.generativeai.types import HarmCategory, HarmBlockThreshold
|
|
|
-
|
|
|
from common import Common
|
|
|
-from common.feishu_data import Material
|
|
|
|
|
|
|
|
|
class VideoAnalyzer:
|
|
@@ -63,6 +57,12 @@ class VideoAnalyzer:
|
|
|
print("达到最大重试次数,视频下载失败")
|
|
|
return None
|
|
|
|
|
|
+ async def delete_video(self):
|
|
|
+ try:
|
|
|
+ self.video_file.delete()
|
|
|
+ except Exception as e:
|
|
|
+ Common.logger( "ai" ).info( f'视频删除异常{e}' )
|
|
|
+
|
|
|
async def upload_video(self, save_path, mime_type = None):
|
|
|
"""上传视频文件并获取视频文件对象"""
|
|
|
try:
|
|
@@ -70,7 +70,6 @@ class VideoAnalyzer:
|
|
|
await self._wait_for_processing()
|
|
|
except Exception as e:
|
|
|
Common.logger("ai").info(f'上传视频文件并获取视频文件对象失败异常信息{e}')
|
|
|
- self.video_file.delete()
|
|
|
return f"上传视频文件并获取视频文件对象失败:{e}"
|
|
|
|
|
|
async def _wait_for_processing(self):
|
|
@@ -100,7 +99,6 @@ class VideoAnalyzer:
|
|
|
return model
|
|
|
except Exception as e:
|
|
|
Common.logger("ai").info(f'视频创建缓存内容,并返回生成模型异常信息{e}')
|
|
|
- self.video_file.delete()
|
|
|
print(f"视频创建缓存内容,并返回生成模型异常信息{e}")
|
|
|
return f"视频创建缓存内容,并返回生成模型异常信息{e}"
|
|
|
|
|
@@ -116,11 +114,9 @@ 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}" )
|
|
|
return f"视频处理请求失败:{e}"
|
|
|
|
|
@@ -158,10 +154,12 @@ async def main(video_path, api_key, prompt, mark, sample_data):
|
|
|
# 上传并处理视频
|
|
|
upload_response = await analyzer.upload_video( save_path )
|
|
|
if upload_response:
|
|
|
+ await analyzer.delete_video()
|
|
|
return upload_response, mark
|
|
|
# 创建缓存模型
|
|
|
model =await analyzer.create_cache()
|
|
|
if isinstance( model, str ):
|
|
|
+ await analyzer.delete_video()
|
|
|
return model, mark
|
|
|
print("创建缓存模型成功")
|
|
|
# sample_data = {
|
|
@@ -199,12 +197,14 @@ async def main(video_path, api_key, prompt, mark, sample_data):
|
|
|
# }
|
|
|
response =await analyzer.analyze_video( model, prompt, sample_data )
|
|
|
if isinstance( response, str ):
|
|
|
+ await analyzer.delete_video()
|
|
|
return response, mark
|
|
|
print( response.usage_metadata )
|
|
|
print(response.text)
|
|
|
if os.path.exists( save_path ):
|
|
|
os.remove( save_path )
|
|
|
print( f"文件已删除: {save_path}" )
|
|
|
+ await analyzer.delete_video()
|
|
|
return response.text, mark
|
|
|
except Exception as e:
|
|
|
attempt += 1 # 增加尝试次数
|