[ChatGPT] Automating MidJourney Image Generation with Web Scraping (Part 2)
Table of Contents
- Introduction
- Automating Midjourney Image Generation
- Setting Up your Server
- Subscribing to MidJourney
- Creating your Discord Server
- Adding the MidJourney Bot to your Server
- Installing the Required Packages
- Writing the Discord Bot Code
- Downloading Images Automatically
- Generating MidJourney Prompts
- Setting up OpenAI Playground
- Using the Prompt Generator
- Generating an Image Prompt
- Automating the Image Generation Process
- Setting up the Payload
- Setting the Headers
- Sending the POST Request
- Downloading and Viewing the Generated Image
- Conclusion
🤖 Automating MidJourney Image Generation
MidJourney provides the ability to generate customized images using prompts. However, the automation process of downloading these images is not yet complete. In this guide, we will walk You through the steps to fully automate this process.
Setting Up your Server
To automate the image download process from MidJourney, you need to set up your own server. This involves creating your own Discord server and adding the required applications and bots.
Subscribing to MidJourney
Before getting started, you need to have a subscription to MidJourney by subscribing to their Channel and typing Subscribe
. Once you have upgraded to an advanced account, you can proceed with the following steps.
Creating your Discord Server
To begin, Create your own Discord server by clicking on the "+" symbol in Discord. Select "Create a Server" and give it a name, such as "MidJourney Downloader." You can also add a custom image as the server icon.
Adding the MidJourney Bot to your Server
Next, you need to add the MidJourney bot to your newly created server. Open a private message with the MidJourney bot and search for it in your Discord chat. Right-click on the bot's name, select "Profile," and click on "Add to Server." Choose your server, in this case, "MidJourney Downloader," and click "Continue." Grant all the required permissions and authorize the bot. Confirm that you are a human when prompted.
Installing the Required Packages
To automate the image download, you need to install the necessary packages. Start by running the following command in your Jupyter notebook:
!pip install discord.py
This will allow you to Interact with Discord using Python.
Writing the Discord Bot Code
To create the Discord bot that will download the images, we need to write some code. Copy and paste the following code into your Jupyter notebook:
import discord
import asyncio
import requests
# Set up your bot token here
TOKEN = 'your_bot_token'
client = discord.Client()
@client.event
async def on_ready():
print('Logged in as {0.user}'.format(client))
print('Bot is ready to download images.')
@client.event
async def on_message(message):
if message.attachments:
for attach in message.attachments:
if attach.filename.endswith(('jpg', 'png')):
await download_image(attach.url, attach.filename)
print(f'Downloaded image: {attach.filename}')
async def download_image(url, filename):
res = requests.get(url)
with open(f'mid_images/{filename}', 'wb') as f:
f.write(res.content)
print(f'Download image: {filename}')
client.run(TOKEN)
Make sure to replace 'your_bot_token'
with the actual token generated for your bot.
Downloading Images Automatically
With the bot code in place, you can now automatically download images sent to your Discord server. Start the bot by running the code. Once it's logged in and ready, go to your Discord server and test it out. Send a message with the command imagine cute corgi
. The bot will generate and download an image of a cute corgi. You can check the downloaded image in the mid_images
folder.
Generating MidJourney Prompts
Before automating the image generation process, we need to set up the prompt required by MidJourney. This involves using OpenAI Playground to generate the prompt.
Setting up OpenAI Playground
Access the OpenAI Playground and enable the desired OpenAI models. Follow the instructions from the previous episode to set up your playground.
Using the Prompt Generator
To generate the MidJourney prompt, use the Generate_Prompt
function. This function takes the headline of the article as input and generates the prompt using the GPT-3.5 Turbo model.
from typing import List
import openai
def Generate_Prompt(headline: str) -> str:
openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": headline},
{"role": "assistant", "content": "Generate a midjourney prompt."}],
max_tokens=100,
n=1,
stop=None,
temperature=0.6,
)
Use this Generate_Prompt
function to convert the headline into a MidJourney prompt.
Continue with the steps in the code to complete the process of automating the MidJourney image generation and downloading.