Groundspout commited on
Commit
0daa710
·
1 Parent(s): 28cd149

Upload stbd.py

Browse files
Files changed (1) hide show
  1. stbd.py +81 -0
stbd.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import discord
3
+ from discord.ext import commands
4
+ import os
5
+ from diffusers import StableDiffusionPipeline, EulerDiscreteScheduler
6
+ import torch
7
+ import asyncio
8
+ import random
9
+ import threading
10
+ from concurrent.futures import ThreadPoolExecutor
11
+ from discord import app_commands
12
+ executor = ThreadPoolExecutor()
13
+ bot = commands.Bot(command_prefix='!', intents=discord.Intents.all())
14
+
15
+
16
+
17
+
18
+ model_id = "stabilityai/stable-diffusion-2-base"
19
+ queue = []
20
+ # Use the Euler scheduler here instead
21
+ scheduler = EulerDiscreteScheduler.from_pretrained(model_id, subfolder="scheduler")
22
+ pipe = StableDiffusionPipeline.from_pretrained(model_id, scheduler=scheduler, torch_dtype=torch.float16)
23
+ pipe = pipe.to("cuda")
24
+
25
+
26
+
27
+
28
+
29
+ semaphore = threading.Semaphore(value=1)
30
+
31
+ from collections import deque
32
+ from typing import Deque, Tuple
33
+
34
+ queue: Deque[Tuple[int, discord.Message]] = deque()
35
+
36
+ async def update_queue_messages():
37
+ for i, (_, message) in enumerate(queue):
38
+ await message.edit(content=f"Generating image... (position in queue: {i + 1})")
39
+
40
+ def generate_image(prompt, id):
41
+ semaphore.acquire()
42
+ try:
43
+ image = pipe(prompt).images[0]
44
+ path = f"{prompt}{random.randint(1,10000000)}.png"
45
+ image.save(path)
46
+ file = discord.File(path)
47
+ finally:
48
+ semaphore.release()
49
+ queue.popleft()
50
+ asyncio.run_coroutine_threadsafe(update_queue_messages(), bot.loop)
51
+ return (file,path)
52
+
53
+
54
+
55
+
56
+ @bot.event
57
+ async def on_ready():
58
+ await bot.tree.sync()
59
+
60
+ @bot.tree.command(name="ai", description="Generate an image using AI, describe the image with up to 100 characters.")
61
+ async def generate(interaction:discord.Interaction, prompt: str=None):
62
+ if not prompt or len(prompt)>100:
63
+ await interaction.response.send_message("Please write a description of the image up to 100 characters.")
64
+ position = len(queue) + 1
65
+ await interaction.response.send_message(f"Generating image... (position in queue: {position})")
66
+ response = await interaction.original_response()
67
+ queue.append((interaction.user.id, response))
68
+ asyncio.run_coroutine_threadsafe(update_queue_messages(), bot.loop)
69
+ value = await bot.loop.run_in_executor(executor, generate_image, prompt, interaction.user.id)
70
+ image = value[0]
71
+ if image:
72
+ await interaction.followup.send(content=prompt, file=image)
73
+ os.remove(value[1])
74
+ else:
75
+ await interaction.followup.send("there was an error, try again later...")
76
+ await response.edit(content="Finished generating")
77
+
78
+
79
+
80
+ bot.run(
81
+ 'MTExMzEwNDIyNzIyMTM4MTE1MA.GoPxZb.d0w449b8n3DMxasP1-HHiJo7nRCT1YnHQ20hYY')