feat: 新增 AI 生图工具(文生图/图生图)
- 新工具 ai-gen: 文生图+图生图, 模型来自NEWAPI生图模型 - 模型下拉+胶囊/提示词+示例/宽高比网格/结果预览下载 - relay: /ai/image-models + /ai/gen-image + /ai/edit-image, 接入ai-gen授权 - 修复 edit-image 对 cgi.FieldStorage 真值判断崩溃(TypeError)→is None - 文生图可用(pollinations); 图生图需NEWAPI有可用i2i通道(当前dall-e/flux上游500) 验证: 文生图出图✓, 图生图上传+清晰错误提示✓ CHANGELOG记录
This commit is contained in:
1 parent
3c9932b7e4
commit
04131a6145
7 files changed
+701
-2
No files matched your search
@@ -0,0 +1,552 @@
|
||||
<script setup lang="ts">
|
||||
import { getMachineId, licenseHeaders } from '@/tools/license/license';
|
||||
|
||||
type Mode = 'txt' | 'img';
|
||||
|
||||
const mode = ref<Mode>('txt');
|
||||
const models = ref<{ id: string; i2i: boolean }[]>([]);
|
||||
const model = ref('pollinations-flux');
|
||||
const prompt = ref('');
|
||||
const loading = ref(false);
|
||||
const error = ref('');
|
||||
const resultUrl = ref('');
|
||||
|
||||
// 图生图:源图(dataURL)
|
||||
const srcData = ref('');
|
||||
const srcInput = ref<HTMLInputElement>();
|
||||
|
||||
// 宽高比
|
||||
type Ratio = { label: string; w: number; h: number; sw: number; sh: number };
|
||||
const RATIOS: Ratio[] = [
|
||||
{ label: '默认', w: 1024, h: 1024, sw: 26, sh: 26 },
|
||||
{ label: '1:1', w: 1024, h: 1024, sw: 26, sh: 26 },
|
||||
{ label: '3:2', w: 1152, h: 768, sw: 30, sh: 20 },
|
||||
{ label: '2:3', w: 768, h: 1152, sw: 20, sh: 30 },
|
||||
{ label: '9:16', w: 720, h: 1280, sw: 15, sh: 27 },
|
||||
{ label: '16:9', w: 1280, h: 720, sw: 30, sh: 17 },
|
||||
{ label: '3:4', w: 768, h: 1024, sw: 21, sh: 28 },
|
||||
{ label: '4:3', w: 1024, h: 768, sw: 28, sh: 21 },
|
||||
{ label: '21:9', w: 1344, h: 576, sw: 32, sh: 14 },
|
||||
{ label: '9:21', w: 576, h: 1344, sw: 14, sh: 32 },
|
||||
];
|
||||
const ratio = ref('默认');
|
||||
const ratioObj = computed(() => RATIOS.find(r => r.label === ratio.value) || RATIOS[0]);
|
||||
|
||||
// 示例提示词
|
||||
const EXAMPLES = [
|
||||
{ n: '电影感人像', p: '电影感人像,柔和侧光,浅景深,35mm 胶片质感,戏剧性光影,细节丰富' },
|
||||
{ n: '奇幻风景', p: '奇幻风景,漂浮的岛屿与瀑布,魔幻光线,超广角,史诗感,细节丰富' },
|
||||
{ n: '赛博朋克城市', p: '赛博朋克城市夜景,霓虹灯,雨后街道反光,未来感,高细节' },
|
||||
{ n: '动漫角色', p: '动漫角色立绘,大眼睛,鲜艳色彩,干净线条,动漫风格,白底' },
|
||||
{ n: '产品摄影', p: '产品摄影,纯白背景,柔和打光,高清晰度,商业级,居中构图' },
|
||||
{ n: '油画风格', p: '油画风格,厚涂笔触,丰富色彩,印象派,艺术感,画布质感' },
|
||||
];
|
||||
|
||||
const currentModel = computed(() => models.value.find(m => m.id === model.value));
|
||||
const canGen = computed(() => {
|
||||
if (!prompt.value.trim()) return false;
|
||||
if (mode.value === 'img' && !srcData.value) return false;
|
||||
return true;
|
||||
});
|
||||
|
||||
onMounted(async () => {
|
||||
try {
|
||||
const res = await fetch('/relay/ai/image-models');
|
||||
const j = await res.json();
|
||||
if (j.code === 200 && Array.isArray(j.data) && j.data.length) {
|
||||
models.value = j.data;
|
||||
if (!j.data.some((m: any) => m.id === model.value)) {
|
||||
// 默认选一个 pollinations(稳定可用)
|
||||
const p = j.data.find((m: any) => m.id.startsWith('pollinations'));
|
||||
model.value = (p || j.data[0]).id;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch { /* 模型列表加载失败则用默认 */ }
|
||||
});
|
||||
|
||||
function pickSrc() {
|
||||
srcInput.value?.click();
|
||||
}
|
||||
|
||||
function onSrc(e: Event) {
|
||||
const input = e.target as HTMLInputElement;
|
||||
const f = input.files?.[0];
|
||||
input.value = '';
|
||||
if (!f) return;
|
||||
if (f.size > 10 * 1024 * 1024) {
|
||||
error.value = '图片超过 10MB,请压缩后再上传';
|
||||
return;
|
||||
}
|
||||
const rd = new FileReader();
|
||||
rd.onload = () => {
|
||||
srcData.value = String(rd.result || '');
|
||||
};
|
||||
rd.readAsDataURL(f);
|
||||
}
|
||||
|
||||
function b64DataUrl(b64: string) {
|
||||
let mime = 'image/png';
|
||||
if (b64.startsWith('/9j/')) mime = 'image/jpeg';
|
||||
else if (b64.startsWith('UklGR')) mime = 'image/webp';
|
||||
return `data:${mime};base64,${b64}`;
|
||||
}
|
||||
|
||||
async function generate() {
|
||||
if (loading.value || !canGen.value) return;
|
||||
loading.value = true;
|
||||
error.value = '';
|
||||
resultUrl.value = '';
|
||||
try {
|
||||
if (mode.value === 'txt') {
|
||||
const r = ratioObj.value;
|
||||
const res = await fetch('/relay/ai/gen-image', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json', ...licenseHeaders('ai-gen') },
|
||||
body: JSON.stringify({
|
||||
machine: getMachineId(),
|
||||
prompt: prompt.value.trim(),
|
||||
model: model.value,
|
||||
width: r.w,
|
||||
height: r.h,
|
||||
}),
|
||||
});
|
||||
const j = await res.json().catch(() => ({}));
|
||||
if (!res.ok || j.code !== 200) throw new Error(j.message || `HTTP ${res.status}`);
|
||||
resultUrl.value = j.data.b64 ? b64DataUrl(j.data.b64) : j.data.url;
|
||||
}
|
||||
else {
|
||||
const blob = await (await fetch(srcData.value)).blob();
|
||||
const fd = new FormData();
|
||||
fd.append('image', blob, 'src.png');
|
||||
fd.append('prompt', prompt.value.trim());
|
||||
fd.append('model', model.value);
|
||||
const res = await fetch('/relay/ai/edit-image', {
|
||||
method: 'POST',
|
||||
headers: licenseHeaders('ai-gen'),
|
||||
body: fd,
|
||||
});
|
||||
const j = await res.json().catch(() => ({}));
|
||||
if (!res.ok || j.code !== 200) throw new Error(j.message || `HTTP ${res.status}`);
|
||||
resultUrl.value = j.data.b64 ? b64DataUrl(j.data.b64) : j.data.url;
|
||||
}
|
||||
}
|
||||
catch (e: any) {
|
||||
error.value = String(e?.message || e);
|
||||
}
|
||||
finally {
|
||||
loading.value = false;
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="gen-root">
|
||||
<!-- 模式切换 -->
|
||||
<div class="gen-modes">
|
||||
<button class="gen-mode" :class="{ on: mode === 'txt' }" @click="mode = 'txt'">✏️ 文生图</button>
|
||||
<button class="gen-mode" :class="{ on: mode === 'img' }" @click="mode = 'img'">🖼️ 图生图</button>
|
||||
</div>
|
||||
|
||||
<div class="gen-body">
|
||||
<!-- 左侧控制 -->
|
||||
<div class="gen-ctrl">
|
||||
<!-- 模型 -->
|
||||
<div class="gen-sec">
|
||||
<div class="gen-h">模型</div>
|
||||
<select v-model="model" class="gen-model-sel">
|
||||
<option v-for="m in models" :key="m.id" :value="m.id">
|
||||
{{ m.id }}{{ m.i2i ? '' : ' · 仅文生图' }}
|
||||
</option>
|
||||
<option v-if="!models.length" value="pollinations-flux">pollinations-flux</option>
|
||||
</select>
|
||||
<div class="gen-pills">
|
||||
<button
|
||||
v-for="m in models"
|
||||
:key="m.id"
|
||||
class="gen-pill"
|
||||
:class="{ on: model === m.id }"
|
||||
:title="m.i2i ? '支持图生图' : '仅文生图'"
|
||||
@click="model = m.id"
|
||||
>
|
||||
{{ m.id }}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 图生图:源图上传 -->
|
||||
<div v-if="mode === 'img'" class="gen-sec">
|
||||
<div class="gen-h">原图</div>
|
||||
<input ref="srcInput" type="file" hidden accept="image/*" @change="onSrc">
|
||||
<div class="gen-upload" @click="pickSrc">
|
||||
<template v-if="!srcData">
|
||||
📁 点击上传图片<br><span>JPG / PNG / WEBP · ≤10MB</span>
|
||||
</template>
|
||||
<template v-else>
|
||||
<img class="gen-thumb" :src="srcData" alt="">
|
||||
<a href="javascript:;" class="gen-clear" @click.stop="srcData = ''">✕</a>
|
||||
</template>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 提示词 -->
|
||||
<div class="gen-sec">
|
||||
<div class="gen-h">提示词</div>
|
||||
<textarea
|
||||
v-model="prompt"
|
||||
class="gen-prompt"
|
||||
maxlength="1000"
|
||||
placeholder="描述你想生成的图像,例如:将背景更改为蓝天 / 一只戴帽子的柴犬,卡通风格"
|
||||
/>
|
||||
<div class="gen-count">{{ prompt.length }}/1000</div>
|
||||
</div>
|
||||
|
||||
<!-- 示例 -->
|
||||
<div class="gen-sec">
|
||||
<div class="gen-h">尝试这些示例</div>
|
||||
<div class="gen-examples">
|
||||
<button v-for="ex in EXAMPLES" :key="ex.n" class="gen-example" @click="prompt = ex.p">{{ ex.n }}</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 宽高比(仅文生图) -->
|
||||
<div v-if="mode === 'txt'" class="gen-sec">
|
||||
<div class="gen-h">宽高比</div>
|
||||
<div class="gen-ratios">
|
||||
<button
|
||||
v-for="r in RATIOS"
|
||||
:key="r.label"
|
||||
class="gen-ratio"
|
||||
:class="{ on: ratio === r.label }"
|
||||
@click="ratio = r.label"
|
||||
>
|
||||
<span class="gen-rshape" :style="{ width: r.sw + 'px', height: r.sh + 'px' }" />
|
||||
<span class="gen-rl">{{ r.label }}</span>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<button class="gen-go" :disabled="loading || !canGen" @click="generate()">
|
||||
{{ loading ? '⏳ 生成中…' : '🎨 生成图片' }}
|
||||
</button>
|
||||
<div v-if="error" class="gen-err">⚠️ {{ error }}</div>
|
||||
<div v-if="mode === 'img' && !currentModel?.i2i && models.length" class="gen-hint">
|
||||
提示:当前模型偏文生图,图生图建议选 dall-e / flux 系列。
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 右侧结果 -->
|
||||
<div class="gen-result">
|
||||
<div v-if="resultUrl" class="gen-img-box">
|
||||
<img :src="resultUrl" alt="生成结果">
|
||||
<div class="gen-img-acts">
|
||||
<a class="gen-dl" :href="resultUrl" target="_blank" rel="noopener" download>⬇ 下载图片</a>
|
||||
<a class="gen-dl" :href="resultUrl" target="_blank" rel="noopener">🔗 新窗口打开</a>
|
||||
</div>
|
||||
</div>
|
||||
<div v-else-if="loading" class="gen-empty">
|
||||
<n-spin size="large" />
|
||||
<p>AI 正在生成图片,请稍候…</p>
|
||||
</div>
|
||||
<div v-else class="gen-empty">生成的图片会显示在这里</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<style scoped lang="less">
|
||||
.gen-root {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 14px;
|
||||
}
|
||||
|
||||
.gen-modes {
|
||||
display: flex;
|
||||
gap: 8px;
|
||||
|
||||
.gen-mode {
|
||||
padding: 8px 18px;
|
||||
border-radius: 9px;
|
||||
border: 1px solid rgba(128, 128, 128, 0.3);
|
||||
background: #fff;
|
||||
font-size: 14px;
|
||||
cursor: pointer;
|
||||
transition: all 0.15s;
|
||||
|
||||
&.on {
|
||||
background: #0ea5a0;
|
||||
border-color: #0ea5a0;
|
||||
color: #fff;
|
||||
font-weight: 700;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.gen-body {
|
||||
display: flex;
|
||||
gap: 16px;
|
||||
align-items: flex-start;
|
||||
|
||||
@media (max-width: 900px) {
|
||||
flex-direction: column;
|
||||
}
|
||||
}
|
||||
|
||||
.gen-ctrl {
|
||||
width: 380px;
|
||||
flex-shrink: 0;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 14px;
|
||||
|
||||
@media (max-width: 900px) {
|
||||
width: 100%;
|
||||
}
|
||||
}
|
||||
|
||||
.gen-sec {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 7px;
|
||||
}
|
||||
|
||||
.gen-h {
|
||||
font-size: 13px;
|
||||
font-weight: 800;
|
||||
}
|
||||
|
||||
.gen-model-sel {
|
||||
padding: 8px 10px;
|
||||
border: 1px solid rgba(128, 128, 128, 0.3);
|
||||
border-radius: 8px;
|
||||
font-size: 13px;
|
||||
background: #fff;
|
||||
outline: none;
|
||||
|
||||
&:focus {
|
||||
border-color: #0ea5a0;
|
||||
}
|
||||
}
|
||||
|
||||
.gen-pills {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
gap: 6px;
|
||||
|
||||
.gen-pill {
|
||||
padding: 5px 10px;
|
||||
border-radius: 999px;
|
||||
border: 1px solid rgba(128, 128, 128, 0.28);
|
||||
background: rgba(128, 128, 128, 0.05);
|
||||
font-size: 12px;
|
||||
cursor: pointer;
|
||||
transition: all 0.15s;
|
||||
|
||||
&.on {
|
||||
background: rgba(14, 165, 160, 0.15);
|
||||
border-color: #0ea5a0;
|
||||
color: #0e8b87;
|
||||
font-weight: 700;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.gen-upload {
|
||||
position: relative;
|
||||
border: 2px dashed rgba(14, 165, 160, 0.5);
|
||||
border-radius: 10px;
|
||||
padding: 18px;
|
||||
text-align: center;
|
||||
font-size: 13px;
|
||||
cursor: pointer;
|
||||
color: #0e8b87;
|
||||
background: rgba(14, 165, 160, 0.04);
|
||||
overflow: hidden;
|
||||
|
||||
span {
|
||||
display: block;
|
||||
font-size: 11.5px;
|
||||
opacity: 0.6;
|
||||
margin-top: 4px;
|
||||
}
|
||||
|
||||
.gen-thumb {
|
||||
max-width: 100%;
|
||||
max-height: 180px;
|
||||
border-radius: 8px;
|
||||
display: block;
|
||||
margin: 0 auto;
|
||||
}
|
||||
|
||||
.gen-clear {
|
||||
position: absolute;
|
||||
top: 6px;
|
||||
right: 8px;
|
||||
color: #d03050;
|
||||
font-weight: 700;
|
||||
text-decoration: none;
|
||||
}
|
||||
}
|
||||
|
||||
.gen-prompt {
|
||||
width: 100%;
|
||||
min-height: 96px;
|
||||
resize: vertical;
|
||||
padding: 10px 12px;
|
||||
border: 1px solid rgba(128, 128, 128, 0.3);
|
||||
border-radius: 9px;
|
||||
font-size: 13.5px;
|
||||
line-height: 1.6;
|
||||
outline: none;
|
||||
box-sizing: border-box;
|
||||
font-family: inherit;
|
||||
|
||||
&:focus {
|
||||
border-color: #0ea5a0;
|
||||
}
|
||||
}
|
||||
|
||||
.gen-count {
|
||||
text-align: right;
|
||||
font-size: 11.5px;
|
||||
opacity: 0.5;
|
||||
}
|
||||
|
||||
.gen-examples {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
gap: 6px;
|
||||
|
||||
.gen-example {
|
||||
padding: 6px 12px;
|
||||
border-radius: 999px;
|
||||
border: 1px solid rgba(128, 128, 128, 0.28);
|
||||
background: #fff;
|
||||
font-size: 12.5px;
|
||||
cursor: pointer;
|
||||
transition: all 0.15s;
|
||||
|
||||
&:hover {
|
||||
border-color: #0ea5a0;
|
||||
color: #0e8b87;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.gen-ratios {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(5, 1fr);
|
||||
gap: 7px;
|
||||
|
||||
.gen-ratio {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
gap: 5px;
|
||||
padding: 9px 4px;
|
||||
border: 1px solid rgba(128, 128, 128, 0.28);
|
||||
border-radius: 9px;
|
||||
background: #fff;
|
||||
cursor: pointer;
|
||||
transition: all 0.15s;
|
||||
|
||||
&.on {
|
||||
background: rgba(14, 165, 160, 0.12);
|
||||
border-color: #0ea5a0;
|
||||
}
|
||||
|
||||
.gen-rshape {
|
||||
background: #0ea5a0;
|
||||
border-radius: 3px;
|
||||
opacity: 0.75;
|
||||
}
|
||||
|
||||
.gen-rl {
|
||||
font-size: 11px;
|
||||
opacity: 0.8;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.gen-go {
|
||||
padding: 12px;
|
||||
border: none;
|
||||
border-radius: 10px;
|
||||
background: linear-gradient(48deg, #0e9384, #14b8a6);
|
||||
color: #fff;
|
||||
font-size: 15px;
|
||||
font-weight: 700;
|
||||
cursor: pointer;
|
||||
transition: opacity 0.15s;
|
||||
|
||||
&:disabled {
|
||||
opacity: 0.5;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
}
|
||||
|
||||
.gen-err {
|
||||
font-size: 12.5px;
|
||||
color: #d03050;
|
||||
word-break: break-all;
|
||||
}
|
||||
|
||||
.gen-hint {
|
||||
font-size: 12px;
|
||||
opacity: 0.6;
|
||||
}
|
||||
|
||||
.gen-result {
|
||||
flex: 1;
|
||||
min-width: 0;
|
||||
min-height: 420px;
|
||||
border: 1px solid rgba(128, 128, 128, 0.25);
|
||||
border-radius: 12px;
|
||||
background: rgba(128, 128, 128, 0.04);
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
padding: 16px;
|
||||
}
|
||||
|
||||
.gen-img-box {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
gap: 12px;
|
||||
max-width: 100%;
|
||||
|
||||
img {
|
||||
max-width: 100%;
|
||||
max-height: 60vh;
|
||||
border-radius: 10px;
|
||||
box-shadow: 0 4px 20px rgba(0, 0, 0, 0.12);
|
||||
}
|
||||
|
||||
.gen-img-acts {
|
||||
display: flex;
|
||||
gap: 12px;
|
||||
}
|
||||
|
||||
.gen-dl {
|
||||
padding: 8px 16px;
|
||||
border-radius: 8px;
|
||||
background: rgba(14, 165, 160, 0.12);
|
||||
color: #0e8b87;
|
||||
font-size: 13px;
|
||||
font-weight: 600;
|
||||
text-decoration: none;
|
||||
}
|
||||
}
|
||||
|
||||
.gen-empty {
|
||||
text-align: center;
|
||||
opacity: 0.5;
|
||||
font-size: 13.5px;
|
||||
|
||||
p {
|
||||
margin-top: 12px;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,11 @@
|
||||
import { Photo } from '@vicons/tabler';
|
||||
import { defineTool } from '../tool';
|
||||
|
||||
export const tool = defineTool({
|
||||
name: 'AI 生图',
|
||||
path: '/ai-gen',
|
||||
description: 'AI 文生图与图生图:多模型选择、宽高比、示例提示词,一键生成图片',
|
||||
keywords: ['ai', 'image', 'generate', '生图', '文生图', '图生图', 'ai绘画', 'ai图片', '生成图片', '绘图'],
|
||||
component: () => import('./ai-gen.vue'),
|
||||
icon: Photo,
|
||||
});
|
||||
+2
-1
@@ -86,6 +86,7 @@ import { tool as audioTools } from './audio-tools';
|
||||
import { tool as sealMaker } from './seal-maker';
|
||||
import { tool as signaturePad } from './signature-pad';
|
||||
import { tool as textToImage } from './text-to-image';
|
||||
import { tool as aiGen } from './ai-gen';
|
||||
import { tool as zhConvert } from './zh-convert';
|
||||
import { tool as pinyinConverter } from './pinyin-converter';
|
||||
import { tool as aiImageLab } from './ai-image-lab';
|
||||
@@ -162,7 +163,7 @@ export const toolsByCategory: ToolCategory[] = [
|
||||
},
|
||||
{
|
||||
name: 'Images and videos',
|
||||
components: [qrCodeGenerator, wifiQrCodeGenerator, batchBarcodeGenerator, codeReader, imageOcr, pdfOcr, pdfTools, imageTools, videoTools, audioTools, sealMaker, signaturePad, textToImage, docConvert, aiRetouch, labelDesigner, siteBookmarks, svgPlaceholderGenerator, cameraRecorder],
|
||||
components: [qrCodeGenerator, wifiQrCodeGenerator, batchBarcodeGenerator, codeReader, imageOcr, pdfOcr, pdfTools, imageTools, videoTools, audioTools, sealMaker, signaturePad, textToImage, aiGen, docConvert, aiRetouch, labelDesigner, siteBookmarks, svgPlaceholderGenerator, cameraRecorder],
|
||||
},
|
||||
{
|
||||
name: 'Development',
|
||||
|
||||
@@ -20,6 +20,7 @@ export const PROTECTED_TOOLS: Record<string, string> = {
|
||||
'/ai-image-lab': 'AI图片实验室',
|
||||
'/ai-chat': 'AI问答',
|
||||
'/copywriting': '文案工具箱',
|
||||
'/ai-gen': 'AI 生图',
|
||||
};
|
||||
|
||||
export const TRIAL_MS = 30 * 60 * 1000; // 每个功能免费 30 分钟
|
||||
|
||||
Reference in new issue
Block a user