-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
106 lines (88 loc) · 3.79 KB
/
utils.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
import logging
from datetime import datetime
from random import randint
import ephem
from clarifai_grpc.channel.clarifai_channel import ClarifaiChannel
from clarifai_grpc.grpc.api import resources_pb2, service_pb2, service_pb2_grpc
from clarifai_grpc.grpc.api.status import status_code_pb2
from telegram import KeyboardButton, ReplyKeyboardMarkup, InlineKeyboardButton, InlineKeyboardMarkup
from config import CLARYFAI_API_KEY, CLARYFAI_MODEL_ID_VERSION
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
filename="bot.log",
level=logging.INFO,
)
def find_constellation(planet: str) -> tuple:
now = datetime.now()
date = f"{now.year}/{now.month}/{now.day}"
planets = {
"Mercury": ephem.Mercury(date),
"Venus": ephem.Venus(date),
"Mars": ephem.Mars(date),
"Jupiter": ephem.Jupiter(date),
"Saturn": ephem.Saturn(date),
"Uranus": ephem.Uranus(date),
"Neptune": ephem.Neptune(date),
"Pluto": ephem.Pluto(date),
}
planet_query = planets[planet]
constellation = ephem.constellation(planet_query)
return constellation
def get_bot_number(user_number: int) -> int:
return randint(user_number - 10, user_number + 10)
def play_random_number(user_number: int, bot_number: int) -> str:
if bot_number < user_number:
message = f"Ваше число {user_number}, мое число {bot_number}. Вы выиграли!"
elif bot_number > user_number:
message = f"Ваше число {user_number}, мое число {bot_number}. Я выиграл!"
else:
message = f"Ваше число {user_number}, мое число {bot_number}. Ничья!"
return message
def main_keyboard() -> None:
return ReplyKeyboardMarkup(
[
[
"Прислать котика",
KeyboardButton("Мои координаты", request_location=True),
"Заполнить анкету",
]
],
resize_keyboard=True,
)
def has_object_on_image(file_name: str, object_name: str) -> bool:
"""
Функция, при помощи сервиса 'Clarifai', проверяет наличие обьекта 'object_name'
на изображении 'file_name'. При вероятности наличия обьекта на изображении >= 90%
возвращает 'True', иначе 'False'.
"""
logging.info("Отправляем фото в Clarifai")
channel = ClarifaiChannel.get_grpc_channel()
app = service_pb2_grpc.V2Stub(channel)
metadata = (("authorization", f"Key {CLARYFAI_API_KEY}"),)
request = service_pb2.PostModelOutputsRequest(
model_id=CLARYFAI_MODEL_ID_VERSION["default"],
inputs=[
resources_pb2.Input(
data=resources_pb2.Data(image=resources_pb2.Image(url=file_name))
)
],
)
response = app.PostModelOutputs(request, metadata=metadata)
return check_response_for_object(response, object_name)
def check_response_for_object(response, object_name: str) -> bool:
if response.status.code == status_code_pb2.SUCCESS:
for concept in response.outputs[0].data.concepts:
if concept.name == object_name and concept.value >= 0.90:
return True
else:
print(f"Ошибка распознования картинки {response.outputs[0].status.datails}")
return False
def cat_rating_inline_keyboard(image_name: str):
callback_text = f"rating|{image_name}|"
keyboard = [
[
InlineKeyboardButton("Нравится", callback_data=callback_text + "1"),
InlineKeyboardButton("Не нравится", callback_data=callback_text + "-1")
]
]
return InlineKeyboardMarkup(keyboard)