【开源推荐】基于 Discord 的 Midjourney API 绘画接口

AI绘画 2023年11月6日 1.01K

总感觉自己会慢半拍,之前ChatGPT火的时候,我只是听说了,没立马就开始研究,现如今AI画画火的时候,我也没立马开始研究,直到群里有个哥们用AI画画开始赚钱的时候,我才开始研究,研究不要紧,一入坑难以自拔呀,根本停不下来,根本停不下来,深深的被这破玩意吸引啦,公众号也不写了,活也不想干了,这不在逛 github时,发现了这个开源项目,分享给大家。

项目流程图

使用条件

1、先要有 Midjourney、Discord 账户,要不然用不了。

2、你会用midjourney画画,否在不用会。

3、确保程序启动环境能访问 Discord,这个就需要你自己琢磨啦。

安装启动

git clone
pip install -r requirements.txt

将文件.env.template重命名为.env,并填入参数值:

USER_TOKEN=用户token
BOT_TOKEN=机器人token
GUILD_ID=服务器ID
CHANNEL_ID=频道ID
CALLBACK_URL=回调地址,默认http post请求,用于接收 midjourney 作图进度和结果的服务

直接启动

# 启动监听机器人
python task_bot.py
# 启动http服务
python server.py

docker 启动

填写 start.sh 中 -e 后的环境变量,直接启动:

sh start.sh

或者本地构建镜像:

# 构建镜像
sh build.sh
# 启动容器
sh start.sh

接口swagger文档:http://127.0.0.1:8062/docs

midjourney-api 提供接口

/v1/api/trigger/imagine:触发绘画任务(图生图,Prompt 前加上图片链接即可)
/v1/api/trigger/upscale:U
/v1/api/trigger/variation:V
/v1/api/trigger/solo_variation:Make Variations
/v1/api/trigger/solo_low_variation:Vary(Subtle)
/v1/api/trigger/solo_high_variation:Vary(Strong)
/v1/api/trigger/zoomout:Zoom Out 2x/1.5x
/v1/api/trigger/expand:⬅️ ➡️ ⬆️ ⬇️
/v1/api/trigger/reset:重绘
/v1/api/trigger/upload:上传图片
/v1/api/trigger/describe:通过上传图片名,生成 Prompt
/v1/api/trigger/message:发送图片消息,返回图片链接,用于图生图功能

使用方法

imagine

文生图

curl -X 'POST' \
'http://127.0.0.1:8062/v1/api/trigger/imagine' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"prompt": "a cute cat"
}'

图生图,需带上图片 URL

curl -X 'POST' \
  'http://127.0.0.1:8062/v1/api/trigger/imagine' \
  -H 'accept: application/json' \
  -H 'Content-Type: application/json' \
  -d '{
  "prompt": "a cute cat",
  "picurl": "https://xxxxxx/xxxxxxxxxxxx.jpg"
}'

upscale

curl -X 'POST' \
'http://127.0.0.1:8062/v1/api/trigger/upscale' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"index": 1,
"msg_id": "xxxxxxxxxx",
"msg_hash": "xxxxx-xxx-xxxx-xxxx-xxxxxx",
"trigger_id": "xxxxxxxxxx"
}'

index: 图片索引,取值:1、2、3、4
msg_id: imagine 绘画完成后回调报文 id 字段
msg_hash: imagine 绘画完成后回调报文 attachments[0].filename.split(“_”)[-1].split(“.”).[0]
trigger_id: imagine 绘画完成后回调报文 trigger_id 字段

variation

curl -X 'POST' \
'http://127.0.0.1:8062/v1/api/trigger/variation' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"index": 2,
"msg_id": "xxxxxxxxxx",
"msg_hash": "xxxxx-xxx-xxxx-xxxx-xxxxxx",
"trigger_id": "xxxxxxxxxx"
}'

solo_variation

对 upscale 的单张图片进行 “Make Variations” 操作

curl -X 'POST' \
'http://127.0.0.1:8062/v1/api/trigger/solo_variation' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"index": 1,
"msg_id": "xxxxxxxxxx",
"msg_hash": "xxxxx-xxx-xxxx-xxxx-xxxxxx",
"trigger_id": "xxxxxxxxxx"
}'

index: 图片索引,此处无用,取值:1
msg_id: upscale 绘画完成后回调报文 id 字段
msg_hash: upscale 绘画完成后回调报文 attachments[0].filename.split(“_”)[-1].split(“.”).[0]
trigger_id: upscale 绘画完成后回调报文 trigger_id 字段
solo_low_variation
对 upscale 的单张图片进行 “Vary(Subtle)” 操作

