lunarflu HF Staff commited on
Commit
80b1281
Β·
1 Parent(s): 9a2d11e

testing some magic

Browse files
Files changed (1) hide show
  1. app.py +32 -13
app.py CHANGED
@@ -61,6 +61,8 @@ async def deepfloydif(ctx, *, prompt: str):
61
  guidance_scale = 7
62
  custom_timesteps_1 = 'smart50'
63
  number_of_inference_steps = 50
 
 
64
 
65
  except Exception as e:
66
  print(f"Error: {e}")
@@ -71,13 +73,15 @@ async def deepfloydif(ctx, *, prompt: str):
71
  try:
72
  stage_1_results, stage_1_param_path, stage_1_result_path = df.predict(
73
  prompt, negative_prompt, seed, number_of_images, guidance_scale, custom_timesteps_1, number_of_inference_steps, api_name='/generate64')
 
 
74
  except Exception as e:
75
  print(f"Error: {e}")
76
  await ctx.reply('stage 1 error -> during generation')
77
  await ctx.message.add_reaction('❌')
78
 
79
  try:
80
- await thread.send(f'{ctx.author.mention}Generating images in thread, can take ~1 minute...')
81
 
82
  png_files = [f for f in os.listdir(stage_1_results) if f.endswith('.png')]
83
 
@@ -85,16 +89,16 @@ async def deepfloydif(ctx, *, prompt: str):
85
  for i, png_file in enumerate(png_files):
86
  png_file_path = os.path.join(stage_1_results, png_file)
87
  img = Image.open(png_file_path)
88
- image_path = os.path.join(stage_1_results, f'image{i+1}.png')
89
  img.save(image_path)
90
  with open(image_path, 'rb') as f:
91
- await thread.send(f'{ctx.author.mention}Image {i+1}', file=discord.File(f, f'image{i+1}.png'))
92
- await asyncio.sleep(1) # Add a delay between posting each image
93
 
94
  await thread.send(f'{ctx.author.mention}React with πŸ‘ to the image you want to upscale!')
95
  except Exception as e:
96
  print(f"Error: {e}")
97
- await ctx.reply('stage 1 error -> threads')
98
  await ctx.message.add_reaction('❌')
99
 
100
 
@@ -107,7 +111,7 @@ async def deepfloydif(ctx, *, prompt: str):
107
 
108
  #----------------------------------------------------------------------------------------------------------------------------
109
  # Stage 2
110
- async def dfif2(index: int, stage_1_result_path, message):
111
  try:
112
  selected_index_for_stage_2 = index
113
  seed_2 = 0
@@ -119,7 +123,7 @@ async def dfif2(index: int, stage_1_result_path, message):
119
 
120
 
121
  with open(result_path, 'rb') as f:
122
- await message.reply(f'Here is the upscaled image! :) ', file=discord.File(f, 'result.png'))
123
  #await ctx.reply('Here is the result of the second stage', file=discord.File(f, 'result.png'))
124
  #await ctx.message.add_reaction('βœ…') need to fix this
125
 
@@ -148,17 +152,32 @@ async def on_reaction_add(reaction, user):
148
  if required_role not in user.roles:
149
  return
150
 
151
- attachment = reaction.message.attachments[0]
152
  emoji = reaction.emoji
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
153
 
154
  if emoji == 'πŸ‘':
155
  if reaction.message.attachments:
156
  if user.id == reaction.message.mentions[0].id: # all we care about is upscaling whatever image this is
157
- stage_1_result_path = "/tmp/s1rp"
158
- os.makedirs(stage_1_result_path, exist_ok=True)
159
- attachment_path = os.path.join(stage_1_result_path, attachment.filename)
160
- await attachment.save(attachment_path)
161
- await dfif2(0, attachment_path, message=reaction.message)
162
 
163
 
164
 
 
61
  guidance_scale = 7
62
  custom_timesteps_1 = 'smart50'
63
  number_of_inference_steps = 50
64
+
65
+ await thread.send(f'{ctx.author.mention}Generating images in thread, can take ~1 minute...')
66
 
67
  except Exception as e:
68
  print(f"Error: {e}")
 
73
  try:
74
  stage_1_results, stage_1_param_path, stage_1_result_path = df.predict(
75
  prompt, negative_prompt, seed, number_of_images, guidance_scale, custom_timesteps_1, number_of_inference_steps, api_name='/generate64')
76
+
77
+ partialpath = stage_1_result_path[5:] #magic for later
78
  except Exception as e:
79
  print(f"Error: {e}")
80
  await ctx.reply('stage 1 error -> during generation')
81
  await ctx.message.add_reaction('❌')
82
 
83
  try:
84
+
85
 
86
  png_files = [f for f in os.listdir(stage_1_results) if f.endswith('.png')]
87
 
 
89
  for i, png_file in enumerate(png_files):
90
  png_file_path = os.path.join(stage_1_results, png_file)
91
  img = Image.open(png_file_path)
92
+ image_path = os.path.join(stage_1_results, f'{i+1}{partialpath}.png')
93
  img.save(image_path)
94
  with open(image_path, 'rb') as f:
95
+ await thread.send(f'{ctx.author.mention}Image {i+1}', file=discord.File(f, f'{i+1}{partialpath}.png'))
96
+ await asyncio.sleep(1)
97
 
98
  await thread.send(f'{ctx.author.mention}React with πŸ‘ to the image you want to upscale!')
99
  except Exception as e:
100
  print(f"Error: {e}")
101
+ await ctx.reply('stage 1 error -> posting images in thread')
102
  await ctx.message.add_reaction('❌')
103
 
104
 
 
111
 
112
  #----------------------------------------------------------------------------------------------------------------------------
113
  # Stage 2
114
+ async def dfif2(index: int, stage_1_result_path, thread):
115
  try:
116
  selected_index_for_stage_2 = index
117
  seed_2 = 0
 
123
 
124
 
125
  with open(result_path, 'rb') as f:
126
+ await thread.send(f'Here is the upscaled image! :) ', file=discord.File(f, 'result.png'))
127
  #await ctx.reply('Here is the result of the second stage', file=discord.File(f, 'result.png'))
128
  #await ctx.message.add_reaction('βœ…') need to fix this
129
 
 
152
  if required_role not in user.roles:
153
  return
154
 
 
155
  emoji = reaction.emoji
156
+
157
+ thread = reaction.message.channel
158
+ tbd = reaction.message.content
159
+ #index = 0 decided by index in front of partialpath
160
+
161
+
162
+ # magic begins
163
+ attachment = reaction.message.attachments[0]
164
+ image_name = attachment.filename
165
+ # we know image_name will be something like 1tmpgtv4qjix.png
166
+ # remove .png first
167
+ indexpartialpath = image_name[:-4] # should be 1tmpgtv4qjix
168
+ # extract index as an integer (dfif2 needs integer)
169
+ index = int(indexpartialpath[0]) # should be 1
170
+ # extract partialpath
171
+ partialpath = indexpartialpath[1:] # should be tmpgtv4qjix
172
+ # add /tmp/ to partialpath, save as new variable
173
+ fullpath = "/tmp/" + partialpath # should be /tmp/tmpgtv4qjix
174
 
175
  if emoji == 'πŸ‘':
176
  if reaction.message.attachments:
177
  if user.id == reaction.message.mentions[0].id: # all we care about is upscaling whatever image this is
178
+ stage_1_result_path = fullpath
179
+ index = index
180
+ await dfif2(index, stage_1_result_path, thread)
 
 
181
 
182
 
183