Spaces:
Configuration error
Configuration error
Danila-Pechenev
commited on
Commit
·
0a98e8a
1
Parent(s):
36a8e57
Implement telegram bot
Browse files- {test → telegram-bot}/__init__.py +0 -0
- telegram-bot/bot.py +47 -0
- telegram-bot/user_data/.gitkeep +0 -0
{test → telegram-bot}/__init__.py
RENAMED
File without changes
|
telegram-bot/bot.py
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import telebot
|
2 |
+
from PIL import Image
|
3 |
+
import sys
|
4 |
+
import os
|
5 |
+
import io
|
6 |
+
|
7 |
+
sys.path.append(os.path.join(os.path.dirname(__file__), "..", "app"))
|
8 |
+
import model
|
9 |
+
|
10 |
+
bot = telebot.TeleBot("<Paste your token here>")
|
11 |
+
model_instance = model.create_model()
|
12 |
+
|
13 |
+
|
14 |
+
@bot.message_handler(content_types=["photo"])
|
15 |
+
def photo(message):
|
16 |
+
user_id = message.from_user.id
|
17 |
+
file_id = message.photo[-1].file_id
|
18 |
+
file_info = bot.get_file(file_id)
|
19 |
+
downloaded_file = bot.download_file(file_info.file_path)
|
20 |
+
|
21 |
+
if not os.path.exists("user_data"):
|
22 |
+
os.makedirs("user_data")
|
23 |
+
filename = f"user_data/image_{user_id}_{file_id}.jpg"
|
24 |
+
with open(filename, "wb") as new_file:
|
25 |
+
new_file.write(downloaded_file)
|
26 |
+
|
27 |
+
bot.send_message(message.from_user.id, "Изображение обрабатывается...")
|
28 |
+
|
29 |
+
image = Image.open(filename)
|
30 |
+
image_bytes = io.BytesIO()
|
31 |
+
image.save(image_bytes, format=image.format)
|
32 |
+
output_image = model.run_model(image_bytes, model_instance)
|
33 |
+
os.remove(filename)
|
34 |
+
|
35 |
+
bot.send_photo(user_id, output_image)
|
36 |
+
|
37 |
+
|
38 |
+
@bot.message_handler(content_types=["text"])
|
39 |
+
def start(message):
|
40 |
+
bot.send_message(
|
41 |
+
message.from_user.id,
|
42 |
+
"""Этот бот осветляет фотографии, сделанные при плохом освещении, при помощи нейронной сети.\
|
43 |
+
Просто пришлите сюда фото!""",
|
44 |
+
)
|
45 |
+
|
46 |
+
|
47 |
+
bot.polling(none_stop=True, interval=0)
|
telegram-bot/user_data/.gitkeep
ADDED
File without changes
|