118 lines
4.0 KiB
Python
118 lines
4.0 KiB
Python
#!/usr/bin/env python
|
||
# -*- coding: utf-8 -*-
|
||
|
||
# 导入新旧实现
|
||
from core.contentGen import ContentGenerator as OldContentGenerator
|
||
from utils.content_generator import ContentGenerator as NewContentGenerator
|
||
|
||
def test_both_implementations():
|
||
"""测试新旧实现的兼容性"""
|
||
# 创建测试文本内容
|
||
test_content = """<title>
|
||
🌿清明遛娃天花板!悬空古寺+非遗探秘
|
||
</title>
|
||
<content>
|
||
清明假期带娃哪里玩?泰宁甘露寺藏着明代建筑奇迹!一柱擎天的悬空阁楼+状元祈福传说,让孩子边玩边涨知识✨
|
||
|
||
🎒行程亮点:
|
||
✅ 安全科普第一站:讲解"一柱插地"千年不倒的秘密,用乐高积木模型让孩子理解力学原理
|
||
✅ 文化沉浸体验:穿汉服听"叶状元还愿建寺"故事,触摸3.38米粗的"状元柱"许愿
|
||
</content>
|
||
"""
|
||
|
||
print("=" * 50)
|
||
print("测试新旧ContentGenerator实现")
|
||
print("=" * 50)
|
||
|
||
# 创建输出目录
|
||
import os
|
||
os.makedirs("./test_output", exist_ok=True)
|
||
|
||
# 测试参数
|
||
api_url = "http://localhost:8000/v1" # 替换为实际URL
|
||
model_name = "qwenQWQ" # 替换为实际模型
|
||
api_key = "EMPTY" # 替换为实际密钥
|
||
poster_num = 2 # 生成2个海报配置
|
||
|
||
# 1. 测试旧实现(现在委托给新实现)
|
||
print("\n1. 测试旧实现 (core.contentGen.ContentGenerator)")
|
||
old_generator = OldContentGenerator(
|
||
model_name=model_name,
|
||
api_base_url=api_url,
|
||
api_key=api_key,
|
||
output_dir="./test_output/old"
|
||
)
|
||
|
||
# 设置生成参数
|
||
old_generator.set_model_para(0.7, 0.8, 1.2)
|
||
|
||
# 运行生成
|
||
print("正在使用旧实现生成海报配置...")
|
||
old_result = old_generator.run([], poster_num, test_content)
|
||
|
||
if old_result:
|
||
print(f"旧实现成功生成 {len(old_result)} 个配置项")
|
||
for i, config in enumerate(old_result):
|
||
print(f" 配置 {i+1}: {config.get('main_title')} - {config.get('texts')}")
|
||
else:
|
||
print("旧实现生成失败")
|
||
|
||
# 2. 测试新实现
|
||
print("\n2. 测试新实现 (utils.content_generator.ContentGenerator)")
|
||
new_generator = NewContentGenerator(
|
||
output_dir="./test_output/new",
|
||
temperature=0.7,
|
||
top_p=0.8,
|
||
presence_penalty=1.2
|
||
)
|
||
|
||
# 运行生成
|
||
print("正在使用新实现生成海报配置...")
|
||
new_result = new_generator.run(
|
||
[],
|
||
poster_num,
|
||
test_content,
|
||
api_url=api_url,
|
||
model_name=model_name,
|
||
api_key=api_key
|
||
)
|
||
|
||
if new_result:
|
||
print(f"新实现成功生成 {len(new_result)} 个配置项")
|
||
for i, config in enumerate(new_result):
|
||
print(f" 配置 {i+1}: {config.get('main_title')} - {config.get('texts')}")
|
||
else:
|
||
print("新实现生成失败")
|
||
|
||
print("\n3. 比较结果")
|
||
if old_result and new_result:
|
||
import json
|
||
|
||
# 格式化输出结果比较
|
||
print("\n旧实现结果:")
|
||
print(json.dumps(old_result, ensure_ascii=False, indent=2))
|
||
|
||
print("\n新实现结果:")
|
||
print(json.dumps(new_result, ensure_ascii=False, indent=2))
|
||
|
||
# 结构比较
|
||
old_format = [type(config) for config in old_result]
|
||
new_format = [type(config) for config in new_result]
|
||
|
||
print(f"\n结构比较: 旧实现: {old_format}, 新实现: {new_format}")
|
||
print(f"数量比较: 旧实现: {len(old_result)}, 新实现: {len(new_result)}")
|
||
|
||
# 检查所有必要的字段
|
||
field_check = True
|
||
for result in [old_result, new_result]:
|
||
for config in result:
|
||
if not all(key in config for key in ["index", "main_title", "texts"]):
|
||
field_check = False
|
||
break
|
||
|
||
print(f"字段检查: {'通过' if field_check else '失败'}")
|
||
|
||
print("\n测试完成!")
|
||
|
||
if __name__ == "__main__":
|
||
test_both_implementations() |