From 609c741ae9ba047d97a06d92e0c05188568b1457 Mon Sep 17 00:00:00 2001 From: Konstantine Tsafatinos Date: Wed, 19 Jul 2023 00:58:55 -0400 Subject: [PATCH] get db, config, help, cool, and txt2img working --- skynet/cli.py | 2 +- skynet/frontend/discord/__init__.py | 47 ++++++------ skynet/frontend/discord/handlers.py | 110 ++++++++++++++++++++++------ 3 files changed, 112 insertions(+), 47 deletions(-) diff --git a/skynet/cli.py b/skynet/cli.py index 34c4562..ce4c351 100755 --- a/skynet/cli.py +++ b/skynet/cli.py @@ -485,7 +485,7 @@ def discord( permission, node_url, hyperion_url, - # db_host, db_user, db_pass, + db_host, db_user, db_pass, remote_ipfs_node=ipfs_url, key=key ) diff --git a/skynet/frontend/discord/__init__.py b/skynet/frontend/discord/__init__.py index 1c0df56..445eade 100644 --- a/skynet/frontend/discord/__init__.py +++ b/skynet/frontend/discord/__init__.py @@ -40,9 +40,9 @@ class SkynetDiscordFrontend: permission: str, node_url: str, hyperion_url: str, - # db_host: str, - # db_user: str, - # db_pass: str, + db_host: str, + db_user: str, + db_pass: str, remote_ipfs_node: str, key: str ): @@ -51,9 +51,9 @@ class SkynetDiscordFrontend: self.permission = permission self.node_url = node_url self.hyperion_url = hyperion_url - # self.db_host = db_host - # self.db_user = db_user - # self.db_pass = db_pass + self.db_host = db_host + self.db_user = db_user + self.db_pass = db_pass self.remote_ipfs_node = remote_ipfs_node self.key = key @@ -72,9 +72,9 @@ class SkynetDiscordFrontend: logging.info( f'connected to remote ipfs node: {self.remote_ipfs_node}') - # self.db_call = await self._async_exit_stack.enter_async_context( - # open_database_connection( - # self.db_user, self.db_pass, self.db_host)) + self.db_call = await self._async_exit_stack.enter_async_context( + open_database_connection( + self.db_user, self.db_pass, self.db_host)) create_handler_context(self) @@ -143,10 +143,10 @@ class SkynetDiscordFrontend: # f'[{timestamp_pretty()}] broadcasting transaction to chain...', # parse_mode='HTML' # ) - message = await ctx.send( - f'processing a \'{method}\' request by {user}\n \ - [{timestamp_pretty()}] *broadcasting transaction to chain...*' - ) + # message = await ctx.send( + # f'processing a \'{method}\' request by {user}\n \ + # [{timestamp_pretty()}] *broadcasting transaction to chain...*' + # ) reward = '20.0000 GPU' res = await self.cleos.a_push_action( @@ -183,12 +183,11 @@ class SkynetDiscordFrontend: # f'[{timestamp_pretty()}] workers are processing request...', # parse_mode='HTML' # ) - - await message.edit( - f'**broadcasted!**\n \ - **{enqueue_tx_link}**\n \ - [{timestamp_pretty()}] *workers are processing request...*' - ) + # await message.edit(content= + # f'**broadcasted!**\n \ + # **{enqueue_tx_link}**\n \ + # [{timestamp_pretty()}] *workers are processing request...*' + # ) out = collect_stdout(res) @@ -250,11 +249,11 @@ class SkynetDiscordFrontend: # f'[{timestamp_pretty()}] trying to download image...\n', # parse_mode='HTML' # ) - await message.edit( - f'**request processed!**\n \ - **{tx_link}**\n \ - [{timestamp_pretty()}] *trying to download image...*\n' - ) + # await message.edit(content= + # f'**request processed!**\n \ + # **{tx_link}**\n \ + # [{timestamp_pretty()}] *trying to download image...*\n' + # ) # attempt to get the image and send it ipfs_link = f'https://ipfs.{DEFAULT_DOMAIN}/ipfs/{ipfs_hash}/image.png' diff --git a/skynet/frontend/discord/handlers.py b/skynet/frontend/discord/handlers.py index cbde27e..a72ab72 100644 --- a/skynet/frontend/discord/handlers.py +++ b/skynet/frontend/discord/handlers.py @@ -17,46 +17,112 @@ def create_handler_context(frontend: 'SkynetDiscordFrontend'): bot = frontend.bot cleos = frontend.cleos - # db_call = frontend.db_call + db_call = frontend.db_call work_request = frontend.work_request ipfs_node = frontend.ipfs_node @bot.command(name='config', help='Responds with the configuration') - async def config(ctx): - response = "This is the bot configuration" # Put your bot configuration here - await ctx.send(response) + async def set_config(ctx): + + user = ctx.author + try: + attr, val, reply_txt = validate_user_config_request( + ctx.message.content) + + logging.info(f'user config update: {attr} to {val}') + await db_call('update_user_config', user.id, attr, val) + logging.info('done') + + except BaseException as e: + reply_txt = str(e) + + finally: + await ctx.reply(content=reply_txt) @bot.command(name='helper', help='Responds with a help') async def helper(ctx): - response = "This is help information" # Put your help response here - await ctx.send(response) + splt_msg = ctx.message.content.split(' ') + + if len(splt_msg) == 1: + await ctx.reply(content=HELP_TEXT) + + else: + param = splt_msg[1] + if param in HELP_TOPICS: + await ctx.reply(content=HELP_TOPICS[param]) + + else: + await ctx.reply(content=HELP_UNKWNOWN_PARAM) + + @bot.command(name='cool', help='Display a list of cool prompt words') + async def send_cool_words(ctx): + await ctx.reply(content='\n'.join(COOL_WORDS)) @bot.command(name='txt2img', help='Responds with an image') - async def send_txt2img(ctx, *, arg): - user = 'testworker3' - status_msg = 'status' + async def send_txt2img(ctx): + + # grab user from ctx + user = ctx.author + user_row = await db_call('get_or_create_user', user.id) + + # init new msg + init_msg = 'started processing txt2img request...' + status_msg = await ctx.reply(init_msg) + await db_call( + 'new_user_request', user.id, ctx.message.id, status_msg.id, status=init_msg) + + prompt = ' '.join(ctx.message.content.split(' ')[1:]) + + if len(prompt) == 0: + await status_msg.edit(content= + 'Empty text prompt ignored.' + ) + await db_call('update_user_request', status_msg.id, 'Empty text prompt ignored.') + return + + logging.info(f'mid: {ctx.message.id}') + + user_config = {**user_row} + del user_config['id'] + params = { - 'prompt': arg, - 'seed': None, - 'step': 35, - 'guidance': 7.5, - 'strength': 0.5, - 'width': 512, - 'height': 512, - 'upscaler': None, - 'model': 'prompthero/openjourney', + 'prompt': prompt, + **user_config } - ec = await work_request(user, status_msg, 'txt2img', params, ctx) - print(ec) + await db_call( + 'update_user_stats', user.id, 'txt2img', last_prompt=prompt) + + ec = await work_request(user.name, status_msg, 'txt2img', params, ctx) + + if ec == 0: + await db_call('increment_generated', user.id) + + # TODO: DELETE BELOW + # user = 'testworker3' + # status_msg = 'status' + # params = { + # 'prompt': arg, + # 'seed': None, + # 'step': 35, + # 'guidance': 7.5, + # 'strength': 0.5, + # 'width': 512, + # 'height': 512, + # 'upscaler': None, + # 'model': 'prompthero/openjourney', + # } + # + # ec = await work_request(user, status_msg, 'txt2img', params, ctx) + # print(ec) # if ec == 0: # await db_call('increment_generated', user.id) - response = f"This is your prompt: {arg}" - await ctx.send(response) + # response = f"This is your prompt: {arg}" + # await ctx.send(response) # generic / simple handlers