artintel235 commited on
Commit
7916fee
·
verified ·
1 Parent(s): e085e45

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -6
app.py CHANGED
@@ -70,15 +70,22 @@ async def generate_command(
70
  image_url_or_error = await generate_image_async(prompt, aspect_ratio.value)
71
 
72
  if image_url_or_error.startswith("http"):
 
 
 
 
 
 
 
73
  # Format the prompt for display with expand/collapse
74
  formatted_prompt, initial_content, full_content = format_prompt_for_discord(prompt)
75
 
76
  # Create buttons
77
  async def show_more_callback(interaction):
78
- await interaction.response.edit_message(content=f"{full_content}\n{image_url_or_error}", view=view)
79
 
80
  async def show_less_callback(interaction):
81
- await interaction.response.edit_message(content=f"{initial_content}\n{image_url_or_error}", view=view)
82
 
83
  show_more_button = Button(label="Read More", style=discord.ButtonStyle.primary)
84
  show_more_button.callback = show_more_callback
@@ -90,10 +97,14 @@ async def generate_command(
90
  view.add_item(show_more_button)
91
  view.add_item(show_less_button)
92
 
93
- await interaction.followup.send(
94
- f"{initial_content}\n{image_url_or_error}", # Include image URL initially
95
- view=view
96
- )
 
 
 
 
97
  else:
98
  await interaction.followup.send(
99
  f"Sorry, I couldn't generate an image. {image_url_or_error}"
 
70
  image_url_or_error = await generate_image_async(prompt, aspect_ratio.value)
71
 
72
  if image_url_or_error.startswith("http"):
73
+ # Download the image
74
+ async with aiohttp.ClientSession() as session:
75
+ async with session.get(image_url_or_error) as resp:
76
+ if resp.status != 200:
77
+ return await interaction.followup.send('Could not download file...')
78
+ image_data = await resp.read()
79
+
80
  # Format the prompt for display with expand/collapse
81
  formatted_prompt, initial_content, full_content = format_prompt_for_discord(prompt)
82
 
83
  # Create buttons
84
  async def show_more_callback(interaction):
85
+ await interaction.response.edit_message(content=full_content, view=view)
86
 
87
  async def show_less_callback(interaction):
88
+ await interaction.response.edit_message(content=initial_content, view=view)
89
 
90
  show_more_button = Button(label="Read More", style=discord.ButtonStyle.primary)
91
  show_more_button.callback = show_more_callback
 
97
  view.add_item(show_more_button)
98
  view.add_item(show_less_button)
99
 
100
+ # Send the image as an attachment
101
+ with io.BytesIO(image_data) as image_file:
102
+ await interaction.followup.send(
103
+ content=initial_content,
104
+ file=discord.File(image_file, 'generated_image.png'),
105
+ view=view
106
+ )
107
+
108
  else:
109
  await interaction.followup.send(
110
  f"Sorry, I couldn't generate an image. {image_url_or_error}"