I am using an ML model. I need to initialize the model at start and pass (or receive) model and tokenizer to the handler to make it run the model. How to do this?
Right now I am using FSMContext
and the model is loaded and saved on /start command for each user separately.
Structure:
model.py
def load_model():
return model
def load_tokenizer():
return tokenizer
def run_model(model, tokenizer):
<...>
return output
bot.py
class TelegramBot:
def __init__(self):
self.TOKEN = "TOKEN"
self.storage = MemoryStorage()
self.dp = Dispatcher(storage=self.storage)
async def main(self):
self.bot = Bot(token=self.TOKEN)
self.dp.include_routers(
start_handler.router,
main_handler.router,
)
await self.dp.start_polling(self.bot)
asyncio.run(TelegramBot().main())
main_handler.py
async def main_handler(message: Message, state: FSMContext):
data = await state.get_data()
model = data["model"]
tokenizer = data["tokenizer"]
result = run_model(model, tokenizer)
await message.answer(result)
start_handler.py
from model import load_model, load_tokenizer
async def start_handler(message: Message, state: FSMContext):
await state.set_data(
{
"model": load_model(),
"tokenizer": load_tokenizer()
}
)
New contributor
Stepan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.