├── dotenv.sample ├── package.json ├── README.md ├── LICENSE ├── .gitignore └── midjourney.js /dotenv.sample: -------------------------------------------------------------------------------- 1 | TNL_TOKEN = '...' -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "dependencies": { 3 | "axios": "^1.4.0", 4 | "dotenv": "^16.0.3" 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # MidJourney connect 2 | Sample code for showing how to connect with mjdjourney 3 | 4 | ## How it works 5 | its wrapping discord API that provided by mjdjourney. 6 | the workflow is first generate 4 low resolution images (which is the default behavor of mjdjourney) 7 | and choose one image to upscale and then return the imageURL 8 | 9 | ## API 10 | ```js 11 | //prompt string 12 | //aspect , e.g: 2:3 for portiat or 3:2 for widescreen 13 | //aspect default value is 3:2 14 | const imageUrl = await generate(prompt, aspect); 15 | 16 | //example: 17 | const imageUrl = await generate('a pretty boy with short hair', '2:3'); 18 | ``` 19 | 20 | ## Installation 21 | ```sh 22 | npm install 23 | ``` 24 | 25 | ## Usage 26 | ```sh 27 | # syntax 28 | ./mjdjourney [prompt] 29 | 30 | # example: 31 | ./mjdjourney a blone girl looking at the window by disney style 32 | ``` -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | BSD 3-Clause License 2 | 3 | Copyright (c) 2023, Jixian Wang 4 | 5 | Redistribution and use in source and binary forms, with or without 6 | modification, are permitted provided that the following conditions are met: 7 | 8 | 1. Redistributions of source code must retain the above copyright notice, this 9 | list of conditions and the following disclaimer. 10 | 11 | 2. Redistributions in binary form must reproduce the above copyright notice, 12 | this list of conditions and the following disclaimer in the documentation 13 | and/or other materials provided with the distribution. 14 | 15 | 3. Neither the name of the copyright holder nor the names of its 16 | contributors may be used to endorse or promote products derived from 17 | this software without specific prior written permission. 18 | 19 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 22 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 23 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 25 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 26 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 27 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | lerna-debug.log* 8 | .pnpm-debug.log* 9 | 10 | # Diagnostic reports (https://nodejs.org/api/report.html) 11 | report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json 12 | 13 | # Runtime data 14 | pids 15 | *.pid 16 | *.seed 17 | *.pid.lock 18 | 19 | # Directory for instrumented libs generated by jscoverage/JSCover 20 | lib-cov 21 | 22 | # Coverage directory used by tools like istanbul 23 | coverage 24 | *.lcov 25 | 26 | # nyc test coverage 27 | .nyc_output 28 | 29 | # Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) 30 | .grunt 31 | 32 | # Bower dependency directory (https://bower.io/) 33 | bower_components 34 | 35 | # node-waf configuration 36 | .lock-wscript 37 | 38 | # Compiled binary addons (https://nodejs.org/api/addons.html) 39 | build/Release 40 | 41 | # Dependency directories 42 | node_modules/ 43 | jspm_packages/ 44 | 45 | # Snowpack dependency directory (https://snowpack.dev/) 46 | web_modules/ 47 | 48 | # TypeScript cache 49 | *.tsbuildinfo 50 | 51 | # Optional npm cache directory 52 | .npm 53 | 54 | # Optional eslint cache 55 | .eslintcache 56 | 57 | # Optional stylelint cache 58 | .stylelintcache 59 | 60 | # Microbundle cache 61 | .rpt2_cache/ 62 | .rts2_cache_cjs/ 63 | .rts2_cache_es/ 64 | .rts2_cache_umd/ 65 | 66 | # Optional REPL history 67 | .node_repl_history 68 | 69 | # Output of 'npm pack' 70 | *.tgz 71 | 72 | # Yarn Integrity file 73 | .yarn-integrity 74 | 75 | # dotenv environment variable files 76 | .env 77 | .env.development.local 78 | .env.test.local 79 | .env.production.local 80 | .env.local 81 | 82 | # parcel-bundler cache (https://parceljs.org/) 83 | .cache 84 | .parcel-cache 85 | 86 | # Next.js build output 87 | .next 88 | out 89 | 90 | # Nuxt.js build / generate output 91 | .nuxt 92 | dist 93 | 94 | # Gatsby files 95 | .cache/ 96 | # Comment in the public line in if your project uses Gatsby and not Next.js 97 | # https://nextjs.org/blog/next-9-1#public-directory-support 98 | # public 99 | 100 | # vuepress build output 101 | .vuepress/dist 102 | 103 | # vuepress v2.x temp and cache directory 104 | .temp 105 | .cache 106 | 107 | # Docusaurus cache and generated files 108 | .docusaurus 109 | 110 | # Serverless directories 111 | .serverless/ 112 | 113 | # FuseBox cache 114 | .fusebox/ 115 | 116 | # DynamoDB Local files 117 | .dynamodb/ 118 | 119 | # TernJS port file 120 | .tern-port 121 | 122 | # Stores VSCode versions used for testing VSCode extensions 123 | .vscode-test 124 | 125 | # yarn v2 126 | .yarn/cache 127 | .yarn/unplugged 128 | .yarn/build-state.yml 129 | .yarn/install-state.gz 130 | .pnp.* 131 | -------------------------------------------------------------------------------- /midjourney.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | const axios = require('axios'); 3 | const { performance } = require('perf_hooks'); 4 | 5 | require('dotenv').config(); 6 | const token = process.env.TNL_TOKEN 7 | 8 | const imagine = async (prompt, aspect) => { 9 | try { 10 | const data = JSON.stringify({ 11 | msg: `${prompt} --aspect ${aspect} --quality 1`, 12 | ref: '', 13 | webhookOverride: '' 14 | }); 15 | 16 | const config = { 17 | method: 'post', 18 | url: 'https://api.thenextleg.io/v2/imagine', 19 | headers: { 20 | 'Authorization': `Bearer ${token}`, 21 | 'Content-Type': 'application/json' 22 | }, 23 | data: data 24 | }; 25 | 26 | const response = await axios(config); 27 | return response.data 28 | } catch (error) { 29 | console.log(error); 30 | } 31 | }; 32 | 33 | 34 | const getMessage = async(messageId) => { 35 | try { 36 | const config = { 37 | method: 'get', 38 | url: `https://api.thenextleg.io/v2/message/${messageId}`, 39 | headers: { 40 | 'Authorization': `Bearer ${token}`, 41 | 'Content-Type': 'application/json' 42 | }, 43 | }; 44 | 45 | const response = await axios(config); 46 | return response.data 47 | } catch (error) { 48 | console.log(error); 49 | } 50 | } 51 | 52 | const upscale = async (buttonMessageId, id) => { 53 | try { 54 | const data = JSON.stringify({ 55 | button: `U${id}`, 56 | buttonMessageId: buttonMessageId, 57 | ref: '', 58 | webhookOverride: '' 59 | }); 60 | 61 | const config = { 62 | method: 'post', 63 | url: 'https://api.thenextleg.io/v2/button', 64 | headers: { 65 | 'Authorization': `Bearer ${token}`, 66 | 'Content-Type': 'application/json' 67 | }, 68 | data: data 69 | }; 70 | 71 | const response = await axios(config); 72 | return response.data 73 | } catch (error) { 74 | console.log(error); 75 | } 76 | } 77 | 78 | const swtichModel = async (mode='fast') => { 79 | try { 80 | const data = JSON.stringify({ 81 | cmd: `${mode}`, 82 | ref: '', 83 | webhookOverride: '' 84 | }); 85 | 86 | const config = { 87 | method: 'post', 88 | url: 'https://api.thenextleg.io/v2/slash-commands', 89 | headers: { 90 | 'Authorization': `Bearer ${token}`, 91 | 'Content-Type': 'application/json' 92 | }, 93 | data: data 94 | }; 95 | 96 | const response = await axios(config); 97 | return response.data 98 | } catch (error) { 99 | console.log(error); 100 | } 101 | } 102 | 103 | const generate = async (prompt, aspect='2:3') => { 104 | let progress; 105 | // ensure in fast mode 106 | let result = await swtichModel('fast'); 107 | if (!result['success']) return; 108 | 109 | // wait for result 110 | progress = 0; 111 | while(progress < 100) { 112 | const {messageId} = result; 113 | await new Promise(resolve => setTimeout(resolve, 1000)); 114 | message = await getMessage(messageId); 115 | progress = message.progress; 116 | } 117 | const {content} = message.response; 118 | console.log(`${content}`) 119 | 120 | // generate image 121 | result = await imagine(prompt, aspect); 122 | if (!result['success']) return; 123 | 124 | // wait for result 125 | progress = 0; 126 | while(progress < 100) { 127 | // wait for one second 128 | await new Promise(resolve => setTimeout(resolve, 1000)); 129 | const { messageId } = result; 130 | message = await getMessage(messageId); 131 | if (message) { 132 | progress = message.progress; 133 | process.stdout.write(`\rGenerating progress: ${progress}% `); 134 | } 135 | } 136 | const {buttonMessageId} = message.response; 137 | process.stdout.write("\n"); 138 | 139 | // upscale v1 140 | result = await upscale(buttonMessageId, 1); 141 | // wait for result 142 | progress = 0; 143 | while(progress < 100) { 144 | // wait for one second 145 | await new Promise(resolve => setTimeout(resolve, 1000)); 146 | const { messageId } = result; 147 | message = await getMessage(messageId); 148 | if (message) { 149 | progress = message.progress; 150 | process.stdout.write(`\rUpscaling progress: ${progress}% `); 151 | } 152 | } 153 | process.stdout.write("\n"); 154 | const {imageUrl} = message.response; 155 | return imageUrl; 156 | } 157 | 158 | 159 | const default_prompt = 'a beautiful girl with blue eyes, by disney'; 160 | const aspect = '3:2'; 161 | //main logic 162 | (async () => { 163 | const args = process.argv.slice(2); 164 | const prompt = args.join(' ') || default_prompt; 165 | console.log(`prompt: ${prompt}`); 166 | const start = performance.now(); 167 | const imageUrl = await generate(prompt, aspect) 168 | const end = performance.now(); 169 | const duration = end - start; 170 | //print result 171 | console.log(`myFunction took ${(duration/1000).toFixed(1)} seconds to run.`); 172 | console.log(`imageUrl: ${imageUrl}`); 173 | 174 | //open image in browser 175 | const { spawn } = require('child_process'); 176 | const open_args = ['--new-window',imageUrl]; 177 | spawn('google-chrome', open_args); 178 | })(); 179 | --------------------------------------------------------------------------------