jihuaqiang hace 3 semanas
padre
commit
b9d265e4a1
Se han modificado 1 ficheros con 54 adiciones y 31 borrados
  1. 54 31
      utils/google_ai_analyze.py

+ 54 - 31
utils/google_ai_analyze.py

@@ -199,44 +199,67 @@ class GoogleAI(object):
                     "prompt": VIDEO_ANALYSIS_PROMPT, 
                     "model":"gemini-2.0-flash",
                     "temperature":"0.3"
-                }
+                },
+                timeout=300  # 添加超时设置
             )
             response.raise_for_status()
             result = response.json()
-            print(f"[内容分析] API分析完成, 结果: {result}")
+            logger.info(f"[内容分析] API原始响应: {result}")
             
-            if not result or result.get('code') != 0:
-                raise Exception("API分析结果异常")
+            if not result:
+                raise Exception("API返回结果为空")
                 
-            # 解析返回的JSON字符串
-            analysis_result = orjson.loads(result['data']['result'])
-            
-            # 构建analysis_data
-            analysis_data = {
-                '视频选题与要点理解': {
-                    "视频简介": analysis_result.get('视频简介', ''),
-                    "视频内容类型": analysis_result.get('视频内容类型', ''),
-                    "段落类型相似度": analysis_result.get('段落类型相似度', 1)
-                },
-                '视频分段与时间点分析': {
-                    "内容分段": analysis_result.get('内容分段', [])
+            if result.get('code') != 0:
+                error_msg = result.get('msg', '未知错误')
+                raise Exception(f"API返回错误: {error_msg}")
+                
+            if not result.get('data') or not result['data'].get('result'):
+                raise Exception("API返回数据格式错误: 缺少result字段")
+                
+            try:
+                # 解析返回的JSON字符串
+                analysis_result = orjson.loads(result['data']['result'])
+                if not isinstance(analysis_result, dict):
+                    raise ValueError("API返回的result不是有效的JSON对象")
+                    
+                # 构建analysis_data
+                analysis_data = {
+                    '视频选题与要点理解': {
+                        "视频简介": analysis_result.get('视频简介', ''),
+                        "视频内容类型": analysis_result.get('视频内容类型', ''),
+                        "段落类型相似度": analysis_result.get('段落类型相似度', 1)
+                    },
+                    '视频分段与时间点分析': {
+                        "内容分段": analysis_result.get('内容分段', [])
+                    }
                 }
-            }
-            
-            # 使用coze_hook处理数据
-            coze_hook = CozeHook()
-            demand_list = coze_hook.run(
-                analysis_data["视频选题与要点理解"], 
-                analysis_data["视频分段与时间点分析"]
-            )
-
-            print(f"[内容分析] API分析完成, 结果: {analysis_data}, {demand_list}")
-            
-            return analysis_data, demand_list
-            
+                
+                # 使用coze_hook处理数据
+                coze_hook = CozeHook()
+                demand_list = coze_hook.run(
+                    analysis_data["视频选题与要点理解"], 
+                    analysis_data["视频分段与时间点分析"]
+                )
+                
+                if not demand_list:
+                    raise Exception("CozeHook处理结果为空")
+                    
+                logger.info(f"[内容分析] API分析完成, 结果: {analysis_data}, {demand_list}")
+                return analysis_data, demand_list
+                
+            except orjson.JSONDecodeError as e:
+                raise Exception(f"解析API返回的JSON失败: {str(e)}")
+            except Exception as e:
+                raise Exception(f"处理API返回数据时出错: {str(e)}")
+                
+        except requests.exceptions.RequestException as e:
+            error_msg = f"API请求失败: {str(e)}"
+            logger.error(f"[内容分析] {error_msg}")
+            return f"[异常] {error_msg}", None
         except Exception as e:
-            logger.error(f"[内容分析] API分析失败,异常信息{e}")
-            return f"[异常] {e}", ""
+            error_msg = f"API分析失败: {str(e)}"
+            logger.error(f"[内容分析] {error_msg}")
+            return f"[异常] {error_msg}", None