ps:我是標題黨,目前還沒見過三分鐘完成任務的,三分鐘只能打通Midjourney接口。我花了一天時間接入應用哈哈哈!
首先,我要感謝laf贊助我,讓我可以免費使用Midjourney進行開發(fā)和測試。來自白嫖黨的快樂。
其次,我要感謝白夜、米開朗基楊@sealos.io等大佬的耐心解答和AlexDev大佬很細、全網(wǎng)最細的指導文檔,讓我更快地借助laf搭建我的項目。
那么,什么是laf呢?它可以幫助開發(fā)者像寫博客一樣寫代碼,隨時隨地快速發(fā)布上線應用。點擊了解一下
最后,別忘了文末的【體驗網(wǎng)址】哦。如果不及時體驗,說不定哪天接口就掛了。所以,趕緊去試試吧,感受一下laf+mj帶來的奇妙體驗!
開始
最近laf又在搞事情,有一個快速上手Midjourney《人人都能接入 Midjourney》的活動,具體活動,可以查看laf開發(fā)者社區(qū)?,F(xiàn)在只要注冊新賬號就送一個月免費試用,因為mj需要魔法,所以國內(nèi)的laf官網(wǎng)不能體驗mj,我們需要注冊laf的新加坡環(huán)境賬號,網(wǎng)址????laf 云開發(fā)。
首先我的思路是,先打通mj接口,再通過上層代碼處理mj的數(shù)據(jù),不管是mj還是GPT,都是要有底層代碼對接api,再去擴展,至于最后要對接什么應用看個人需求。
代碼
第一步,你得先添加依賴才能進行后續(xù)開發(fā)??
添加Midjourney依賴
添加完成后保存并重啟
laf可以通過云函數(shù)實現(xiàn)api接口,不理解的可以先去了解一下laf云平臺快速入門。
laf官方提供了一個云函數(shù)對接mj:
import cloud from '@lafjs/cloud'
import { Midjourney, MidjourneyMessage } from 'midjourney'
const SERVER_ID = '' // Midjourney 服務 ID
const CHANNEL_ID = '' // Midjourney 頻道 ID
const SALAI_TOKEN = '' // Midjourney 服務 Token
const Limit = 100
const MaxWait = 3
const client = new Midjourney({
ServerId: SERVER_ID,
ChannelId: CHANNEL_ID,
SalaiToken: SALAI_TOKEN,
Debug: true,
SessionId: SALAI_TOKEN,
Limit: Limit,
MaxWait: MaxWait
});
export default async function (ctx: FunctionContext) {
const { type, param } = ctx.body
switch (type) {
case 'RetrieveMessages':
return await RetrieveMessages(param)
case 'imagine':
return await imagine(param)
case 'upscale':
return await upscale(param)
case 'variation':
return await variation(param)
}
}
// 查詢最近消息
async function RetrieveMessages(param) {
console.log("RetrieveMessages")
const client = new MidjourneyMessage({
ChannelId: CHANNEL_ID,
SalaiToken: SALAI_TOKEN,
});
const msg = await client.RetrieveMessages();
console.log("RetrieveMessages success ", msg)
return msg
}
// 創(chuàng)建生圖任務
async function imagine(param) {
console.log("imagine", param)
const { question, msg_Id } = param
const msg = await client.Imagine(
`[${msg_Id}] ${question}`,
(uri: string, progress: string) => {
console.log("loading", uri, "progress", progress);
}
);
console.log("imagine success ", msg)
return true
}
// upscale 放大圖片
async function upscale(param) {
console.log("upscale", param)
const { question, index, id, url } = param
const hash = url.split("_").pop()?.split(".")[0] ?? ""
console.log(hash)
const msg = await client.Upscale(
question,
index,
id,
hash,
(uri: string, progress: string) => {
console.log("loading", uri, "progress", progress);
}
);
console.log("upscale success ", msg)
return msg
}
// variation 變換圖片
async function variation(param) {
console.log("variation", param)
const client = new Midjourney({
ServerId: SERVER_ID,
ChannelId: CHANNEL_ID,
SalaiToken: SALAI_TOKEN,
Debug: true,
SessionId: SALAI_TOKEN,
Limit: Limit,
MaxWait: 100
});
const { question, index, id, url } = param
const hash = url.split("_").pop()?.split(".")[0] ?? ""
const msg = await client.Variation(
question,
index,
id,
hash,
(uri: string, progress: string) => {
console.log("loading", uri, "progress", progress);
}
);
console.log("variation success ", msg)
return msg
}
沒有賬號可以到社區(qū)找 米開朗基楊@sealos.io 大佬 要獲取方式。
- 畫圖
畫圖首先需要創(chuàng)建繪畫任務,比如我的需求就是一只雞正在打籃球穿著吊帶,調(diào)用云函數(shù)的imagine方法。請求方式都是用post。
{
"type": "imagine",
"param": {
"question": "A chicken is playing basketball, wearing a white shirt and black suspender, with gray white hair in the middle, and leather shoes,He is thin and thin",
"msg_Id": 1684585158 //自己定義便于按id查詢生成的圖片,msg_Id 別打錯了
}
}
請求成功會返回true,稍等一會兒就可以調(diào)用查詢方法查看生成的圖片鏈接
- 查詢
RetrieveMessages方法是查詢最近的圖片信息,傳type就可以了
{
"type":"RetrieveMessages"
}
如果要根據(jù)id查詢,可以修改一下云函數(shù)里的方法:
// 查詢最近消息根據(jù)id
async function RetrieveMessagesById(param) {
console.log("RetrieveMessages")
const client = new MidjourneyMessage({
ChannelId: CHANNEL_ID,
SalaiToken: SALAI_TOKEN,
});
const msg = await client.RetrieveMessages();
const result = msg.find(v=>v.content.includes(param.msg_Id))
return result
}
這樣就可以按id查詢了,參數(shù)只要多傳一個id就行
{
"param": {
"msg_Id": "tudou007"
},
"type": "RetrieveMessagesById"
}
返回結(jié)果就會包含生成的圖片鏈接,url,但是在國內(nèi)是打不開的,解決辦法看個人。這里推薦使用laf云存儲,你的免費laf有一定額度的云存儲空間,一般測試夠用了,具體怎么用,我還在研究哈哈哈。
注意:返回的數(shù)據(jù)可能會缺少部分屬性,不用在意,有可能任務創(chuàng)建失敗,有可能在排隊出圖,或者是正在出圖,一般情況下,寬高大于512就成功了。我的處理方案就是,只要拿不到我想要的數(shù)據(jù),統(tǒng)一按正在出圖中處理。
- 放大
放大圖片就是四張縮略圖選一張放大,這里的id就是返回的id,index就是四張圖片的下標,question就是你創(chuàng)建繪圖任務的prompt,url不用說了。放大之后重新根據(jù)id獲取就會得到一張大圖。
{
"type": "upscale",
"param": {
"id": "dasdasdasdasd23123",
"question":"chekin",
"index": 3,
"url":"https://cdn.discordapp.com/attachments/1109368983364313204/1109460469628022915/johnsonmaureen_1684585158_a_chekin_d5b7e35c-0fce-4f7d-b440-35f5602d2f25.png"
}
}
- 重繪
可以從四張圖中選一張圖的風格進行重繪,參數(shù)和放大一樣的,只是type換了
{
"type": "variation",
"param": {
"id": "1109460470152319086",
"question": "a chekin",
"index": 3,
"url": "https://cdn.discordapp.com/attachments/1109368983364313204/1109460469628022915/johnsonmaureen_1684585158_a_chekin_d5b7e35c-0fce-4f7d-b440-35f5602d2f25.png"
}
}
完結(jié)
完結(jié)撒花,第一次在csdn寫文章,寫的不好多多包涵,
我的公眾號有個小彩蛋: 瑪卡巴卡和他的貓? ? ?文章來源:http://www.zghlxwxcb.cn/news/detail-483142.html
不用找體驗地址了,mj體驗賬號被封了,目前來晚的還想白嫖的同學就等我的應用吧,等我做完會有體驗入口。文章來源地址http://www.zghlxwxcb.cn/news/detail-483142.html
到了這里,關(guān)于無需魔法三分鐘上線Midjourney應用,【附源碼】【示例】的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!