|
@@ -38,55 +38,64 @@ class GoogleAI(object):
|
|
|
|
|
|
@classmethod
|
|
|
def run(cls, api_key, video_path):
|
|
|
- try:
|
|
|
- genai.configure(api_key=api_key)
|
|
|
- video = genai.upload_file(path=video_path, mime_type='video/mp4')
|
|
|
- while video.state.name == 'PROCESSING':
|
|
|
- time.sleep(1)
|
|
|
- video = genai.get_file(name=video.name)
|
|
|
- if video.state.name != 'ACTIVE':
|
|
|
+ max_retries = 3 # 最大重试次数
|
|
|
+ for retry in range(max_retries):
|
|
|
+ try:
|
|
|
+ genai.configure(api_key=api_key)
|
|
|
+ video = genai.upload_file(path=video_path, mime_type='video/mp4')
|
|
|
+ while video.state.name == 'PROCESSING':
|
|
|
+ time.sleep(1)
|
|
|
+ video = genai.get_file(name=video.name)
|
|
|
+ if video.state.name != 'ACTIVE':
|
|
|
+ genai.delete_file(name=video.name)
|
|
|
+ return
|
|
|
+ # 定义 response_schema
|
|
|
+ response_schema = {
|
|
|
+ "type": "object",
|
|
|
+ "properties": {
|
|
|
+ "text": {
|
|
|
+ "type": "string"
|
|
|
+ }
|
|
|
+ },
|
|
|
+ "required": ["text"]
|
|
|
+ }
|
|
|
+ model = genai.GenerativeModel(
|
|
|
+ model_name='gemini-1.5-flash',
|
|
|
+ generation_config=genai.GenerationConfig(response_mime_type='application/json',
|
|
|
+ response_schema=response_schema),
|
|
|
+ safety_settings={
|
|
|
+ HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: HarmBlockThreshold.BLOCK_NONE,
|
|
|
+ },
|
|
|
+ )
|
|
|
+ response = model.generate_content(
|
|
|
+ contents=[
|
|
|
+ video,
|
|
|
+ '你是一名专业的短视频分析师,请你输出这个视频的完整口播,只输出文字即可。使用以下JSON格式输出,不要包含任何额外的解释、注释或非JSON文本:{"text": string}',
|
|
|
+ ],
|
|
|
+ stream=False,
|
|
|
+ request_options={
|
|
|
+ 'timeout': 600,
|
|
|
+ },
|
|
|
+ )
|
|
|
+ # 打印响应内容用于调试
|
|
|
+ logger.info(f"[+] 响应内容: {response.text}")
|
|
|
+ # 使用通用 JSON 解析函数解析响应
|
|
|
+ text = parse_general_json(response.text, key='text')
|
|
|
+ if text is None:
|
|
|
+ return
|
|
|
+ # text = orjson.loads(response.text.strip())['text']
|
|
|
genai.delete_file(name=video.name)
|
|
|
- return
|
|
|
- # 定义 response_schema
|
|
|
- response_schema = {
|
|
|
- "type": "object",
|
|
|
- "properties": {
|
|
|
- "text": {
|
|
|
- "type": "string"
|
|
|
- }
|
|
|
- },
|
|
|
- "required": ["text"]
|
|
|
- }
|
|
|
- model = genai.GenerativeModel(
|
|
|
- model_name='gemini-1.5-flash',
|
|
|
- generation_config=genai.GenerationConfig(response_mime_type='application/json',
|
|
|
- response_schema=response_schema),
|
|
|
- safety_settings={
|
|
|
- HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: HarmBlockThreshold.BLOCK_NONE,
|
|
|
- },
|
|
|
- )
|
|
|
- response = model.generate_content(
|
|
|
- contents=[
|
|
|
- video,
|
|
|
- '你是一名专业的短视频分析师,请你输出这个视频的完整口播,只输出文字即可。使用以下JSON格式输出,不要包含任何额外的解释、注释或非JSON文本:{"text": string}',
|
|
|
- ],
|
|
|
- stream=False,
|
|
|
- request_options={
|
|
|
- 'timeout': 600,
|
|
|
- },
|
|
|
- )
|
|
|
- # 打印响应内容用于调试
|
|
|
- logger.info(f"[+] 响应内容: {response.text}")
|
|
|
- # 使用通用 JSON 解析函数解析响应
|
|
|
- text = parse_general_json(response.text, key='text')
|
|
|
- if text is None:
|
|
|
- return
|
|
|
- # text = orjson.loads(response.text.strip())['text']
|
|
|
- genai.delete_file(name=video.name)
|
|
|
- return text
|
|
|
- except Exception as e:
|
|
|
- logger.error(f"[内容分析] 处理异常, 异常类型: {type(e).__name__}, 异常信息: {e}\n{traceback.format_exc()}")
|
|
|
- return
|
|
|
+ return text
|
|
|
+ except Exception as e:
|
|
|
+ # 异常处理逻辑(添加重试延迟和日志)
|
|
|
+ logger.error(
|
|
|
+ f"[内容分析] 第 {retry + 1}/{max_retries} 次尝试失败, 异常信息: {e}\n{traceback.format_exc()}")
|
|
|
+ if retry < max_retries - 1: # 非最后一次重试,等待后继续
|
|
|
+ time.sleep(2 ** retry) # 指数退避延迟(1s, 2s, 4s)
|
|
|
+ else: # 最后一次重试失败,返回失败标识
|
|
|
+ genai.delete_file(name=video.name) if 'video' in locals() else None
|
|
|
+ logger.error("[内容分析] 已耗尽重试次数,任务失败")
|
|
|
+ return
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|