revert to friday version
Browse files
app.py
CHANGED
@@ -7,8 +7,6 @@ import json
|
|
7 |
import random
|
8 |
import time
|
9 |
import re
|
10 |
-
|
11 |
-
breaking bot for a moment
|
12 |
from discord import Embed, Color
|
13 |
from discord.ext import commands
|
14 |
# test
|
@@ -29,78 +27,156 @@ def convert_to_timezone(dt, tz):
|
|
29 |
return dt.astimezone(tz).strftime("%Y-%m-%d %H:%M:%S %Z")
|
30 |
|
31 |
DFIF_TOKEN = os.getenv('HF_TOKEN')
|
32 |
-
|
33 |
-
|
34 |
|
|
|
|
|
|
|
35 |
|
|
|
36 |
|
37 |
|
38 |
-
#----------------------------------------------------------------------------------------------------------------------------------------------
|
39 |
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
seed = random.randint(0, 2**32 - 1)
|
58 |
-
stage_1_results, stage_1_param_path, stage_1_result_path = dfif.predict(prompt, "blur", seed, number_of_images, 7.0, 'smart100', 50, api_name="/generate64")
|
59 |
-
png_files = [f for f in os.listdir(stage_1_results) if f.endswith('.png')]
|
60 |
-
|
61 |
-
if png_files:
|
62 |
-
first_png = png_files[0]
|
63 |
-
second_png = png_files[1]
|
64 |
-
third_png = png_files[2]
|
65 |
-
fourth_png = png_files[3]
|
66 |
-
|
67 |
-
first_png_path = os.path.join(stage_1_results, first_png)
|
68 |
-
second_png_path = os.path.join(stage_1_results, second_png)
|
69 |
-
third_png_path = os.path.join(stage_1_results, third_png)
|
70 |
-
fourth_png_path = os.path.join(stage_1_results, fourth_png)
|
71 |
-
|
72 |
-
img1 = Image.open(first_png_path)
|
73 |
-
img2 = Image.open(second_png_path)
|
74 |
-
img3 = Image.open(third_png_path)
|
75 |
-
img4 = Image.open(fourth_png_path)
|
76 |
-
|
77 |
-
combined_image = Image.new('RGB', (img1.width * 2, img1.height * 2))
|
78 |
-
|
79 |
-
combined_image.paste(img1, (0, 0))
|
80 |
-
combined_image.paste(img2, (img1.width, 0))
|
81 |
-
combined_image.paste(img3, (0, img1.height))
|
82 |
-
combined_image.paste(img4, (img1.width, img1.height))
|
83 |
-
|
84 |
-
combined_image_path = os.path.join(stage_1_results, 'combined_image.png')
|
85 |
-
combined_image.save(combined_image_path)
|
86 |
|
|
|
|
|
|
|
|
|
|
|
87 |
|
88 |
-
|
|
|
|
|
|
|
89 |
|
|
|
|
|
|
|
|
|
90 |
|
91 |
-
|
|
|
|
|
|
|
|
|
|
|
92 |
|
|
|
|
|
93 |
|
|
|
|
|
94 |
|
95 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
96 |
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
101 |
|
102 |
def run_bot():
|
103 |
-
|
104 |
|
105 |
threading.Thread(target=run_bot).start()
|
106 |
|
|
|
7 |
import random
|
8 |
import time
|
9 |
import re
|
|
|
|
|
10 |
from discord import Embed, Color
|
11 |
from discord.ext import commands
|
12 |
# test
|
|
|
27 |
return dt.astimezone(tz).strftime("%Y-%m-%d %H:%M:%S %Z")
|
28 |
|
29 |
DFIF_TOKEN = os.getenv('HF_TOKEN')
|
30 |
+
df = Client("huggingface-projects/IF", DFIF_TOKEN)
|
31 |
+
sdlu = Client("huggingface-projects/stable-diffusion-latent-upscaler", DFIF_TOKEN)
|
32 |
|
33 |
+
DISCORD_TOKEN = os.environ.get("GRADIOTEST_TOKEN", None)
|
34 |
+
intents = discord.Intents.default()
|
35 |
+
intents.message_content = True
|
36 |
|
37 |
+
bot = commands.Bot(command_prefix='!', intents=intents)
|
38 |
|
39 |
|
40 |
+
#----------------------------------------------------------------------------------------------------------------------------------------------
|
41 |
|
42 |
+
@bot.event
|
43 |
+
async def on_ready():
|
44 |
+
print('Logged on as', bot.user)
|
45 |
+
bot.log_channel = bot.get_channel(1100458786826747945) # 1100458786826747945 = bot-test, 1107006391547342910 = lunarbot server
|
46 |
|
47 |
+
#----------------------------------------------------------------------------------------------------------------------------------------------
|
48 |
+
# Stage 1
|
49 |
+
@bot.command()
|
50 |
+
async def deepfloydif(ctx, *, prompt: str):
|
51 |
+
try:
|
52 |
+
prompt = prompt.strip()[:100] # Limit the prompt length to 100 characters
|
53 |
+
prompt = re.sub(r'[^\w\s]', '', prompt) # Remove special characters
|
54 |
+
|
55 |
+
def check_reaction(reaction, user):
|
56 |
+
return user == ctx.author and str(reaction.emoji) in ['1️⃣', '2️⃣', '3️⃣', '4️⃣']
|
57 |
+
|
58 |
+
await ctx.message.add_reaction('👍')
|
59 |
+
thread = await ctx.message.create_thread(name=f'{ctx.author} Image Upscaling Thread ')
|
60 |
+
# create thread -> send new message inside thread + combined_image -> add reactions -> dfif2
|
61 |
+
await thread.send(f'{ctx.author.mention}Generating images in thread, can take ~1 minute...')
|
62 |
|
63 |
+
number_of_images = 4
|
64 |
+
current_time = int(time.time())
|
65 |
+
random.seed(current_time)
|
66 |
+
seed = random.randint(0, 2**32 - 1)
|
67 |
+
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")
|
68 |
+
png_files = [f for f in os.listdir(stage_1_results) if f.endswith('.png')]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
|
70 |
+
if png_files:
|
71 |
+
first_png = png_files[0]
|
72 |
+
second_png = png_files[1]
|
73 |
+
third_png = png_files[2]
|
74 |
+
fourth_png = png_files[3]
|
75 |
|
76 |
+
first_png_path = os.path.join(stage_1_results, first_png)
|
77 |
+
second_png_path = os.path.join(stage_1_results, second_png)
|
78 |
+
third_png_path = os.path.join(stage_1_results, third_png)
|
79 |
+
fourth_png_path = os.path.join(stage_1_results, fourth_png)
|
80 |
|
81 |
+
img1 = Image.open(first_png_path)
|
82 |
+
img2 = Image.open(second_png_path)
|
83 |
+
img3 = Image.open(third_png_path)
|
84 |
+
img4 = Image.open(fourth_png_path)
|
85 |
|
86 |
+
combined_image = Image.new('RGB', (img1.width * 2, img1.height * 2))
|
87 |
+
|
88 |
+
combined_image.paste(img1, (0, 0))
|
89 |
+
combined_image.paste(img2, (img1.width, 0))
|
90 |
+
combined_image.paste(img3, (0, img1.height))
|
91 |
+
combined_image.paste(img4, (img1.width, img1.height))
|
92 |
|
93 |
+
combined_image_path = os.path.join(stage_1_results, 'combined_image.png')
|
94 |
+
combined_image.save(combined_image_path)
|
95 |
|
96 |
+
# Trigger the second stage prediction
|
97 |
+
#await dfif2(ctx, stage_1_result_path)
|
98 |
|
99 |
|
100 |
+
#1
|
101 |
+
#sent_message = None
|
102 |
+
|
103 |
+
#2
|
104 |
+
# create thread -> send new message inside thread + combined_image -> add reactions -> dfif2
|
105 |
+
#sent_message = await ctx.reply('Creating thread...')
|
106 |
+
|
107 |
+
#3
|
108 |
+
#ctx.message
|
109 |
+
# could replace sent_message with initial user command message? try later
|
110 |
+
#thread = await ctx.message.create_thread(name='Image Upscaling Thread')
|
111 |
+
|
112 |
+
|
113 |
+
#thread = await sent_message.create_thread(name='Image Upscaling Thread')
|
114 |
+
|
115 |
+
#4
|
116 |
+
with open(combined_image_path, 'rb') as f:
|
117 |
+
threadmsg = await thread.send(f'{ctx.author.mention}React with the image number you want to upscale!', file=discord.File(f, 'combined_image.png'))
|
118 |
+
|
119 |
+
# bot reacts with appropriate emojis to the post, both showing the user what options they have,
|
120 |
+
# as well as showing which post to react to.
|
121 |
+
for emoji in ['1️⃣', '2️⃣', '3️⃣', '4️⃣']:
|
122 |
+
await threadmsg.add_reaction(emoji)
|
123 |
+
|
124 |
+
reaction, user = await bot.wait_for('reaction_add', check=check_reaction)
|
125 |
+
if str(reaction.emoji) == '1️⃣':
|
126 |
+
await thread.send(f"{ctx.author.mention}Upscaling the first image...")
|
127 |
+
index = 0
|
128 |
+
await dfif2(ctx, index, stage_1_result_path, thread)
|
129 |
+
elif str(reaction.emoji) == '2️⃣':
|
130 |
+
await thread.send(f"{ctx.author.mention}Upscaling the second image...")
|
131 |
+
index = 1
|
132 |
+
await dfif2(ctx, index, stage_1_result_path, thread)
|
133 |
+
elif str(reaction.emoji) == '3️⃣':
|
134 |
+
await thread.send(f"{ctx.author.mention}Upscaling the third image...")
|
135 |
+
index = 2
|
136 |
+
await dfif2(ctx, index, stage_1_result_path, thread)
|
137 |
+
elif str(reaction.emoji) == '4️⃣':
|
138 |
+
await thread.send(f"{ctx.author.mention}Upscaling the fourth image...")
|
139 |
+
index = 3
|
140 |
+
await dfif2(ctx, index, stage_1_result_path, thread)
|
141 |
+
|
142 |
+
#deepfloydif try/except
|
143 |
+
except Exception as e:
|
144 |
+
print(f"Error: {e}")
|
145 |
+
await ctx.reply('An error occurred while processing your request. Please wait 5 seconds before retrying.')
|
146 |
+
await ctx.message.add_reaction('❌')
|
147 |
+
|
148 |
+
#----------------------------------------------------------------------------------------------------------------------------
|
149 |
+
# Stage 2
|
150 |
+
async def dfif2(ctx, index: int, stage_1_result_path, thread):
|
151 |
+
try:
|
152 |
+
selected_index_for_stage_2 = index
|
153 |
+
seed_2 = 0
|
154 |
+
guidance_scale_2 = 4
|
155 |
+
custom_timesteps_2 = 'smart50'
|
156 |
+
number_of_inference_steps_2 = 50
|
157 |
+
result_path = df.predict(stage_1_result_path, selected_index_for_stage_2, seed_2,
|
158 |
+
guidance_scale_2, custom_timesteps_2, number_of_inference_steps_2, api_name='/upscale256')
|
159 |
|
160 |
+
|
161 |
+
with open(result_path, 'rb') as f:
|
162 |
+
await thread.send(f'{ctx.author.mention}Here is the upscaled image! :) ', file=discord.File(f, 'result.png'))
|
163 |
+
#await ctx.reply('Here is the result of the second stage', file=discord.File(f, 'result.png'))
|
164 |
+
await ctx.message.add_reaction('✔️')
|
165 |
+
|
166 |
+
|
167 |
+
|
168 |
+
|
169 |
+
except Exception as e:
|
170 |
+
print(f"Error: {e}")
|
171 |
+
await ctx.reply('An error occurred while processing stage 2 upscaling. Please try again later.')
|
172 |
+
await ctx.message.add_reaction('❌')
|
173 |
+
#----------------------------------------------------------------------------------------------------------------------------
|
174 |
+
|
175 |
+
|
176 |
+
|
177 |
|
178 |
def run_bot():
|
179 |
+
bot.run(DISCORD_TOKEN)
|
180 |
|
181 |
threading.Thread(target=run_bot).start()
|
182 |
|