赞
踩
众所周知,Midjourney并没有提供任何的Api服务,但是基于Midjourney目前的行业龙头位置,很多企业以及个人会有相关的需求。TTApi平台基于Midjourney现有功能整理出一套完整的可集成性高的服务,如果你有类似的需求,那么恭喜你找到了正确的使用方式。
新用户注册免费送 100 配额,最多可以免费请求 50 次imagine接口
import cn.hutool.http.HttpRequest; import cn.hutool.json.JSONUtil; import java.util.HashMap; import java.util.Map; /** * 参考文档 * (中文版):https://docs-zh.mjapiapp.com/midjourney/midjourney-api * (英文版):https://docs.mjapiapp.com/reference/midjourney-api */ public class TTApiMain { //TT-API-KEY private static final String TT_API_KET = "your_key"; //提交绘图申请 private static final String IMAGINE_URL = "https://api.ttapi.io/midjourney/v1/imagine"; //查询绘图结果 private static final String FETCH_RESULT = "https://api.ttapi.io/midjourney/v1/fetch"; public static void main(String[] args) { //发送imagine绘图请求 String result = imagine("dog"); //查询绘图结果 fetch(JSONUtil.parseObj(result).getJSONObject("data").getStr("jobId")); } /** * 发送 imagine 绘图请求 * @param prompt */ public static String imagine(String prompt){ Map<String, Object> map = new HashMap<>(); //提示词 map.put("prompt", prompt); String result = HttpRequest.post(IMAGINE_URL) .body(JSONUtil.toJsonStr(map)) .header("TT-API-KEY", TT_API_KET) .execute().body(); System.out.println("绘图请求响应:" + result); //{"status":"SUCCESS","message":"success","data":{"jobId":"******************************"}} return result; } /** * 查询绘图结果 * @param jobId 任务ID */ public static void fetch(String jobId){ String result = HttpRequest.get(FETCH_RESULT + "?jobId=" + jobId) .header("TT-API-KEY", TT_API_KET) .execute().body(); System.out.println("绘图结果:" + result); //{"status":"ON_QUEUE","message":"","jobId":"******************************","data":{"action":"imagine","jobId":"******************************","progress":null,"prompt":"dog","discordImage":null,"cdnImage":null,"hookUrl":null,"components":null,"seed":null}} } }
import requests endpoint = "https://api.ttapi.io/midjourney/v1/imagine" headers = { "TT-API-KEY": your_key } data = { "prompt": "a cute cat", "mode": "fast", "hookUrl": "", "timeout": 300 } response = requests.post(endpoint, headers=headers, json=data) print(response.status_code) print(response.json())
fast,relax,turbo
对应的也就是midjourney的模式,不设置默认为fastCopyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。