Spaces:
Sleeping
Sleeping
Rifat Azad commited on
Commit ·
21b9666
1
Parent(s): 137f80d
update
Browse files- pydvpl_bot.py +15 -15
pydvpl_bot.py
CHANGED
|
@@ -579,6 +579,9 @@ async def chat(ctx, model, *, message):
|
|
| 579 |
# FUNCTION ------------------------------------------------------ SPLIT
|
| 580 |
|
| 581 |
|
|
|
|
|
|
|
|
|
|
| 582 |
@bot.hybrid_command(help="Generate images with 'craiyon' ai.")
|
| 583 |
async def image(ctx, *, prompt: str):
|
| 584 |
# Check if the user is authorized to use the command
|
|
@@ -590,24 +593,21 @@ async def image(ctx, *, prompt: str):
|
|
| 590 |
await ctx.send(f"Generating images for prompt: `{prompt}`. Please be patient...")
|
| 591 |
|
| 592 |
try:
|
| 593 |
-
|
| 594 |
-
|
| 595 |
-
async with aiohttp.ClientSession(headers=headers) as session:
|
| 596 |
-
generator = Craiyon(session=session)
|
| 597 |
-
generated_images = await generator.async_generate(prompt)
|
| 598 |
|
| 599 |
-
|
| 600 |
-
|
| 601 |
|
| 602 |
-
|
| 603 |
-
|
| 604 |
-
|
| 605 |
-
|
| 606 |
-
|
| 607 |
-
|
| 608 |
|
| 609 |
-
|
| 610 |
-
|
| 611 |
|
| 612 |
except Exception as e:
|
| 613 |
# Print the specific exception message for debugging
|
|
|
|
| 579 |
# FUNCTION ------------------------------------------------------ SPLIT
|
| 580 |
|
| 581 |
|
| 582 |
+
# Create a global aiohttp.ClientSession object
|
| 583 |
+
session = aiohttp.ClientSession()
|
| 584 |
+
|
| 585 |
@bot.hybrid_command(help="Generate images with 'craiyon' ai.")
|
| 586 |
async def image(ctx, *, prompt: str):
|
| 587 |
# Check if the user is authorized to use the command
|
|
|
|
| 593 |
await ctx.send(f"Generating images for prompt: `{prompt}`. Please be patient...")
|
| 594 |
|
| 595 |
try:
|
| 596 |
+
generator = Craiyon() # Instantiate Craiyon without passing a session
|
| 597 |
+
generated_images = await generator.async_generate(prompt, session=session) # Pass the session object here
|
|
|
|
|
|
|
|
|
|
| 598 |
|
| 599 |
+
# Encode images to base64
|
| 600 |
+
b64_list = await craiyon_utils.async_encode_base64(generated_images.images)
|
| 601 |
|
| 602 |
+
# Prepare images for sending
|
| 603 |
+
images = []
|
| 604 |
+
for index, image_b64 in enumerate(b64_list):
|
| 605 |
+
img_bytes = BytesIO(base64.b64decode(image_b64))
|
| 606 |
+
image_file = discord.File(img_bytes, filename=f"result{index}.webp")
|
| 607 |
+
images.append(image_file)
|
| 608 |
|
| 609 |
+
# Send images to the user
|
| 610 |
+
await ctx.reply(files=images)
|
| 611 |
|
| 612 |
except Exception as e:
|
| 613 |
# Print the specific exception message for debugging
|