curl -X 'POST' \
'http://127.0.0.1:8062/v1/api/trigger/solo_low_variation' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"index": 1,
"msg_id": "xxxxxxxxxx",
"msg_hash": "xxxxx-xxx-xxxx-xxxx-xxxxxx",
"trigger_id": "xxxxxxxxxx"
}'

index: 图片索引,此处无用,取值:1
msg_id: upscale 绘画完成后回调报文 id 字段
msg_hash: upscale 绘画完成后回调报文 attachments[0].filename.split(“_”)[-1].split(“.”).[0]
trigger_id: upscale 绘画完成后回调报文 trigger_id 字段

solo_high_variation

对 upscale 的单张图片进行 “Vary(Strong)” 操作

curl -X 'POST' \
'http://127.0.0.1:8062/v1/api/trigger/solo_high_variation' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"index": 1,
"msg_id": "xxxxxxxxxx",
"msg_hash": "xxxxx-xxx-xxxx-xxxx-xxxxxx",
"trigger_id": "xxxxxxxxxx"
}'

index: 图片索引,此处无用,取值:1
msg_id: upscale 绘画完成后回调报文 id 字段
msg_hash: upscale 绘画完成后回调报文 attachments[0].filename.split(“_”)[-1].split(“.”).[0]
trigger_id: upscale 绘画完成后回调报文 trigger_id 字段

zoomout

对 upscale 的单张图片进行 Zoom Out 2x/1.5x 操作

curl -X 'POST' \
'http://127.0.0.1:8062/v1/api/trigger/zoomout' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"msg_id": "xxxxxxxxxx",
"msg_hash": "xxxxx-xxx-xxxx-xxxx-xxxxxx",
"zoomout": 50
"trigger_id": "xxxxxxxxxx"
}'

zoomout: 图片扩大(Outpaint)系数,2x -> 50、1.5x -> 75

expand

对 upscale 的单张图片进行某方向的扩展操作

curl -X 'POST' \
'http://127.0.0.1:8062/v1/api/trigger/expand' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"msg_id": "xxxxxxxxxx",
"msg_hash": "xxxxx-xxx-xxxx-xxxx-xxxxxx",
"direction": "up"
"trigger_id": "xxxxxxxxxx"
}'

direction: 图片扩大方向,取值:left/right/up/down

reset

curl -X 'POST' \
'http://127.0.0.1:8062/v1/api/trigger/reset' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"msg_id": "xxxxxxxxxx",
"msg_hash": "xxxxx-xxx-xxxx-xxxx-xxxxxx",
"trigger_id": "xxxxxxxxxx"
}'

describe

先上传图片

curl -X 'POST' \
'http://127.0.0.1:8062/v1/api/trigger/upload' \
-H 'accept: application/json' \
-H 'Content-Type: multipart/form-data' \
-F 'file=@cH16Ifh.jpg;type=image/jpeg'

根据返回的图片文件名,调用 describe

curl -X 'POST' \
'http://127.0.0.1:8062/v1/api/trigger/describe' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"upload_filename": "b56ca21a-5fbe-40b4-89ab-6e0aa732f561/9231228408.jpg",
"trigger_id": "9231228408"
}'

trigger_id 先用 upload 返回的 trigger_id
upload_filename upload 返回的文件名

message

和 describe 一样,先 /v1/api/trigger/upload 上传图片,然后根据返回文件名,发送消息:

curl -X 'POST' \
'http://127.0.0.1:8062/v1/api/trigger/message' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"upload_filename": "560a1e26-36a2-4d5f-a48d-9dd877642b51/7185811546.jpg"
}'

发送图片后,会返回图片链接。 该链接用于以图生图中,拼接 Prompt 形如 图片URL Prompt,调用 /v1/api/trigger/imagine。

资源下载
PHP学习网
公众号回复“PHPER7131”获取下载!
请打开微信扫描右边的二维码回复关键字“PHPER7131”也可以微信直接搜索“PHP学习网”关注微信公众号获取。


关注微信公众号『PHP学习网

第一时间了解最新网络动态
关注博主不迷路~

PHP学习网:站内收集的部分资源来源于网络,若侵犯了您的合法权益,请联系我们删除!
分享到:
赞(0)

文章评论

您需要之后才可以评论
0点赞 0评论 收藏 QQ分享 微博分享

PHP学习网

PHP学习网