-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
147 lines (101 loc) · 5.18 KB
/
main.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
from telegram import Update, File, User
from telegram import ReplyKeyboardMarkup, ReplyKeyboardRemove
from telegram.ext import Updater, Dispatcher, CallbackContext
from telegram.ext import CommandHandler, MessageHandler, Filters, ConversationHandler
from misc import load_model, clear_image, get_transferred_image
TOKEN = '...' # insert token here
MENU, STYLE, TARGET = range(3)
MODEL = load_model()
def get_keyboard_markup(user_data: dict) -> ReplyKeyboardMarkup:
reply_keyboard = [[f'{"✔️" if user_data["style"] else "❌"} Set style'],
[f'{"✔️" if user_data["target"] else "❌"} Set target'],
[f'{"✔️" if (user_data["style"] and user_data["target"]) else "❌"} Make transfer'],
['/stop']]
return ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=False,
input_field_placeholder='Select command')
def start(update: Update, context: CallbackContext) -> int:
context.user_data['style'] = None
context.user_data['target'] = None
keyboard_markup = get_keyboard_markup(context.user_data)
update.message.reply_text('Hello! I am Style Transfer Bot.\n'
'To make a transfer:\n'
'- Set the style image\n'
'- Set the target image\n'
'❌ or ✔️ shows the status of the command.\n'
'Send /stop to stop the conversation and reset images.',
reply_markup=keyboard_markup)
return MENU
def set_style(update: Update, context: CallbackContext) -> int:
update.message.reply_text('Ok. Send me the style image for transfer.',
reply_markup=ReplyKeyboardRemove())
return STYLE
def set_style_image(update: Update, context: CallbackContext) -> int:
if context.user_data['style']:
clear_image(context.user_data['style'])
photo_file: File = update.message.photo[-1].get_file()
path = photo_file.download(f'images/{photo_file.file_id}.jpg')
context.user_data['style'] = path
keyboard_markup = get_keyboard_markup(context.user_data)
update.message.reply_text('Style image was successfully set!',
reply_markup=keyboard_markup)
return MENU
def set_target(update: Update, context: CallbackContext) -> int:
update.message.reply_text('Ok. Send me the target image for transfer.',
reply_markup=ReplyKeyboardRemove())
return TARGET
def set_target_image(update: Update, context: CallbackContext) -> int:
if context.user_data['target']:
clear_image(context.user_data['target'])
photo_file: File = update.message.photo[-1].get_file()
path = photo_file.download(f'images/{photo_file.file_id}.jpg')
context.user_data['target'] = path
keyboard_markup = get_keyboard_markup(context.user_data)
update.message.reply_text('Target image was successfully set!',
reply_markup=keyboard_markup)
return MENU
def make_transfer(update: Update, context: CallbackContext) -> int:
user: User = update.message.from_user
style = context.user_data['style']
target = context.user_data['target']
if style and target:
image = get_transferred_image(MODEL, style, target)
clear_image(context.user_data['style'])
clear_image(context.user_data['target'])
context.user_data['style'] = None
context.user_data['target'] = None
keyboard_markup = get_keyboard_markup(context.user_data)
user.send_photo(image, 'Success!', reply_markup=keyboard_markup)
else:
update.message.reply_text('Please set style and target image first.\n'
'After that you will see the ✔️ status near commands.')
return MENU
def stop(update: Update, context: CallbackContext) -> int:
clear_image(context.user_data['style'])
clear_image(context.user_data['target'])
context.user_data['style'] = None
context.user_data['target'] = None
update.message.reply_text('Images were reset.\n'
'Send /start to start the conversation again.\n'
'Bye!', reply_markup=ReplyKeyboardRemove())
return ConversationHandler.END
def main() -> None:
updater: Updater = Updater(TOKEN)
dispatcher: Dispatcher = updater.dispatcher
conversation_handler = ConversationHandler(
entry_points=[CommandHandler('start', start)],
states={
MENU: [
MessageHandler(Filters.regex('^(❌ Set style|✔️ Set style)$'), set_style),
MessageHandler(Filters.regex('^(❌ Set target|✔️ Set target)$'), set_target),
MessageHandler(Filters.regex('^(❌ Make transfer|✔️ Make transfer)$'), make_transfer)
],
STYLE: [MessageHandler(Filters.photo, set_style_image)],
TARGET: [MessageHandler(Filters.photo, set_target_image)],
},
fallbacks=[CommandHandler('stop', stop)]
)
dispatcher.add_handler(conversation_handler)
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()