Generate MoneyPrinter Comfy prompts from form
This commit is contained in:
@@ -687,6 +687,35 @@ fn workflow_prompt_from_graph(graph: &Value) -> Value {
|
|||||||
.unwrap_or_else(|| graph.clone())
|
.unwrap_or_else(|| graph.clone())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn generated_comfy_prompt_from_request(body: &Value) -> Value {
|
||||||
|
let model = string_from(body, &["videoModel", "video_model", "model"])
|
||||||
|
.unwrap_or_else(|| "seedance-1-0-lite-t2v".to_string());
|
||||||
|
let ratio = string_from(body, &["ratio", "aspectRatio", "aspect_ratio"]).unwrap_or_else(|| "9:16".to_string());
|
||||||
|
let resolution = string_from(body, &["resolution", "size"]).unwrap_or_else(|| "1080p".to_string());
|
||||||
|
let duration = body
|
||||||
|
.get("duration")
|
||||||
|
.cloned()
|
||||||
|
.unwrap_or_else(|| json!(30));
|
||||||
|
json!({
|
||||||
|
"1": {
|
||||||
|
"class_type": "BoosAPITextToVideo",
|
||||||
|
"inputs": {
|
||||||
|
"model": model,
|
||||||
|
"prompt": "",
|
||||||
|
"ratio": ratio,
|
||||||
|
"resolution": resolution,
|
||||||
|
"duration": duration
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"2": {
|
||||||
|
"class_type": "output.preview_video",
|
||||||
|
"inputs": {
|
||||||
|
"video": ["1", 0]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
async fn workflow_prompt_from_body(
|
async fn workflow_prompt_from_body(
|
||||||
state: &AppState,
|
state: &AppState,
|
||||||
auth: &MoneyPrinterAuth,
|
auth: &MoneyPrinterAuth,
|
||||||
@@ -711,9 +740,7 @@ async fn workflow_prompt_from_body(
|
|||||||
.await?;
|
.await?;
|
||||||
return Ok(workflow_prompt_from_graph(&workflow.graph));
|
return Ok(workflow_prompt_from_graph(&workflow.graph));
|
||||||
}
|
}
|
||||||
Err(AppError::BadRequest(
|
Ok(generated_comfy_prompt_from_request(body))
|
||||||
"Comfy API 模式需要选择工作流模板,或传入 comfyPrompt/promptWorkflow 和节点映射".into(),
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn apply_node_input(prompt: &mut Value, node_id: &str, field: &str, value: Value) -> AppResult<()> {
|
fn apply_node_input(prompt: &mut Value, node_id: &str, field: &str, value: Value) -> AppResult<()> {
|
||||||
|
|||||||
@@ -146,6 +146,36 @@ function templateLabel(workflow: WorkflowSummary) {
|
|||||||
return '私有工作流';
|
return '私有工作流';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function generatedComfyPromptFromForm(model: string, ratio: string, resolution: string, duration: number) {
|
||||||
|
return {
|
||||||
|
'1': {
|
||||||
|
class_type: 'BoosAPITextToVideo',
|
||||||
|
inputs: {
|
||||||
|
model,
|
||||||
|
prompt: '',
|
||||||
|
ratio,
|
||||||
|
resolution,
|
||||||
|
duration,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
'2': {
|
||||||
|
class_type: 'output.preview_video',
|
||||||
|
inputs: {
|
||||||
|
video: ['1', 0],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function generatedComfyMapping() {
|
||||||
|
return {
|
||||||
|
prompt: { nodeId: '1', field: 'prompt' },
|
||||||
|
ratio: { nodeId: '1', field: 'ratio' },
|
||||||
|
resolution: { nodeId: '1', field: 'resolution' },
|
||||||
|
duration: { nodeId: '1', field: 'duration' },
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
export default function MoneyPrinter() {
|
export default function MoneyPrinter() {
|
||||||
const location = useLocation();
|
const location = useLocation();
|
||||||
const audience: SessionAudience = location.pathname.startsWith('/user/') ? 'user' : 'admin';
|
const audience: SessionAudience = location.pathname.startsWith('/user/') ? 'user' : 'admin';
|
||||||
@@ -165,6 +195,7 @@ export default function MoneyPrinter() {
|
|||||||
const [comfyNodeMapping, setComfyNodeMapping] = useState('{\n "prompt": { "nodeId": "", "field": "prompt" },\n "negativePrompt": { "nodeId": "", "field": "text" },\n "ratio": { "nodeId": "", "field": "ratio" },\n "resolution": { "nodeId": "", "field": "resolution" },\n "duration": { "nodeId": "", "field": "duration" }\n}');
|
const [comfyNodeMapping, setComfyNodeMapping] = useState('{\n "prompt": { "nodeId": "", "field": "prompt" },\n "negativePrompt": { "nodeId": "", "field": "text" },\n "ratio": { "nodeId": "", "field": "ratio" },\n "resolution": { "nodeId": "", "field": "resolution" },\n "duration": { "nodeId": "", "field": "duration" }\n}');
|
||||||
const [workflows, setWorkflows] = useState<WorkflowSummary[]>([]);
|
const [workflows, setWorkflows] = useState<WorkflowSummary[]>([]);
|
||||||
const [templateId, setTemplateId] = useState('');
|
const [templateId, setTemplateId] = useState('');
|
||||||
|
const [useAdvancedComfy, setUseAdvancedComfy] = useState(false);
|
||||||
const [jobs, setJobs] = useState<MoneyPrinterJob[]>([]);
|
const [jobs, setJobs] = useState<MoneyPrinterJob[]>([]);
|
||||||
const [submitting, setSubmitting] = useState(false);
|
const [submitting, setSubmitting] = useState(false);
|
||||||
const [error, setError] = useState('');
|
const [error, setError] = useState('');
|
||||||
@@ -188,13 +219,19 @@ export default function MoneyPrinter() {
|
|||||||
const selectedWorkflow = workflows.find((item) => item.publicId === templateId) || null;
|
const selectedWorkflow = workflows.find((item) => item.publicId === templateId) || null;
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!selectedWorkflow || videoBackend !== 'comfy') return;
|
if (!selectedWorkflow || videoBackend !== 'comfy' || !useAdvancedComfy) return;
|
||||||
const prompt = workflowPromptFromGraph(selectedWorkflow.graph);
|
const prompt = workflowPromptFromGraph(selectedWorkflow.graph);
|
||||||
if (prompt) setComfyPrompt(JSON.stringify(prompt, null, 2));
|
if (prompt) setComfyPrompt(JSON.stringify(prompt, null, 2));
|
||||||
const mapping = mappingFromGraph(selectedWorkflow.graph);
|
const mapping = mappingFromGraph(selectedWorkflow.graph);
|
||||||
setComfyNodeMapping(JSON.stringify(mapping || inferMappingFromPrompt(prompt), null, 2));
|
setComfyNodeMapping(JSON.stringify(mapping || inferMappingFromPrompt(prompt), null, 2));
|
||||||
}, [selectedWorkflow?.publicId, videoBackend]);
|
}, [selectedWorkflow?.publicId, videoBackend]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (videoBackend !== 'comfy' || useAdvancedComfy) return;
|
||||||
|
setComfyPrompt(JSON.stringify(generatedComfyPromptFromForm(videoModel.trim(), ratio, resolution, duration), null, 2));
|
||||||
|
setComfyNodeMapping(JSON.stringify(generatedComfyMapping(), null, 2));
|
||||||
|
}, [videoBackend, useAdvancedComfy, videoModel, ratio, resolution, duration]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!jobs.some((job) => !terminal(job.status))) return;
|
if (!jobs.some((job) => !terminal(job.status))) return;
|
||||||
const timer = window.setInterval(() => {
|
const timer = window.setInterval(() => {
|
||||||
@@ -223,7 +260,7 @@ export default function MoneyPrinter() {
|
|||||||
requirements,
|
requirements,
|
||||||
language: '中文',
|
language: '中文',
|
||||||
...(videoBackend === 'comfy' ? {
|
...(videoBackend === 'comfy' ? {
|
||||||
templateId: templateId || undefined,
|
templateId: useAdvancedComfy ? templateId || undefined : undefined,
|
||||||
comfyPrompt: comfyPrompt.trim() ? JSON.parse(comfyPrompt) : undefined,
|
comfyPrompt: comfyPrompt.trim() ? JSON.parse(comfyPrompt) : undefined,
|
||||||
comfyNodeMapping: comfyNodeMapping.trim() ? JSON.parse(comfyNodeMapping) : undefined,
|
comfyNodeMapping: comfyNodeMapping.trim() ? JSON.parse(comfyNodeMapping) : undefined,
|
||||||
} : {}),
|
} : {}),
|
||||||
@@ -261,6 +298,7 @@ export default function MoneyPrinter() {
|
|||||||
setComfyPrompt(request.comfyPrompt ? JSON.stringify(request.comfyPrompt, null, 2) : comfyPrompt);
|
setComfyPrompt(request.comfyPrompt ? JSON.stringify(request.comfyPrompt, null, 2) : comfyPrompt);
|
||||||
setComfyNodeMapping(request.comfyNodeMapping ? JSON.stringify(request.comfyNodeMapping, null, 2) : comfyNodeMapping);
|
setComfyNodeMapping(request.comfyNodeMapping ? JSON.stringify(request.comfyNodeMapping, null, 2) : comfyNodeMapping);
|
||||||
setTemplateId(request.templateId || templateId);
|
setTemplateId(request.templateId || templateId);
|
||||||
|
setUseAdvancedComfy(Boolean(request.templateId));
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -352,34 +390,47 @@ export default function MoneyPrinter() {
|
|||||||
<div className="money-printer-section-head">
|
<div className="money-printer-section-head">
|
||||||
<div>
|
<div>
|
||||||
<h2>Comfy API 工作流</h2>
|
<h2>Comfy API 工作流</h2>
|
||||||
<p>选择保存过的工作流模板,会自动带入 Comfy API prompt 和节点映射。</p>
|
<p>{useAdvancedComfy ? '使用保存过的工作流模板,会自动带入 Comfy API prompt 和节点映射。' : '已根据上方表单自动生成 Comfy prompt,生成时会注入 AI 改写后的视频提示词。'}</p>
|
||||||
</div>
|
</div>
|
||||||
|
<button type="button" className="btn btn-ghost" onClick={() => setUseAdvancedComfy((value) => !value)}>
|
||||||
|
{useAdvancedComfy ? '使用自动生成' : '高级模板'}
|
||||||
|
</button>
|
||||||
</div>
|
</div>
|
||||||
<label className="money-printer-field">
|
{useAdvancedComfy && (
|
||||||
<span>工作流模板</span>
|
<>
|
||||||
<select value={templateId} onChange={(event) => setTemplateId(event.target.value)}>
|
<label className="money-printer-field">
|
||||||
<option value="">请选择模板</option>
|
<span>工作流模板</span>
|
||||||
{workflows.map((workflow) => (
|
<select value={templateId} onChange={(event) => setTemplateId(event.target.value)}>
|
||||||
<option key={workflow.publicId} value={workflow.publicId}>
|
<option value="">请选择模板</option>
|
||||||
{workflow.name} · {templateLabel(workflow)}
|
{workflows.map((workflow) => (
|
||||||
</option>
|
<option key={workflow.publicId} value={workflow.publicId}>
|
||||||
))}
|
{workflow.name} · {templateLabel(workflow)}
|
||||||
</select>
|
</option>
|
||||||
</label>
|
))}
|
||||||
{selectedWorkflow && (
|
</select>
|
||||||
|
</label>
|
||||||
|
{selectedWorkflow && (
|
||||||
|
<div className="money-printer-template-meta">
|
||||||
|
<strong>{selectedWorkflow.name}</strong>
|
||||||
|
<span>{mappingFromGraph(selectedWorkflow.graph) ? '已带节点映射' : '这个模板没有内置节点映射,系统会尝试自动推断。'}</span>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
<label className="money-printer-field">
|
||||||
|
<span>Comfy prompt JSON</span>
|
||||||
|
<textarea value={comfyPrompt} onChange={(event) => setComfyPrompt(event.target.value)} rows={7} placeholder='{"12":{"class_type":"...","inputs":{...}}}' />
|
||||||
|
</label>
|
||||||
|
<label className="money-printer-field">
|
||||||
|
<span>节点映射 JSON</span>
|
||||||
|
<textarea value={comfyNodeMapping} onChange={(event) => setComfyNodeMapping(event.target.value)} rows={7} />
|
||||||
|
</label>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
{!useAdvancedComfy && (
|
||||||
<div className="money-printer-template-meta">
|
<div className="money-printer-template-meta">
|
||||||
<strong>{selectedWorkflow.name}</strong>
|
<strong>自动生成 Comfy prompt</strong>
|
||||||
<span>{mappingFromGraph(selectedWorkflow.graph) ? '已带节点映射' : '这个模板没有内置节点映射,可在下方手动填写。'}</span>
|
<span>模型 {videoModel || '未填写'} / {ratio} / {resolution} / {duration} 秒</span>
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
<label className="money-printer-field">
|
|
||||||
<span>Comfy prompt JSON</span>
|
|
||||||
<textarea value={comfyPrompt} onChange={(event) => setComfyPrompt(event.target.value)} rows={7} placeholder='{"12":{"class_type":"...","inputs":{...}}}' />
|
|
||||||
</label>
|
|
||||||
<label className="money-printer-field">
|
|
||||||
<span>节点映射 JSON</span>
|
|
||||||
<textarea value={comfyNodeMapping} onChange={(event) => setComfyNodeMapping(event.target.value)} rows={7} />
|
|
||||||
</label>
|
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
</section>
|
</section>
|
||||||
|
|||||||
Reference in New Issue
Block a user