添加 文生图

This commit is contained in:
root
2025-07-11 13:47:27 +08:00
parent e2ffc87d53
commit fb5cf0f8f1
12 changed files with 3229 additions and 5 deletions

View File

@@ -0,0 +1,416 @@
/**
* Text Video Agent API 使用示例
*/
import {
TextVideoAgentAPI,
textVideoAgentAPI,
ImageGenerationParams,
VideoGenerationParams,
TaskRequest
} from '../src/services/textVideoAgentAPI'
import {
TaskType,
AspectRatio,
VideoDuration,
PRESET_CONFIGS
} from '../src/services/textVideoAgentTypes'
// ==================== 基础使用示例 ====================
/**
* 示例1: 基础健康检查
*/
async function example1_HealthCheck() {
console.log('=== 健康检查示例 ===')
try {
const health = await textVideoAgentAPI.healthCheck()
console.log('服务状态:', health)
const mjHealth = await textVideoAgentAPI.mjHealthCheck()
console.log('Midjourney状态:', mjHealth)
const jmHealth = await textVideoAgentAPI.jmHealthCheck()
console.log('极梦状态:', jmHealth)
} catch (error) {
console.error('健康检查失败:', error)
}
}
/**
* 示例2: 获取示例提示词
*/
async function example2_GetSamplePrompts() {
console.log('=== 获取示例提示词 ===')
try {
const prompts = await textVideoAgentAPI.getSamplePrompt(TaskType.VLOG)
console.log('VLOG类型示例提示词:', prompts)
} catch (error) {
console.error('获取提示词失败:', error)
}
}
/**
* 示例3: 文件上传
*/
async function example3_FileUpload() {
console.log('=== 文件上传示例 ===')
// 注意:这里需要实际的文件对象
// const fileInput = document.getElementById('fileInput') as HTMLInputElement
// const file = fileInput.files?.[0]
// 模拟文件对象(实际使用时替换为真实文件)
const mockFile = new File(['mock content'], 'test.jpg', { type: 'image/jpeg' })
try {
const uploadResult = await textVideoAgentAPI.uploadFile(mockFile)
console.log('文件上传结果:', uploadResult)
return uploadResult.data // 返回文件URL
} catch (error) {
console.error('文件上传失败:', error)
}
}
// ==================== 图片生成示例 ====================
/**
* 示例4: 基础图片生成
*/
async function example4_BasicImageGeneration() {
console.log('=== 基础图片生成示例 ===')
const params: ImageGenerationParams = {
prompt: '一个美丽的女孩在喝茶,温馨的下午时光,自然光线,高质量摄影',
max_wait_time: 120,
poll_interval: 2
}
try {
const result = await textVideoAgentAPI.generateImageSync(params)
console.log('图片生成结果:', result)
return result.data?.image_url
} catch (error) {
console.error('图片生成失败:', error)
}
}
/**
* 示例5: 带参考图片的图片生成
*/
async function example5_ImageGenerationWithReference() {
console.log('=== 带参考图片的图片生成示例 ===')
// 模拟参考图片文件
const referenceImage = new File(['reference'], 'reference.jpg', { type: 'image/jpeg' })
const params: ImageGenerationParams = {
prompt: '保持人物特征,改变背景为咖啡厅环境',
img_file: referenceImage,
...PRESET_CONFIGS.STANDARD
}
try {
const result = await textVideoAgentAPI.generateImageWithRetry(params, 3)
console.log('带参考图片生成结果:', result)
return result.data?.image_url
} catch (error) {
console.error('带参考图片生成失败:', error)
}
}
/**
* 示例6: 异步图片生成
*/
async function example6_AsyncImageGeneration() {
console.log('=== 异步图片生成示例 ===')
const prompt = '现代简约风格的室内设计,明亮的客厅'
try {
// 提交异步任务
const taskResult = await textVideoAgentAPI.generateImageAsync(prompt)
const taskId = taskResult.data?.task_id
if (!taskId) {
throw new Error('未获取到任务ID')
}
console.log('任务已提交ID:', taskId)
// 轮询任务状态
const finalResult = await textVideoAgentAPI.pollTaskUntilComplete(
taskId,
2000, // 2秒轮询间隔
120000, // 2分钟超时
(status) => {
console.log('任务状态更新:', status)
}
)
console.log('异步图片生成完成:', finalResult)
return finalResult.data?.image_url
} catch (error) {
console.error('异步图片生成失败:', error)
}
}
// ==================== 视频生成示例 ====================
/**
* 示例7: 基础视频生成
*/
async function example7_BasicVideoGeneration() {
console.log('=== 基础视频生成示例 ===')
// 首先生成一张图片作为视频的基础
const imageUrl = await example4_BasicImageGeneration()
if (!imageUrl) {
console.error('无法获取基础图片,跳过视频生成')
return
}
const params: VideoGenerationParams = {
prompt: '女孩优雅地品茶,轻柔的动作,温馨的氛围',
img_url: imageUrl,
duration: VideoDuration.MEDIUM,
max_wait_time: 300,
poll_interval: 5
}
try {
const result = await textVideoAgentAPI.generateVideoSync(params)
console.log('视频生成结果:', result)
return result.data?.video_url
} catch (error) {
console.error('视频生成失败:', error)
}
}
/**
* 示例8: 异步视频生成
*/
async function example8_AsyncVideoGeneration() {
console.log('=== 异步视频生成示例 ===')
const params: VideoGenerationParams = {
prompt: '城市夜景延时摄影,车流如水,霓虹闪烁',
duration: VideoDuration.LONG
}
try {
const taskResult = await textVideoAgentAPI.generateVideoAsync(params)
const taskId = taskResult.data?.task_id
if (!taskId) {
throw new Error('未获取到任务ID')
}
console.log('视频生成任务已提交ID:', taskId)
// 轮询任务状态
const finalResult = await textVideoAgentAPI.pollTaskUntilComplete(
taskId,
5000, // 5秒轮询间隔
600000, // 10分钟超时
(status) => {
console.log('视频生成进度:', status)
}
)
console.log('异步视频生成完成:', finalResult)
return finalResult.data?.video_url
} catch (error) {
console.error('异步视频生成失败:', error)
}
}
// ==================== 高级功能示例 ====================
/**
* 示例9: 图片描述功能
*/
async function example9_ImageDescription() {
console.log('=== 图片描述示例 ===')
const imageUrl = 'https://example.com/sample-image.jpg'
try {
const description = await textVideoAgentAPI.describeImageByUrl({
image_url: imageUrl,
max_wait_time: 60
})
console.log('图片描述结果:', description)
return description.data?.description
} catch (error) {
console.error('图片描述失败:', error)
}
}
/**
* 示例10: 任务管理
*/
async function example10_TaskManagement() {
console.log('=== 任务管理示例 ===')
const taskRequest: TaskRequest = {
task_type: TaskType.VLOG,
prompt: '制作一个关于健康生活方式的短视频',
ar: AspectRatio.PORTRAIT
}
try {
// 创建任务
const createResult = await textVideoAgentAPI.createTask(taskRequest)
const taskId = createResult.data?.task_id
if (!taskId) {
throw new Error('任务创建失败')
}
console.log('任务创建成功ID:', taskId)
// 查询任务状态
const statusResult = await textVideoAgentAPI.getTaskStatusAsync(taskId)
console.log('任务状态:', statusResult)
// 等待任务完成(同步方式)
const finalResult = await textVideoAgentAPI.getTaskResultSync(taskId)
console.log('任务最终结果:', finalResult)
return finalResult
} catch (error) {
console.error('任务管理失败:', error)
}
}
/**
* 示例11: 端到端内容生成
*/
async function example11_EndToEndGeneration() {
console.log('=== 端到端内容生成示例 ===')
try {
const result = await textVideoAgentAPI.generateContentEndToEnd(
'一个关于咖啡文化的精美内容,展现咖啡师的专业技艺',
{
taskType: TaskType.VLOG,
aspectRatio: AspectRatio.PORTRAIT,
videoDuration: VideoDuration.MEDIUM,
generateVideo: true,
onProgress: (step, progress) => {
console.log(`进度更新: ${step} - ${progress}%`)
}
}
)
console.log('端到端生成完成:', result)
return result
} catch (error) {
console.error('端到端生成失败:', error)
}
}
// ==================== 批量处理示例 ====================
/**
* 示例12: 批量图片生成
*/
async function example12_BatchImageGeneration() {
console.log('=== 批量图片生成示例 ===')
const prompts = [
'春天的樱花盛开',
'夏日的海滩风光',
'秋天的枫叶满山',
'冬日的雪景如画'
]
try {
const results = await Promise.allSettled(
prompts.map(async (prompt, index) => {
const params: ImageGenerationParams = {
prompt,
...PRESET_CONFIGS.FAST // 使用快速配置
}
console.log(`开始生成图片 ${index + 1}: ${prompt}`)
const result = await textVideoAgentAPI.generateImageSync(params)
console.log(`图片 ${index + 1} 生成完成`)
return {
prompt,
result: result.data?.image_url,
success: result.status
}
})
)
console.log('批量图片生成结果:', results)
return results
} catch (error) {
console.error('批量图片生成失败:', error)
}
}
// ==================== 主函数 ====================
/**
* 运行所有示例
*/
async function runAllExamples() {
console.log('开始运行 Text Video Agent API 示例...\n')
// 基础功能示例
await example1_HealthCheck()
await example2_GetSamplePrompts()
// 文件操作示例
// await example3_FileUpload() // 需要实际文件
// 图片生成示例
await example4_BasicImageGeneration()
// await example5_ImageGenerationWithReference() // 需要实际文件
await example6_AsyncImageGeneration()
// 视频生成示例
await example7_BasicVideoGeneration()
await example8_AsyncVideoGeneration()
// 高级功能示例
await example9_ImageDescription()
await example10_TaskManagement()
await example11_EndToEndGeneration()
// 批量处理示例
await example12_BatchImageGeneration()
console.log('\n所有示例运行完成')
}
// 导出示例函数
export {
example1_HealthCheck,
example2_GetSamplePrompts,
example3_FileUpload,
example4_BasicImageGeneration,
example5_ImageGenerationWithReference,
example6_AsyncImageGeneration,
example7_BasicVideoGeneration,
example8_AsyncVideoGeneration,
example9_ImageDescription,
example10_TaskManagement,
example11_EndToEndGeneration,
example12_BatchImageGeneration,
runAllExamples
}
// 如果直接运行此文件,执行所有示例
if (require.main === module) {
runAllExamples().catch(console.error)
}