Skip to content

Commit aed46a7

Browse files
committed
Improve image generation
1 parent 8988c5e commit aed46a7

File tree

2 files changed

+69
-33
lines changed

2 files changed

+69
-33
lines changed

app/bolt_listeners.py

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from slack_bolt import App, Ack, BoltContext, BoltResponse
88
from slack_bolt.request.payload_utils import is_event
99
from slack_sdk.web import WebClient
10+
from slack_sdk.errors import SlackApiError
1011

1112
from app.env import (
1213
OPENAI_TIMEOUT_SECONDS,
@@ -64,6 +65,7 @@
6465
build_image_generation_wip_modal,
6566
build_image_generation_result_modal,
6667
build_image_generation_text_modal,
68+
build_image_generation_result_blocks,
6769
)
6870

6971

@@ -772,7 +774,7 @@ def display_image_generation_result(
772774
"OPENAI_IMAGE_GENERATION_MODEL", OPENAI_IMAGE_GENERATION_MODEL
773775
)
774776
text = "\n".join(map(lambda s: f">{s}", prompt.split("\n")))
775-
view = build_image_generation_result_modal(
777+
blocks = build_image_generation_result_blocks(
776778
text=text,
777779
spent_seconds=str(round(spent_seconds, 2)),
778780
image_url=image_url,
@@ -781,22 +783,48 @@ def display_image_generation_result(
781783
quality=quality,
782784
style=style,
783785
)
784-
client.views_update(view_id=payload["id"], view=view)
786+
client.chat_postMessage(
787+
channel=context.actor_user_id,
788+
text=f"Here is the generated image URL: {image_url}",
789+
blocks=blocks,
790+
)
791+
client.views_update(
792+
view_id=payload["id"],
793+
view=build_image_generation_result_modal(blocks),
794+
)
785795

786796
except (APITimeoutError, TimeoutError):
797+
client.chat_postMessage(
798+
channel=context.actor_user_id,
799+
text=TIMEOUT_ERROR_MESSAGE,
800+
)
787801
client.views_update(
788802
view_id=payload["id"],
789803
view=build_image_generation_text_modal(TIMEOUT_ERROR_MESSAGE),
790804
)
791-
except Exception as e:
792-
logger.exception(f"Failed to share a generated image: {e}")
805+
except SlackApiError as e:
806+
logger.exception(f"Failed to call Slack APIs for image generation: {e}")
793807
client.views_update(
794808
view_id=payload["id"],
795809
view=build_image_generation_text_modal(
796810
f"{text}\n\n:warning: My apologies! "
797-
f"An error occurred while generating an image: {e}"
811+
f"An error occurred while calling Slack APIs: {e}"
798812
),
799813
)
814+
except Exception as e:
815+
logger.exception(f"Failed to share a generated image: {e}")
816+
error = (
817+
f"{text}\n\n:warning: My apologies! "
818+
f"An error occurred while generating an image: {e}"
819+
)
820+
client.chat_postMessage(
821+
channel=context.actor_user_id,
822+
text=error,
823+
)
824+
client.views_update(
825+
view_id=payload["id"],
826+
view=build_image_generation_text_modal(error),
827+
)
800828

801829

802830
#

app/slack_ui.py

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -684,10 +684,24 @@ def build_image_generation_input_modal(prompt: str) -> dict:
684684

685685

686686
def build_image_generation_wip_modal() -> dict:
687-
return build_image_generation_text_modal("Working on this now ... :hourglass:")
687+
return build_image_generation_text_modal(
688+
"Working on this now ... :hourglass:\n\n"
689+
"Once the image is ready, this app will send it to you in a DM. "
690+
"If you don't want to wait here, you can close this modal at any time."
691+
)
692+
693+
694+
def build_image_generation_result_modal(blocks: list) -> dict:
695+
return {
696+
"type": "modal",
697+
"callback_id": "image-generation",
698+
"title": {"type": "plain_text", "text": "Image Generation"},
699+
"close": {"type": "plain_text", "text": "Close"},
700+
"blocks": blocks,
701+
}
688702

689703

690-
def build_image_generation_result_modal(
704+
def build_image_generation_result_blocks(
691705
*,
692706
text: str,
693707
size: str,
@@ -696,34 +710,28 @@ def build_image_generation_result_modal(
696710
spent_seconds: str,
697711
image_url: str,
698712
model: str,
699-
) -> dict:
700-
return {
701-
"type": "modal",
702-
"callback_id": "image-generation",
703-
"title": {"type": "plain_text", "text": "Image Generation"},
704-
"close": {"type": "plain_text", "text": "Close"},
705-
"blocks": [
706-
{
707-
"type": "section",
708-
"text": {
709-
"type": "mrkdwn",
710-
"text": f"{text}\n>\n>(model: {model}, size: {size}, quality: {quality}, style: {style})\n\n"
711-
f"Here is the image content URL generated by the above prompt "
712-
f"(time spent: {spent_seconds} seconds):"
713-
f" <{image_url}|Click>",
714-
},
713+
) -> list[dict]:
714+
return [
715+
{
716+
"type": "section",
717+
"text": {
718+
"type": "mrkdwn",
719+
"text": f"{text}\n>\n>(model: {model}, size: {size}, quality: {quality}, style: {style})\n\n"
720+
f"Here is the image content URL generated by the above prompt "
721+
f"(time spent: {spent_seconds} seconds):"
722+
f" <{image_url}|Click>",
715723
},
716-
{
717-
"type": "image",
718-
"title": {
719-
"type": "plain_text",
720-
"text": f"This image was generated using ChatGPT's {model} model",
721-
},
722-
"image_url": image_url,
723-
"alt_text": f"Generated by {model}",
724+
},
725+
{
726+
"type": "image",
727+
"title": {
728+
"type": "plain_text",
729+
"text": f"This image was generated using ChatGPT's {model} model",
724730
},
725-
],
726-
}
731+
"image_url": image_url,
732+
"alt_text": f"Generated by {model}",
733+
},
734+
]
727735

728736

729737
def build_image_generation_text_modal(section_text: str) -> dict:

0 commit comments

Comments
 (0)