|
import logging |
|
from telegram import Update |
|
from telegram.ext import Updater, CommandHandler, CallbackContext |
|
from transformers import pipeline |
|
|
|
|
|
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO) |
|
|
|
|
|
generator = pipeline('text-generation', model='distilgpt2') |
|
|
|
|
|
def start(update: Update, context: CallbackContext) -> None: |
|
update.message.reply_text('Привет! Я бот для ответов на сообщения с Авито.') |
|
|
|
|
|
def respond(update: Update, context: CallbackContext) -> None: |
|
user_message = update.message.text |
|
response = generate_response(user_message) |
|
update.message.reply_text(response) |
|
|
|
|
|
def generate_response(message: str) -> str: |
|
|
|
generated = generator(message, max_length=50, num_return_sequences=1) |
|
return generated[0]['generated_text'] |
|
|
|
|
|
def main(): |
|
|
|
updater = Updater("7651372274:AAF-BCJL1PTKm_BbG9TpUllCnp8e0ifXPkA", use_context=True) |
|
dispatcher = updater.dispatcher |
|
|
|
|
|
dispatcher.add_handler(CommandHandler("start", start)) |
|
dispatcher.add_handler(CommandHandler("respond", respond)) |
|
|
|
|
|
updater.start_polling() |
|
updater.idle() |
|
|
|
if __name__ == '__main__': |
|
main() |