This commit is contained in:
zyc404 2023-02-25 16:53:00 +08:00
commit 5f84ab4396
7 changed files with 144 additions and 5 deletions

1
.npmrc Normal file
View file

@ -0,0 +1 @@
sharp_libvips_binary_host="https://registry.npmmirror.com/-/binary/sharp-libvips"

View file

@ -72,7 +72,11 @@ pnpm i
> 3. 使用vnc客户端连接至云桌面
>
> 右键Applications > Shells > Bash打开终端然后进入Yunzai目录下运行node app即可。
>
> 4. 执行pnpm i时sharp安装失败
>
> sharp不影响chatgpt聊天仅影响Dalle2绘图功能。ubuntu可以执行`apt install libvips-dev`之后再`pnpm i`
>
> 实测该方案资源占用低运行稳定基本1核2G的轻量云服务器就足够了。
---

View file

@ -281,7 +281,7 @@ export class chatgpt extends plugin {
}
if (reply) {
for (let val of reply) {
if (val.type == 'image') {
if (val.type === 'image') {
e.img = [val.url]
break
}

View file

@ -1,7 +1,7 @@
import plugin from '../../../lib/plugins/plugin.js'
import { segment } from 'oicq'
import { createImage } from '../utils/dalle.js'
import { makeForwardMsg} from "../utils/common.js";
import { createImage, imageVariation } from '../utils/dalle.js'
import { makeForwardMsg } from '../utils/common.js'
import _ from 'lodash'
export class dalle extends plugin {
@ -15,6 +15,10 @@ export class dalle extends plugin {
{
reg: '#(chatgpt|ChatGPT|dalle|Dalle)(绘图|画图)',
fnc: 'draw'
},
{
reg: '#(chatgpt|ChatGPT|dalle|Dalle)(修图|图片变形|改图)',
fnc: 'variation'
}
]
})
@ -37,13 +41,17 @@ export class dalle extends plugin {
this.reply('大小不符合要求必须是256x256/512x512/1024x1024中的一个')
return false
}
await redis.set(`CHATGPT:DRAW:${e.sender.user_id}`, 'c', {EX: 30})
await redis.set(`CHATGPT:DRAW:${e.sender.user_id}`, 'c', { EX: 30 })
let priceMap = {
'1024x1024': 0.02,
'512x512': 0.018,
'256x256': 0.016
}
num = parseInt(num, 10)
if (num > 5) {
this.reply('太多啦!你要花光我的余额吗!')
return false
}
await this.reply(`正在为您绘制大小为${size}${num}张图片,预计消耗${priceMap[size] * num}美元余额,请稍候……`)
try {
let images = (await createImage(prompt, num, size)).map(image => segment.image(`base64://${image}`))
@ -57,7 +65,51 @@ export class dalle extends plugin {
this.reply(`绘图失败: ${err}`, true)
await redis.del(`CHATGPT:DRAW:${e.sender.user_id}`)
}
}
async variation (e) {
let ttl = await redis.ttl(`CHATGPT:VARIATION:${e.sender.user_id}`)
if (ttl > 0 && !e.isMaster) {
this.reply(`冷却中,请${ttl}秒后再试`)
return false
}
let imgUrl
if (e.source) {
let reply
if (e.isGroup) {
reply = (await e.group.getChatHistory(e.source.seq, 1)).pop()?.message
} else {
reply = (await e.friend.getChatHistory(e.source.time, 1)).pop()?.message
}
if (reply) {
for (let val of reply) {
if (val.type === 'image') {
console.log(val)
imgUrl = val.url
break
}
}
}
} else if (e.img) {
imgUrl = e.img[0]?.url
}
if (!imgUrl) {
this.reply('图呢?')
return false
}
await redis.set(`CHATGPT:VARIATION:${e.sender.user_id}`, 'c', { EX: 30 })
await this.reply('正在为您生成图片变形,请稍候……')
try {
let images = (await imageVariation(imgUrl)).map(image => segment.image(`base64://${image}`))
if (images.length > 1) {
this.reply(await makeForwardMsg(e, images))
} else {
this.reply(images[0], true)
}
} catch (err) {
console.log(err)
this.reply(`绘图失败: ${err}`, true)
await redis.del(`CHATGPT:VARIATION:${e.sender.user_id}`)
}
}
}

View file

@ -15,5 +15,8 @@
"random": "^4.1.0",
"undici": "^5.19.1",
"uuid": "^9.0.0"
},
"optionalDependencies": {
"sharp": "^0.31.3"
}
}

