-
Notifications
You must be signed in to change notification settings - Fork 2
/
botv2.py
160 lines (139 loc) · 4.96 KB
/
botv2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
"""
Image-generating Bot that assists with image design ideas and image generation based on its own text-to-image prompt
Usage:
Sequence of automated questions to answer and then generating an image based on suggested image design.
Press Ctrl-C on the command line or send a signal to the process to stop the bot.
"""
import openai
import logging
from dotenv import dotenv_values
import argparse
import os
from api.conversation import (
start_command,
cancel_command,
get_image_purpose,
select_theme,
select_image_design,
get_image_prompt,
generate_image,
)
from api.utils import run_in_threadpool_decorator
from api.outpainting import outpainting_handler
from telegram import __version__ as TG_VER
from telegram import Update
from telegram.ext import (
Application,
CommandHandler,
ContextTypes,
MessageHandler,
filters,
ConversationHandler,
PicklePersistence,
)
try:
from telegram import __version_info__
except ImportError:
__version_info__ = (0, 0, 0, 0, 0) # type: ignore[assignment]
if __version_info__ < (20, 0, 0, "alpha", 1):
raise RuntimeError(
f"This example is not compatible with your current PTB version {TG_VER}. To view the "
f"{TG_VER} version of this example, "
f"visit https://docs.python-telegram-bot.org/en/v{TG_VER}/examples.html"
)
# get config
config = dotenv_values(".env")
# get API tokens
HF_TOKEN = config["HF_API_KEY"]
openai.api_key = config["OPENAI_API_KEY"]
# TELEBOT_TOKEN = config['TELEBOT_TOKEN']
# Enable logging
logging.basicConfig(
format="%(asctime)s - %(processName)s - %(threadName)s - [%(thread)d] - %(name)s - %(levelname)s - %(message)s",
level=logging.INFO,
)
logger = logging.getLogger(__name__)
huggingFaceLogger = logging.getLogger("huggingface_hub").setLevel(logging.INFO)
messageHandlerLogger = logging.getLogger("telegram.bot").setLevel(logging.INFO)
applicationLogger = logging.getLogger("telegram.ext").setLevel(logging.INFO)
openAILogger = logging.getLogger("openai").setLevel(logging.INFO)
# assign variable name for each integer in sequence for easy tracking of conversation
(
IMAGE_TYPE,
IMAGE_PURPOSE,
SELECTED_THEME,
SELECTED_IMAGE_DESIGN,
TEXT_TO_IMAGE_PROMPT,
GENERATED_IMAGE,
) = range(6)
async def pong(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
"""serves a health check status for debugging
Args:
update (Update): _description_
context (ContextTypes.DEFAULT_TYPE): _description_
Returns:
Pong to the user
"""
await update.message.reply_text("Pong")
# function to start the bot
def main(dev_mode) -> None:
if dev_mode:
TELEBOT_TOKEN = config["TELEBOT_DEV_TOKEN"]
else:
TELEBOT_TOKEN = config["TELEBOT_TOKEN"]
# create folders for outputs
if not os.path.exists("data"):
os.mkdir("data")
if not os.path.exists("data/image_output"):
os.mkdir("data/image_output")
# configure chatbot's persistence
persistence = PicklePersistence(filepath="data/conversation")
# create the Application pass telebot's token to application
application = (
Application.builder()
.token(TELEBOT_TOKEN)
.concurrent_updates(True)
.persistence(persistence)
.build()
)
# Conversation Handler with the states IMAGE_TYPE, IMAGE_PURPOSE, SELECTED_THEME, SELECTED_IMAGE_DESIGN
conv_handler = ConversationHandler(
entry_points=[CommandHandler("start", start_command)],
states={
IMAGE_TYPE: [
CommandHandler("Yes", start_command),
CommandHandler("No", cancel_command),
],
IMAGE_PURPOSE: [
MessageHandler(filters.TEXT, get_image_purpose, block=False)
],
SELECTED_THEME: [MessageHandler(filters.TEXT, select_theme, block=False)],
SELECTED_IMAGE_DESIGN: [
MessageHandler(filters.TEXT, select_image_design, block=False)
],
TEXT_TO_IMAGE_PROMPT: [
MessageHandler(filters.TEXT, get_image_prompt, block=False)
],
GENERATED_IMAGE: [
MessageHandler(filters.TEXT, generate_image, block=False)
],
},
fallbacks=[CommandHandler("cancel", cancel_command)],
allow_reentry=True, # allow user to enter back any state of the ConversationHandler
name="ImageGeneratingBot",
persistent=True,
block=False,
)
ping_handler = CommandHandler("ping", pong, block=False)
# add conversation handler to application
application.add_handler(conv_handler)
application.add_handler(ping_handler)
application.add_handler(outpainting_handler)
application.run_polling(allowed_updates=Update.ALL_TYPES)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"-DEV", "--dev", action="store_true", help="Run with local Tele API token"
)
args = parser.parse_args()
main(args.dev)