├── requirements.txt ├── __pycache__ └── messages.cpython-38.pyc ├── README.md ├── messages.py └── bot_ImageGeneration.py /requirements.txt: -------------------------------------------------------------------------------- 1 | requests>=2.25.0 2 | replicate 3 | asyncio 4 | discord 5 | pillow 6 | numpy -------------------------------------------------------------------------------- /__pycache__/messages.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Glory413/DiscordBot_ImageGeneration/main/__pycache__/messages.cpython-38.pyc -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | # Discord bot 3 | 4 | ``` 5 | pip install -r requirements.txt 6 | ``` 7 | ### dev mode 8 | ``` 9 | python3 bot_ImageGeneration.py 10 | ``` 11 | ### product mode 12 | ``` 13 | pm2 start bot_ImageGeneration.py --name bot_ImageGeneration --interpreter python3 14 | ``` 15 | ``` 16 | pm2 stop bot_ImageGeneration.py 17 | ``` 18 | -------------------------------------------------------------------------------- /messages.py: -------------------------------------------------------------------------------- 1 | message = { 2 | "/start": """👋 Hello there!\n\nThis bot returns AI-generated images from any prompt you give.\nExample: Send a message like
/imagine a gorgeous supermodel with blonde hair\n\nEnjoy! 💖""", 3 | "/help": "Send a message like
/imagine a gorgeous supermodel with blonde hairto generate a image.", 4 | "/wait":"The image is being generated. Please wait a few seconds for it..." 5 | } -------------------------------------------------------------------------------- /bot_ImageGeneration.py: -------------------------------------------------------------------------------- 1 | import discord 2 | import os 3 | import asyncio 4 | import replicate 5 | import urllib.request 6 | import messages 7 | 8 | os.environ['REPLICATE_API_TOKEN'] = '57b5717e8632693a79f0747038512564a640764c' 9 | client = discord.Client(intents=discord.Intents.all()) 10 | PREFIX = "/imagine" 11 | 12 | @client.event 13 | async def on_ready(): 14 | print( 15 | 'I am ready for listening.' 16 | ) 17 | 18 | @client.event 19 | async def on_message(message): 20 | if message.author == client.user: 21 | return 22 | 23 | if message.content.startswith(PREFIX + " "): 24 | await handle_input (message) 25 | 26 | async def handle_input(message): 27 | print (message.content) 28 | prompt = message.content.split(PREFIX + " ")[1] 29 | if prompt.strip() == "": 30 | return 31 | 32 | username = message.author.id 33 | model = replicate.models.get("stability-ai/stable-diffusion") 34 | 35 | version = model.versions.get( 36 | "f178fa7a1ae43a9a9af01b833b9d2ecf97b1bcb0acfd2dc5dd04895e042863f1") 37 | 38 | inputs = { 39 | 'width': 768, 40 | 'height': 768, 41 | 'prompt_strength': 0.8, 42 | 'num_outputs': 1, 43 | 'num_inference_steps': 50, 44 | 'guidance_scale': 7.5, 45 | 'scheduler': "DPMSolverMultistep", 46 | } 47 | inputs['prompt'] = prompt 48 | wait_m = await message.channel.send(messages.message["/wait"]) 49 | prediction = replicate.predictions.create(version=version, input=inputs) 50 | output = [] 51 | while prediction.status == 'processing' or prediction.status == 'starting': 52 | prediction.reload() 53 | print (prediction.status) 54 | 55 | if prediction.status == "failed": 56 | break 57 | 58 | if prediction.status == 'succeeded': 59 | output = prediction.output 60 | break 61 | await asyncio.sleep(5) 62 | 63 | if len(output) == 0: 64 | return 65 | 66 | generated_image_url = output[0] 67 | urllib.request.urlretrieve(generated_image_url, f"images/{username}.png") 68 | photo = open(f"images/{username}.png", "rb") 69 | await wait_m.delete() 70 | embed = discord.Embed(title=f"{prompt}\nAn Image Generated by @{message.author}\n\n", color=9699539) 71 | embed.set_author( 72 | name=f"{message.author}", 73 | ) 74 | file = discord.File(photo, 'image.jpg') 75 | embed.set_image (url=f"attachment://images/{username}.png") 76 | embed.set_thumbnail(url=f"attachment://images/{username}.png") 77 | await message.channel.send(file=file, embed=embed) 78 | 79 | client.run('MTA3OTY1MDkyNDA5NzcyMDM0Mg.GhBdjz.b1NTpAFtydJx5MCtO8c1h1Exp7JbHXpj2rOEts') --------------------------------------------------------------------------------