forked from invictus2010/dnd-ai-art-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
53 lines (46 loc) · 1.67 KB
/
bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from fileinput import filename
import discord
from discord import Intents
from discord.ext import commands
from dotenv import load_dotenv
from PIL import Image
import warnings
import os
import io
from stability_sdk import client
import stability_sdk.interfaces.gooseai.generation.generation_pb2 as generation
import logging
load_dotenv()
stability_api = client.StabilityInference(
key=os.environ['STABLE_DIFFUSION_TOKEN'],
verbose=True,
engine="stable-diffusion-512-v2-1"
)
intents = Intents.default()
intents.message_content = True
bot = commands.Bot(
command_prefix="!",
description="Make art.",
intents=intents,
)
@bot.hybrid_command()
async def imagine(ctx, *, prompt):
msg = await ctx.send(f"“{prompt}”\n> Generating...")
answers = stability_api.generate(prompt=prompt)
for resp in answers:
for artifact in resp.artifacts:
if artifact.finish_reason == generation.FILTER:
warnings.warn(
"Your request activated the API's safety filters and could not be processed."
"Please modify the prompt and try again.")
msg = await ctx.send(
"You have triggered the filter, please try again")
if artifact.type == generation.ARTIFACT_IMAGE:
img = Image.open(io.BytesIO(artifact.binary))
arr = io.BytesIO(artifact.binary)
img.save(arr, format='PNG')
arr.seek(0)
file = discord.File(arr, filename='art.png')
await msg.edit(content=f"“{prompt}” \n")
await ctx.send(file=file)
bot.run(os.environ["DISCORD_TOKEN"], log_level=logging.DEBUG)