agent-Specialization/users/ziyin/project/content_automation_agent/examples/basic_usage.py
2025-11-14 16:44:12 +08:00

208 lines
5.8 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
基本使用示例
展示如何使用内容自动化创作Agent
"""
import asyncio
import sys
from pathlib import Path
# 添加项目根目录到Python路径
sys.path.append(str(Path(__file__).parent.parent))
from main import ContentAutomationAgent
async def example_single_url():
"""单个URL处理示例"""
print("=== 单个URL处理示例 ===")
# 初始化Agent
agent = ContentAutomationAgent()
# 要处理的URL
url = "https://www.zhihu.com/question/123456789" # 替换为实际的知乎问题URL
try:
# 生成配音文件
result = await agent.process_url(url, output_format="voice")
print(f"处理完成!")
print(f"原始内容长度: {len(result['original_content'])} 字符")
print(f"改写后内容长度: {len(result['rewritten_content'])} 字符")
print(f"生成文件: {result['output_files']}")
# 显示改写后的内容预览
print("\n改写后内容预览:")
print("-" * 50)
print(result['rewritten_content'][:200] + "..." if len(result['rewritten_content']) > 200 else result['rewritten_content'])
print("-" * 50)
except Exception as e:
print(f"处理失败: {e}")
async def example_video_generation():
"""视频生成示例"""
print("\n=== 视频生成示例 ===")
# 初始化Agent
agent = ContentAutomationAgent()
# 要处理的URL
url = "https://www.juejin.cn/post/123456789" # 替换为实际的掘金文章URL
try:
# 生成视频文件
result = await agent.process_url(url, output_format="video")
print(f"视频生成完成!")
print(f"生成文件: {result['output_files']}")
except Exception as e:
print(f"视频生成失败: {e}")
async def example_batch_processing():
"""批量处理示例"""
print("\n=== 批量处理示例 ===")
# 初始化Agent
agent = ContentAutomationAgent()
# 要批量处理的URL列表
urls = [
"https://www.zhihu.com/question/123456789",
"https://www.juejin.cn/post/123456789",
"https://segmentfault.com/a/123456789"
]
try:
# 批量处理
results = await agent.process_batch(urls, output_format="voice")
# 生成报告
report_path = agent.generate_report(results)
print(f"批量处理完成!")
print(f"处理了 {len(results)} 个URL")
print(f"详细报告: {report_path}")
# 统计成功和失败的数量
successful = sum(1 for r in results if not isinstance(r, Exception))
failed = sum(1 for r in results if isinstance(r, Exception))
print(f"成功: {successful}, 失败: {failed}")
except Exception as e:
print(f"批量处理失败: {e}")
async def example_custom_parameters():
"""自定义参数示例"""
print("\n=== 自定义参数示例 ===")
# 初始化Agent
agent = ContentAutomationAgent()
# 要处理的URL
url = "https://example.com/article"
try:
# 自定义语音参数
voice_params = {
'voice_name': 'zh-CN-YunxiNeural', # 使用男声
'speed': 1.2, # 语速稍快
}
# 生成配音文件(带自定义参数)
result = await agent.process_url(url, output_format="voice")
print(f"自定义参数处理完成!")
print(f"生成文件: {result['output_files']}")
# 自定义视频参数
video_params = {
'resolution': '1280x720', # 720p分辨率
'background_color': '#2c3e50', # 深蓝色背景
'text_color': '#ecf0f1', # 浅灰色文字
'font_size': 32, # 稍大字体
'duration_per_sentence': 4, # 每句4秒
}
# 生成视频文件(带自定义参数)
result = await agent.process_url(url, output_format="video")
print(f"自定义视频参数处理完成!")
print(f"生成文件: {result['output_files']}")
except Exception as e:
print(f"自定义参数处理失败: {e}")
async def example_text_only():
"""仅文本处理示例"""
print("\n=== 仅文本处理示例 ===")
# 初始化Agent
agent = ContentAutomationAgent()
# 要处理的URL
url = "https://www.zhihu.com/question/123456789"
try:
# 仅提取和改写文本,不生成媒体文件
result = await agent.process_url(url, output_format="text")
print(f"文本处理完成!")
print(f"原始内容长度: {len(result['original_content'])} 字符")
print(f"改写后内容长度: {len(result['rewritten_content'])} 字符")
# 显示完整内容
print("\n原始内容:")
print("-" * 50)
print(result['original_content'])
print("-" * 50)
print("\n改写后内容:")
print("-" * 50)
print(result['rewritten_content'])
print("-" * 50)
except Exception as e:
print(f"文本处理失败: {e}")
async def main():
"""主函数"""
print("内容自动化创作Agent - 使用示例")
print("=" * 50)
# 运行示例
await example_single_url()
await asyncio.sleep(2) # 等待2秒
await example_video_generation()
await asyncio.sleep(2)
await example_batch_processing()
await asyncio.sleep(2)
await example_custom_parameters()
await asyncio.sleep(2)
await example_text_only()
print("\n" + "=" * 50)
print("所有示例执行完成!")
if __name__ == "__main__":
# 运行异步主函数
asyncio.run(main())