View file

@ -2,6 +2,8 @@
// import stripMarkdown from 'strip-markdown'
import { exec } from 'child_process'
import lodash from 'lodash'
import fs from 'node:fs'
import path from 'node:path'
// export function markdownToText (markdown) {
// return remark()
// .use(stripMarkdown)
@ -205,3 +207,14 @@ async function execSync (cmd) {
})
})
}
export function mkdirs (dirname) {
if (fs.existsSync(dirname)) {
return true
} else {
if (mkdirs(path.dirname(dirname))) {
fs.mkdirSync(dirname)
return true
}
}
}

View file

@ -1,5 +1,7 @@
import { Configuration, OpenAIApi } from 'openai'
import { Config } from './config.js'
import fs from 'fs'
import { mkdirs } from './common.js'
export async function createImage (prompt, n = 1, size = '512x512') {
const configuration = new Configuration({
@ -17,3 +19,67 @@ export async function createImage (prompt, n = 1, size = '512x512') {
})
return response.data.data?.map(pic => pic.b64_json)
}
export async function imageVariation (imageUrl, n = 1, size = '512x512') {
const configuration = new Configuration({
apiKey: Config.apiKey
})
const openai = new OpenAIApi(configuration)
if (Config.debug) {
logger.info({ imageUrl, n, size })
}
const imageResponse = await fetch(imageUrl)
const fileType = imageResponse.headers.get('Content-Type').split('/')[1]
let fileLoc = `data/chatgpt/imagesAccept/${Date.now()}.${fileType}`
mkdirs('data/chatgpt/imagesAccept')
const blob = await imageResponse.blob()
const arrayBuffer = await blob.arrayBuffer()
const buffer = Buffer.from(arrayBuffer)
await fs.writeFileSync(fileLoc, buffer)
let croppedFileLoc = `data/chatgpt/imagesAccept/${Date.now()}_cropped.png`
await resizeAndCropImage(fileLoc, croppedFileLoc, 512)
const response = await openai.createImageVariation(
fs.createReadStream(croppedFileLoc),
n,
size,
'b64_json'
)
if (response.status !== 200) {
console.log(response.data.error)
}
await fs.unlinkSync(fileLoc)
await fs.unlinkSync(croppedFileLoc)
return response.data.data?.map(pic => pic.b64_json)
}
async function resizeAndCropImage (inputFilePath, outputFilePath, size = 512) {
// Determine the maximum dimension of the input image
let sharp
try {
sharp = (await import('sharp')).default
} catch (e) {
logger.error('sharp未安装请执行 pnpm install sharp@0.31.3')
throw new Error('sharp未安装请执行 pnpm install sharp@0.31.3')
}
const metadata = await sharp(inputFilePath).metadata()
const maxDimension = Math.max(metadata.width, metadata.height)
logger.mark(`original picture size is ${metadata.width} x ${metadata.height}`)
// Calculate the required dimensions for the output image
const outputWidth = Math.round(size * metadata.width / maxDimension)
const outputHeight = Math.round(size * metadata.height / maxDimension)
// Resize the image to the required dimensions
await sharp(inputFilePath)
.resize(outputWidth, outputHeight, {
fit: 'contain',
background: { r: 255, g: 255, b: 255, alpha: 1 }
})
.resize(size, size, { fit: 'cover', position: 'center' })
.png()
.toFile(outputFilePath)
console.log('Image resized successfully!')
console.log('Image resized and cropped successfully!')
}