From ed61f5f67edf7f3702f8cf8238f8c9f640c67c19 Mon Sep 17 00:00:00 2001 From: Hendrik Langer Date: Wed, 5 Apr 2023 18:59:44 +0200 Subject: [PATCH] exception handling --- matrix_pygmalion_bot/ai/runpod_pygmalion.py | 4 +- matrix_pygmalion_bot/core.py | 54 ++++++++++++--------- 2 files changed, 32 insertions(+), 26 deletions(-) diff --git a/matrix_pygmalion_bot/ai/runpod_pygmalion.py b/matrix_pygmalion_bot/ai/runpod_pygmalion.py index 66a6bf7..92b723d 100644 --- a/matrix_pygmalion_bot/ai/runpod_pygmalion.py +++ b/matrix_pygmalion_bot/ai/runpod_pygmalion.py @@ -196,7 +196,7 @@ async def generate_image(input_prompt: str, negative_prompt: str, api_url: str, # Make the request r = requests.post(endpoint, json=input_data, headers=headers) r_json = r.json() - logger.info(r_json) + logger.debug(r_json) if r.status_code == 200: status = r_json["status"] @@ -210,7 +210,7 @@ async def generate_image(input_prompt: str, negative_prompt: str, api_url: str, endpoint = api_url + "status/" + job_id r = requests.get(endpoint, headers=headers) r_json = r.json() - logger.info(r_json) + logger.debug(r_json) status = r_json["status"] if status == 'IN_PROGRESS': await asyncio.sleep(DELAY) diff --git a/matrix_pygmalion_bot/core.py b/matrix_pygmalion_bot/core.py index 027f27e..510fa8e 100644 --- a/matrix_pygmalion_bot/core.py +++ b/matrix_pygmalion_bot/core.py @@ -107,31 +107,37 @@ class Callbacks(object): negative_prompt = "ugly, tiling, poorly drawn hands, poorly drawn feet, poorly drawn face, out of frame, extra limbs, disfigured, deformed, body out of frame, bad anatomy, watermark, signature, cut off, low contrast, underexposed, overexposed, bad art, beginner, amateur, distorted face" # else: # negative_prompt = "ugly, deformed, out of frame" - if self.bot.service == "runpod": - if num == 1: - output = await ai.generate_image1(prompt, negative_prompt, self.bot.runpod_api_key) - elif num == 2: - output = await ai.generate_image2(prompt, negative_prompt, self.bot.runpod_api_key) - elif num == 3: - output = await ai.generate_image3(prompt, negative_prompt, self.bot.runpod_api_key) - elif num == 4: - output = await ai.generate_image4(prompt, negative_prompt, self.bot.runpod_api_key) - elif num == 5: - output = await ai.generate_image5(prompt, negative_prompt, self.bot.runpod_api_key) - elif num == 6: - output = await ai.generate_image6(prompt, negative_prompt, self.bot.runpod_api_key) + try: + if self.bot.service == "runpod": + if num == 1: + output = await ai.generate_image1(prompt, negative_prompt, self.bot.runpod_api_key) + elif num == 2: + output = await ai.generate_image2(prompt, negative_prompt, self.bot.runpod_api_key) + elif num == 3: + output = await ai.generate_image3(prompt, negative_prompt, self.bot.runpod_api_key) + elif num == 4: + output = await ai.generate_image4(prompt, negative_prompt, self.bot.runpod_api_key) + elif num == 5: + output = await ai.generate_image5(prompt, negative_prompt, self.bot.runpod_api_key) + elif num == 6: + output = await ai.generate_image6(prompt, negative_prompt, self.bot.runpod_api_key) + else: + raise ValueError('no image generator with that number') + elif self.bot.service == "stablehorde": + if num == 1: + output = await ai.generate_image1(prompt, negative_prompt, self.bot.stablehorde_api_key) + elif num == 2: + output = await ai.generate_image2(prompt, negative_prompt, self.bot.stablehorde_api_key) + elif num == 3: + output = await ai.generate_image3(prompt, negative_prompt, self.bot.stablehorde_api_key) + else: + raise ValueError('no image generator with that number') else: - return - elif self.bot.service == "stablehorde": - if num == 1: - output = await ai.generate_image1(prompt, negative_prompt, self.bot.stablehorde_api_key) - elif num == 2: - output = await ai.generate_image2(prompt, negative_prompt, self.bot.stablehorde_api_key) - elif num == 3: - output = await ai.generate_image3(prompt, negative_prompt, self.bot.stablehorde_api_key) - else: - return - else: + raise ValueError('remote image generation not configured properly') + except ValueError as err: + errormessage = f" {err=}, {type(err)=}" + logger.error(errormessage) + await self.bot.send_message(self.client, room.room_id, errormessage) return for imagefile in output: