/* 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 . 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 ( option.value === params.size) ?.description || '' } position='topLeft' trigger={['hover']} > updateParam('quality', value)} options={qualityOptions} disabled={disabled} className='w-full' /> ); }; // 最大图片数量 const maxImages = modelCapabilities.maxImages || 1; return (
{/* 提示词输入 */}
<div className='flex items-center'> <Sparkles size={16} className='mr-2 text-blue-500' /> {t('提示词')} </div>