优化接口
This commit is contained in:
parent
56aedee011
commit
bd60524fde
@ -90,15 +90,15 @@ export default router.post(
|
||||
})),
|
||||
),
|
||||
);
|
||||
|
||||
const generateTask = async (item: (typeof storyboardData)[number]) => {
|
||||
const repeloadObj = {
|
||||
prompt: item.prompt!,
|
||||
size: projectSettingData?.imageQuality as "1K" | "2K" | "4K",
|
||||
aspectRatio: projectSettingData?.videoRatio as `${number}:${number}`,
|
||||
};
|
||||
|
||||
await u.Ai.Image(projectSettingData?.imageModel as `${string}:${string}`)
|
||||
.run(
|
||||
try {
|
||||
const imageCls = await u.Ai.Image(projectSettingData?.imageModel as `${string}:${string}`).run(
|
||||
{
|
||||
referenceList: await getAssetsImageBase64(assetRecord[item.id!] || []),
|
||||
...repeloadObj,
|
||||
@ -109,25 +109,22 @@ export default router.post(
|
||||
relatedObjects: JSON.stringify(repeloadObj),
|
||||
projectId: projectId,
|
||||
},
|
||||
)
|
||||
.then(async (imageCls) => {
|
||||
);
|
||||
const savePath = `/${projectId}/assets/${scriptId}/${u.uuid()}.jpg`;
|
||||
await imageCls.save(savePath);
|
||||
await u.db("o_storyboard").where("id", item.id).update({
|
||||
filePath: savePath,
|
||||
state: "已完成",
|
||||
});
|
||||
})
|
||||
.catch(async (e) => {
|
||||
await u
|
||||
.db("o_storyboard")
|
||||
} catch (e) {
|
||||
u.db("o_storyboard")
|
||||
.where("id", item.id)
|
||||
.update({
|
||||
filePath: "",
|
||||
reason: u.error(e).message,
|
||||
state: "生成失败",
|
||||
});
|
||||
});
|
||||
}
|
||||
};
|
||||
// 按 concurrentCount 控制并发数,分批执行;跳过 shouldGenerateImage === 0 的分镜
|
||||
let generateList = [];
|
||||
|
||||
@ -94,15 +94,8 @@ export default router.post(
|
||||
);
|
||||
|
||||
res.status(200).send(success(tasks.map((t) => ({ videoId: t.videoId, trackId: t.trackId }))));
|
||||
|
||||
// 分批执行,3个一批
|
||||
(async () => {
|
||||
const batchSize = 3;
|
||||
for (let i = 0; i < tasks.length; i += batchSize) {
|
||||
const batch = tasks.slice(i, i + batchSize);
|
||||
await Promise.all(
|
||||
batch.map(async ({ videoId, videoPath, prompt, duration, images }) => {
|
||||
// 调用时再转 base64
|
||||
for (const { videoId, videoPath, prompt, duration, images } of tasks) {
|
||||
// 所有任务全部并发后台执行,完全不阻塞任何进程
|
||||
const base64 = await Promise.all(
|
||||
images.map(async (item) => {
|
||||
if (!item) return null;
|
||||
@ -111,7 +104,7 @@ export default router.post(
|
||||
);
|
||||
const relatedObjects = { projectId, videoId, scriptId, type: "视频" };
|
||||
const aiVideo = u.Ai.Video(model);
|
||||
return aiVideo
|
||||
aiVideo
|
||||
.run(
|
||||
{
|
||||
prompt,
|
||||
@ -129,8 +122,8 @@ export default router.post(
|
||||
relatedObjects: JSON.stringify(relatedObjects),
|
||||
},
|
||||
)
|
||||
.then(() => aiVideo.save(videoPath))
|
||||
.then(() => u.db("o_video").where("id", videoId).update({ state: "生成成功" }))
|
||||
.then(async () => await aiVideo.save(videoPath))
|
||||
.then(async () => await u.db("o_video").where("id", videoId).update({ state: "生成成功" }))
|
||||
.catch(async (error: any) => {
|
||||
await u
|
||||
.db("o_video")
|
||||
@ -140,9 +133,6 @@ export default router.post(
|
||||
errorReason: u.error(error).message,
|
||||
});
|
||||
});
|
||||
}),
|
||||
);
|
||||
}
|
||||
})();
|
||||
},
|
||||
);
|
||||
|
||||
@ -84,8 +84,6 @@ export default router.post(
|
||||
videoTrackId: trackId,
|
||||
});
|
||||
res.status(200).send(success(videoId));
|
||||
(async () => {
|
||||
try {
|
||||
const relatedObjects = {
|
||||
projectId,
|
||||
videoId,
|
||||
@ -93,9 +91,8 @@ export default router.post(
|
||||
type: "视频",
|
||||
};
|
||||
const aiVideo = u.Ai.Video(model);
|
||||
|
||||
console.log("%c Line:47 🍩 modeData", "background:#93c0a4", modeData);
|
||||
await aiVideo.run(
|
||||
aiVideo
|
||||
.run(
|
||||
{
|
||||
prompt,
|
||||
referenceList: base64.filter(Boolean) as ReferenceList[],
|
||||
@ -111,10 +108,10 @@ export default router.post(
|
||||
describe: "根据提示词生成视频",
|
||||
relatedObjects: JSON.stringify(relatedObjects),
|
||||
},
|
||||
);
|
||||
await aiVideo.save(videoPath);
|
||||
await u.db("o_video").where("id", videoId).update({ state: "生成成功" });
|
||||
} catch (error: any) {
|
||||
)
|
||||
.then(async () => await aiVideo.save(videoPath))
|
||||
.then(async () => await u.db("o_video").where("id", videoId).update({ state: "生成成功" }))
|
||||
.catch(async (error: any) => {
|
||||
await u
|
||||
.db("o_video")
|
||||
.where("id", videoId)
|
||||
@ -122,7 +119,6 @@ export default router.post(
|
||||
state: "生成失败",
|
||||
errorReason: u.error(error).message,
|
||||
});
|
||||
}
|
||||
})();
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
@ -152,11 +152,12 @@ async function withTaskRecord<T>(
|
||||
const taskRecord = await u.task(projectId, taskClass, model, { describe: describe, content: relatedObjects });
|
||||
try {
|
||||
const result = await fn(modelName, false, 0);
|
||||
|
||||
taskRecord(1);
|
||||
return result;
|
||||
} catch (e) {
|
||||
taskRecord(-1, u.error(e).message);
|
||||
throw e;
|
||||
throw new Error(u.error(e).message);
|
||||
}
|
||||
}
|
||||
|
||||
@ -258,9 +259,11 @@ class AiImage {
|
||||
return this;
|
||||
};
|
||||
if (taskRecord) {
|
||||
return withTaskRecord(this.key, taskRecord.taskClass, taskRecord.describe, taskRecord.relatedObjects, taskRecord.projectId, exec);
|
||||
await withTaskRecord(this.key, taskRecord.taskClass, taskRecord.describe, taskRecord.relatedObjects, taskRecord.projectId, exec);
|
||||
return this;
|
||||
}
|
||||
return exec(modelName);
|
||||
await exec(modelName);
|
||||
return this;
|
||||
}
|
||||
async save(path: string) {
|
||||
await u.oss.writeFile(path, this.result);
|
||||
@ -294,18 +297,24 @@ class AiVideo {
|
||||
}
|
||||
async run(input: VideoConfig, taskRecord?: TaskRecord) {
|
||||
const modelName = await resolveModelName(this.key);
|
||||
try {
|
||||
const exec = async (mn: `${string}:${string}`) => {
|
||||
const fn = await getVendorTemplateFn("videoRequest", mn);
|
||||
await referenceList2imageBase642(mn.split(/:(.+)/)[0], input);
|
||||
|
||||
this.result = await fn(input);
|
||||
|
||||
if (this.result.startsWith("http")) this.result = await urlToBase64(this.result);
|
||||
return this;
|
||||
};
|
||||
if (taskRecord) {
|
||||
return withTaskRecord(this.key, taskRecord.taskClass, taskRecord.describe, taskRecord.relatedObjects, taskRecord.projectId, exec);
|
||||
await withTaskRecord(this.key, taskRecord.taskClass, taskRecord.describe, taskRecord.relatedObjects, taskRecord.projectId, exec);
|
||||
return this;
|
||||
}
|
||||
await exec(modelName);
|
||||
return this;
|
||||
} catch (e) {
|
||||
throw e;
|
||||
}
|
||||
return exec(modelName);
|
||||
}
|
||||
async save(path: string) {
|
||||
await u.oss.writeFile(path, this.result);
|
||||
@ -321,16 +330,19 @@ class AiAudio {
|
||||
async run(input: VideoConfig, taskRecord?: TaskRecord) {
|
||||
const modelName = await resolveModelName(this.key);
|
||||
const exec = async (mn: `${string}:${string}`) => {
|
||||
try {
|
||||
const fn = await getVendorTemplateFn("ttsRequest", mn);
|
||||
await referenceList2imageBase642(mn.split(/:(.+)/)[0], input);
|
||||
this.result = await fn(input);
|
||||
|
||||
if (this.result.startsWith("http")) this.result = await urlToBase64(this.result);
|
||||
return this;
|
||||
} catch (e) {}
|
||||
};
|
||||
if (taskRecord) {
|
||||
return withTaskRecord(this.key, taskRecord.taskClass, taskRecord.describe, taskRecord.relatedObjects, taskRecord.projectId, exec);
|
||||
}
|
||||
return exec(modelName);
|
||||
return await exec(modelName);
|
||||
}
|
||||
async save(path: string) {
|
||||
await u.oss.writeFile(path, this.result);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user