At the end of the article, there is a demo
Jianghu practice: first, a wave of thanks, thank you laf, let us access Midjourney for free without using magic, if you don’t know laf, please poke Laf’s introduction
1. The background of writing this blog
Laf officially released an event recently, the event link , novices can also access it! Without further ado, click the link to sign up to win great prizes.
2. Access preparation
Register an account at laf.dev (note that you must use laf.dev, not laf.run), new users can experience it for free for one month, old users skip it
3. Start writing the interface
- First add dependencies to NPM dependencies in the lower left corner
- Create a new cloud function, choose any name
- Edit the cloud function code, copy and paste all the following codes, this sample code is provided by laf, you can modify it yourself
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
});
// 云函数主入口代码
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
}
// 创建生图任务
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
}
You may have noticed that there are three parameters with no value during initialization, please read the activity link carefully to get
Click Publish in the upper right corner, and your app will go live. How about it? The title didn't lie to you, didn't it last three minutes.
4. Debug interface
-
The generated image
msg_Id can be filled in at will, but it is best not to be too simple to avoid duplication with other users in the channel -
RetrieveMessages retrieves
the latest 50 results generated by all users in the current channel.
Please pay attention to the red line in the picture. This is the one abovemsg_Id
. You can filter your own results through this id.attachments
The corresponding url is yours. link to picture -
Transform pictures (choose one you like to adjust)
type
fixed pass variationid
It is the id of the piece of data obtained through msg_Id filtering in the second stepurl
is the url in the corresponding attachmentsquestion
is your own questionindex
It is the serial number of the picture, see the picture below for the serial number of the picture
- Enlarging the picture
is similar to adjusting the picture parameters, just change the type to upscale
5. How to call the interface
Copy the link next to the publish button in the upper right corner to call it in postman or the front-end page. The front-end interface is up to you, welcome to join the group to exchange ideas and works
Six, end
The access is over here, thanks again to the laf official, the demo is here