SD plugin update image info

This commit is contained in:
Hash Borgir 2024-03-03 13:17:01 -07:00
parent c4b6c750fb
commit 4ef6f3beb7
2 changed files with 18 additions and 2 deletions

View File

@ -8,6 +8,11 @@ from asyncio import Queue
import argparse import argparse
import simplematrixbotlib as botlib import simplematrixbotlib as botlib
import markdown2 import markdown2
from slugify import slugify
def slugify_prompt(prompt):
# Generate a slug from the prompt
return slugify(prompt)
# Queue to store pending commands # Queue to store pending commands
command_queue = Queue() command_queue = Queue()
@ -58,9 +63,18 @@ async def handle_command(room, message, bot, prefix, config):
response = requests.post(url=url, json=payload) response = requests.post(url=url, json=payload)
r = response.json() r = response.json()
with open("/tmp/output.jpg", 'wb') as f:
# Slugify the prompt
prompt_slug = (prompt)
# Construct filename
filename = f"{prompt_slug}_{args.steps}_{args.h}x{args.w}_{sampler_name}_{args.cfg}.jpg"
with open(f"/tmp/{filename}", 'wb') as f:
f.write(base64.b64decode(r['images'][0])) f.write(base64.b64decode(r['images'][0]))
await bot.api.send_image_message(room_id=room.room_id, image_filepath="/tmp/output.jpg") # Corrected argument name await bot.api.send_image_message(room_id=room.room_id, image_filepath=f"/tmp/{filename}") # Corrected argument name
neg = neg.replace(" ", "")
info_msg = f"""<details><summary>🍄⤵Image Info:⤵︎</summary><strong>Prompt:</strong> {prompt_slug}<br><strong>Steps:</strong> {args.steps}<br><strong>Dimensions:</strong> {args.h}x{args.w}<br><strong>Sampler:</strong> {sampler_name}<br><strong>CFG Scale:</strong> {args.cfg}<br><strong>Negative Prompt: {neg}</strong></details>"""
await bot.api.send_markdown_message(room.room_id, info_msg)
except argparse.ArgumentError as e: except argparse.ArgumentError as e:
await bot.api.send_text_message(room.room_id, f"Error: {e}") await bot.api.send_text_message(room.room_id, f"Error: {e}")
await bot.api.send_markdown_message(room.room_id, "<details><summary>Stable Diffusion Help</summary>" + print_help() + "</details>") await bot.api.send_markdown_message(room.room_id, "<details><summary>Stable Diffusion Help</summary>" + print_help() + "</details>")
@ -71,6 +85,7 @@ async def handle_command(room, message, bot, prefix, config):
next_command = await command_queue.get() next_command = await command_queue.get()
await handle_command(*next_command) await handle_command(*next_command)
def print_help(): def print_help():
return """ return """
<p>Generate images using self-hosted Stable Diffusion</p> <p>Generate images using self-hosted Stable Diffusion</p>

View File

@ -6,3 +6,4 @@ nio
markdown2 markdown2
watchdog watchdog
emoji emoji
python-slugify