Chandima Prabhath commited on
Commit
856d629
·
1 Parent(s): 59da9b5

Refactor image sending logic to send first three images without caption and the last image with a caption listing all URLs.

Browse files
Files changed (1) hide show
  1. app.py +17 -6
app.py CHANGED
@@ -191,20 +191,31 @@ def handle_image_generation(message_id, chat_id, prompt):
191
  send_message(message_id, chat_id, "Image generation failed.")
192
  return
193
  images_info.append((path, ret_prompt, url))
194
- # Create a caption listing the URLs of all generated images.
 
195
  caption = "✨ Images ready:\n" + "\n".join(url for _, _, url in images_info)
196
- # Extract the file paths from the generated images.
197
- image_paths = [path for path, _, _ in images_info]
198
- # Send all generated images concurrently.
199
- send_images(message_id, chat_id, image_paths, caption=caption)
 
 
 
 
 
 
 
 
 
200
  # Cleanup generated image files.
201
- for path in image_paths:
202
  if os.path.exists(path):
203
  os.remove(path)
204
  except Exception as e:
205
  logging.error("Error in handle_image_generation: %s", e)
206
  send_message(message_id, chat_id, "Error generating images.")
207
 
 
208
  # --- Startup Message ---
209
  def send_startup_message():
210
  if BOT_STATUS_CHAT:
 
191
  send_message(message_id, chat_id, "Image generation failed.")
192
  return
193
  images_info.append((path, ret_prompt, url))
194
+
195
+ # Build caption with URLs of all images.
196
  caption = "✨ Images ready:\n" + "\n".join(url for _, _, url in images_info)
197
+
198
+ # Send first 3 images without caption.
199
+ for path, _, _ in images_info[:3]:
200
+ result = send_image(message_id, chat_id, path, caption="")
201
+ if "error" in result:
202
+ send_message(message_id, chat_id, "Failed to send one of the images.")
203
+
204
+ # Send the last image with the caption.
205
+ last_image_path = images_info[3][0]
206
+ result = send_image(message_id, chat_id, last_image_path, caption=caption)
207
+ if "error" in result:
208
+ send_message(message_id, chat_id, "Failed to send the final image with caption.")
209
+
210
  # Cleanup generated image files.
211
+ for path, _, _ in images_info:
212
  if os.path.exists(path):
213
  os.remove(path)
214
  except Exception as e:
215
  logging.error("Error in handle_image_generation: %s", e)
216
  send_message(message_id, chat_id, "Error generating images.")
217
 
218
+
219
  # --- Startup Message ---
220
  def send_startup_message():
221
  if BOT_STATUS_CHAT: