lunarflu HF Staff commited on
Commit
282fda9
·
1 Parent(s): 7b6968e

buttons class test

Browse files
Files changed (1) hide show
  1. app.py +59 -31
app.py CHANGED
@@ -37,6 +37,42 @@ df = Client("huggingface-projects/IF", DFIF_TOKEN)
37
  #stable diffusion upscaler
38
  sdlu = Client("huggingface-projects/stable-diffusion-latent-upscaler", DFIF_TOKEN)
39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
  # Set up discord bot
41
  class MyClient(discord.Client):
42
  async def on_ready(self):
@@ -141,37 +177,29 @@ class MyClient(discord.Client):
141
  combined_image_path = os.path.join(stage_1_results, 'combined_image.png')
142
  combined_image.save(combined_image_path)
143
 
144
- embed = discord.Embed(title="Combined Image")
145
- embed.set_image(url="attachment://combined_image.png")
146
-
147
- components = discord.ui.MessageComponents()
148
- components.add_action_row(
149
- Button(style=ButtonStyle.blue, label="Image 1", custom_id="image_1"),
150
- Button(style=ButtonStyle.blue, label="Image 2", custom_id="image_2"),
151
- Button(style=ButtonStyle.blue, label="Image 3", custom_id="image_3"),
152
- Button(style=ButtonStyle.blue, label="Image 4", custom_id="image_4"),
153
- )
154
-
155
- await message.channel.send(embed=embed, file=discord.File(combined_image_path, 'combined_image.png'), components=components)
156
-
157
-
158
- async def on_button_click(self, interaction):
159
- if interaction.custom_id.startswith("image_"):
160
- image_index = int(interaction.custom_id.split("_")[1])
161
- await interaction.respond(content=f"You clicked image {image_index}")
162
-
163
- #dfif stage 2
164
- selected_index_for_stage_2 = image_index - 1
165
- seed_2 = 0
166
- guidance_scale_2 = 4
167
- custom_timesteps_2 = 'smart50'
168
- number_of_inference_steps_2 = 50
169
- result_path = df.predict(stage_1_result_path, selected_index_for_stage_2, seed_2, guidance_scale_2, custom_timesteps_2, number_of_inference_steps_2, api_name='/upscale256')
170
-
171
- # Send the upscaled image in a new message
172
- upscale_image_path = os.path.join(result_path, "output.png")
173
- with open(upscale_image_path, 'rb') as f:
174
- await interaction.channel.send('Here is the upscaled image:', file=discord.File(f, 'upscaled_image.png'))
175
 
176
 
177
  DISCORD_TOKEN = os.environ.get("GRADIOTEST_TOKEN", None)
 
37
  #stable diffusion upscaler
38
  sdlu = Client("huggingface-projects/stable-diffusion-latent-upscaler", DFIF_TOKEN)
39
 
40
+ #---------------------------------------------------------------------------------------------------------------------------------------------------
41
+ class ButtonView(discord.ui.View):
42
+ def __init__(self, ctx, image_paths, stage_1_result_path):
43
+ super().__init__()
44
+ self.ctx = ctx
45
+ self.image_paths = image_paths
46
+ self.stage_1_result_path = stage_1_result_path
47
+
48
+ async def on_timeout(self):
49
+ for child in self.children:
50
+ child.disabled = True
51
+ self.stop()
52
+
53
+ @discord.ui.button(label='Image 1', style=discord.ButtonStyle.blurple)
54
+ async def image1_button(self, button: discord.ui.Button, interaction: discord.Interaction):
55
+ await self.ctx.invoke(self.ctx.bot.get_command('dfif2'), image_path=self.image_paths[0], stage_1_result_path=self.stage_1_result_path)
56
+ self.stop()
57
+
58
+ @discord.ui.button(label='Image 2', style=discord.ButtonStyle.blurple)
59
+ async def image2_button(self, button: discord.ui.Button, interaction: discord.Interaction):
60
+ await self.ctx.invoke(self.ctx.bot.get_command('dfif2'), image_path=self.image_paths[1], stage_1_result_path=self.stage_1_result_path)
61
+ self.stop()
62
+
63
+ @discord.ui.button(label='Image 3', style=discord.ButtonStyle.blurple)
64
+ async def image3_button(self, button: discord.ui.Button, interaction: discord.Interaction):
65
+ await self.ctx.invoke(self.ctx.bot.get_command('dfif2'), image_path=self.image_paths[2], stage_1_result_path=self.stage_1_result_path)
66
+ self.stop()
67
+
68
+ @discord.ui.button(label='Image 4', style=discord.ButtonStyle.blurple)
69
+ async def image4_button(self, button: discord.ui.Button, interaction: discord.Interaction):
70
+ await self.ctx.invoke(self.ctx.bot.get_command('dfif2'), image_path=self.image_paths[3], stage_1_result_path=self.stage_1_result_path)
71
+ self.stop()
72
+ #---------------------------------------------------------------------------------------------------------------------------------------------------
73
+
74
+
75
+ #---------------------------------------------------------------------------------------------------------------------------------------------------
76
  # Set up discord bot
77
  class MyClient(discord.Client):
78
  async def on_ready(self):
 
177
  combined_image_path = os.path.join(stage_1_results, 'combined_image.png')
178
  combined_image.save(combined_image_path)
179
 
180
+
181
+ # Send the combined image file as a discord attachment with the button view
182
+ with open(combined_image_path, 'rb') as f:
183
+ view = ButtonView(message, [first_png_path, second_png_path, third_png_path, fourth_png_path], stage_1_result_path)
184
+ await message.reply('Here is the combined image', file=discord.File(f, 'combined_image.png'), view=view)
185
+
186
+
187
+
188
+
189
+ #stage 2, can be stable diffusion too---------------------------------------------------------------------------------------------------------------------------------------------------
190
+ async def dfif2(self, ctx, image_path, stage_1_result_path):
191
+ selected_index_for_stage_2 = 0
192
+ seed_2 = 0
193
+ guidance_scale_2 = 4
194
+ custom_timesteps_2 = 'smart50'
195
+ number_of_inference_steps_2 = 50
196
+ result_path = df.predict(stage_1_result_path, selected_index_for_stage_2, seed_2, guidance_scale_2, custom_timesteps_2, number_of_inference_steps_2, api_name='/upscale256')
197
+
198
+ with open(result_path, 'rb') as f:
199
+ await ctx.reply('Here is the result of the second stage', file=discord.File(f, 'result.png'))
200
+
201
+ #---------------------------------------------------------------------------------------------------------------------------------------------------
202
+
 
 
 
 
 
 
 
 
203
 
204
 
205
  DISCORD_TOKEN = os.environ.get("GRADIOTEST_TOKEN", None)