From 2e93ef7b3a5c375f7243b36855e3ccb59517bc41 Mon Sep 17 00:00:00 2001 From: Hendrik Langer Date: Thu, 30 Mar 2023 02:23:20 +0200 Subject: [PATCH] events --- bot.conf2 | 6 ++++ matrix_pygmalion_bot/core.py | 53 +++++++++++++++++++++++++++++---- matrix_pygmalion_bot/helpers.py | 22 +++++++++----- 3 files changed, 68 insertions(+), 13 deletions(-) create mode 100644 bot.conf2 diff --git a/bot.conf2 b/bot.conf2 new file mode 100644 index 0000000..c2e3359 --- /dev/null +++ b/bot.conf2 @@ -0,0 +1,6 @@ +[Julia] +timestamp = 1680134005.6927342 + +[Hendrik] +timestamp = 1680131376.732477 + diff --git a/matrix_pygmalion_bot/core.py b/matrix_pygmalion_bot/core.py index ff420bb..bea3aa1 100644 --- a/matrix_pygmalion_bot/core.py +++ b/matrix_pygmalion_bot/core.py @@ -13,7 +13,7 @@ import magic from PIL import Image import re -from .helpers import ChatItem +from .helpers import ChatItem, Event ai = importlib.import_module("matrix_pygmalion_bot.ai.runpod_pygmalion") #from .llama_cpp import generate, get_full_prompt, get_full_prompt_chat_style #from .runpod_pygmalion import generate_sync, get_full_prompt @@ -69,11 +69,14 @@ class Callbacks(object): if is_own_message: return - if hasattr(event, 'body') and event.body.startswith('!replybot'): + if not hasattr(event, 'body'): + return + + if event.body.startswith('!replybot'): print(event) await self.bot.send_message(self.client, room.room_id, "Hello World!") return - elif hasattr(event, 'body') and event.body.startswith('!image'): + elif event.body.startswith('!image'): prompt = event.body.removeprefix('!image').strip() negative_prompt = "out of frame, (ugly:1.3), (fused fingers), (too many fingers), (bad anatomy:1.5), (watermark:1.5), (words), letters, untracked eyes, asymmetric eyes, floating head, (logo:1.5), (bad hands:1.3), (mangled hands:1.2), (missing hands), (missing arms), backward hands, floating jewelry, unattached jewelry, floating head, doubled head, unattached head, doubled head, head in body, (misshapen body:1.1), (badly fitted headwear:1.2), floating arms, (too many arms:1.5), limbs fused with body, (facial blemish:1.5), badly fitted clothes, imperfect eyes, untracked eyes, crossed eyes, hair growing from clothes, partial faces, hair not attached to head" if len(prompt) == 0: @@ -82,8 +85,10 @@ class Callbacks(object): for imagefile in output: await self.bot.send_image(self.client, room.room_id, imagefile) return - elif hasattr(event, 'body') and event.body.startswith('!begin'): + elif event.body.startswith('!begin'): self.bot.chat_history = {} + self.bot.timestamp = time.time() + await self.bot.write_conf2(self.bot.name) await self.bot.send_message(self.client, room.room_id, self.bot.greeting) return elif event.body.startswith('!!!'): @@ -109,6 +114,11 @@ class Callbacks(object): translated_message = message # don't return, we generate a new answer + # Other commands + if re.search("^(?=.*\bsend\b)(?=.*\bpicture\b).*$", message): + # send, mail, drop, snap picture, photo, image, portrait + pass + full_prompt = await ai.get_full_prompt(translated_message, self.bot) num_tokens = await ai.num_tokens(full_prompt) logger.info(full_prompt) @@ -164,6 +174,7 @@ class ChatBot(object): self.callbacks = None self.config = None self.not_synced = True + self.timestamp = time.time() self.owner = None self.translate = None @@ -187,9 +198,17 @@ class ChatBot(object): async def event_loop(self): while True: await asyncio.sleep(60) - print(time.time()) + #print(time.time()) for event in self.events: - event.loop() + event.loop(self) + + async def add_event(self, event_string): + items = event_string.split(',', 3) + for item in items: + item = item.strip() + event = Event(float(items[0]), float(items[1]), float(items[2]), items[3]) + self.events.append(event) + pass async def login(self): self.config = AsyncClientConfig(store_sync_tokens=True) @@ -218,6 +237,22 @@ class ChatBot(object): print("Client is synced") self.not_synced = False + async def read_conf2(self, section): + config2 = configparser.ConfigParser() + config2.read('bot.conf2') + if config2.has_section(section) and config2.has_option(section, 'timestamp'): + self.timestamp = float(config2[section]['timestamp']) + else: + self.timestamp = time.time() + + async def write_conf2(self, section): + config2 = configparser.ConfigParser() + config2.read('bot.conf2') + config2[section] = {} + config2[section]['timestamp'] = str(self.timestamp) + with open('bot.conf2', 'w') as configfile: + config2.write(configfile) + async def send_message(self, client, room_id, message, reply_to=None, original_message=None): content={"msgtype": "m.text", "body": message} if reply_to: @@ -297,8 +332,14 @@ async def main() -> None: translate.init("en", bot.translate) if config.has_option(section, 'image_prompt'): bot.image_prompt = config[section]['image_prompt'] + if config.has_option(section, 'events'): + events = config[section]['events'].strip().split('\n') + for event in events: + await bot.add_event(event) if config.has_option('DEFAULT', 'runpod_api_key'): bot.runpod_api_key = config['DEFAULT']['runpod_api_key'] + await bot.read_conf2(section) + await bot.write_conf2(section) bots.append(bot) await bot.login() print("gather") diff --git a/matrix_pygmalion_bot/helpers.py b/matrix_pygmalion_bot/helpers.py index d84bfa7..bf27704 100644 --- a/matrix_pygmalion_bot/helpers.py +++ b/matrix_pygmalion_bot/helpers.py @@ -15,13 +15,21 @@ class ChatItem: return str("{}: {}".format(self.user_name, self.message)) class Event: - def __init__(self, timestamp_start, timestamp_stop=None, chance=1): - self.timestamp_start = timestamp_start - self.timestamp_stop = timestamp_stop + def __init__(self, time_start, time_stop, chance, command): + self.time_start = time_start + self.time_stop = time_stop self.chance = chance + self.command = command self.executed = 0 def __str__(self): - return str("Event starting at timestamp {}".format(self.timestamp_start)) - def loop(self): - if timestamp_start > time.time(): - pass + return str("Event starting at timestamp {}".format(self.time_start)) + def loop(self, bot): + if time.time() - bot.timestamp >= self.time_start: + if time.time() - bot.timestamp < self.time_stop: + self.execute() + elif (self.time_stop == 0 or self.time_stop == self.time_start) and self.executed == 0: + self.execute() + def execute(self, bot): + if self.command.startswith('printtime'): + print(time.time()) + self.executed += 1