artintel235 commited on
Commit
d3c6432
·
verified ·
1 Parent(s): 2f46363

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -13
app.py CHANGED
@@ -5,6 +5,7 @@ import requests
5
  import asyncio
6
  import aiohttp # Use aiohttp for asynchronous HTTP requests
7
  import gradio as gr # Import Gradio
 
8
 
9
  # --- Environment Variables & Setup ---
10
  DISCORD_BOT_TOKEN = os.getenv("DISCORD_BOT_TOKEN")
@@ -84,21 +85,29 @@ async def generate_command(
84
  try:
85
  await interaction.response.defer()
86
 
 
87
  image_url_or_error = await generate_image_async(prompt, aspect_ratio.value)
88
- # await interaction.response.send_message(f"Error:{image_url_or_error}")
89
- if image_url_or_error:
90
- # if image_url_or_error.startswith("http"):
91
- await interaction.followup.send(
92
- # f"Here's your generated image based on the prompt '{prompt}' with aspect ratio {aspect_ratio.name}:\n{image_url_or_error}"
93
- f"{image_url_or_error}"
94
- )
95
- else:
96
- await interaction.followup.send(
97
- f"Sorry, I couldn't generate an image"
98
- )
99
- except Exception as e:
100
- await interaction.followup.send(f"Error")
101
 
 
 
 
 
 
 
 
 
102
 
103
  @tree.command(name="hello", description="Says hello!")
104
  async def hello_command(interaction):
 
5
  import asyncio
6
  import aiohttp # Use aiohttp for asynchronous HTTP requests
7
  import gradio as gr # Import Gradio
8
+ from io import BytesIO
9
 
10
  # --- Environment Variables & Setup ---
11
  DISCORD_BOT_TOKEN = os.getenv("DISCORD_BOT_TOKEN")
 
85
  try:
86
  await interaction.response.defer()
87
 
88
+ # Fetch the URL of the image
89
  image_url_or_error = await generate_image_async(prompt, aspect_ratio.value)
90
+ if not image_url_or_error.startswith("http"):
91
+ await interaction.followup.send(f"Sorry, I couldn't generate an image: {image_url_or_error}")
92
+ return
93
+
94
+ # Download the image
95
+ async with aiohttp.ClientSession() as session:
96
+ async with session.get(image_url_or_error) as image_response:
97
+ image_response.raise_for_status()
98
+ image_data = await image_response.read()
99
+
100
+ # Prepare the file-like object
101
+ file = discord.File(BytesIO(image_data), filename="generated_image.png")
 
102
 
103
+ # Send the image as an attachment
104
+ await interaction.followup.send(
105
+ f"Here's your generated image based on the prompt '{prompt}' with aspect ratio {aspect_ratio.name}:",
106
+ file=file
107
+ )
108
+
109
+ except Exception as e:
110
+ await interaction.followup.send(f"Error: {str(e)}")
111
 
112
  @tree.command(name="hello", description="Says hello!")
113
  async def hello_command(interaction):