File size: 1,449 Bytes
0a98e8a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import telebot
from PIL import Image
import sys
import os
import io

sys.path.append(os.path.join(os.path.dirname(__file__), "..", "app"))
import model

bot = telebot.TeleBot("<Paste your token here>")
model_instance = model.create_model()


@bot.message_handler(content_types=["photo"])
def photo(message):
    user_id = message.from_user.id
    file_id = message.photo[-1].file_id
    file_info = bot.get_file(file_id)
    downloaded_file = bot.download_file(file_info.file_path)

    if not os.path.exists("user_data"):
        os.makedirs("user_data")
    filename = f"user_data/image_{user_id}_{file_id}.jpg"
    with open(filename, "wb") as new_file:
        new_file.write(downloaded_file)

    bot.send_message(message.from_user.id, "Изображение обрабатывается...")

    image = Image.open(filename)
    image_bytes = io.BytesIO()
    image.save(image_bytes, format=image.format)
    output_image = model.run_model(image_bytes, model_instance)
    os.remove(filename)

    bot.send_photo(user_id, output_image)


@bot.message_handler(content_types=["text"])
def start(message):
    bot.send_message(
        message.from_user.id,
        """Этот бот осветляет фотографии, сделанные при плохом освещении, при помощи нейронной сети.\
        Просто пришлите сюда фото!""",
    )


bot.polling(none_stop=True, interval=0)