Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
)
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
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):
|