Files
monkey-ai-web/src/components/image/ImageGenerationParams.jsx
T
2026-10-15 16:33:46 +08:00

614 lines
20 KiB
React
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*
Copyright (C) 2025 QuantumNous
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
For commercial licensing, please contact support@quantumnous.com
*/
import React, { useState, useCallback, useEffect } from 'react';
import { useTranslation } from 'react-i18next';
import {
Input,
Select,
Card,
Button,
Collapse,
Divider,
Tag,
Tooltip,
Slider,
Switch,
Typography,
Alert,
} from '@douyinfe/semi-ui';
import {
PlusCircle,
Zap,
Info,
Palette,
Image as ImageIcon,
Eye,
Sparkles,
Trash2,
} from 'lucide-react';
const { TextArea, InputNumber } = Input;
const { Title, Text } = Typography;
/**
* 图像生成参数配置组件
* @param {Object} props 组件属性
* @param {Object} props.modelInfo 当前选择的模型信息
* @param {Function} props.onParamsChange 参数变化回调函数
* @param {Function} props.onGenerate 生成按钮点击回调
* @param {boolean} props.isGenerating 是否正在生成中
* @param {Object} props.initialParams 初始参数
*/
const ImageGenerationParams = ({
modelInfo,
onParamsChange,
onGenerate,
isGenerating = false,
initialParams = {},
}) => {
const { t } = useTranslation();
// 提取模型能力信息
const modelCapabilities = modelInfo?.capabilities || {};
// 状态管理
const [params, setParams] = useState(() => ({
prompt: initialParams.prompt || '',
negativePrompt: initialParams.negativePrompt || '',
n: initialParams.n || 1,
size: initialParams.size || modelCapabilities.sizes?.[0] || '1024x1024',
quality: initialParams.quality || 'standard',
style: initialParams.style || 'natural',
creativity: initialParams.creativity || 0.7,
guidanceScale: initialParams.guidanceScale || 7.5,
seed: initialParams.seed || null,
useSeed: initialParams.useSeed || false,
showAdvanced: initialParams.showAdvanced || false,
}));
// 当模型信息变化时,更新可用参数
useEffect(() => {
setParams((prev) => {
const newParams = { ...prev };
// 检查当前尺寸是否在模型支持的尺寸列表中
if (
modelCapabilities.sizes &&
!modelCapabilities.sizes.includes(prev.size)
) {
newParams.size = modelCapabilities.sizes[0];
}
// 如果模型不支持质量选择,确保质量设置为standard
if (!modelCapabilities.supportsQuality && prev.quality !== 'standard') {
newParams.quality = 'standard';
}
// 限制生成数量不超过模型支持的最大值
if (modelCapabilities.maxImages && prev.n > modelCapabilities.maxImages) {
newParams.n = modelCapabilities.maxImages;
}
return newParams;
});
}, [modelInfo]);
// 参数更新函数
const updateParam = useCallback(
(key, value) => {
setParams((prev) => {
const newParams = { ...prev, [key]: value };
// 触发参数变化回调
onParamsChange(newParams);
return newParams;
});
},
[onParamsChange],
);
// 处理生成按钮点击
const handleGenerate = useCallback(() => {
// 验证提示词
if (!params.prompt || !params.prompt.trim()) {
alert(t('请输入提示词'));
return;
}
// 调用生成回调
onGenerate(params);
}, [params, onGenerate, t]);
// 样式选项
const styleOptions = [
{
value: 'natural',
label: t('自然'),
description: t('生成逼真、自然的图像'),
},
{
value: 'vivid',
label: t('生动'),
description: t('生成更具表现力、丰富色彩的图像'),
},
{
value: 'artistic',
label: t('艺术'),
description: t('生成具有艺术风格的图像'),
},
{
value: 'photographic',
label: t('摄影'),
description: t('生成类似照片的真实图像'),
},
];
// 获取尺寸选项
const getSizeOptions = useCallback(() => {
const sizeMap = {
'256x256': { label: '256×256', description: t('最小尺寸,生成速度快') },
'512x512': {
label: '512×512',
description: t('中等尺寸,平衡质量和速度'),
},
'1024x1024': { label: '1024×1024', description: t('标准高清尺寸') },
'1024x1792': {
label: '1024×1792 (9:16)',
description: t('纵向比例,适合手机壁纸'),
},
'1792x1024': {
label: '1792×1024 (16:9)',
description: t('横向比例,适合桌面壁纸'),
},
'1344x1024': { label: '1344×1024', description: t('宽屏比例') },
'1024x1344': { label: '1024×1344', description: '竖屏比例' },
};
// 如果有模型支持的尺寸列表,使用它过滤
if (modelCapabilities.sizes) {
return modelCapabilities.sizes.map((size) => ({
value: size,
label: sizeMap[size]?.label || size,
description: sizeMap[size]?.description || '',
}));
}
// 否则返回所有尺寸
return Object.keys(sizeMap).map((size) => ({
value: size,
label: sizeMap[size].label,
description: sizeMap[size].description,
}));
}, [modelCapabilities.sizes, t]);
// 质量选项
const qualityOptions = [
{
value: 'standard',
label: t('标准'),
description: t('标准质量,速度较快'),
},
{ value: 'hd', label: t('高清'), description: t('更高质量,细节更丰富') },
];
// 预设提示词模板
const promptTemplates = [
{
title: t('风景照片'),
prompt: t(
'a beautiful landscape with mountains, lake and trees, in realistic style, high detail, natural lighting',
),
},
{
title: t('肖像'),
prompt: t(
'portrait of a person, detailed facial features, professional photography, soft lighting',
),
},
{
title: t('科幻场景'),
prompt: t(
'futuristic cityscape, flying cars, neon lights, science fiction, highly detailed',
),
},
{
title: t('动画风格'),
prompt: t(
'cute anime character, colorful, detailed, digital illustration, anime style',
),
},
];
// 应用提示词模板
const applyPromptTemplate = useCallback(
(template) => {
updateParam('prompt', template.prompt);
},
[updateParam],
);
// 清除所有参数
const clearAllParams = useCallback(() => {
const clearedParams = {
prompt: '',
negativePrompt: '',
n: 1,
size: modelCapabilities.sizes?.[0] || '1024x1024',
quality: 'standard',
style: 'natural',
creativity: 0.7,
guidanceScale: 7.5,
seed: null,
useSeed: false,
};
setParams(clearedParams);
onParamsChange(clearedParams);
}, [modelCapabilities.sizes, onParamsChange]);
// 渲染尺寸选项下拉框
const renderSizeSelect = () => {
const sizeOptions = getSizeOptions();
return (
<Tooltip
content={
sizeOptions.find((option) => option.value === params.size)
?.description || ''
}
position='topLeft'
trigger={['hover']}
>
<Select
value={params.size}
onChange={(value) => updateParam('size', value)}
options={sizeOptions.map((option) => ({
value: option.value,
label: option.label,
}))}
className='w-full'
/>
</Tooltip>
);
};
// 渲染质量选项下拉框
const renderQualitySelect = () => {
const disabled = !modelCapabilities.supportsQuality;
return (
<Tooltip
content={disabled ? t('当前模型不支持质量选择') : ''}
position='topLeft'
trigger={['hover']}
>
<Select
value={params.quality}
onChange={(value) => updateParam('quality', value)}
options={qualityOptions}
disabled={disabled}
className='w-full'
/>
</Tooltip>
);
};
// 最大图片数量
const maxImages = modelCapabilities.maxImages || 1;
return (
<Card className='overflow-hidden'>
<div className='p-6'>
{/* 提示词输入 */}
<div className='mb-6'>
<div className='flex justify-between items-center mb-2'>
<Title level={4} className='mb-0'>
<div className='flex items-center'>
<Sparkles size={16} className='mr-2 text-blue-500' />
{t('提示词')}
</div>
</Title>
<Tooltip content={t('提示词越详细,生成的图片越符合预期')}>
<Info size={16} className='text-gray-400 cursor-help' />
</Tooltip>
</div>
<TextArea
value={params.prompt}
onChange={(e) => updateParam('prompt', e.target.value)}
placeholder={t('描述你想要生成的图像...')}
rows={5}
className='mb-3'
maxLength={2000}
/>
<div className='text-xs text-gray-500 dark:text-gray-400 mb-4'>
{params.prompt.length}/2000
</div>
{/* 提示词模板 */}
{promptTemplates.length > 0 && (
<div>
<Text type='secondary' className='text-xs mb-2'>
{t('快速模板:')}
</Text>
<div className='flex flex-wrap gap-2'>
{promptTemplates.map((template, index) => (
<Tag
key={index}
clickable
color='blue'
size='small'
onClick={() => applyPromptTemplate(template)}
className='cursor-pointer'
>
{template.title}
</Tag>
))}
</div>
</div>
)}
</div>
{/* 高级选项 */}
<Collapse
items={[
{
title: (
<div className='flex items-center cursor-pointer'>
<PlusCircle size={16} className='mr-2' />
<span>{t('高级选项')}</span>
</div>
),
content: (
<div className='space-y-4 pt-2'>
{/* 反向提示词 */}
<div>
<div className='flex justify-between items-center mb-2'>
<Text type='secondary'>{t('反向提示词')}</Text>
<Tooltip content={t('描述你不想要在图像中出现的内容')}>
<Info size={14} className='text-gray-400 cursor-help' />
</Tooltip>
</div>
<TextArea
value={params.negativePrompt}
onChange={(e) =>
updateParam('negativePrompt', e.target.value)
}
placeholder={t('例如: 模糊, 扭曲, 低质量...')}
rows={3}
maxLength={1000}
/>
<div className='text-xs text-gray-500 dark:text-gray-400'>
{params.negativePrompt.length}/1000
</div>
</div>
<Divider />
{/* 基本参数 */}
<div>
<div className='flex justify-between items-center mb-3'>
<Text type='secondary'>{t('基本参数')}</Text>
</div>
<div className='grid grid-cols-1 sm:grid-cols-2 gap-4'>
{/* 图片数量 */}
<div>
<Text type='secondary' className='text-xs mb-1'>
{t('图片数量')}
</Text>
<InputNumber
value={params.n}
onChange={(value) =>
updateParam(
'n',
Math.min(Math.max(1, value), maxImages),
)
}
min={1}
max={maxImages}
className='w-full'
/>
<Text type='secondary' className='text-xs mt-1'>
{t('最大: {{max}}', { max: maxImages })}
</Text>
</div>
{/* 图片尺寸 */}
<div>
<Text type='secondary' className='text-xs mb-1'>
<div className='flex items-center'>
<ImageIcon size={12} className='mr-1' />
{t('图片尺寸')}
</div>
</Text>
{renderSizeSelect()}
</div>
{/* 图片质量 */}
<div>
<Text type='secondary' className='text-xs mb-1'>
<div className='flex items-center'>
<Eye size={12} className='mr-1' />
{t('图片质量')}
</div>
</Text>
{renderQualitySelect()}
</div>
{/* 风格 */}
<div>
<Text type='secondary' className='text-xs mb-1'>
<div className='flex items-center'>
<Palette size={12} className='mr-1' />
{t('风格')}
</div>
</Text>
<Select
value={params.style}
onChange={(value) => updateParam('style', value)}
options={styleOptions}
className='w-full'
/>
</div>
</div>
</div>
<Divider />
{/* 高级控制参数 */}
<div>
<div className='flex justify-between items-center mb-3'>
<Text type='secondary'>{t('高级控制')}</Text>
</div>
{/* 创意思维度 */}
<div className='mb-4'>
<div className='flex justify-between items-center mb-1'>
<Text type='secondary' className='text-xs'>
{t('创意思维度')}
</Text>
<Text type='secondary' className='text-xs'>
{params.creativity.toFixed(1)}
</Text>
</div>
<Slider
value={params.creativity}
onChange={(value) => updateParam('creativity', value)}
min={0.1}
max={1.0}
step={0.1}
/>
<div className='flex justify-between text-xs text-gray-500 dark:text-gray-400'>
<Text>{t('保守')}</Text>
<Text>{t('平衡')}</Text>
<Text>{t('创新')}</Text>
</div>
</div>
{/* 引导系数 */}
<div className='mb-4'>
<div className='flex justify-between items-center mb-1'>
<Text type='secondary' className='text-xs'>
{t('引导系数')}
</Text>
<Text type='secondary' className='text-xs'>
{params.guidanceScale.toFixed(1)}
</Text>
</div>
<Slider
value={params.guidanceScale}
onChange={(value) =>
updateParam('guidanceScale', value)
}
min={1}
max={20}
step={0.5}
/>
<div className='flex justify-between text-xs text-gray-500 dark:text-gray-400'>
<Text>{t('更自由')}</Text>
<Text>{t('更贴合提示')}</Text>
</div>
</div>
{/* 随机种子 */}
<div>
<div className='flex justify-between items-center mb-1'>
<div className='flex items-center'>
<Text type='secondary' className='text-xs mr-2'>
{t('使用随机种子')}
</Text>
<Switch
size='small'
checked={params.useSeed}
onChange={(checked) =>
updateParam('useSeed', checked)
}
/>
</div>
</div>
<InputNumber
value={params.seed}
onChange={(value) => updateParam('seed', value || null)}
disabled={!params.useSeed}
placeholder={t('输入种子值')}
className='w-full'
min={0}
max={Number.MAX_SAFE_INTEGER}
/>
<Text type='secondary' className='text-xs mt-1'>
{t('相同种子值将生成相似的图像')}
</Text>
</div>
</div>
</div>
),
isOpen: params.showAdvanced,
onItemClick: () =>
updateParam('showAdvanced', !params.showAdvanced),
},
]}
/>
<Divider className='my-4' />
{/* 操作按钮 */}
<div className='flex flex-col sm:flex-row gap-3'>
<Button
type='primary'
onClick={handleGenerate}
loading={isGenerating}
disabled={isGenerating || !params.prompt?.trim()}
icon={<Zap size={16} />}
className='flex-1'
>
{isGenerating ? t('生成中...') : t('生成图片')}
</Button>
<Button
type='tertiary'
onClick={clearAllParams}
disabled={isGenerating}
icon={<Trash2 size={16} />}
>
{t('清空')}
</Button>
</div>
{/* 提示信息 */}
<Alert
type='info'
showIcon
description={
<div className='text-xs'>
<Text type='secondary'>
{t(
'提示:详细的提示词能生成更符合您预期的图像。尝试描述场景、风格、光线、构图等元素。',
)}
</Text>
</div>
}
className='mt-4'
/>
</div>
</Card>
);
};
export default ImageGenerationParams;