Various plugin updates. New plugin for stable diffusion

This commit is contained in:
2024-02-29 20:18:58 -07:00
parent 5962eb53ad
commit 542bb5d5cd
8 changed files with 59 additions and 3 deletions

View File

@@ -0,0 +1,42 @@
"""
This plugin provides a command to generate images using self hosted Stable Diffusion and send to the room
"""
# plugins/stable-diffusion.py
import requests
import base64
from asyncio import Queue
import simplematrixbotlib as botlib
# Queue to store pending commands
command_queue = Queue()
async def process_command(room, message, bot, prefix, config):
match = botlib.MessageMatch(room, message, bot, prefix)
if match.prefix() and match.command("sd"):
if command_queue.empty():
await handle_command(room, message, bot, prefix, config)
else:
await command_queue.put((room, message, bot, prefix, config))
async def handle_command(room, message, bot, prefix, config):
match = botlib.MessageMatch(room, message, bot, prefix)
if match.prefix() and match.command("sd"):
prompt = message.body[len(prefix) + len("sd"):].strip() # Extract prompt from message body
payload = {
"prompt": prompt,
"steps": 32
}
url = "http://127.0.0.1:7860/sdapi/v1/txt2img"
try:
response = requests.post(url=url, json=payload)
r = response.json()
with open("/tmp/output.png", 'wb') as f:
f.write(base64.b64decode(r['images'][0]))
await bot.api.send_image_message(room_id=room.room_id, image_filepath="/tmp/output.png") # Corrected argument name
except Exception as e:
await bot.api.send_text_message(room.room_id, f"Error processing the command: {str(e)}")
finally:
if not command_queue.empty():
next_command = await command_queue.get()
await handle_command(*next_command)