You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
170 lines
5.2 KiB
170 lines
5.2 KiB
"""
|
|
AI分析功能测试脚本
|
|
"""
|
|
|
|
import os
|
|
import sys
|
|
from pathlib import Path
|
|
from dotenv import load_dotenv
|
|
|
|
# 加载环境变量(从项目根目录)
|
|
env_path = Path(__file__).parent / '.env'
|
|
load_dotenv(dotenv_path=env_path)
|
|
|
|
from ai_analyzer import AIAnalyzer
|
|
|
|
|
|
def test_analyze_sample_data():
|
|
"""测试分析示例数据"""
|
|
|
|
# 示例视频数据
|
|
sample_videos = [
|
|
{
|
|
"index": 1,
|
|
"author": "游戏解说小王",
|
|
"description": "王者荣耀S35赛季上分攻略,这几个英雄必须会!#王者荣耀 #游戏攻略 #上分技巧",
|
|
"duration": "03:25",
|
|
"hot": "98.5万",
|
|
"plays": "150万",
|
|
"likes": "8.5万",
|
|
"comments": "3200",
|
|
"hotWords": ["上分", "新赛季", "英雄推荐"],
|
|
"hashTags": ["#王者荣耀", "#游戏攻略", "#上分技巧"]
|
|
},
|
|
{
|
|
"index": 2,
|
|
"author": "电竞小姐姐",
|
|
"description": "原神新角色实测,伤害爆炸!#原神 #游戏测评",
|
|
"duration": "05:12",
|
|
"hot": "125万",
|
|
"plays": "200万",
|
|
"likes": "12万",
|
|
"comments": "5600",
|
|
"hotWords": ["新角色", "伤害测试", "抽卡"],
|
|
"hashTags": ["#原神", "#游戏测评"]
|
|
},
|
|
{
|
|
"index": 3,
|
|
"author": "游戏日常",
|
|
"description": "和平精英新地图探索,这些点位太强了!#和平精英 #吃鸡游戏",
|
|
"duration": "04:30",
|
|
"hot": "76万",
|
|
"plays": "120万",
|
|
"likes": "6.2万",
|
|
"comments": "2100",
|
|
"hotWords": ["新地图", "点位", "战术"],
|
|
"hashTags": ["#和平精英", "#吃鸡游戏"]
|
|
}
|
|
]
|
|
|
|
print("=" * 80)
|
|
print("AI分析功能测试")
|
|
print("=" * 80)
|
|
print()
|
|
|
|
# 检查API Key
|
|
api_key = os.getenv("DASHSCOPE_API_KEY")
|
|
if not api_key:
|
|
print("⚠ 警告:未设置DASHSCOPE_API_KEY环境变量")
|
|
print("请在 .env 文件中配置API Key,或者:")
|
|
print(" export DASHSCOPE_API_KEY=your_api_key")
|
|
print()
|
|
return
|
|
|
|
print(f"✓ API Key已配置(前8位): {api_key[:8]}...")
|
|
print()
|
|
|
|
try:
|
|
# 创建分析器
|
|
print("正在初始化AI分析器...")
|
|
analyzer = AIAnalyzer(model="qwen-plus")
|
|
print("✓ 分析器初始化成功")
|
|
print()
|
|
|
|
# 分析数据
|
|
print("正在分析示例数据...")
|
|
print(f" 视频数量: {len(sample_videos)}")
|
|
print(f" 使用模型: qwen-plus")
|
|
print(f" 提示词文件: prompts/analyze_prompt.md")
|
|
print()
|
|
|
|
result = analyzer.analyze(
|
|
videos=sample_videos,
|
|
prompt_file="prompts/analyze_prompt.md",
|
|
custom_instruction="重点分析游戏类视频的爆款特征和创作建议"
|
|
)
|
|
|
|
if result["success"]:
|
|
print("=" * 80)
|
|
print("分析结果:")
|
|
print("=" * 80)
|
|
print(result["analysis"])
|
|
print()
|
|
print("=" * 80)
|
|
print("统计信息:")
|
|
print("=" * 80)
|
|
print(f" 视频数量: {result['video_count']}")
|
|
print(f" 使用模型: {result['model']}")
|
|
print(f" 输入Token: {result['usage']['input_tokens']}")
|
|
print(f" 输出Token: {result['usage']['output_tokens']}")
|
|
print(f" 总Token: {result['usage']['total_tokens']}")
|
|
print()
|
|
print("✓ 测试成功!")
|
|
else:
|
|
print(f"✗ 分析失败: {result['error']}")
|
|
|
|
except Exception as e:
|
|
print(f"✗ 测试出错: {e}")
|
|
import traceback
|
|
traceback.print_exc()
|
|
|
|
|
|
def test_analyze_from_file():
|
|
"""测试从文件分析"""
|
|
|
|
print("\n" + "=" * 80)
|
|
print("测试从文件分析")
|
|
print("=" * 80)
|
|
print()
|
|
|
|
# 查找可用的JSON文件
|
|
data_dir = "douyin_data_soupce/douyin_data"
|
|
if not os.path.exists(data_dir):
|
|
print(f"⚠ 数据目录不存在: {data_dir}")
|
|
return
|
|
|
|
json_files = [f for f in os.listdir(data_dir) if f.endswith('.json')]
|
|
if not json_files:
|
|
print(f"⚠ 未找到JSON数据文件")
|
|
return
|
|
|
|
# 使用第一个JSON文件
|
|
json_file = os.path.join(data_dir, json_files[0])
|
|
print(f"使用文件: {json_file}")
|
|
print()
|
|
|
|
try:
|
|
analyzer = AIAnalyzer(model="qwen-plus")
|
|
|
|
result = analyzer.analyze_from_file(
|
|
json_file=json_file,
|
|
prompt_file="prompts/analyze_prompt.md"
|
|
)
|
|
|
|
if result["success"]:
|
|
print("✓ 文件分析成功")
|
|
print(f" 视频数量: {result['video_count']}")
|
|
print(f" Token消耗: {result['usage']['total_tokens']}")
|
|
else:
|
|
print(f"✗ 分析失败: {result['error']}")
|
|
|
|
except Exception as e:
|
|
print(f"✗ 测试出错: {e}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
# 测试1:分析示例数据
|
|
test_analyze_sample_data()
|
|
|
|
# 测试2:从文件分析(可选)
|
|
# test_analyze_from_file()
|
|
|