423 lines
9.8 KiB
Vue
Raw Normal View History

2024-06-21 15:26:08 +08:00
<!-- dall3 -->
<template>
<div class="prompt">
<el-text tag="b">画面描述</el-text>
<el-text tag="p">建议使用形容词+动词+风格的格式使用隔开</el-text>
<!-- TODO @fanstyle 看看能不能哟 unocss 替代 -->
<el-input
v-model="prompt"
maxlength="1024"
rows="5"
style="width: 100%; margin-top: 15px"
2024-06-21 15:26:08 +08:00
input-style="border-radius: 7px;"
placeholder="例如:童话里的小屋应该是什么样子?"
show-word-limit
type="textarea"
/>
</div>
<div class="hot-words">
<div>
<el-text tag="b">随机热词</el-text>
</div>
<el-space wrap class="word-list">
<el-button
round
class="btn"
:type="selectHotWord === hotWord ? 'primary' : 'default'"
v-for="hotWord in hotWords"
:key="hotWord"
2024-06-30 16:10:45 +08:00
@click="handleHotWordClick(hotWord)"
2024-06-21 15:26:08 +08:00
>
{{ hotWord }}
</el-button>
</el-space>
</div>
<div class="group-item">
<div>
<el-text tag="b">采样方法</el-text>
2024-06-21 15:26:08 +08:00
</div>
<el-space wrap class="group-item-body">
<el-select v-model="selectSampler" placeholder="Select" size="large" style="width: 350px">
<el-option v-for="item in sampler" :key="item.key" :label="item.name" :value="item.key" />
2024-06-21 15:26:08 +08:00
</el-select>
</el-space>
</div>
<div class="group-item">
<div>
2024-06-30 16:10:45 +08:00
<el-text tag="b">CLIP</el-text>
2024-06-21 15:26:08 +08:00
</div>
<el-space wrap class="group-item-body">
2024-06-30 16:10:45 +08:00
<el-select v-model="selectClipGuidancePreset" placeholder="Select" size="large" style="width: 350px">
<el-option v-for="item in clipGuidancePresets" :key="item.key" :label="item.name" :value="item.key" />
2024-06-21 15:26:08 +08:00
</el-select>
</el-space>
</div>
2024-06-30 16:10:45 +08:00
<div class="group-item">
<div>
<el-text tag="b">风格</el-text>
</div>
<el-space wrap class="group-item-body">
<el-select v-model="selectStylePreset" placeholder="Select" size="large" style="width: 350px">
<el-option v-for="item in stylePresets" :key="item.key" :label="item.name" :value="item.key" />
</el-select>
</el-space>
</div>
<div class="group-item">
<div>
<el-text tag="b">图片尺寸</el-text>
</div>
<el-space wrap class="group-item-body">
<el-input v-model="imageWidth" style="width: 170px" placeholder="图片宽度" />
<el-input v-model="imageHeight" style="width: 170px" placeholder="图片高度" />
</el-space>
</div>
2024-06-21 15:26:08 +08:00
<div class="group-item">
<div>
<el-text tag="b">迭代步数</el-text>
</div>
<el-space wrap class="group-item-body">
<el-input
v-model="steps"
type="number"
size="large"
style="width: 350px"
placeholder="Please input"
/>
2024-06-21 15:26:08 +08:00
</el-space>
</div>
2024-06-30 16:10:45 +08:00
<div class="group-item">
<div>
<el-text tag="b">引导系数</el-text>
</div>
<el-space wrap class="group-item-body">
<el-input
v-model="scale"
type="number"
size="large"
style="width: 350px"
placeholder="Please input"
/>
</el-space>
</div>
2024-06-21 15:26:08 +08:00
<div class="group-item">
<div>
<el-text tag="b">随机因子</el-text>
2024-06-21 15:26:08 +08:00
</div>
<el-space wrap class="group-item-body">
<el-input
v-model="seed"
type="number"
size="large"
style="width: 350px"
placeholder="Please input"
/>
2024-06-21 15:26:08 +08:00
</el-space>
</div>
<div class="btns">
2024-06-30 16:10:45 +08:00
<el-button type="primary" size="large" round :loading="drawIn" @click="handleGenerateImage">
{{ drawIn ? '生成中' : '生成内容' }}
2024-06-21 15:26:08 +08:00
</el-button>
</div>
</template>
<script setup lang="ts">
import {ImageApi, ImageDrawReqVO, ImageVO} from '@/api/ai/image'
2024-07-04 09:51:39 +08:00
import {hasChinese} from '../../utils/common-utils'
2024-06-21 15:26:08 +08:00
// image 模型
interface ImageModelVO {
key: string
name: string
}
// 定义属性
const prompt = ref<string>('') // 提示词
const drawIn = ref<boolean>(false) // 生成中
2024-06-21 15:26:08 +08:00
const selectHotWord = ref<string>('') // 选中的热词
2024-06-30 16:10:45 +08:00
const imageWidth = ref<number>(512) // 图片宽度
const imageHeight = ref<number>(512) // 图片高度
const hotWords = ref<string[]>([
'中国旗袍',
'古装美女',
'卡通头像',
'机甲战士',
'童话小屋',
'中国长城'
]) // 热词
2024-06-21 15:26:08 +08:00
// message
const message = useMessage()
2024-06-30 16:10:45 +08:00
// 采样方法 TODO @fan有 Euler aDPM++ 2S aDPM++ 2MDPM++ SDEDPM++ 2M SDEUniPCRestart另外要不这种枚举我们都放到 image 里?写成 stableDiffusionSampler
const selectSampler = ref<string>('DDIM') // 模型
2024-06-30 16:10:45 +08:00
// DDIM DDPM K_DPMPP_2M K_DPMPP_2S_ANCESTRAL K_DPM_2 K_DPM_2_ANCESTRAL K_EULER K_EULER_ANCESTRAL K_HEUN K_LMS
2024-06-21 15:26:08 +08:00
const sampler = ref<ImageModelVO[]>([
{
2024-06-30 16:10:45 +08:00
key: 'DDIM',
name: 'DDIM'
2024-06-21 15:26:08 +08:00
},
{
2024-06-30 16:10:45 +08:00
key: 'DDPM',
name: 'DDPM'
2024-06-21 15:26:08 +08:00
},
{
2024-06-30 16:10:45 +08:00
key: 'K_DPMPP_2M',
name: 'K_DPMPP_2M'
2024-06-21 15:26:08 +08:00
},
{
2024-06-30 16:10:45 +08:00
key: 'K_DPMPP_2S_ANCESTRAL',
name: 'K_DPMPP_2S_ANCESTRAL'
2024-06-21 15:26:08 +08:00
},
{
2024-06-30 16:10:45 +08:00
key: 'K_DPM_2',
name: 'K_DPM_2'
},
{
key: 'K_DPM_2_ANCESTRAL',
name: 'K_DPM_2_ANCESTRAL'
},
{
key: 'K_EULER',
name: 'K_EULER'
},
{
key: 'K_EULER_ANCESTRAL',
name: 'K_EULER_ANCESTRAL'
},
{
key: 'K_HEUN',
name: 'K_HEUN'
},
{
key: 'K_LMS',
name: 'K_LMS'
},
])
2024-06-21 15:26:08 +08:00
2024-06-30 16:10:45 +08:00
// 风格
// 3d-model analog-film anime cinematic comic-book digital-art enhance fantasy-art isometric
// line-art low-poly modeling-compound neon-punk origami photographic pixel-art tile-texture
const selectStylePreset = ref<string>('3d-model') // 模型
2024-06-30 16:10:45 +08:00
const stylePresets = ref<ImageModelVO[]>([
2024-06-21 15:26:08 +08:00
{
2024-06-30 16:10:45 +08:00
key: '3d-model',
name: '3d-model'
2024-06-21 15:26:08 +08:00
},
{
2024-06-30 16:10:45 +08:00
key: 'analog-film',
name: 'analog-film'
2024-06-21 15:26:08 +08:00
},
{
2024-06-30 16:10:45 +08:00
key: 'anime',
name: 'anime'
2024-06-21 15:26:08 +08:00
},
{
2024-06-30 16:10:45 +08:00
key: 'cinematic',
name: 'cinematic'
2024-06-21 15:26:08 +08:00
},
{
2024-06-30 16:10:45 +08:00
key: 'comic-book',
name: 'comic-book'
2024-06-21 15:26:08 +08:00
},
{
2024-06-30 16:10:45 +08:00
key: 'digital-art',
name: 'digital-art'
2024-06-21 15:26:08 +08:00
},
{
2024-06-30 16:10:45 +08:00
key: 'enhance',
name: 'enhance'
2024-06-21 15:26:08 +08:00
},
{
2024-06-30 16:10:45 +08:00
key: 'fantasy-art',
name: 'fantasy-art'
2024-06-21 15:26:08 +08:00
},
{
2024-06-30 16:10:45 +08:00
key: 'isometric',
name: 'isometric'
},
{
key: 'line-art',
name: 'line-art'
},
{
key: 'low-poly',
name: 'low-poly'
},
{
key: 'modeling-compound',
name: 'modeling-compound'
},
// neon-punk origami photographic pixel-art tile-texture
{
key: 'neon-punk',
name: 'neon-punk'
},
{
key: 'origami',
name: 'origami'
},
{
key: 'photographic',
name: 'photographic'
},
{
key: 'pixel-art',
name: 'pixel-art'
},
{
key: 'tile-texture',
name: 'tile-texture'
},
])
// 文本提示相匹配的图像(clip_guidance_preset) 简称 CLIP
// https://platform.stability.ai/docs/api-reference#tag/SDXL-and-SD1.6/operation/textToImage
// FAST_BLUE FAST_GREEN NONE SIMPLE SLOW SLOWER SLOWEST
const selectClipGuidancePreset = ref<string>('NONE') // 模型
2024-06-30 16:10:45 +08:00
const clipGuidancePresets = ref<ImageModelVO[]>([
{
key: 'NONE',
name: 'NONE'
},
{
key: 'FAST_BLUE',
name: 'FAST_BLUE'
},
{
key: 'FAST_GREEN',
name: 'FAST_GREEN'
},
{
key: 'SIMPLE',
name: 'SIMPLE'
},
{
key: 'SLOW',
name: 'SLOW'
},
{
key: 'SLOWER',
name: 'SLOWER'
},
{
key: 'SLOWEST',
name: 'SLOWEST'
},
])
2024-06-21 15:26:08 +08:00
const steps = ref<number>(20) // 迭代步数
2024-06-30 16:10:45 +08:00
const seed = ref<number>(42) // 控制生成图像的随机性
const scale = ref<number>(7.5) // 引导系数
2024-06-21 15:26:08 +08:00
// 定义 Props
const props = defineProps({})
// 定义 emits
const emits = defineEmits(['onDrawStart', 'onDrawComplete'])
/** 热词 - click */
2024-06-30 16:10:45 +08:00
const handleHotWordClick = async (hotWord: string) => {
2024-06-21 15:26:08 +08:00
// 取消选中
if (selectHotWord.value == hotWord) {
selectHotWord.value = ''
return
}
// 选中
selectHotWord.value = hotWord
// 替换提示词
prompt.value = hotWord
}
/** 图片生产 */
2024-06-30 16:10:45 +08:00
const handleGenerateImage = async () => {
2024-06-21 15:26:08 +08:00
// 二次确认
await message.confirm(`确认生成内容?`)
if (await hasChinese(prompt.value)) {
2024-07-02 21:58:44 +08:00
message.alert('暂不支持中文!')
return
}
2024-06-21 15:26:08 +08:00
try {
// 加载中
drawIn.value = true
// 回调
emits('onDrawStart', 'StableDiffusion')
2024-06-30 16:10:45 +08:00
// 发送请求
2024-06-21 15:26:08 +08:00
const form = {
platform: 'StableDiffusion',
model: 'stable-diffusion-v1-6',
prompt: prompt.value, // 提示词
2024-06-30 16:10:45 +08:00
width: imageWidth.value, // 图片宽度
height: imageHeight.value, // 图片高度
2024-06-21 15:26:08 +08:00
options: {
seed: seed.value, // 随机种子
2024-06-30 16:10:45 +08:00
steps: steps.value, // 图片生成步数
scale: scale.value, // 引导系数
sampler: selectSampler.value, // 采样算法
2024-06-30 16:10:45 +08:00
clipGuidancePreset: selectClipGuidancePreset.value.key, // 文本提示相匹配的图像 CLIP
stylePreset: selectStylePreset.value, // 风格
}
2024-06-21 15:26:08 +08:00
} as ImageDrawReqVO
await ImageApi.drawImage(form)
} finally {
// 回调
emits('onDrawComplete', 'StableDiffusion')
// 加载结束
drawIn.value = false
}
}
/** 填充值 */
const settingValues = async (imageDetail: ImageVO) => {
prompt.value = imageDetail.prompt
imageWidth.value = imageDetail.width
imageHeight.value = imageDetail.height
seed.value = imageDetail.options?.seed
steps.value = imageDetail.options?.steps
scale.value = imageDetail.options?.scale
selectSampler.value = imageDetail.options?.sampler
selectClipGuidancePreset.value = imageDetail.options?.clipGuidancePreset
selectStylePreset.value = imageDetail.options?.stylePreset
}
/** 暴露组件方法 */
defineExpose({ settingValues })
2024-06-21 15:26:08 +08:00
</script>
<style scoped lang="scss">
// 提示词
.prompt {
}
// 热词
.hot-words {
display: flex;
flex-direction: column;
margin-top: 30px;
.word-list {
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: start;
margin-top: 15px;
.btn {
margin: 0;
}
}
}
// 模型
.group-item {
margin-top: 30px;
.group-item-body {
margin-top: 15px;
width: 100%;
}
}
.btns {
display: flex;
justify-content: center;
margin-top: 50px;
}
</style>