[decorators] revert
Browse files
app.py
CHANGED
@@ -1,167 +1,160 @@
|
|
1 |
import discord
|
2 |
-
import gradio_client
|
3 |
-
from gradio_client import Client
|
4 |
-
import gradio as gr
|
5 |
import os
|
6 |
import threading
|
7 |
-
|
8 |
-
#for deepfloydif
|
9 |
import requests
|
10 |
import json
|
11 |
import random
|
12 |
-
from PIL import Image
|
13 |
-
import matplotlib.pyplot as plt
|
14 |
-
import matplotlib.image as mpimg
|
15 |
import time
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
-
# random + small llama #
|
18 |
|
|
|
|
|
19 |
|
20 |
-
#todos
|
21 |
-
#alert
|
22 |
-
#fix error on first command on bot startup
|
23 |
-
#stable diffusion upscale
|
24 |
-
#buttons for deepfloydIF (1,2,3,4)
|
25 |
-
#application commands instead of message content checks (more user-friendly)
|
26 |
|
|
|
|
|
|
|
27 |
|
|
|
28 |
|
29 |
|
30 |
-
|
31 |
|
32 |
-
|
33 |
-
|
34 |
-
|
|
|
35 |
|
36 |
-
|
37 |
-
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
-
|
40 |
-
|
41 |
-
async def on_ready(self):
|
42 |
-
print('Logged on as', self.user)
|
43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
|
45 |
-
|
|
|
|
|
|
|
|
|
46 |
|
47 |
-
|
|
|
|
|
|
|
48 |
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
# bot won't respond to itself, prevents feedback loop + API spam
|
55 |
-
if message.author == self.user:
|
56 |
-
return
|
57 |
-
|
58 |
-
# if the bot has this role, it won't run
|
59 |
-
OFFLINE_ROLE_ID = 1103676632667017266 # 1103676632667017266 = @offline / under maintenance
|
60 |
-
guild = message.guild
|
61 |
-
bot_member = guild.get_member(self.user.id)
|
62 |
-
if any(role.id == OFFLINE_ROLE_ID for role in bot_member.roles):
|
63 |
-
return
|
64 |
-
|
65 |
-
# the message author needs this role in order to use the bot
|
66 |
-
REQUIRED_ROLE_ID = 897376942817419265 # 900063512829755413 = @verified, 897376942817419265 = @huggingfolks
|
67 |
-
if not any(role.id == REQUIRED_ROLE_ID for role in message.author.roles):
|
68 |
-
return
|
69 |
-
|
70 |
-
# channels where bot will accept commands
|
71 |
-
ALLOWED_CHANNEL_IDS = [1100458786826747945] # 1100458786826747945 = #bot-test
|
72 |
-
if message.channel.id not in ALLOWED_CHANNEL_IDS:
|
73 |
-
return
|
74 |
-
|
75 |
-
|
76 |
-
#deepfloydif----------------------------------------------------------------------------------------------------
|
77 |
-
|
78 |
-
if message.content.startswith('!deepfloydif'): # change to application commands, more intuitive
|
79 |
|
80 |
-
|
81 |
-
#-> (stage_1_results, stage_1_param_path, stage_1_result_path)
|
82 |
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
number_of_images,
|
98 |
-
guidance_scale,
|
99 |
-
custom_timesteps_1,
|
100 |
-
number_of_inference_steps,
|
101 |
-
api_name='/generate64')
|
102 |
-
|
103 |
-
#stage_1_results, stage_1_param_path, stage_1_result_path = df.predict("gradio written on a wall", "blur", 1,1,7.0, 'smart100',50, api_name="/generate64")
|
104 |
-
|
105 |
-
# stage_1_results -> path to directory with png files, so we isolate those
|
106 |
-
png_files = [f for f in os.listdir(stage_1_results) if f.endswith('.png')]
|
107 |
-
|
108 |
-
# merge images into larger, 2x2 image the way midjourney does it
|
109 |
-
if png_files:
|
110 |
-
first_png = png_files[0]
|
111 |
-
second_png = png_files[1]
|
112 |
-
third_png = png_files[2]
|
113 |
-
fourth_png = png_files[3]
|
114 |
-
|
115 |
-
'''
|
116 |
-
[],[],[],[] -> [][]
|
117 |
-
[][]
|
118 |
-
|
119 |
-
'''
|
120 |
-
|
121 |
-
first_png_path = os.path.join(stage_1_results, first_png)
|
122 |
-
second_png_path = os.path.join(stage_1_results, second_png)
|
123 |
-
third_png_path = os.path.join(stage_1_results, third_png)
|
124 |
-
fourth_png_path = os.path.join(stage_1_results, fourth_png)
|
125 |
-
|
126 |
-
img1 = Image.open(first_png_path)
|
127 |
-
img2 = Image.open(second_png_path)
|
128 |
-
img3 = Image.open(third_png_path)
|
129 |
-
img4 = Image.open(fourth_png_path)
|
130 |
-
|
131 |
-
# create a new blank image with the size of the combined images (2x2)
|
132 |
-
combined_image = Image.new('RGB', (img1.width * 2, img1.height * 2))
|
133 |
-
|
134 |
-
# paste the individual images into the combined image
|
135 |
-
combined_image.paste(img1, (0, 0))
|
136 |
-
combined_image.paste(img2, (img1.width, 0))
|
137 |
-
combined_image.paste(img3, (0, img1.height))
|
138 |
-
combined_image.paste(img4, (img1.width, img1.height))
|
139 |
|
140 |
-
|
141 |
-
|
142 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
143 |
|
144 |
-
# send the combined image file as a discord attachment
|
145 |
-
with open(combined_image_path, 'rb') as f:
|
146 |
-
sent_message = await message.reply('Here is the combined image', file=discord.File(f, 'combined_image.png'))
|
147 |
-
|
148 |
-
#-------------------------------------------------------------------------------------------------------------
|
149 |
|
150 |
-
|
151 |
-
|
152 |
|
|
|
|
|
|
|
|
|
|
|
153 |
|
154 |
-
# deepfloydif -> bot_react ->
|
155 |
-
# await bot_react
|
156 |
-
# on_react -> if we react to a message, then we call dfif2
|
157 |
|
158 |
-
|
159 |
-
intents = discord.Intents.default()
|
160 |
-
intents.message_content = True
|
161 |
-
client = MyClient(intents=intents)
|
162 |
|
163 |
def run_bot():
|
164 |
-
|
165 |
|
166 |
threading.Thread(target=run_bot).start()
|
167 |
|
@@ -169,4 +162,4 @@ def greet(name):
|
|
169 |
return "Hello " + name + "!"
|
170 |
|
171 |
demo = gr.Interface(fn=greet, inputs="text", outputs="text")
|
172 |
-
demo.launch()
|
|
|
1 |
import discord
|
|
|
|
|
|
|
2 |
import os
|
3 |
import threading
|
4 |
+
import gradio as gr
|
|
|
5 |
import requests
|
6 |
import json
|
7 |
import random
|
|
|
|
|
|
|
8 |
import time
|
9 |
+
import re
|
10 |
+
from discord import Embed, Color
|
11 |
+
from discord.ext import commands
|
12 |
+
# test
|
13 |
+
from gradio_client import Client
|
14 |
+
from PIL import Image
|
15 |
+
#from ratelimiter import RateLimiter
|
16 |
+
#
|
17 |
+
from datetime import datetime
|
18 |
+
from pytz import timezone
|
19 |
+
#
|
20 |
+
import asyncio
|
21 |
|
|
|
22 |
|
23 |
+
DFIF_TOKEN = os.getenv('HF_TOKEN')
|
24 |
+
df = Client("huggingface-projects/IF", DFIF_TOKEN)
|
25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
+
DISCORD_TOKEN = os.environ.get("GRADIOTEST_TOKEN", None)
|
28 |
+
intents = discord.Intents.default()
|
29 |
+
intents.message_content = True
|
30 |
|
31 |
+
bot = commands.Bot(command_prefix='!', intents=intents)
|
32 |
|
33 |
|
34 |
+
#----------------------------------------------------------------------------------------------------------------------------------------------
|
35 |
|
36 |
+
@bot.event
|
37 |
+
async def on_ready():
|
38 |
+
print('Logged on as', bot.user)
|
39 |
+
bot.log_channel = bot.get_channel(1100458786826747945) # 1100458786826747945 = bot-test, 1107006391547342910 = lunarbot server
|
40 |
|
41 |
+
#----------------------------------------------------------------------------------------------------------------------------------------------
|
42 |
+
# Stage 1
|
43 |
+
@bot.command()
|
44 |
+
async def deepfloydif(ctx, *, prompt: str):
|
45 |
+
try:
|
46 |
+
prompt = prompt.strip()[:100] # Limit the prompt length to 100 characters
|
47 |
+
prompt = re.sub(r'[^\w\s]', '', prompt) # Remove special characters
|
48 |
|
49 |
+
def check_reaction(reaction, user):
|
50 |
+
return user == ctx.author and str(reaction.emoji) in ['1️⃣', '2️⃣', '3️⃣', '4️⃣']
|
|
|
|
|
51 |
|
52 |
+
await ctx.message.add_reaction('👍')
|
53 |
+
thread = await ctx.message.create_thread(name=f'{ctx.author} Image Upscaling Thread ')
|
54 |
+
# create thread -> send new message inside thread + combined_image -> add reactions -> dfif2
|
55 |
+
await thread.send(f'{ctx.author.mention}Generating images in thread, can take ~1 minute...')
|
56 |
+
|
57 |
+
number_of_images = 4
|
58 |
+
current_time = int(time.time())
|
59 |
+
random.seed(current_time)
|
60 |
+
seed = random.randint(0, 2**32 - 1)
|
61 |
+
stage_1_results, stage_1_param_path, stage_1_result_path = df.predict(prompt, "blur", seed, number_of_images, 7.0, 'smart100', 50, api_name="/generate64")
|
62 |
+
png_files = [f for f in os.listdir(stage_1_results) if f.endswith('.png')]
|
63 |
|
64 |
+
if png_files:
|
65 |
+
first_png = png_files[0]
|
66 |
+
second_png = png_files[1]
|
67 |
+
third_png = png_files[2]
|
68 |
+
fourth_png = png_files[3]
|
69 |
|
70 |
+
first_png_path = os.path.join(stage_1_results, first_png)
|
71 |
+
second_png_path = os.path.join(stage_1_results, second_png)
|
72 |
+
third_png_path = os.path.join(stage_1_results, third_png)
|
73 |
+
fourth_png_path = os.path.join(stage_1_results, fourth_png)
|
74 |
|
75 |
+
img1 = Image.open(first_png_path)
|
76 |
+
img2 = Image.open(second_png_path)
|
77 |
+
img3 = Image.open(third_png_path)
|
78 |
+
img4 = Image.open(fourth_png_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
|
80 |
+
combined_image = Image.new('RGB', (img1.width * 2, img1.height * 2))
|
|
|
81 |
|
82 |
+
combined_image.paste(img1, (0, 0))
|
83 |
+
combined_image.paste(img2, (img1.width, 0))
|
84 |
+
combined_image.paste(img3, (0, img1.height))
|
85 |
+
combined_image.paste(img4, (img1.width, img1.height))
|
86 |
+
|
87 |
+
combined_image_path = os.path.join(stage_1_results, 'combined_image.png')
|
88 |
+
combined_image.save(combined_image_path)
|
89 |
+
|
90 |
+
# Trigger the second stage prediction
|
91 |
+
#await dfif2(ctx, stage_1_result_path)
|
92 |
+
|
93 |
+
|
94 |
+
with open(combined_image_path, 'rb') as f:
|
95 |
+
threadmsg = await thread.send(f'{ctx.author.mention}React with the image number you want to upscale!', file=discord.File(f, 'combined_image.png'))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
96 |
|
97 |
+
# bot reacts with appropriate emojis to the post, both showing the user what options they have,
|
98 |
+
# as well as showing which post to react to.
|
99 |
+
for emoji in ['1️⃣', '2️⃣', '3️⃣', '4️⃣']:
|
100 |
+
await threadmsg.add_reaction(emoji)
|
101 |
+
|
102 |
+
reaction, user = await bot.wait_for('reaction_add', check=check_reaction)
|
103 |
+
if str(reaction.emoji) == '1️⃣':
|
104 |
+
await thread.send(f"{ctx.author.mention}Upscaling the first image...")
|
105 |
+
index = 0
|
106 |
+
await dfif2(ctx, index, stage_1_result_path, thread)
|
107 |
+
elif str(reaction.emoji) == '2️⃣':
|
108 |
+
await thread.send(f"{ctx.author.mention}Upscaling the second image...")
|
109 |
+
index = 1
|
110 |
+
await dfif2(ctx, index, stage_1_result_path, thread)
|
111 |
+
elif str(reaction.emoji) == '3️⃣':
|
112 |
+
await thread.send(f"{ctx.author.mention}Upscaling the third image...")
|
113 |
+
index = 2
|
114 |
+
await dfif2(ctx, index, stage_1_result_path, thread)
|
115 |
+
elif str(reaction.emoji) == '4️⃣':
|
116 |
+
await thread.send(f"{ctx.author.mention}Upscaling the fourth image...")
|
117 |
+
index = 3
|
118 |
+
await dfif2(ctx, index, stage_1_result_path, thread)
|
119 |
+
|
120 |
+
#deepfloydif try/except
|
121 |
+
except Exception as e:
|
122 |
+
print(f"Error: {e}")
|
123 |
+
await ctx.reply('An error occurred while processing your request. Please wait 5 seconds before retrying.')
|
124 |
+
await ctx.message.add_reaction('❌')
|
125 |
+
|
126 |
+
#----------------------------------------------------------------------------------------------------------------------------
|
127 |
+
# Stage 2
|
128 |
+
async def dfif2(ctx, index: int, stage_1_result_path, thread):
|
129 |
+
try:
|
130 |
+
selected_index_for_stage_2 = index
|
131 |
+
seed_2 = 0
|
132 |
+
guidance_scale_2 = 4
|
133 |
+
custom_timesteps_2 = 'smart50'
|
134 |
+
number_of_inference_steps_2 = 50
|
135 |
+
result_path = df.predict(stage_1_result_path, selected_index_for_stage_2, seed_2,
|
136 |
+
guidance_scale_2, custom_timesteps_2, number_of_inference_steps_2, api_name='/upscale256')
|
137 |
+
|
138 |
+
|
139 |
+
with open(result_path, 'rb') as f:
|
140 |
+
await thread.send(f'{ctx.author.mention}Here is the upscaled image! :) ', file=discord.File(f, 'result.png'))
|
141 |
+
#await ctx.reply('Here is the result of the second stage', file=discord.File(f, 'result.png'))
|
142 |
+
await ctx.message.add_reaction('✔️')
|
143 |
|
|
|
|
|
|
|
|
|
|
|
144 |
|
145 |
+
|
|
|
146 |
|
147 |
+
except Exception as e:
|
148 |
+
print(f"Error: {e}")
|
149 |
+
await ctx.reply('An error occurred while processing stage 2 upscaling. Please try again later.')
|
150 |
+
await ctx.message.add_reaction('❌')
|
151 |
+
#----------------------------------------------------------------------------------------------------------------------------
|
152 |
|
|
|
|
|
|
|
153 |
|
154 |
+
|
|
|
|
|
|
|
155 |
|
156 |
def run_bot():
|
157 |
+
bot.run(DISCORD_TOKEN)
|
158 |
|
159 |
threading.Thread(target=run_bot).start()
|
160 |
|
|
|
162 |
return "Hello " + name + "!"
|
163 |
|
164 |
demo = gr.Interface(fn=greet, inputs="text", outputs="text")
|
165 |
+
demo.launch()
|