文章尾部有demo
江湖慣例:先來一波感謝,感謝laf,讓我們可以不使用魔法、免費接入Midjourney,不了解laf的請猛戳 Laf介紹
一、寫這篇博客的背景
laf官方最近發(fā)布了一個活動,活動鏈接,新手也可以接入哦!廢話不多說,趕緊點擊鏈接報名贏取豐厚獎品吧。
二、接入準備
在laf.dev(注意一定要用laf.dev,不要使用laf.run)注冊賬號,新用戶可以免費一個月體驗,老用戶略過
三、開始寫接口
- 先在左下角NPM依賴添加依賴
- 新建一個云函數(shù),隨便取個名字
- 編輯云函數(shù)代碼,復制以下代碼全部粘貼進去,這段示例代碼由laf提供,有能力的可以自行修改
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
// 初始化Midjourney
const client = new Midjourney({
ServerId: SERVER_ID,
ChannelId: CHANNEL_ID,
SalaiToken: SALAI_TOKEN,
Debug: true,
SessionId: SALAI_TOKEN,
Limit: Limit,
MaxWait: MaxWait
});
// 云函數(shù)主入口代碼
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
}
你可能已經注意到初始化時有三個參數(shù)沒有值,請仔細閱讀活動鏈接獲取
點擊右上角發(fā)布,你的應用就上線了。怎么樣?標題沒有騙你吧,是不是三分鐘。
四、調試接口
-
生成圖片
msg_Id可以隨便填寫,但最好不要太簡單,避免與頻道內其他用戶重復 -
獲取結果
RetrieveMessages獲取到的是當前頻道內所有用戶生成的最近50個結果,
請注意圖中紅線部分,這就是上面那個msg_Id
,通過這個id可以篩選出你自己的結果,對應的attachments
內有個url就是你要的圖片鏈接 -
變換圖片(選取一張你喜歡的做調整)
-
type
固定傳variation -
id
是第二步中通過msg_Id篩選獲取到的那條數(shù)據的id -
url
是對應的attachments內的url -
question
是你自己的提問內容 -
index
是圖片的序號,圖片序號見下圖
- 放大圖片
跟調整圖片參數(shù)差不多,只需要把type改為upscale
五、如何調用接口
復制右上角發(fā)布按鈕旁的鏈接,即可在postman或前端頁面中調用。前端界面隨你發(fā)揮,歡迎入群交流想法和作品文章來源:http://www.zghlxwxcb.cn/news/detail-463599.html
六、end
到這里就接入結束了,再次感謝laf官方,demo在此文章來源地址http://www.zghlxwxcb.cn/news/detail-463599.html
到了這里,關于使用laf云開發(fā)三分鐘上線你自己的Midjourney的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!