lunarflu HF Staff commited on
Commit
b3352a3
·
1 Parent(s): 37509a8

simplifying, testing where errors come from

Browse files
Files changed (1) hide show
  1. app.py +54 -132
app.py CHANGED
@@ -27,156 +27,78 @@ def convert_to_timezone(dt, tz):
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
 
 
27
  return dt.astimezone(tz).strftime("%Y-%m-%d %H:%M:%S %Z")
28
 
29
  DFIF_TOKEN = os.getenv('HF_TOKEN')
30
+ dfif = Client("huggingface-projects/IF", DFIF_TOKEN)
31
+ #sdlu = Client("huggingface-projects/stable-diffusion-latent-upscaler", DFIF_TOKEN)
32
 
 
 
 
 
 
33
 
34
 
 
 
 
 
 
 
35
 
36
  #----------------------------------------------------------------------------------------------------------------------------------------------
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
 
38
+ # Set up discord bot
39
+ class MyClient(discord.Client):
40
+ async def on_ready(self):
41
+ print('Logged on as', self.user)
42
 
43
+ #------------------------------------------
44
+ async def on_message(self, message):
45
+ ctx = await self.get_context(message)
46
+
47
+ if message.content.startswith('!dfif'): #ex: !dfif sleepy cat
48
+ prompt = string[5:] #ex: sleepy cat
49
+ await dfif (ctx, prompt) # pass prompt to dfif (for generation) and ctx (for keeping track of command msg)
50
+
51
+
52
+ number_of_images = 4
53
+ current_time = int(time.time())
54
+ random.seed(current_time)
55
+ seed = random.randint(0, 2**32 - 1)
56
+ 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")
57
+ png_files = [f for f in os.listdir(stage_1_results) if f.endswith('.png')]
58
+
59
+ if png_files:
60
+ first_png = png_files[0]
61
+ second_png = png_files[1]
62
+ third_png = png_files[2]
63
+ fourth_png = png_files[3]
64
+
65
+ first_png_path = os.path.join(stage_1_results, first_png)
66
+ second_png_path = os.path.join(stage_1_results, second_png)
67
+ third_png_path = os.path.join(stage_1_results, third_png)
68
+ fourth_png_path = os.path.join(stage_1_results, fourth_png)
69
+
70
+ img1 = Image.open(first_png_path)
71
+ img2 = Image.open(second_png_path)
72
+ img3 = Image.open(third_png_path)
73
+ img4 = Image.open(fourth_png_path)
74
+
75
+ combined_image = Image.new('RGB', (img1.width * 2, img1.height * 2))
76
+
77
+ combined_image.paste(img1, (0, 0))
78
+ combined_image.paste(img2, (img1.width, 0))
79
+ combined_image.paste(img3, (0, img1.height))
80
+ combined_image.paste(img4, (img1.width, img1.height))
81
+
82
+ combined_image_path = os.path.join(stage_1_results, 'combined_image.png')
83
+ combined_image.save(combined_image_path)
84
 
 
 
 
 
85
 
86
+ sent_message = await message.reply('Here is the combined image. React with the image number you want to upscale!', file=discord.File(f, 'combined_image.png'))
 
87
 
 
 
88
 
89
+ #----------------------------------------------------------------------------------------------------------------------------------------------
90
 
 
 
91
 
 
 
 
92
 
 
 
 
 
93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94
 
95
+ DISCORD_TOKEN = os.environ.get("GRADIOTEST_TOKEN", None)
96
+ intents = discord.Intents.default()
97
+ intents.message_content = True
98
+ client = MyClient(intents=intents)
 
 
 
 
 
 
 
 
 
 
 
 
 
99
 
100
  def run_bot():
101
+ client.run(DISCORD_TOKEN)
102
 
103
  threading.Thread(target=run_bot).start()
104